mirror of https://github.com/gorhill/uBlock.git
Ensure cache storage backend is selected before use
Related commit:
bfa28b960e
This commit is contained in:
parent
acc2950736
commit
a3e3cd9216
|
@ -61,27 +61,40 @@ const STORAGE_NAME = 'uBlock0CacheStorage';
|
||||||
// Default to webext storage.
|
// Default to webext storage.
|
||||||
const storageLocal = webext.storage.local;
|
const storageLocal = webext.storage.local;
|
||||||
|
|
||||||
|
let storageReadyResolve;
|
||||||
|
const storageReadyPromise = new Promise(resolve => {
|
||||||
|
storageReadyResolve = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
const cacheStorage = {
|
const cacheStorage = {
|
||||||
name: 'browser.storage.local',
|
name: 'browser.storage.local',
|
||||||
get(...args) {
|
get(...args) {
|
||||||
return storageLocal.get(...args).catch(reason => {
|
return storageReadyPromise.then(( ) =>
|
||||||
console.log(reason);
|
storageLocal.get(...args).catch(reason => {
|
||||||
});
|
console.log(reason);
|
||||||
|
})
|
||||||
|
);
|
||||||
},
|
},
|
||||||
set(...args) {
|
set(...args) {
|
||||||
return storageLocal.set(...args).catch(reason => {
|
return storageReadyPromise.then(( ) =>
|
||||||
console.log(reason);
|
storageLocal.set(...args).catch(reason => {
|
||||||
});
|
console.log(reason);
|
||||||
|
})
|
||||||
|
);
|
||||||
},
|
},
|
||||||
remove(...args) {
|
remove(...args) {
|
||||||
return storageLocal.remove(...args).catch(reason => {
|
return storageReadyPromise.then(( ) =>
|
||||||
console.log(reason);
|
storageLocal.remove(...args).catch(reason => {
|
||||||
});
|
console.log(reason);
|
||||||
|
})
|
||||||
|
);
|
||||||
},
|
},
|
||||||
clear(...args) {
|
clear(...args) {
|
||||||
return storageLocal.clear(...args).catch(reason => {
|
return storageReadyPromise.then(( ) =>
|
||||||
console.log(reason);
|
storageLocal.clear(...args).catch(reason => {
|
||||||
});
|
console.log(reason);
|
||||||
|
})
|
||||||
|
);
|
||||||
},
|
},
|
||||||
select: function(selectedBackend) {
|
select: function(selectedBackend) {
|
||||||
let actualBackend = selectedBackend;
|
let actualBackend = selectedBackend;
|
||||||
|
@ -94,15 +107,18 @@ const cacheStorage = {
|
||||||
return selectIDB().then(success => {
|
return selectIDB().then(success => {
|
||||||
if ( success || selectedBackend === 'indexedDB' ) {
|
if ( success || selectedBackend === 'indexedDB' ) {
|
||||||
clearWebext();
|
clearWebext();
|
||||||
|
storageReadyResolve();
|
||||||
return 'indexedDB';
|
return 'indexedDB';
|
||||||
}
|
}
|
||||||
clearIDB();
|
clearIDB();
|
||||||
|
storageReadyResolve();
|
||||||
return 'browser.storage.local';
|
return 'browser.storage.local';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if ( actualBackend === 'browser.storage.local' ) {
|
if ( actualBackend === 'browser.storage.local' ) {
|
||||||
clearIDB();
|
clearIDB();
|
||||||
}
|
}
|
||||||
|
storageReadyResolve();
|
||||||
return Promise.resolve('browser.storage.local');
|
return Promise.resolve('browser.storage.local');
|
||||||
|
|
||||||
},
|
},
|
||||||
|
@ -435,36 +451,44 @@ const selectIDB = async function() {
|
||||||
|
|
||||||
cacheStorage.name = 'indexedDB';
|
cacheStorage.name = 'indexedDB';
|
||||||
cacheStorage.get = function get(keys) {
|
cacheStorage.get = function get(keys) {
|
||||||
return new Promise(resolve => {
|
return storageReadyPromise.then(( ) =>
|
||||||
if ( keys === null ) {
|
new Promise(resolve => {
|
||||||
return getAllFromDb(bin => resolve(bin));
|
if ( keys === null ) {
|
||||||
}
|
return getAllFromDb(bin => resolve(bin));
|
||||||
let toRead, output = {};
|
}
|
||||||
if ( typeof keys === 'string' ) {
|
let toRead, output = {};
|
||||||
toRead = [ keys ];
|
if ( typeof keys === 'string' ) {
|
||||||
} else if ( Array.isArray(keys) ) {
|
toRead = [ keys ];
|
||||||
toRead = keys;
|
} else if ( Array.isArray(keys) ) {
|
||||||
} else /* if ( typeof keys === 'object' ) */ {
|
toRead = keys;
|
||||||
toRead = Object.keys(keys);
|
} else /* if ( typeof keys === 'object' ) */ {
|
||||||
output = keys;
|
toRead = Object.keys(keys);
|
||||||
}
|
output = keys;
|
||||||
getFromDb(toRead, output, bin => resolve(bin));
|
}
|
||||||
});
|
getFromDb(toRead, output, bin => resolve(bin));
|
||||||
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
cacheStorage.set = function set(keys) {
|
cacheStorage.set = function set(keys) {
|
||||||
return new Promise(resolve => {
|
return storageReadyPromise.then(( ) =>
|
||||||
putToDb(keys, details => resolve(details));
|
new Promise(resolve => {
|
||||||
});
|
putToDb(keys, details => resolve(details));
|
||||||
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
cacheStorage.remove = function remove(keys) {
|
cacheStorage.remove = function remove(keys) {
|
||||||
return new Promise(resolve => {
|
return storageReadyPromise.then(( ) =>
|
||||||
deleteFromDb(keys, ( ) => resolve());
|
new Promise(resolve => {
|
||||||
});
|
deleteFromDb(keys, ( ) => resolve());
|
||||||
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
cacheStorage.clear = function clear() {
|
cacheStorage.clear = function clear() {
|
||||||
return new Promise(resolve => {
|
return storageReadyPromise.then(( ) =>
|
||||||
clearDb(( ) => resolve());
|
new Promise(resolve => {
|
||||||
});
|
clearDb(( ) => resolve());
|
||||||
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
cacheStorage.getBytesInUse = function getBytesInUse() {
|
cacheStorage.getBytesInUse = function getBytesInUse() {
|
||||||
return Promise.resolve(0);
|
return Promise.resolve(0);
|
||||||
|
@ -475,18 +499,17 @@ const selectIDB = async function() {
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/328
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/328
|
||||||
// Delete cache-related entries from webext storage.
|
// Delete cache-related entries from webext storage.
|
||||||
const clearWebext = async function() {
|
const clearWebext = async function() {
|
||||||
const bin = await webext.storage.local.get('assetCacheRegistry');
|
let bin;
|
||||||
if (
|
try {
|
||||||
bin instanceof Object === false ||
|
bin = await webext.storage.local.get('assetCacheRegistry');
|
||||||
bin.assetCacheRegistry instanceof Object === false
|
} catch(ex) {
|
||||||
) {
|
console.error(ex);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if ( bin instanceof Object === false ) { return; }
|
||||||
|
if ( bin.assetCacheRegistry instanceof Object === false ) { return; }
|
||||||
const toRemove = [
|
const toRemove = [
|
||||||
'assetCacheRegistry',
|
'assetCacheRegistry',
|
||||||
'assetSourceRegistry',
|
'assetSourceRegistry',
|
||||||
'resourcesSelfie',
|
|
||||||
'selfie'
|
|
||||||
];
|
];
|
||||||
for ( const key in bin.assetCacheRegistry ) {
|
for ( const key in bin.assetCacheRegistry ) {
|
||||||
if ( bin.assetCacheRegistry.hasOwnProperty(key) ) {
|
if ( bin.assetCacheRegistry.hasOwnProperty(key) ) {
|
||||||
|
|
Loading…
Reference in New Issue