1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-14 23:12:28 +02:00

Simplyfy code to gather storage used with StorageManager.estimate()

Documentation:
https://developer.mozilla.org/docs/Web/API/StorageManager
This commit is contained in:
Raymond Hill 2019-03-22 22:09:27 -03:00
parent ac71d6577a
commit 2fd587b7ae
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 8 additions and 38 deletions

View File

@ -537,17 +537,7 @@ const assetCacheWrite = function(assetKey, details, callback) {
if ( details instanceof Object && typeof details.url === 'string' ) { if ( details instanceof Object && typeof details.url === 'string' ) {
entry.remoteURL = details.url; entry.remoteURL = details.url;
} }
µBlock.cacheStorage.set( µBlock.cacheStorage.set({ assetCacheRegistry, [internalKey]: content });
{ [internalKey]: content }
).then(details => {
if (
details instanceof Object &&
typeof details.bytesInUse === 'number'
) {
entry.byteLength = details.bytesInUse;
}
saveAssetCacheRegistry(true);
});
const result = { assetKey, content }; const result = { assetKey, content };
if ( typeof callback === 'function' ) { if ( typeof callback === 'function' ) {
callback(result); callback(result);
@ -893,19 +883,6 @@ api.metadata = function(callback) {
/******************************************************************************/ /******************************************************************************/
api.getBytesInUse = function() {
return getAssetCacheRegistry().then(cacheDict => {
let bytesUsed = 0;
for ( const assetKey in cacheDict ) {
if ( cacheDict.hasOwnProperty(assetKey) === false ) { continue; }
bytesUsed += cacheDict[assetKey].byteLength || 0;
}
return bytesUsed;
});
};
/******************************************************************************/
api.purge = assetCacheMarkAsDirty; api.purge = assetCacheMarkAsDirty;
api.remove = function(pattern, callback) { api.remove = function(pattern, callback) {

View File

@ -215,7 +215,6 @@
STORAGE_NAME, STORAGE_NAME,
{ keyPath: 'key' } { keyPath: 'key' }
); );
table.createIndex('value', 'value', { unique: false });
} catch(ex) { } catch(ex) {
req.onerror(); req.onerror();
} }
@ -349,22 +348,13 @@
const entries = []; const entries = [];
const dontCompress = const dontCompress =
µBlock.hiddenSettings.cacheStorageCompression !== true; µBlock.hiddenSettings.cacheStorageCompression !== true;
let bytesInUse = 0;
const handleEncodingResult = result => { const handleEncodingResult = result => {
if ( typeof result.data === 'string' ) {
bytesInUse += result.data.length;
} else if ( result.data instanceof Blob ) {
bytesInUse += result.data.size;
}
entries.push({ key: result.key, value: result.data }); entries.push({ key: result.key, value: result.data });
}; };
for ( const key of keys ) { for ( const key of keys ) {
const data = keyvalStore[key]; const data = keyvalStore[key];
const isString = typeof data === 'string'; const isString = typeof data === 'string';
if ( isString === false || dontCompress ) { if ( isString === false || dontCompress ) {
if ( isString ) {
bytesInUse += data.length;
}
entries.push({ key, value: data }); entries.push({ key, value: data });
continue; continue;
} }
@ -379,7 +369,7 @@
if ( callback === undefined ) { return; } if ( callback === undefined ) { return; }
let cb = callback; let cb = callback;
callback = undefined; callback = undefined;
cb({ bytesInUse }); cb();
}; };
try { try {
const transaction = db.transaction( const transaction = db.transaction(

View File

@ -50,10 +50,13 @@
countdown += 1; countdown += 1;
vAPI.storage.getBytesInUse(null, process); vAPI.storage.getBytesInUse(null, process);
} }
if ( this.cacheStorage.name !== 'browser.storage.local' ) { if (
navigator.storage instanceof Object &&
navigator.storage.estimate instanceof Function
) {
countdown += 1; countdown += 1;
this.assets.getBytesInUse().then(count => { navigator.storage.estimate().then(estimate => {
process(count); process(estimate.usage);
}); });
} }
if ( countdown === 0 ) { if ( countdown === 0 ) {