From 2fd587b7ae36661d1a20557618fcaba55ae317a0 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 22 Mar 2019 22:09:27 -0300 Subject: [PATCH] Simplyfy code to gather storage used with StorageManager.estimate() Documentation: https://developer.mozilla.org/docs/Web/API/StorageManager --- src/js/assets.js | 25 +------------------------ src/js/cachestorage.js | 12 +----------- src/js/storage.js | 9 ++++++--- 3 files changed, 8 insertions(+), 38 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 7c6b32b86..9317bba09 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -537,17 +537,7 @@ const assetCacheWrite = function(assetKey, details, callback) { if ( details instanceof Object && typeof details.url === 'string' ) { entry.remoteURL = details.url; } - µBlock.cacheStorage.set( - { [internalKey]: content } - ).then(details => { - if ( - details instanceof Object && - typeof details.bytesInUse === 'number' - ) { - entry.byteLength = details.bytesInUse; - } - saveAssetCacheRegistry(true); - }); + µBlock.cacheStorage.set({ assetCacheRegistry, [internalKey]: content }); const result = { assetKey, content }; if ( typeof callback === 'function' ) { 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.remove = function(pattern, callback) { diff --git a/src/js/cachestorage.js b/src/js/cachestorage.js index f47cd805e..6538732ac 100644 --- a/src/js/cachestorage.js +++ b/src/js/cachestorage.js @@ -215,7 +215,6 @@ STORAGE_NAME, { keyPath: 'key' } ); - table.createIndex('value', 'value', { unique: false }); } catch(ex) { req.onerror(); } @@ -349,22 +348,13 @@ const entries = []; const dontCompress = µBlock.hiddenSettings.cacheStorageCompression !== true; - let bytesInUse = 0; 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 }); }; for ( const key of keys ) { const data = keyvalStore[key]; const isString = typeof data === 'string'; if ( isString === false || dontCompress ) { - if ( isString ) { - bytesInUse += data.length; - } entries.push({ key, value: data }); continue; } @@ -379,7 +369,7 @@ if ( callback === undefined ) { return; } let cb = callback; callback = undefined; - cb({ bytesInUse }); + cb(); }; try { const transaction = db.transaction( diff --git a/src/js/storage.js b/src/js/storage.js index 049db27a8..ba0c732b6 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -50,10 +50,13 @@ countdown += 1; vAPI.storage.getBytesInUse(null, process); } - if ( this.cacheStorage.name !== 'browser.storage.local' ) { + if ( + navigator.storage instanceof Object && + navigator.storage.estimate instanceof Function + ) { countdown += 1; - this.assets.getBytesInUse().then(count => { - process(count); + navigator.storage.estimate().then(estimate => { + process(estimate.usage); }); } if ( countdown === 0 ) {