2017-08-28 21:30:01 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-08-06 18:34:41 +02:00
|
|
|
Copyright (C) 2016-present The uBlock Origin authors
|
2017-08-28 21:30:01 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2018-08-06 18:34:41 +02:00
|
|
|
/* global IDBDatabase, indexedDB */
|
2017-08-30 00:32:00 +02:00
|
|
|
|
2017-08-28 21:30:01 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// The code below has been originally manually imported from:
|
|
|
|
// Commit: https://github.com/nikrolls/uBlock-Edge/commit/d1538ea9bea89d507219d3219592382eee306134
|
|
|
|
// Commit date: 29 October 2016
|
|
|
|
// Commit author: https://github.com/nikrolls
|
|
|
|
// Commit message: "Implement cacheStorage using IndexedDB"
|
|
|
|
|
2017-08-30 00:32:00 +02:00
|
|
|
// The original imported code has been subsequently modified as it was not
|
|
|
|
// compatible with Firefox.
|
|
|
|
// (a Promise thing, see https://github.com/dfahlander/Dexie.js/issues/317)
|
2019-02-17 21:40:09 +01:00
|
|
|
// Furthermore, code to migrate from browser.storage.local to vAPI.storage
|
2017-08-30 00:32:00 +02:00
|
|
|
// has been added, for seamless migration of cache-related entries into
|
|
|
|
// indexedDB.
|
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1371255
|
|
|
|
// Firefox-specific: we use indexedDB because chrome.storage.local() has
|
|
|
|
// poor performance in Firefox.
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/328
|
|
|
|
// Use IndexedDB for Chromium as well, to take advantage of LZ4
|
|
|
|
// compression.
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/399
|
|
|
|
// Revert Chromium support of IndexedDB, use advanced setting to force
|
|
|
|
// IndexedDB.
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/409
|
|
|
|
// Allow forcing the use of webext storage on Firefox.
|
|
|
|
|
2018-08-11 16:39:43 +02:00
|
|
|
µBlock.cacheStorage = (function() {
|
2018-04-26 17:36:22 +02:00
|
|
|
|
2018-12-06 01:18:20 +01:00
|
|
|
const STORAGE_NAME = 'uBlock0CacheStorage';
|
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
// Default to webext storage. Wrapped into promises if the API does not
|
|
|
|
// support returning promises.
|
|
|
|
const promisified = (function() {
|
|
|
|
try {
|
|
|
|
return browser.storage.local.get('_') instanceof Promise;
|
2017-08-30 00:32:00 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
catch(ex) {
|
2017-08-30 00:32:00 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
return false;
|
|
|
|
})();
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2018-12-06 01:18:20 +01:00
|
|
|
const api = {
|
2019-02-17 21:40:09 +01:00
|
|
|
name: 'browser.storage.local',
|
|
|
|
get: promisified ?
|
|
|
|
browser.storage.local.get :
|
|
|
|
function(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.storage.local.get(keys, bin => {
|
|
|
|
resolve(bin);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
set: promisified ?
|
|
|
|
browser.storage.local.set :
|
|
|
|
function(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.storage.local.set(keys, ( ) => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
remove: promisified ?
|
|
|
|
browser.storage.local.remove :
|
|
|
|
function(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.storage.local.remove(keys, ( ) => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clear: promisified ?
|
|
|
|
browser.storage.local.clear :
|
|
|
|
function() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.storage.local.clear(( ) => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getBytesInUse: promisified ?
|
|
|
|
browser.storage.local.getBytesInUse :
|
|
|
|
function(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.storage.local.getBytesInUse(keys, count => {
|
|
|
|
resolve(count);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-02-20 13:05:45 +01:00
|
|
|
select: function(selectedBackend) {
|
|
|
|
let actualBackend = selectedBackend;
|
|
|
|
if ( actualBackend === undefined || actualBackend === 'unset' ) {
|
|
|
|
actualBackend = vAPI.webextFlavor.soup.has('firefox')
|
2019-02-17 21:40:09 +01:00
|
|
|
? 'indexedDB'
|
|
|
|
: 'browser.storage.local';
|
|
|
|
}
|
2019-02-20 13:05:45 +01:00
|
|
|
if ( actualBackend === 'indexedDB' ) {
|
2019-02-17 21:40:09 +01:00
|
|
|
return selectIDB().then(success => {
|
2019-02-20 13:05:45 +01:00
|
|
|
if ( success || selectedBackend === 'indexedDB' ) {
|
2019-02-17 21:40:09 +01:00
|
|
|
clearWebext();
|
|
|
|
return 'indexedDB';
|
|
|
|
}
|
|
|
|
clearIDB();
|
|
|
|
return 'browser.storage.local';
|
|
|
|
});
|
|
|
|
}
|
2019-02-20 13:05:45 +01:00
|
|
|
if ( actualBackend === 'browser.storage.local' ) {
|
2019-02-17 21:40:09 +01:00
|
|
|
clearIDB();
|
|
|
|
}
|
|
|
|
return Promise.resolve('browser.storage.local');
|
|
|
|
|
|
|
|
},
|
2018-08-11 16:39:43 +02:00
|
|
|
error: undefined
|
|
|
|
};
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
// Reassign API entries to that of indexedDB-based ones
|
|
|
|
const selectIDB = function() {
|
2019-02-26 16:37:25 +01:00
|
|
|
let db;
|
2019-02-17 21:40:09 +01:00
|
|
|
let dbPromise;
|
|
|
|
let dbTimer;
|
2017-08-30 14:41:22 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const genericErrorHandler = function(ev) {
|
2019-02-26 16:37:25 +01:00
|
|
|
const error = ev.target && ev.target.error;
|
2019-02-17 21:40:09 +01:00
|
|
|
if ( error && error.name === 'QuotaExceededError' ) {
|
|
|
|
api.error = error.name;
|
|
|
|
}
|
|
|
|
console.error('[%s]', STORAGE_NAME, error && error.name);
|
|
|
|
};
|
2018-12-07 22:51:18 +01:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const noopfn = function () {
|
|
|
|
};
|
2018-12-07 22:51:18 +01:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const disconnect = function() {
|
|
|
|
if ( dbTimer !== undefined ) {
|
|
|
|
clearTimeout(dbTimer);
|
2018-12-07 22:51:18 +01:00
|
|
|
dbTimer = undefined;
|
2019-02-17 21:40:09 +01:00
|
|
|
}
|
2019-02-26 16:37:25 +01:00
|
|
|
if ( db instanceof IDBDatabase ) {
|
|
|
|
db.close();
|
|
|
|
db = undefined;
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const keepAlive = function() {
|
|
|
|
if ( dbTimer !== undefined ) {
|
|
|
|
clearTimeout(dbTimer);
|
|
|
|
}
|
|
|
|
dbTimer = vAPI.setTimeout(
|
|
|
|
( ) => {
|
|
|
|
dbTimer = undefined;
|
|
|
|
disconnect();
|
|
|
|
},
|
|
|
|
Math.max(
|
|
|
|
µBlock.hiddenSettings.autoUpdateAssetFetchPeriod * 2 * 1000,
|
|
|
|
180000
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
2018-12-07 22:51:18 +01:00
|
|
|
|
2017-10-21 14:43:45 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3156
|
|
|
|
// I have observed that no event was fired in Tor Browser 7.0.7 +
|
|
|
|
// medium security level after the request to open the database was
|
|
|
|
// created. When this occurs, I have also observed that the `error`
|
|
|
|
// property was already set, so this means uBO can detect here whether
|
|
|
|
// the database can be opened successfully. A try-catch block is
|
|
|
|
// necessary when reading the `error` property because we are not
|
|
|
|
// allowed to read this propery outside of event handlers in newer
|
|
|
|
// implementation of IDBRequest (my understanding).
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const getDb = function() {
|
|
|
|
keepAlive();
|
2019-02-26 16:37:25 +01:00
|
|
|
if ( db !== undefined ) {
|
|
|
|
return Promise.resolve(db);
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
if ( dbPromise !== undefined ) {
|
|
|
|
return dbPromise;
|
2017-08-30 00:32:00 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
dbPromise = new Promise(resolve => {
|
|
|
|
let req;
|
|
|
|
try {
|
|
|
|
req = indexedDB.open(STORAGE_NAME, 1);
|
|
|
|
if ( req.error ) {
|
|
|
|
console.log(req.error);
|
|
|
|
req = undefined;
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
if ( req === undefined ) {
|
2019-02-26 16:37:25 +01:00
|
|
|
db = null;
|
|
|
|
dbPromise = undefined;
|
2019-02-17 21:40:09 +01:00
|
|
|
return resolve(null);
|
|
|
|
}
|
|
|
|
req.onupgradeneeded = function(ev) {
|
|
|
|
req = undefined;
|
|
|
|
const db = ev.target.result;
|
|
|
|
db.onerror = db.onabort = genericErrorHandler;
|
|
|
|
const table = db.createObjectStore(
|
|
|
|
STORAGE_NAME,
|
|
|
|
{ keyPath: 'key' }
|
|
|
|
);
|
|
|
|
table.createIndex('value', 'value', { unique: false });
|
|
|
|
};
|
|
|
|
req.onsuccess = function(ev) {
|
|
|
|
req = undefined;
|
2019-02-26 16:37:25 +01:00
|
|
|
db = ev.target.result;
|
2019-02-17 21:40:09 +01:00
|
|
|
db.onerror = db.onabort = genericErrorHandler;
|
2019-02-26 16:37:25 +01:00
|
|
|
dbPromise = undefined;
|
2019-02-17 21:40:09 +01:00
|
|
|
resolve(db);
|
|
|
|
};
|
|
|
|
req.onerror = req.onblocked = function() {
|
|
|
|
req = undefined;
|
|
|
|
console.log(this.error);
|
2019-02-26 16:37:25 +01:00
|
|
|
db = null;
|
|
|
|
dbPromise = undefined;
|
2019-02-17 21:40:09 +01:00
|
|
|
resolve(null);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return dbPromise;
|
|
|
|
};
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const getFromDb = function(keys, keyvalStore, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) { return; }
|
|
|
|
if ( keys.length === 0 ) { return callback(keyvalStore); }
|
|
|
|
let promises = [];
|
|
|
|
let gotOne = function() {
|
|
|
|
if ( typeof this.result !== 'object' ) { return; }
|
|
|
|
keyvalStore[this.result.key] = this.result.value;
|
|
|
|
if ( this.result.value instanceof Blob === false ) { return; }
|
|
|
|
promises.push(
|
|
|
|
µBlock.lz4Codec.decode(
|
|
|
|
this.result.key,
|
|
|
|
this.result.value
|
|
|
|
).then(result => {
|
|
|
|
keyvalStore[result.key] = result.data;
|
|
|
|
})
|
|
|
|
);
|
2017-08-30 00:32:00 +02:00
|
|
|
};
|
2019-02-17 21:40:09 +01:00
|
|
|
getDb().then(db => {
|
|
|
|
if ( !db ) { return callback(); }
|
|
|
|
const transaction = db.transaction(STORAGE_NAME);
|
|
|
|
transaction.oncomplete =
|
|
|
|
transaction.onerror =
|
|
|
|
transaction.onabort = ( ) => {
|
|
|
|
Promise.all(promises).then(( ) => {
|
|
|
|
callback(keyvalStore);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const table = transaction.objectStore(STORAGE_NAME);
|
|
|
|
for ( const key of keys ) {
|
|
|
|
let req = table.get(key);
|
|
|
|
req.onsuccess = gotOne;
|
|
|
|
req.onerror = noopfn;
|
|
|
|
req = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-08-11 16:39:43 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const visitAllFromDb = function(visitFn) {
|
|
|
|
getDb().then(db => {
|
|
|
|
if ( !db ) { return visitFn(); }
|
|
|
|
const transaction = db.transaction(STORAGE_NAME);
|
|
|
|
transaction.oncomplete =
|
|
|
|
transaction.onerror =
|
|
|
|
transaction.onabort = ( ) => visitFn();
|
|
|
|
const table = transaction.objectStore(STORAGE_NAME);
|
|
|
|
const req = table.openCursor();
|
|
|
|
req.onsuccess = function(ev) {
|
|
|
|
let cursor = ev.target && ev.target.result;
|
|
|
|
if ( !cursor ) { return; }
|
|
|
|
let entry = cursor.value;
|
|
|
|
visitFn(entry);
|
|
|
|
cursor.continue();
|
|
|
|
};
|
2018-08-11 16:39:43 +02:00
|
|
|
});
|
2019-02-17 21:40:09 +01:00
|
|
|
};
|
2018-08-11 16:39:43 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const getAllFromDb = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) { return; }
|
|
|
|
const promises = [];
|
|
|
|
const keyvalStore = {};
|
|
|
|
visitAllFromDb(entry => {
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
Promise.all(promises).then(( ) => {
|
|
|
|
callback(keyvalStore);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
keyvalStore[entry.key] = entry.value;
|
|
|
|
if ( entry.value instanceof Blob === false ) { return; }
|
|
|
|
promises.push(
|
|
|
|
µBlock.lz4Codec.decode(
|
|
|
|
entry.key,
|
|
|
|
entry.value
|
|
|
|
).then(result => {
|
|
|
|
keyvalStore[result.key] = result.value;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
2018-08-11 16:39:43 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/141
|
|
|
|
// Mind that IDBDatabase.transaction() and IDBObjectStore.put()
|
|
|
|
// can throw:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/transaction
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/put
|
2018-07-23 19:13:47 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const putToDb = function(keyvalStore, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = noopfn;
|
2019-02-14 19:33:55 +01:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
const keys = Object.keys(keyvalStore);
|
|
|
|
if ( keys.length === 0 ) { return callback(); }
|
|
|
|
const promises = [ getDb() ];
|
|
|
|
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;
|
2019-02-14 19:33:55 +01:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
entries.push({ key: result.key, value: result.data });
|
2018-07-23 19:13:47 +02:00
|
|
|
};
|
2019-02-17 21:40:09 +01:00
|
|
|
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;
|
2018-07-23 19:13:47 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
promises.push(
|
|
|
|
µBlock.lz4Codec.encode(key, data).then(handleEncodingResult)
|
|
|
|
);
|
2017-08-30 00:32:00 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
Promise.all(promises).then(results => {
|
|
|
|
const db = results[0];
|
|
|
|
if ( !db ) { return callback(); }
|
|
|
|
const finish = ( ) => {
|
|
|
|
if ( callback === undefined ) { return; }
|
|
|
|
let cb = callback;
|
|
|
|
callback = undefined;
|
|
|
|
cb({ bytesInUse });
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const transaction = db.transaction(
|
|
|
|
STORAGE_NAME,
|
|
|
|
'readwrite'
|
|
|
|
);
|
|
|
|
transaction.oncomplete =
|
|
|
|
transaction.onerror =
|
|
|
|
transaction.onabort = finish;
|
|
|
|
const table = transaction.objectStore(STORAGE_NAME);
|
|
|
|
for ( const entry of entries ) {
|
|
|
|
table.put(entry);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
const deleteFromDb = function(input, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = noopfn;
|
|
|
|
}
|
|
|
|
const keys = Array.isArray(input) ? input.slice() : [ input ];
|
|
|
|
if ( keys.length === 0 ) { return callback(); }
|
|
|
|
getDb().then(db => {
|
|
|
|
if ( !db ) { return callback(); }
|
|
|
|
let finish = ( ) => {
|
|
|
|
if ( callback === undefined ) { return; }
|
|
|
|
let cb = callback;
|
|
|
|
callback = undefined;
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
let transaction = db.transaction(STORAGE_NAME, 'readwrite');
|
|
|
|
transaction.oncomplete =
|
|
|
|
transaction.onerror =
|
|
|
|
transaction.onabort = finish;
|
|
|
|
let table = transaction.objectStore(STORAGE_NAME);
|
|
|
|
for ( let key of keys ) {
|
|
|
|
table.delete(key);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
finish();
|
2018-08-06 18:34:41 +02:00
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearDb = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
callback = noopfn;
|
2017-08-30 00:32:00 +02:00
|
|
|
}
|
2019-02-26 16:37:25 +01:00
|
|
|
disconnect();
|
|
|
|
try {
|
|
|
|
const req = indexedDB.deleteDatabase(STORAGE_NAME);
|
|
|
|
req.onsuccess = req.onerror = ( ) => {
|
|
|
|
callback();
|
2019-02-17 21:40:09 +01:00
|
|
|
};
|
2019-02-26 16:37:25 +01:00
|
|
|
} catch(ex) {
|
|
|
|
callback();
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
};
|
2017-08-28 21:30:01 +02:00
|
|
|
|
2019-02-17 21:40:09 +01:00
|
|
|
return getDb().then(db => {
|
|
|
|
if ( !db ) { return false; }
|
|
|
|
api.name = 'indexedDB';
|
|
|
|
api.get = function get(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if ( keys === null ) {
|
|
|
|
return getAllFromDb(bin => resolve(bin));
|
|
|
|
}
|
|
|
|
let toRead, output = {};
|
|
|
|
if ( typeof keys === 'string' ) {
|
|
|
|
toRead = [ keys ];
|
|
|
|
} else if ( Array.isArray(keys) ) {
|
|
|
|
toRead = keys;
|
|
|
|
} else /* if ( typeof keys === 'object' ) */ {
|
|
|
|
toRead = Object.keys(keys);
|
|
|
|
output = keys;
|
|
|
|
}
|
|
|
|
getFromDb(toRead, output, bin => resolve(bin));
|
|
|
|
});
|
2018-08-06 18:34:41 +02:00
|
|
|
};
|
2019-02-17 21:40:09 +01:00
|
|
|
api.set = function set(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
putToDb(keys, details => resolve(details));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
api.remove = function remove(keys) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
deleteFromDb(keys, ( ) => resolve());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
api.clear = function clear() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
clearDb(( ) => resolve());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
api.getBytesInUse = function getBytesInUse() {
|
|
|
|
return Promise.resolve(0);
|
|
|
|
};
|
|
|
|
return true;
|
2017-08-30 00:32:00 +02:00
|
|
|
});
|
2018-08-06 18:34:41 +02:00
|
|
|
};
|
|
|
|
|
2018-12-06 01:18:20 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/328
|
2019-02-17 21:40:09 +01:00
|
|
|
// Delete cache-related entries from webext storage.
|
|
|
|
const clearWebext = function() {
|
|
|
|
browser.storage.local.get('assetCacheRegistry', bin => {
|
|
|
|
if (
|
|
|
|
bin instanceof Object === false ||
|
|
|
|
bin.assetCacheRegistry instanceof Object === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const toRemove = [
|
|
|
|
'assetCacheRegistry',
|
|
|
|
'assetSourceRegistry',
|
|
|
|
'resourcesSelfie',
|
|
|
|
'selfie'
|
|
|
|
];
|
|
|
|
for ( const key in bin.assetCacheRegistry ) {
|
|
|
|
if ( bin.assetCacheRegistry.hasOwnProperty(key) ) {
|
|
|
|
toRemove.push('cache/' + key);
|
2018-12-06 01:18:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
browser.storage.local.remove(toRemove);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearIDB = function() {
|
2019-02-24 17:28:28 +01:00
|
|
|
try {
|
|
|
|
indexedDB.deleteDatabase(STORAGE_NAME);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
2019-02-17 21:40:09 +01:00
|
|
|
};
|
2018-12-06 01:18:20 +01:00
|
|
|
|
2018-08-06 18:34:41 +02:00
|
|
|
return api;
|
2017-08-28 21:30:01 +02:00
|
|
|
}());
|
|
|
|
|
|
|
|
/******************************************************************************/
|