Skip to content

Commit 0d369cd

Browse files
committed
Allow use of browser.storage.local as cache storage backend in Firefox
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`.
1 parent 3b81841 commit 0d369cd

File tree

6 files changed

+528
-481
lines changed

6 files changed

+528
-481
lines changed

platform/chromium/vapi-background.js

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ vAPI.app.restart = function() {
9191
// chrome.storage.local.get(null, function(bin){ console.debug('%o', bin); });
9292

9393
vAPI.storage = chrome.storage.local;
94-
vAPI.cacheStorage = chrome.storage.local;
9594

9695
/******************************************************************************/
9796
/******************************************************************************/

src/js/assets.js

+34-36
Original file line numberDiff line numberDiff line change
@@ -396,26 +396,25 @@ const updateAssetSourceRegistry = function(json, silent) {
396396

397397
const getAssetSourceRegistry = function(callback) {
398398
if ( assetSourceRegistryPromise === undefined ) {
399-
assetSourceRegistryPromise = new Promise(resolve => {
400-
// start of executor
401-
µBlock.cacheStorage.get('assetSourceRegistry', bin => {
399+
assetSourceRegistryPromise = µBlock.cacheStorage.get(
400+
'assetSourceRegistry'
401+
).then(bin => {
402402
if (
403-
bin instanceof Object === false ||
404-
bin.assetSourceRegistry instanceof Object === false
403+
bin instanceof Object &&
404+
bin.assetSourceRegistry instanceof Object
405405
) {
406+
assetSourceRegistry = bin.assetSourceRegistry;
407+
return;
408+
}
409+
return new Promise(resolve => {
406410
api.fetchText(
407411
µBlock.assetsBootstrapLocation || 'assets/assets.json',
408412
details => {
409413
updateAssetSourceRegistry(details.content, true);
410414
resolve();
411415
}
412416
);
413-
return;
414-
}
415-
assetSourceRegistry = bin.assetSourceRegistry;
416-
resolve();
417-
});
418-
// end of executor
417+
});
419418
});
420419
}
421420

@@ -451,16 +450,15 @@ let assetCacheRegistry = {};
451450

452451
const getAssetCacheRegistry = function() {
453452
if ( assetCacheRegistryPromise === undefined ) {
454-
assetCacheRegistryPromise = new Promise(resolve => {
455-
µBlock.cacheStorage.get('assetCacheRegistry', bin => {
456-
if (
457-
bin instanceof Object &&
458-
bin.assetCacheRegistry instanceof Object
459-
) {
460-
assetCacheRegistry = bin.assetCacheRegistry;
461-
}
462-
resolve();
463-
});
453+
assetCacheRegistryPromise = µBlock.cacheStorage.get(
454+
'assetCacheRegistry'
455+
).then(bin => {
456+
if (
457+
bin instanceof Object &&
458+
bin.assetCacheRegistry instanceof Object
459+
) {
460+
assetCacheRegistry = bin.assetCacheRegistry;
461+
}
464462
});
465463
}
466464

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

512-
getAssetCacheRegistry().then(( ) => {
513-
µBlock.cacheStorage.get(internalKey, onAssetRead);
510+
Promise.all([
511+
getAssetCacheRegistry(),
512+
µBlock.cacheStorage.get(internalKey),
513+
]).then(results => {
514+
onAssetRead(results[1]);
514515
});
515516
};
516517

@@ -537,17 +538,16 @@ const assetCacheWrite = function(assetKey, details, callback) {
537538
entry.remoteURL = details.url;
538539
}
539540
µBlock.cacheStorage.set(
540-
{ [internalKey]: content },
541-
details => {
542-
if (
543-
details instanceof Object &&
544-
typeof details.bytesInUse === 'number'
545-
) {
546-
entry.byteLength = details.bytesInUse;
547-
}
548-
saveAssetCacheRegistry(true);
541+
{ [internalKey]: content }
542+
).then(details => {
543+
if (
544+
details instanceof Object &&
545+
typeof details.bytesInUse === 'number'
546+
) {
547+
entry.byteLength = details.bytesInUse;
549548
}
550-
);
549+
saveAssetCacheRegistry(true);
550+
});
551551
const result = { assetKey, content };
552552
if ( typeof callback === 'function' ) {
553553
callback(result);
@@ -556,9 +556,7 @@ const assetCacheWrite = function(assetKey, details, callback) {
556556
fireNotification('after-asset-updated', result);
557557
};
558558

559-
getAssetCacheRegistry().then(( ) => {
560-
µBlock.cacheStorage.get(internalKey, onReady);
561-
});
559+
getAssetCacheRegistry().then(( ) => onReady());
562560
};
563561

564562
const assetCacheRemove = function(pattern, callback) {

0 commit comments

Comments
 (0)