Skip to content

Commit

Permalink
Allow use of browser.storage.local as cache storage backend in Firefox
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#409

By default `indexedDB` is used in Firefox for purpose of cache storage
backend.

This commit allows to force the use of `browser.storage.local` instead
as cache storage backend. For this to happen, set `cacheStorageAPI` to
`browser.storage.local` in advanced settings.

Additionally, should `indexedDB` not be available for whatever reason,
uBO will automatically fallback to `browser.storage.local`.
  • Loading branch information
gorhill committed Feb 17, 2019
1 parent 3b81841 commit 0d369cd
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 481 deletions.
1 change: 0 additions & 1 deletion platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ vAPI.app.restart = function() {
// chrome.storage.local.get(null, function(bin){ console.debug('%o', bin); });

vAPI.storage = chrome.storage.local;
vAPI.cacheStorage = chrome.storage.local;

/******************************************************************************/
/******************************************************************************/
Expand Down
70 changes: 34 additions & 36 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,25 @@ const updateAssetSourceRegistry = function(json, silent) {

const getAssetSourceRegistry = function(callback) {
if ( assetSourceRegistryPromise === undefined ) {
assetSourceRegistryPromise = new Promise(resolve => {
// start of executor
µBlock.cacheStorage.get('assetSourceRegistry', bin => {
assetSourceRegistryPromise = µBlock.cacheStorage.get(
'assetSourceRegistry'
).then(bin => {
if (
bin instanceof Object === false ||
bin.assetSourceRegistry instanceof Object === false
bin instanceof Object &&
bin.assetSourceRegistry instanceof Object
) {
assetSourceRegistry = bin.assetSourceRegistry;
return;
}
return new Promise(resolve => {
api.fetchText(
µBlock.assetsBootstrapLocation || 'assets/assets.json',
details => {
updateAssetSourceRegistry(details.content, true);
resolve();
}
);
return;
}
assetSourceRegistry = bin.assetSourceRegistry;
resolve();
});
// end of executor
});
});
}

Expand Down Expand Up @@ -451,16 +450,15 @@ let assetCacheRegistry = {};

const getAssetCacheRegistry = function() {
if ( assetCacheRegistryPromise === undefined ) {
assetCacheRegistryPromise = new Promise(resolve => {
µBlock.cacheStorage.get('assetCacheRegistry', bin => {
if (
bin instanceof Object &&
bin.assetCacheRegistry instanceof Object
) {
assetCacheRegistry = bin.assetCacheRegistry;
}
resolve();
});
assetCacheRegistryPromise = µBlock.cacheStorage.get(
'assetCacheRegistry'
).then(bin => {
if (
bin instanceof Object &&
bin.assetCacheRegistry instanceof Object
) {
assetCacheRegistry = bin.assetCacheRegistry;
}
});
}

Expand Down Expand Up @@ -509,8 +507,11 @@ const assetCacheRead = function(assetKey, callback) {
reportBack(bin[internalKey]);
};

getAssetCacheRegistry().then(( ) => {
µBlock.cacheStorage.get(internalKey, onAssetRead);
Promise.all([
getAssetCacheRegistry(),
µBlock.cacheStorage.get(internalKey),
]).then(results => {
onAssetRead(results[1]);
});
};

Expand All @@ -537,17 +538,16 @@ const assetCacheWrite = function(assetKey, details, callback) {
entry.remoteURL = details.url;
}
µBlock.cacheStorage.set(
{ [internalKey]: content },
details => {
if (
details instanceof Object &&
typeof details.bytesInUse === 'number'
) {
entry.byteLength = details.bytesInUse;
}
saveAssetCacheRegistry(true);
{ [internalKey]: content }
).then(details => {
if (
details instanceof Object &&
typeof details.bytesInUse === 'number'
) {
entry.byteLength = details.bytesInUse;
}
);
saveAssetCacheRegistry(true);
});
const result = { assetKey, content };
if ( typeof callback === 'function' ) {
callback(result);
Expand All @@ -556,9 +556,7 @@ const assetCacheWrite = function(assetKey, details, callback) {
fireNotification('after-asset-updated', result);
};

getAssetCacheRegistry().then(( ) => {
µBlock.cacheStorage.get(internalKey, onReady);
});
getAssetCacheRegistry().then(( ) => onReady());
};

const assetCacheRemove = function(pattern, callback) {
Expand Down
Loading

0 comments on commit 0d369cd

Please sign in to comment.