Ensure cache storage backend is selected before use

Related commit:
bfa28b960e
This commit is contained in:
Raymond Hill 2023-12-11 12:17:25 -05:00
parent acc2950736
commit a3e3cd9216
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 67 additions and 44 deletions

View File

@ -61,27 +61,40 @@ const STORAGE_NAME = 'uBlock0CacheStorage';
// Default to webext storage.
const storageLocal = webext.storage.local;
let storageReadyResolve;
const storageReadyPromise = new Promise(resolve => {
storageReadyResolve = resolve;
});
const cacheStorage = {
name: 'browser.storage.local',
get(...args) {
return storageLocal.get(...args).catch(reason => {
return storageReadyPromise.then(( ) =>
storageLocal.get(...args).catch(reason => {
console.log(reason);
});
})
);
},
set(...args) {
return storageLocal.set(...args).catch(reason => {
return storageReadyPromise.then(( ) =>
storageLocal.set(...args).catch(reason => {
console.log(reason);
});
})
);
},
remove(...args) {
return storageLocal.remove(...args).catch(reason => {
return storageReadyPromise.then(( ) =>
storageLocal.remove(...args).catch(reason => {
console.log(reason);
});
})
);
},
clear(...args) {
return storageLocal.clear(...args).catch(reason => {
return storageReadyPromise.then(( ) =>
storageLocal.clear(...args).catch(reason => {
console.log(reason);
});
})
);
},
select: function(selectedBackend) {
let actualBackend = selectedBackend;
@ -94,15 +107,18 @@ const cacheStorage = {
return selectIDB().then(success => {
if ( success || selectedBackend === 'indexedDB' ) {
clearWebext();
storageReadyResolve();
return 'indexedDB';
}
clearIDB();
storageReadyResolve();
return 'browser.storage.local';
});
}
if ( actualBackend === 'browser.storage.local' ) {
clearIDB();
}
storageReadyResolve();
return Promise.resolve('browser.storage.local');
},
@ -435,7 +451,8 @@ const selectIDB = async function() {
cacheStorage.name = 'indexedDB';
cacheStorage.get = function get(keys) {
return new Promise(resolve => {
return storageReadyPromise.then(( ) =>
new Promise(resolve => {
if ( keys === null ) {
return getAllFromDb(bin => resolve(bin));
}
@ -449,22 +466,29 @@ const selectIDB = async function() {
output = keys;
}
getFromDb(toRead, output, bin => resolve(bin));
});
})
);
};
cacheStorage.set = function set(keys) {
return new Promise(resolve => {
return storageReadyPromise.then(( ) =>
new Promise(resolve => {
putToDb(keys, details => resolve(details));
});
})
);
};
cacheStorage.remove = function remove(keys) {
return new Promise(resolve => {
return storageReadyPromise.then(( ) =>
new Promise(resolve => {
deleteFromDb(keys, ( ) => resolve());
});
})
);
};
cacheStorage.clear = function clear() {
return new Promise(resolve => {
return storageReadyPromise.then(( ) =>
new Promise(resolve => {
clearDb(( ) => resolve());
});
})
);
};
cacheStorage.getBytesInUse = function getBytesInUse() {
return Promise.resolve(0);
@ -475,18 +499,17 @@ const selectIDB = async function() {
// https://github.com/uBlockOrigin/uBlock-issues/issues/328
// Delete cache-related entries from webext storage.
const clearWebext = async function() {
const bin = await webext.storage.local.get('assetCacheRegistry');
if (
bin instanceof Object === false ||
bin.assetCacheRegistry instanceof Object === false
) {
return;
let bin;
try {
bin = await webext.storage.local.get('assetCacheRegistry');
} catch(ex) {
console.error(ex);
}
if ( bin instanceof Object === false ) { return; }
if ( bin.assetCacheRegistry instanceof Object === false ) { return; }
const toRemove = [
'assetCacheRegistry',
'assetSourceRegistry',
'resourcesSelfie',
'selfie'
];
for ( const key in bin.assetCacheRegistry ) {
if ( bin.assetCacheRegistry.hasOwnProperty(key) ) {