remove obsolete indexedDB transition-related code

This commit is contained in:
gorhill 2017-11-30 08:31:28 -05:00
parent 39bf7b4c6b
commit 5518e2681a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 13 additions and 50 deletions

View File

@ -108,8 +108,6 @@ vAPI.cacheStorage = (function() {
} }
pending.push(callback); pending.push(callback);
if ( pending.length !== 1 ) { return; } if ( pending.length !== 1 ) { return; }
// This will fail in private browsing mode.
var req = indexedDB.open(STORAGE_NAME, 1);
// https://github.com/gorhill/uBlock/issues/3156 // https://github.com/gorhill/uBlock/issues/3156
// I have observed that no event was fired in Tor Browser 7.0.7 + // 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 // medium security level after the request to open the database was
@ -119,15 +117,20 @@ vAPI.cacheStorage = (function() {
// necessary when reading the `error` property because we are not // necessary when reading the `error` property because we are not
// allowed to read this propery outside of event handlers in newer // allowed to read this propery outside of event handlers in newer
// implementation of IDBRequest (my understanding). // implementation of IDBRequest (my understanding).
var req;
try { try {
req = indexedDB.open(STORAGE_NAME, 1);
if ( req.error ) { if ( req.error ) {
console.log(req.error); console.log(req.error);
req = undefined;
}
} catch(ex) {
}
if ( req === undefined ) {
processPendings(); processPendings();
pending = undefined; pending = undefined;
return; return;
} }
} catch(ex) {
}
req.onupgradeneeded = function(ev) { req.onupgradeneeded = function(ev) {
req = undefined; req = undefined;
db = ev.target.result; db = ev.target.result;
@ -152,11 +155,9 @@ vAPI.cacheStorage = (function() {
function getFromDb(keys, store, callback) { function getFromDb(keys, store, callback) {
if ( typeof callback !== 'function' ) { return; } if ( typeof callback !== 'function' ) { return; }
if ( keys.length === 0 ) { return callback(store); } if ( keys.length === 0 ) { return callback(store); }
var notfoundKeys = new Set(keys);
var gotOne = function() { var gotOne = function() {
if ( typeof this.result === 'object' ) { if ( typeof this.result === 'object' ) {
store[this.result.key] = this.result.value; store[this.result.key] = this.result.value;
notfoundKeys.delete(this.result.key);
} }
}; };
getDb(function(db) { getDb(function(db) {
@ -165,55 +166,18 @@ vAPI.cacheStorage = (function() {
transaction.oncomplete = transaction.oncomplete =
transaction.onerror = transaction.onerror =
transaction.onabort = function() { transaction.onabort = function() {
// TODO: remove once storage.local is clean
if ( notfoundKeys.size === 0 ) {
vAPI.storage.remove(keys);
return callback(store); return callback(store);
}
maybeMigrate(Array.from(notfoundKeys), store, callback);
}; };
var table = transaction.objectStore(STORAGE_NAME); var table = transaction.objectStore(STORAGE_NAME);
for ( var key of keys ) { for ( var key of keys ) {
var req = table.get(key); var req = table.get(key);
req.onsuccess = gotOne; req.onsuccess = gotOne;
req.onerror = noopfn; req.onerror = noopfn;
req = undefined;
} }
}); });
} }
// Migrate from storage API
// TODO: removes once all users are migrated to the new cacheStorage.
function maybeMigrate(keys, store, callback) {
var toMigrate = new Set(),
i = keys.length;
while ( i-- ) {
var key = keys[i];
toMigrate.add(key);
// If migrating a compiled list, also migrate the non-compiled
// counterpart.
if ( /^cache\/compiled\//.test(key) ) {
toMigrate.add(key.replace('/compiled', ''));
}
}
vAPI.storage.get(Array.from(toMigrate), function(bin) {
if ( bin instanceof Object === false ) {
return callback(store);
}
var migratedKeys = Object.keys(bin);
if ( migratedKeys.length === 0 ) {
return callback(store);
}
var i = migratedKeys.length;
while ( i-- ) {
var key = migratedKeys[i];
store[key] = bin[key];
}
vAPI.storage.remove(migratedKeys);
vAPI.cacheStorage.set(bin);
callback(store);
});
}
function getAllFromDb(callback) { function getAllFromDb(callback) {
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
callback = noopfn; callback = noopfn;
@ -250,12 +214,13 @@ vAPI.cacheStorage = (function() {
transaction.oncomplete = transaction.oncomplete =
transaction.onerror = transaction.onerror =
transaction.onabort = callback; transaction.onabort = callback;
var table = transaction.objectStore(STORAGE_NAME), var table = transaction.objectStore(STORAGE_NAME);
entry = {};
for ( var key of keys ) { for ( var key of keys ) {
var entry = {};
entry.key = key; entry.key = key;
entry.value = input[key]; entry.value = input[key];
table.put(entry); table.put(entry);
entry = undefined;
} }
}); });
} }
@ -277,8 +242,6 @@ vAPI.cacheStorage = (function() {
table.delete(key); table.delete(key);
} }
}); });
// TODO: removes once all users are migrated to the new cacheStorage.
vAPI.storage.remove(keys);
} }
function clearDb(callback) { function clearDb(callback) {