Work toward modernizing code base: promisification

Swathes of code have been converted to use
Promises/async/await. More left to do.

In the process, a regression affecting the fix to
<https://github.com/uBlockOrigin/uBlock-issues/issues/682>
has been fixed.
This commit is contained in:
Raymond Hill 2019-09-15 07:58:28 -04:00
parent 6c7d3a40d6
commit e27328f931
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
7 changed files with 1032 additions and 1207 deletions

View file

@ -43,6 +43,15 @@ vAPI.lastError = function() {
return chrome.runtime.lastError; return chrome.runtime.lastError;
}; };
vAPI.apiIsPromisified = (( ) => {
try {
return browser.storage.local.get('_') instanceof Promise;
}
catch(ex) {
}
return false;
})();
// https://github.com/gorhill/uBlock/issues/875 // https://github.com/gorhill/uBlock/issues/875
// https://code.google.com/p/chromium/issues/detail?id=410868#c8 // https://code.google.com/p/chromium/issues/detail?id=410868#c8
// Must not leave `lastError` unchecked. // Must not leave `lastError` unchecked.
@ -107,9 +116,83 @@ vAPI.app = {
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// chrome.storage.local.get(null, function(bin){ console.debug('%o', bin); }); vAPI.storage = (( ) => {
if ( vAPI.apiIsPromisified ) {
vAPI.storage = browser.storage.local; return browser.storage.local;
}
return {
clear: function(callback) {
if ( callback !== undefined ) {
return browser.storage.local.clear(...arguments);
}
return new Promise((resolve, reject) => {
browser.storage.local.clear(( ) => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
get: function(keys, callback) {
if ( callback !== undefined ) {
return browser.storage.local.get(...arguments);
}
return new Promise((resolve, reject) => {
browser.storage.local.get(keys, result => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
getBytesInUse: function(keys, callback) {
if ( callback !== undefined ) {
return browser.storage.local.getBytesInUse(...arguments);
}
return new Promise((resolve, reject) => {
browser.storage.local.getBytesInUse(keys, result => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
remove: function(keys, callback) {
if ( callback !== undefined ) {
return browser.storage.local.remove(...arguments);
}
return new Promise((resolve, reject) => {
browser.storage.local.remove(keys, ( ) => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
set: function(items, callback) {
if ( callback !== undefined ) {
return browser.storage.local.set(...arguments);
}
return new Promise((resolve, reject) => {
browser.storage.local.set(items, ( ) => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve();
});
});
},
};
})();
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
@ -1286,7 +1369,7 @@ vAPI.commands = chrome.commands;
/******************************************************************************/ /******************************************************************************/
// https://github.com/gorhill/uBlock/issues/531 // https://github.com/gorhill/uBlock/issues/531
// Storage area dedicated to admin settings. Read-only. // Storage area dedicated to admin settings. Read-only.
// https://github.com/gorhill/uBlock/commit/43a5ed735b95a575a9339b6e71a1fcb27a99663b#commitcomment-13965030 // https://github.com/gorhill/uBlock/commit/43a5ed735b95a575a9339b6e71a1fcb27a99663b#commitcomment-13965030
// Not all Chromium-based browsers support managed storage. Merely testing or // Not all Chromium-based browsers support managed storage. Merely testing or
@ -1297,26 +1380,48 @@ vAPI.commands = chrome.commands;
// https://github.com/gorhill/uBlock/issues/900 // https://github.com/gorhill/uBlock/issues/900
// Also, UC Browser: http://www.upsieutoc.com/image/WXuH // Also, UC Browser: http://www.upsieutoc.com/image/WXuH
vAPI.adminStorage = chrome.storage.managed && { vAPI.adminStorage = (( ) => {
getItem: function(key, callback) { if ( browser.storage.managed instanceof Object === false ) {
const onRead = function(store) { return {
getItem: function() {
return Promise.resolve();
},
};
}
const managedStorage = vAPI.apiIsPromisified
? browser.storage.managed
: {
get: function(keys) {
return new Promise((resolve, reject) => {
browser.storage.managed.get(keys, result => {
const lastError = browser.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError);
}
resolve(result);
});
});
},
};
return {
getItem: async function(key) {
let bin;
try {
bin = await managedStorage.get(key);
} catch(ex) {
}
let data; let data;
if ( if (
!chrome.runtime.lastError && chrome.runtime.lastError instanceof Object === false &&
typeof store === 'object' && bin instanceof Object
store !== null
) { ) {
data = store[key]; data = bin[key];
} }
callback(data); return data;
};
try {
chrome.storage.managed.get(key, onRead);
} catch (ex) {
callback();
} }
} };
}; })();
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/

File diff suppressed because it is too large Load diff

View file

@ -58,9 +58,10 @@ const onMessage = function(request, sender, callback) {
// https://github.com/chrisaljoudi/uBlock/issues/417 // https://github.com/chrisaljoudi/uBlock/issues/417
µb.assets.get( µb.assets.get(
request.url, request.url,
{ dontCache: true, needSourceURL: true }, { dontCache: true, needSourceURL: true }
callback ).then(result => {
); callback(result);
});
return; return;
case 'listsFromNetFilter': case 'listsFromNetFilter':
@ -791,24 +792,18 @@ vAPI.messaging.listen({
const µb = µBlock; const µb = µBlock;
// Settings // Settings
const getLocalData = function(callback) { const getLocalData = async function() {
const onStorageInfoReady = function(bytesInUse) { const data = Object.assign({}, µb.restoreBackupSettings);
const o = µb.restoreBackupSettings; data.storageUsed = await µb.getBytesInUse();
callback({ return data;
storageUsed: bytesInUse,
lastRestoreFile: o.lastRestoreFile,
lastRestoreTime: o.lastRestoreTime,
lastBackupFile: o.lastBackupFile,
lastBackupTime: o.lastBackupTime,
cloudStorageSupported: µb.cloudStorageSupported,
privacySettingsSupported: µb.privacySettingsSupported
});
};
µb.getBytesInUse(onStorageInfoReady);
}; };
const backupUserData = function(callback) { const backupUserData = async function() {
const [ userFilters, localData ] = await Promise.all([
µb.loadUserFilters(),
getLocalData(),
]);
const userData = { const userData = {
timeStamp: Date.now(), timeStamp: Date.now(),
version: vAPI.app.version, version: vAPI.app.version,
@ -821,23 +816,17 @@ const backupUserData = function(callback) {
dynamicFilteringString: µb.permanentFirewall.toString(), dynamicFilteringString: µb.permanentFirewall.toString(),
urlFilteringString: µb.permanentURLFiltering.toString(), urlFilteringString: µb.permanentURLFiltering.toString(),
hostnameSwitchesString: µb.permanentSwitches.toString(), hostnameSwitchesString: µb.permanentSwitches.toString(),
userFilters: '' userFilters: userFilters.content,
}; };
const onUserFiltersReady = function(details) { const filename = vAPI.i18n('aboutBackupFilename')
userData.userFilters = details.content; .replace('{{datetime}}', µb.dateNowToSensibleString())
const filename = vAPI.i18n('aboutBackupFilename') .replace(/ +/g, '_');
.replace('{{datetime}}', µb.dateNowToSensibleString()) µb.restoreBackupSettings.lastBackupFile = filename;
.replace(/ +/g, '_'); µb.restoreBackupSettings.lastBackupTime = Date.now();
µb.restoreBackupSettings.lastBackupFile = filename; vAPI.storage.set(µb.restoreBackupSettings);
µb.restoreBackupSettings.lastBackupTime = Date.now();
vAPI.storage.set(µb.restoreBackupSettings);
getLocalData(function(localData) {
callback({ localData: localData, userData: userData });
});
};
µb.assets.get(µb.userFiltersPath, onUserFiltersReady); return { localData, userData };
}; };
const restoreUserData = function(request) { const restoreUserData = function(request) {
@ -881,7 +870,7 @@ const restoreUserData = function(request) {
lastBackupFile: '', lastBackupFile: '',
lastBackupTime: 0 lastBackupTime: 0
}); });
µb.assets.put(µb.userFiltersPath, userData.userFilters); µb.saveUserFilters(userData.userFilters);
if ( Array.isArray(userData.selectedFilterLists) ) { if ( Array.isArray(userData.selectedFilterLists) ) {
µb.saveSelectedFilterLists(userData.selectedFilterLists, restart); µb.saveSelectedFilterLists(userData.selectedFilterLists, restart);
} else { } else {
@ -902,18 +891,17 @@ const restoreUserData = function(request) {
// Remove all stored data but keep global counts, people can become // Remove all stored data but keep global counts, people can become
// quite attached to numbers // quite attached to numbers
const resetUserData = function() { const resetUserData = async function() {
let count = 3;
const countdown = ( ) => {
count -= 1;
if ( count === 0 ) {
vAPI.app.restart();
}
};
µb.cacheStorage.clear().then(( ) => countdown()); // 1
vAPI.storage.clear(countdown); // 2
µb.saveLocalSettings(countdown); // 3
vAPI.localStorage.removeItem('immediateHiddenSettings'); vAPI.localStorage.removeItem('immediateHiddenSettings');
await Promise.all([
µb.cacheStorage.clear(),
vAPI.storage.clear(),
]);
await µb.saveLocalSettings();
vAPI.app.restart();
}; };
// 3rd-party filters // 3rd-party filters
@ -932,7 +920,7 @@ const prepListEntries = function(entries) {
} }
}; };
const getLists = function(callback) { const getLists = async function(callback) {
const r = { const r = {
autoUpdate: µb.userSettings.autoUpdate, autoUpdate: µb.userSettings.autoUpdate,
available: null, available: null,
@ -946,17 +934,15 @@ const getLists = function(callback) {
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters, parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
userFiltersPath: µb.userFiltersPath userFiltersPath: µb.userFiltersPath
}; };
const onMetadataReady = function(entries) { const [ lists, metadata ] = await Promise.all([
r.cache = entries; µb.getAvailableLists(),
prepListEntries(r.cache); µb.assets.metadata(),
callback(r); ]);
}; r.available = lists;
const onLists = function(lists) { prepListEntries(r.available);
r.available = lists; r.cache = metadata;
prepListEntries(r.available); prepListEntries(r.cache);
µb.assets.metadata(onMetadataReady); callback(r);
};
µb.getAvailableLists(onLists);
}; };
// My rules // My rules
@ -1060,29 +1046,37 @@ const onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'backupUserData': case 'backupUserData':
return backupUserData(callback); return backupUserData().then(data => {
callback(data);
});
case 'getLists': case 'getLists':
return getLists(callback); return getLists(callback);
case 'getLocalData': case 'getLocalData':
return getLocalData(callback); return getLocalData().then(localData => {
callback(localData);
});
case 'getShortcuts': case 'getShortcuts':
return getShortcuts(callback); return getShortcuts(callback);
case 'readUserFilters': case 'readUserFilters':
return µb.loadUserFilters(callback); return µb.loadUserFilters().then(result => {
callback(result);
});
case 'writeUserFilters': case 'writeUserFilters':
return µb.saveUserFilters(request.content, callback); return µb.saveUserFilters(request.content).then(result => {
callback(result);
});
default: default:
break; break;
} }
// Sync // Sync
var response; let response;
switch ( request.what ) { switch ( request.what ) {
case 'canUpdateShortcuts': case 'canUpdateShortcuts':

View file

@ -573,21 +573,20 @@ RedirectEngine.prototype.toSelfie = function(path) {
/******************************************************************************/ /******************************************************************************/
RedirectEngine.prototype.fromSelfie = function(path) { RedirectEngine.prototype.fromSelfie = async function(path) {
return µBlock.assets.get(`${path}/main`).then(details => { const result = await µBlock.assets.get(`${path}/main`);
let selfie; let selfie;
try { try {
selfie = JSON.parse(details.content); selfie = JSON.parse(result.content);
} catch (ex) { } catch (ex) {
} }
if ( selfie instanceof Object === false ) { return false; } if ( selfie instanceof Object === false ) { return false; }
this.rules = new Map(selfie.rules); this.rules = new Map(selfie.rules);
this.ruleSources = new Set(selfie.ruleSources); this.ruleSources = new Set(selfie.ruleSources);
this.ruleDestinations = new Set(selfie.ruleDestinations); this.ruleDestinations = new Set(selfie.ruleDestinations);
this.resetCache(); this.resetCache();
this.modifyTime = Date.now(); this.modifyTime = Date.now();
return true; return true;
});
}; };
/******************************************************************************/ /******************************************************************************/
@ -788,29 +787,26 @@ RedirectEngine.prototype.selfieFromResources = function() {
); );
}; };
RedirectEngine.prototype.resourcesFromSelfie = function() { RedirectEngine.prototype.resourcesFromSelfie = async function() {
return µBlock.assets.get( const result = await µBlock.assets.get('compiled/redirectEngine/resources');
'compiled/redirectEngine/resources' let selfie;
).then(details => { try {
let selfie; selfie = JSON.parse(result.content);
try { } catch(ex) {
selfie = JSON.parse(details.content); }
} catch(ex) { if (
} selfie instanceof Object === false ||
if ( selfie.version !== resourcesSelfieVersion ||
selfie instanceof Object === false || Array.isArray(selfie.resources) === false
selfie.version !== resourcesSelfieVersion || ) {
Array.isArray(selfie.resources) === false return false;
) { }
return false; this.aliases = new Map(selfie.aliases);
} this.resources = new Map();
this.aliases = new Map(selfie.aliases); for ( const [ token, entry ] of selfie.resources ) {
this.resources = new Map(); this.resources.set(token, RedirectEntry.fromSelfie(entry));
for ( const [ token, entry ] of selfie.resources ) { }
this.resources.set(token, RedirectEntry.fromSelfie(entry)); return true;
}
return true;
});
}; };
RedirectEngine.prototype.invalidateResourcesSelfie = function() { RedirectEngine.prototype.invalidateResourcesSelfie = function() {

View file

@ -23,59 +23,64 @@
/******************************************************************************/ /******************************************************************************/
µBlock.staticFilteringReverseLookup = (function() { µBlock.staticFilteringReverseLookup = (( ) => {
/******************************************************************************/ /******************************************************************************/
var worker = null; const workerTTL = 5 * 60 * 1000;
var workerTTL = 5 * 60 * 1000; const pendingResponses = new Map();
var workerTTLTimer = null;
var needLists = true; let worker = null;
var messageId = 1; let workerTTLTimer;
var pendingResponses = Object.create(null); let needLists = true;
let messageId = 1;
/******************************************************************************/ /******************************************************************************/
var onWorkerMessage = function(e) { const onWorkerMessage = function(e) {
var msg = e.data; const msg = e.data;
var callback = pendingResponses[msg.id]; const callback = pendingResponses.get(msg.id);
delete pendingResponses[msg.id]; pendingResponses.delete(msg.id);
callback(msg.response); callback(msg.response);
}; };
/******************************************************************************/ /******************************************************************************/
var stopWorker = function() { const stopWorker = function() {
workerTTLTimer = null; if ( workerTTLTimer !== undefined ) {
if ( worker === null ) { clearTimeout(workerTTLTimer);
return; workerTTLTimer = undefined;
} }
if ( worker === null ) { return; }
worker.terminate(); worker.terminate();
worker = null; worker = null;
needLists = true; needLists = true;
pendingResponses = Object.create(null); pendingResponses.clear();
}; };
/******************************************************************************/ /******************************************************************************/
var initWorker = function(callback) { const initWorker = function() {
if ( worker === null ) { if ( worker === null ) {
worker = new Worker('js/reverselookup-worker.js'); worker = new Worker('js/reverselookup-worker.js');
worker.onmessage = onWorkerMessage; worker.onmessage = onWorkerMessage;
} }
if ( needLists === false ) { // The worker will be shutdown after n minutes without being used.
callback(); if ( workerTTLTimer !== undefined ) {
return; clearTimeout(workerTTLTimer);
} }
workerTTLTimer = vAPI.setTimeout(stopWorker, workerTTL);
if ( needLists === false ) {
return Promise.resolve();
}
needLists = false; needLists = false;
var entries = Object.create(null); const entries = new Map();
var countdown = 0;
var onListLoaded = function(details) { const onListLoaded = function(details) {
var entry = entries[details.assetKey]; const entry = entries.get(details.assetKey);
// https://github.com/gorhill/uBlock/issues/536 // https://github.com/gorhill/uBlock/issues/536
// Use assetKey when there is no filter list title. // Use assetKey when there is no filter list title.
@ -89,44 +94,40 @@ var initWorker = function(callback) {
content: details.content content: details.content
} }
}); });
countdown -= 1;
if ( countdown === 0 ) {
callback();
}
}; };
var µb = µBlock; const µb = µBlock;
var listKey, entry; for ( const listKey in µb.availableFilterLists ) {
for ( listKey in µb.availableFilterLists ) {
if ( µb.availableFilterLists.hasOwnProperty(listKey) === false ) { if ( µb.availableFilterLists.hasOwnProperty(listKey) === false ) {
continue; continue;
} }
entry = µb.availableFilterLists[listKey]; const entry = µb.availableFilterLists[listKey];
if ( entry.off === true ) { continue; } if ( entry.off === true ) { continue; }
entries[listKey] = { entries.set(listKey, {
title: listKey !== µb.userFiltersPath ? title: listKey !== µb.userFiltersPath ?
entry.title : entry.title :
vAPI.i18n('1pPageName'), vAPI.i18n('1pPageName'),
supportURL: entry.supportURL || '' supportURL: entry.supportURL || ''
}; });
countdown += 1; }
if ( entries.size === 0 ) {
return Promise.resolve();
} }
if ( countdown === 0 ) { const promises = [];
callback(); for ( const listKey of entries.keys() ) {
return; promises.push(
} µb.getCompiledFilterList(listKey).then(details => {
onListLoaded(details);
for ( listKey in entries ) { })
µb.getCompiledFilterList(listKey, onListLoaded); );
} }
return Promise.all(promises);
}; };
/******************************************************************************/ /******************************************************************************/
var fromNetFilter = function(compiledFilter, rawFilter, callback) { const fromNetFilter = async function(compiledFilter, rawFilter, callback) {
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
return; return;
} }
@ -136,32 +137,22 @@ var fromNetFilter = function(compiledFilter, rawFilter, callback) {
return; return;
} }
if ( workerTTLTimer !== null ) { await initWorker();
clearTimeout(workerTTLTimer);
workerTTLTimer = null;
}
var onWorkerReady = function() { const id = messageId++;
var id = messageId++; const message = {
var message = { what: 'fromNetFilter',
what: 'fromNetFilter', id: id,
id: id, compiledFilter: compiledFilter,
compiledFilter: compiledFilter, rawFilter: rawFilter
rawFilter: rawFilter
};
pendingResponses[id] = callback;
worker.postMessage(message);
// The worker will be shutdown after n minutes without being used.
workerTTLTimer = vAPI.setTimeout(stopWorker, workerTTL);
}; };
pendingResponses.set(id, callback);
initWorker(onWorkerReady); worker.postMessage(message);
}; };
/******************************************************************************/ /******************************************************************************/
var fromCosmeticFilter = function(details, callback) { const fromCosmeticFilter = async function(details, callback) {
if ( typeof callback !== 'function' ) { return; } if ( typeof callback !== 'function' ) { return; }
if ( details.rawFilter === '' ) { if ( details.rawFilter === '' ) {
@ -169,50 +160,38 @@ var fromCosmeticFilter = function(details, callback) {
return; return;
} }
if ( workerTTLTimer !== null ) { await initWorker();
clearTimeout(workerTTLTimer);
workerTTLTimer = null;
}
let onWorkerReady = function() { const id = messageId++;
let id = messageId++; const hostname = µBlock.URI.hostnameFromURI(details.url);
let hostname = µBlock.URI.hostnameFromURI(details.url); pendingResponses.set(id, callback);
pendingResponses[id] = callback; worker.postMessage({
worker.postMessage({ what: 'fromCosmeticFilter',
what: 'fromCosmeticFilter', id: id,
id: id, domain: µBlock.URI.domainFromHostname(hostname),
domain: µBlock.URI.domainFromHostname(hostname), hostname: hostname,
hostname: hostname, ignoreGeneric: µBlock.staticNetFilteringEngine
ignoreGeneric: µBlock.staticNetFilteringEngine .matchStringGenericHide(details.url) === 2,
.matchStringGenericHide(details.url) === 2, rawFilter: details.rawFilter
rawFilter: details.rawFilter });
});
// The worker will be shutdown after n minutes without being used.
workerTTLTimer = vAPI.setTimeout(stopWorker, workerTTL);
};
initWorker(onWorkerReady);
}; };
/******************************************************************************/ /******************************************************************************/
// This tells the worker that filter lists may have changed. // This tells the worker that filter lists may have changed.
var resetLists = function() { const resetLists = function() {
needLists = true; needLists = true;
if ( worker === null ) { if ( worker === null ) { return; }
return;
}
worker.postMessage({ what: 'resetLists' }); worker.postMessage({ what: 'resetLists' });
}; };
/******************************************************************************/ /******************************************************************************/
return { return {
fromNetFilter: fromNetFilter, fromNetFilter,
fromCosmeticFilter: fromCosmeticFilter, fromCosmeticFilter,
resetLists: resetLists, resetLists,
shutdown: stopWorker shutdown: stopWorker
}; };

View file

@ -25,8 +25,8 @@
// Load all: executed once. // Load all: executed once.
{ (async ( ) => {
// >>>>> start of local scope // >>>>> start of private scope
const µb = µBlock; const µb = µBlock;
@ -97,8 +97,6 @@ const onAllReady = function() {
vAPI.app.restart(); vAPI.app.restart();
} }
}); });
log.info(`All ready ${Date.now()-vAPI.T0} ms after launch`);
}; };
/******************************************************************************/ /******************************************************************************/
@ -152,27 +150,6 @@ const initializeTabs = function() {
/******************************************************************************/ /******************************************************************************/
// Filtering engines dependencies:
// - PSL
const onPSLReady = function() {
log.info(`PSL ready ${Date.now()-vAPI.T0} ms after launch`);
µb.selfieManager.load().then(valid => {
if ( valid === true ) {
log.info(`Selfie ready ${Date.now()-vAPI.T0} ms after launch`);
onAllReady();
return;
}
µb.loadFilterLists(( ) => {
log.info(`Filter lists ready ${Date.now()-vAPI.T0} ms after launch`);
onAllReady();
});
});
};
/******************************************************************************/
const onCommandShortcutsReady = function(commandShortcuts) { const onCommandShortcutsReady = function(commandShortcuts) {
if ( Array.isArray(commandShortcuts) === false ) { return; } if ( Array.isArray(commandShortcuts) === false ) { return; }
µb.commandShortcuts = new Map(commandShortcuts); µb.commandShortcuts = new Map(commandShortcuts);
@ -226,8 +203,6 @@ const onNetWhitelistReady = function(netWhitelistRaw) {
// User settings are in memory // User settings are in memory
const onUserSettingsReady = function(fetched) { const onUserSettingsReady = function(fetched) {
log.info(`User settings ready ${Date.now()-vAPI.T0} ms after launch`);
const userSettings = µb.userSettings; const userSettings = µb.userSettings;
fromFetch(userSettings, fetched); fromFetch(userSettings, fetched);
@ -271,8 +246,6 @@ const onSystemSettingsReady = function(fetched) {
/******************************************************************************/ /******************************************************************************/
const onFirstFetchReady = function(fetched) { const onFirstFetchReady = function(fetched) {
log.info(`First fetch ready ${Date.now()-vAPI.T0} ms after launch`);
// https://github.com/uBlockOrigin/uBlock-issues/issues/507 // https://github.com/uBlockOrigin/uBlock-issues/issues/507
// Firefox-specific: somehow `fetched` is undefined under certain // Firefox-specific: somehow `fetched` is undefined under certain
// circumstances even though we asked to load with default values. // circumstances even though we asked to load with default values.
@ -291,10 +264,6 @@ const onFirstFetchReady = function(fetched) {
onNetWhitelistReady(fetched.netWhitelist); onNetWhitelistReady(fetched.netWhitelist);
onVersionReady(fetched.version); onVersionReady(fetched.version);
onCommandShortcutsReady(fetched.commandShortcuts); onCommandShortcutsReady(fetched.commandShortcuts);
µb.loadPublicSuffixList().then(( ) => {
onPSLReady();
});
}; };
/******************************************************************************/ /******************************************************************************/
@ -347,42 +316,45 @@ const createDefaultProps = function() {
/******************************************************************************/ /******************************************************************************/
const onHiddenSettingsReady = function() { try {
return µb.cacheStorage.select( // https://github.com/gorhill/uBlock/issues/531
µb.hiddenSettings.cacheStorageAPI await µb.restoreAdminSettings();
).then(backend => {
log.info(`Backend storage for cache will be ${backend}`);
});
};
/******************************************************************************/
// TODO(seamless migration):
// Eventually selected filter list keys will be loaded as a fetchable
// property. Until then we need to handle backward and forward
// compatibility, this means a special asynchronous call to load selected
// filter lists.
const onAdminSettingsRestored = function() {
log.info(`Admin settings ready ${Date.now()-vAPI.T0} ms after launch`); log.info(`Admin settings ready ${Date.now()-vAPI.T0} ms after launch`);
Promise.all([ await µb.loadHiddenSettings();
µb.loadHiddenSettings().then(( ) => log.info(`Hidden settings ready ${Date.now()-vAPI.T0} ms after launch`);
onHiddenSettingsReady()
),
µb.loadSelectedFilterLists(),
]).then(( ) => {
log.info(`List selection ready ${Date.now()-vAPI.T0} ms after launch`);
vAPI.storage.get(createDefaultProps(), onFirstFetchReady);
});
};
/******************************************************************************/ const cacheBackend = await µb.cacheStorage.select(
µb.hiddenSettings.cacheStorageAPI
);
log.info(`Backend storage for cache will be ${cacheBackend}`);
// https://github.com/gorhill/uBlock/issues/531 await Promise.all([
µb.restoreAdminSettings().then(( ) => { µb.loadSelectedFilterLists().then(( ) => {
onAdminSettingsRestored(); log.info(`List selection ready ${Date.now()-vAPI.T0} ms after launch`);
}); }),
vAPI.storage.get(createDefaultProps()).then(fetched => {
log.info(`First fetch ready ${Date.now()-vAPI.T0} ms after launch`);
onFirstFetchReady(fetched);
}),
µb.loadPublicSuffixList().then(( ) => {
log.info(`PSL ready ${Date.now()-vAPI.T0} ms after launch`);
}),
]);
// <<<<< end of local scope const selfieIsValid = await µb.selfieManager.load();
if ( selfieIsValid === true ) {
log.info(`Selfie ready ${Date.now()-vAPI.T0} ms after launch`);
} else {
await µb.loadFilterLists();
log.info(`Filter lists ready ${Date.now()-vAPI.T0} ms after launch`);
}
} catch (ex) {
console.trace(ex);
} }
onAllReady();
log.info(`All ready ${Date.now()-vAPI.T0} ms after launch`);
// <<<<< end of private scope
})();

View file

@ -25,48 +25,44 @@
/******************************************************************************/ /******************************************************************************/
µBlock.getBytesInUse = function(callback) { µBlock.getBytesInUse = async function() {
if ( typeof callback !== 'function' ) { const promises = [];
callback = this.noopFunc;
}
let bytesInUse; let bytesInUse;
let countdown = 0;
const process = count => {
if ( typeof count === 'number' ) {
if ( bytesInUse === undefined ) {
bytesInUse = 0;
}
bytesInUse += count;
}
countdown -= 1;
if ( countdown > 0 ) { return; }
µBlock.storageUsed = bytesInUse;
callback(bytesInUse);
};
// Not all platforms implement this method. // Not all platforms implement this method.
if ( vAPI.storage.getBytesInUse instanceof Function ) { promises.push(
countdown += 1; vAPI.storage.getBytesInUse instanceof Function
vAPI.storage.getBytesInUse(null, process); ? vAPI.storage.getBytesInUse(null)
} : undefined
);
if ( if (
navigator.storage instanceof Object && navigator.storage instanceof Object &&
navigator.storage.estimate instanceof Function navigator.storage.estimate instanceof Function
) { ) {
countdown += 1; promises.push(navigator.storage.estimate());
navigator.storage.estimate().then(estimate => {
process(estimate.usage);
});
} }
if ( countdown === 0 ) {
callback(); const results = await Promise.all(promises);
const processCount = count => {
if ( typeof count !== 'number' ) { return; }
if ( bytesInUse === undefined ) { bytesInUse = 0; }
bytesInUse += count;
return bytesInUse;
};
processCount(results[0]);
if ( results.length > 1 && results[1] instanceof Object ) {
processCount(results[1].usage);
} }
µBlock.storageUsed = bytesInUse;
return bytesInUse;
}; };
/******************************************************************************/ /******************************************************************************/
µBlock.saveLocalSettings = (function() { µBlock.saveLocalSettings = (( ) => {
const saveAfter = 4 * 60 * 1000; const saveAfter = 4 * 60 * 1000;
const onTimeout = ( ) => { const onTimeout = ( ) => {
@ -79,9 +75,9 @@
vAPI.setTimeout(onTimeout, saveAfter); vAPI.setTimeout(onTimeout, saveAfter);
return function(callback) { return function() {
this.localSettingsLastSaved = Date.now(); this.localSettingsLastSaved = Date.now();
vAPI.storage.set(this.localSettings, callback); return vAPI.storage.set(this.localSettings);
}; };
})(); })();
@ -93,40 +89,31 @@
/******************************************************************************/ /******************************************************************************/
µBlock.loadHiddenSettings = function() { µBlock.loadHiddenSettings = async function() {
return new Promise(resolve => { const bin = await vAPI.storage.get('hiddenSettings');
// >>>> start of executor if ( bin instanceof Object === false ) { return; }
vAPI.storage.get('hiddenSettings', bin => { const hs = bin.hiddenSettings;
if ( bin instanceof Object === false ) { if ( hs instanceof Object ) {
return resolve(); const hsDefault = this.hiddenSettingsDefault;
} for ( const key in hsDefault ) {
const hs = bin.hiddenSettings; if (
if ( hs instanceof Object ) { hsDefault.hasOwnProperty(key) &&
const hsDefault = this.hiddenSettingsDefault; hs.hasOwnProperty(key) &&
for ( const key in hsDefault ) { typeof hs[key] === typeof hsDefault[key]
if ( ) {
hsDefault.hasOwnProperty(key) && this.hiddenSettings[key] = hs[key];
hs.hasOwnProperty(key) &&
typeof hs[key] === typeof hsDefault[key]
) {
this.hiddenSettings[key] = hs[key];
}
}
if ( typeof this.hiddenSettings.suspendTabsUntilReady === 'boolean' ) {
this.hiddenSettings.suspendTabsUntilReady =
this.hiddenSettings.suspendTabsUntilReady
? 'yes'
: 'unset';
} }
} }
self.log.verbosity = this.hiddenSettings.consoleLogLevel; if ( typeof this.hiddenSettings.suspendTabsUntilReady === 'boolean' ) {
resolve(); this.hiddenSettings.suspendTabsUntilReady =
this.fireDOMEvent('hiddenSettingsChanged'); this.hiddenSettings.suspendTabsUntilReady
}); ? 'yes'
: 'unset';
// <<<< end of executor }
}); }
self.log.verbosity = this.hiddenSettings.consoleLogLevel;
this.fireDOMEvent('hiddenSettingsChanged');
}; };
// Note: Save only the settings which values differ from the default ones. // Note: Save only the settings which values differ from the default ones.
@ -261,30 +248,16 @@
**/ **/
µBlock.loadSelectedFilterLists = function() { µBlock.loadSelectedFilterLists = async function() {
return new Promise(resolve => { const bin = await vAPI.storage.get('selectedFilterLists');
// >>>> start of executor if ( bin instanceof Object && Array.isArray(bin.selectedFilterLists) ) {
vAPI.storage.get('selectedFilterLists', bin => {
// Select default filter lists if first-time launch.
if (
bin instanceof Object === false ||
Array.isArray(bin.selectedFilterLists) === false
) {
this.assets.metadata(availableLists => {
this.saveSelectedFilterLists(
this.autoSelectRegionalFilterLists(availableLists)
);
resolve();
});
return;
}
this.selectedFilterLists = bin.selectedFilterLists; this.selectedFilterLists = bin.selectedFilterLists;
resolve(); return;
}); }
// <<<< end of executor // Select default filter lists if first-time launch.
}); const lists = await this.assets.metadata();
this.saveSelectedFilterLists(this.autoSelectRegionalFilterLists(lists));
}; };
µBlock.saveSelectedFilterLists = function(newKeys, append, callback) { µBlock.saveSelectedFilterLists = function(newKeys, append, callback) {
@ -409,20 +382,20 @@
/******************************************************************************/ /******************************************************************************/
µBlock.saveUserFilters = function(content, callback) { µBlock.saveUserFilters = function(content) {
// https://github.com/gorhill/uBlock/issues/1022 // https://github.com/gorhill/uBlock/issues/1022
// Be sure to end with an empty line. // Be sure to end with an empty line.
content = content.trim(); content = content.trim();
if ( content !== '' ) { content += '\n'; } if ( content !== '' ) { content += '\n'; }
this.assets.put(this.userFiltersPath, content, callback);
this.removeCompiledFilterList(this.userFiltersPath); this.removeCompiledFilterList(this.userFiltersPath);
return this.assets.put(this.userFiltersPath, content);
}; };
µBlock.loadUserFilters = function(callback) { µBlock.loadUserFilters = function() {
return this.assets.get(this.userFiltersPath, callback); return this.assets.get(this.userFiltersPath);
}; };
µBlock.appendUserFilters = function(filters, options) { µBlock.appendUserFilters = async function(filters, options) {
filters = filters.trim(); filters = filters.trim();
if ( filters.length === 0 ) { return; } if ( filters.length === 0 ) { return; }
@ -443,59 +416,56 @@
.replace('{{origin}}', options.origin); .replace('{{origin}}', options.origin);
} }
const onSaved = ( ) => { const details = await this.loadUserFilters();
const compiledFilters = this.compileFilters( if ( details.error ) { return; }
filters,
{ assetKey: this.userFiltersPath }
);
const snfe = this.staticNetFilteringEngine;
const cfe = this.cosmeticFilteringEngine;
const acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
const discardedCount = snfe.discardedCount + cfe.discardedCount;
this.applyCompiledFilters(compiledFilters, true);
const entry = this.availableFilterLists[this.userFiltersPath];
const deltaEntryCount =
snfe.acceptedCount +
cfe.acceptedCount - acceptedCount;
const deltaEntryUsedCount =
deltaEntryCount -
(snfe.discardedCount + cfe.discardedCount - discardedCount);
entry.entryCount += deltaEntryCount;
entry.entryUsedCount += deltaEntryUsedCount;
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
this.staticNetFilteringEngine.freeze();
this.redirectEngine.freeze();
this.staticExtFilteringEngine.freeze();
this.selfieManager.destroy();
// https://www.reddit.com/r/uBlockOrigin/comments/cj7g7m/ // The comment, if any, will be applied if and only if it is different
// https://www.reddit.com/r/uBlockOrigin/comments/cnq0bi/ // from the last comment found in the user filter list.
if ( options.killCache ) { if ( comment !== '' ) {
browser.webRequest.handlerBehaviorChanged(); const pos = details.content.lastIndexOf(comment);
if (
pos === -1 ||
details.content.indexOf('\n!', pos + 1) !== -1
) {
filters = '\n' + comment + '\n' + filters;
} }
}; }
const onLoaded = details => { // https://github.com/chrisaljoudi/uBlock/issues/976
if ( details.error ) { return; } // If we reached this point, the filter quite probably needs to be
// The comment, if any, will be applied if and only if it is different // added for sure: do not try to be too smart, trying to avoid
// from the last comment found in the user filter list. // duplicates at this point may lead to more issues.
if ( comment !== '' ) { await this.saveUserFilters(details.content.trim() + '\n' + filters);
const pos = details.content.lastIndexOf(comment);
if (
pos === -1 ||
details.content.indexOf('\n!', pos + 1) !== -1
) {
filters = '\n' + comment + '\n' + filters;
}
}
// https://github.com/chrisaljoudi/uBlock/issues/976
// If we reached this point, the filter quite probably needs to be
// added for sure: do not try to be too smart, trying to avoid
// duplicates at this point may lead to more issues.
this.saveUserFilters(details.content.trim() + '\n' + filters, onSaved);
};
this.loadUserFilters(onLoaded); const compiledFilters = this.compileFilters(
filters,
{ assetKey: this.userFiltersPath }
);
const snfe = this.staticNetFilteringEngine;
const cfe = this.cosmeticFilteringEngine;
const acceptedCount = snfe.acceptedCount + cfe.acceptedCount;
const discardedCount = snfe.discardedCount + cfe.discardedCount;
this.applyCompiledFilters(compiledFilters, true);
const entry = this.availableFilterLists[this.userFiltersPath];
const deltaEntryCount =
snfe.acceptedCount +
cfe.acceptedCount - acceptedCount;
const deltaEntryUsedCount =
deltaEntryCount -
(snfe.discardedCount + cfe.discardedCount - discardedCount);
entry.entryCount += deltaEntryCount;
entry.entryUsedCount += deltaEntryUsedCount;
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
this.staticNetFilteringEngine.freeze();
this.redirectEngine.freeze();
this.staticExtFilteringEngine.freeze();
this.selfieManager.destroy();
// https://www.reddit.com/r/uBlockOrigin/comments/cj7g7m/
// https://www.reddit.com/r/uBlockOrigin/comments/cnq0bi/
if ( options.killCache ) {
browser.webRequest.handlerBehaviorChanged();
}
}; };
µBlock.createUserFilters = function(details) { µBlock.createUserFilters = function(details) {
@ -525,7 +495,7 @@
/******************************************************************************/ /******************************************************************************/
µBlock.getAvailableLists = function(callback) { µBlock.getAvailableLists = async function() {
let oldAvailableLists = {}, let oldAvailableLists = {},
newAvailableLists = {}; newAvailableLists = {};
@ -577,105 +547,87 @@
this.saveSelectedFilterLists([ listURL ], true); this.saveSelectedFilterLists([ listURL ], true);
}; };
// Load previously saved available lists -- these contains data
// computed at run-time, we will reuse this data if possible.
const [ bin, entries ] = await Promise.all([
vAPI.storage.get('availableFilterLists'),
this.assets.metadata(),
]);
oldAvailableLists = bin && bin.availableFilterLists || {};
for ( const assetKey in entries ) {
if ( entries.hasOwnProperty(assetKey) === false ) { continue; }
const entry = entries[assetKey];
if ( entry.content !== 'filters' ) { continue; }
newAvailableLists[assetKey] = Object.assign({}, entry);
}
// Load set of currently selected filter lists.
const listKeySet = new Set(this.selectedFilterLists);
for ( const listKey in newAvailableLists ) {
if ( newAvailableLists.hasOwnProperty(listKey) ) {
newAvailableLists[listKey].off = !listKeySet.has(listKey);
}
}
//finalize();
// Final steps: // Final steps:
// - reuse existing list metadata if any; // - reuse existing list metadata if any;
// - unregister unreferenced imported filter lists if any. // - unregister unreferenced imported filter lists if any.
const finalize = ( ) => { // Reuse existing metadata.
// Reuse existing metadata. for ( const assetKey in oldAvailableLists ) {
for ( const assetKey in oldAvailableLists ) { const oldEntry = oldAvailableLists[assetKey];
const oldEntry = oldAvailableLists[assetKey]; const newEntry = newAvailableLists[assetKey];
const newEntry = newAvailableLists[assetKey]; // List no longer exists. If a stock list, try to convert to
// List no longer exists. If a stock list, try to convert to // imported list if it was selected.
// imported list if it was selected. if ( newEntry === undefined ) {
if ( newEntry === undefined ) {
this.removeFilterList(assetKey);
if ( assetKey.indexOf('://') === -1 ) {
customListFromStockList(assetKey);
}
continue;
}
if ( oldEntry.entryCount !== undefined ) {
newEntry.entryCount = oldEntry.entryCount;
}
if ( oldEntry.entryUsedCount !== undefined ) {
newEntry.entryUsedCount = oldEntry.entryUsedCount;
}
// This may happen if the list name was pulled from the list
// content.
// https://github.com/chrisaljoudi/uBlock/issues/982
// There is no guarantee the title was successfully extracted from
// the list content.
if (
newEntry.title === '' &&
typeof oldEntry.title === 'string' &&
oldEntry.title !== ''
) {
newEntry.title = oldEntry.title;
}
}
// Remove unreferenced imported filter lists.
const dict = new Set(importedListKeys);
for ( const assetKey in newAvailableLists ) {
const newEntry = newAvailableLists[assetKey];
if ( newEntry.submitter !== 'user' ) { continue; }
if ( dict.has(assetKey) ) { continue; }
delete newAvailableLists[assetKey];
this.assets.unregisterAssetSource(assetKey);
this.removeFilterList(assetKey); this.removeFilterList(assetKey);
} if ( assetKey.indexOf('://') === -1 ) {
}; customListFromStockList(assetKey);
// Built-in filter lists loaded.
const onBuiltinListsLoaded = entries => {
for ( const assetKey in entries ) {
if ( entries.hasOwnProperty(assetKey) === false ) { continue; }
const entry = entries[assetKey];
if ( entry.content !== 'filters' ) { continue; }
newAvailableLists[assetKey] = Object.assign({}, entry);
}
// Load set of currently selected filter lists.
const listKeySet = new Set(this.selectedFilterLists);
for ( const listKey in newAvailableLists ) {
if ( newAvailableLists.hasOwnProperty(listKey) ) {
newAvailableLists[listKey].off = !listKeySet.has(listKey);
} }
continue;
} }
if ( oldEntry.entryCount !== undefined ) {
newEntry.entryCount = oldEntry.entryCount;
}
if ( oldEntry.entryUsedCount !== undefined ) {
newEntry.entryUsedCount = oldEntry.entryUsedCount;
}
// This may happen if the list name was pulled from the list
// content.
// https://github.com/chrisaljoudi/uBlock/issues/982
// There is no guarantee the title was successfully extracted from
// the list content.
if (
newEntry.title === '' &&
typeof oldEntry.title === 'string' &&
oldEntry.title !== ''
) {
newEntry.title = oldEntry.title;
}
}
finalize(); // Remove unreferenced imported filter lists.
callback(newAvailableLists); for ( const assetKey in newAvailableLists ) {
}; const newEntry = newAvailableLists[assetKey];
if ( newEntry.submitter !== 'user' ) { continue; }
if ( importedListKeys.indexOf(assetKey) !== -1 ) { continue; }
delete newAvailableLists[assetKey];
this.assets.unregisterAssetSource(assetKey);
this.removeFilterList(assetKey);
}
// Available lists previously computed. return newAvailableLists;
const onOldAvailableListsLoaded = bin => {
oldAvailableLists = bin && bin.availableFilterLists || {};
this.assets.metadata(onBuiltinListsLoaded);
};
// Load previously saved available lists -- these contains data
// computed at run-time, we will reuse this data if possible.
vAPI.storage.get('availableFilterLists', onOldAvailableListsLoaded);
}; };
/******************************************************************************/ /******************************************************************************/
// This is used to be re-entrancy resistant. µBlock.loadFilterLists = (( ) => {
µBlock.loadingFilterLists = false;
µBlock.loadFilterLists = function(callback) {
// Callers are expected to check this first.
if ( this.loadingFilterLists ) { return; }
this.loadingFilterLists = true;
const loadedListKeys = []; const loadedListKeys = [];
let filterlistsCount = 0; let loadingPromise;
if ( typeof callback !== 'function' ) { const onDone = function() {
callback = this.noopFunc;
}
const onDone = ( ) => {
this.staticNetFilteringEngine.freeze(); this.staticNetFilteringEngine.freeze();
this.staticExtFilteringEngine.freeze(); this.staticExtFilteringEngine.freeze();
this.redirectEngine.freeze(); this.redirectEngine.freeze();
@ -690,15 +642,13 @@
listKeys: loadedListKeys listKeys: loadedListKeys
}); });
callback();
this.selfieManager.destroy(); this.selfieManager.destroy();
this.lz4Codec.relinquish(); this.lz4Codec.relinquish();
this.loadingFilterLists = false; loadingPromise = undefined;
}; };
const applyCompiledFilters = (assetKey, compiled) => { const applyCompiledFilters = function(assetKey, compiled) {
const snfe = this.staticNetFilteringEngine; const snfe = this.staticNetFilteringEngine;
const sxfe = this.staticExtFilteringEngine; const sxfe = this.staticExtFilteringEngine;
let acceptedCount = snfe.acceptedCount + sxfe.acceptedCount, let acceptedCount = snfe.acceptedCount + sxfe.acceptedCount,
@ -714,15 +664,7 @@
loadedListKeys.push(assetKey); loadedListKeys.push(assetKey);
}; };
const onCompiledListLoaded = details => { const onFilterListsReady = function(lists) {
applyCompiledFilters(details.assetKey, details.content);
filterlistsCount -= 1;
if ( filterlistsCount === 0 ) {
onDone();
}
};
const onFilterListsReady = lists => {
this.availableFilterLists = lists; this.availableFilterLists = lists;
vAPI.net.suspend(); vAPI.net.suspend();
@ -740,66 +682,71 @@
for ( const assetKey in lists ) { for ( const assetKey in lists ) {
if ( lists.hasOwnProperty(assetKey) === false ) { continue; } if ( lists.hasOwnProperty(assetKey) === false ) { continue; }
if ( lists[assetKey].off ) { continue; } if ( lists[assetKey].off ) { continue; }
toLoad.push(assetKey);
} toLoad.push(
filterlistsCount = toLoad.length; this.getCompiledFilterList(assetKey).then(details => {
if ( filterlistsCount === 0 ) { applyCompiledFilters.call(
return onDone(); this,
details.assetKey,
details.content
);
})
);
} }
let i = toLoad.length; return Promise.all(toLoad);
while ( i-- ) {
this.getCompiledFilterList(toLoad[i], onCompiledListLoaded);
}
}; };
this.getAvailableLists(onFilterListsReady); return function() {
this.loadRedirectResources(); if ( loadingPromise instanceof Promise === false ) {
}; loadedListKeys.length = 0;
loadingPromise = Promise.all([
this.getAvailableLists().then(lists => {
return onFilterListsReady.call(this, lists);
}),
this.loadRedirectResources(),
]).then(( ) => {
onDone.call(this);
});
}
return loadingPromise;
};
})();
/******************************************************************************/ /******************************************************************************/
µBlock.getCompiledFilterList = function(assetKey, callback) { µBlock.getCompiledFilterList = async function(assetKey) {
const compiledPath = 'compiled/' + assetKey; const compiledPath = 'compiled/' + assetKey;
let rawContent;
const onCompiledListLoaded2 = details => { let compiledDetails = await this.assets.get(compiledPath);
if ( details.content === '' ) { if ( compiledDetails.content !== '' ) {
details.content = this.compileFilters( compiledDetails.assetKey = assetKey;
rawContent, return compiledDetails;
{ assetKey: assetKey } }
);
this.assets.put(compiledPath, details.content);
}
rawContent = undefined;
details.assetKey = assetKey;
callback(details);
};
const onRawListLoaded = details => { const rawDetails = await this.assets.get(assetKey);
if ( details.content === '' ) { // Compiling an empty string results in an empty string.
details.assetKey = assetKey; if ( rawDetails.content === '' ) {
callback(details); rawDetails.assetKey = assetKey;
return; return rawDetails;
} }
this.extractFilterListMetadata(assetKey, details.content);
// Fectching the raw content may cause the compiled content to be
// generated somewhere else in uBO, hence we try one last time to
// fetch the compiled content in case it has become available.
rawContent = details.content;
this.assets.get(compiledPath, onCompiledListLoaded2);
};
const onCompiledListLoaded1 = details => { this.extractFilterListMetadata(assetKey, rawDetails.content);
if ( details.content === '' ) {
this.assets.get(assetKey, onRawListLoaded);
return;
}
details.assetKey = assetKey;
callback(details);
};
this.assets.get(compiledPath, onCompiledListLoaded1); // Fetching the raw content may cause the compiled content to be
// generated somewhere else in uBO, hence we try one last time to
// fetch the compiled content in case it has become available.
compiledDetails = await this.assets.get(compiledPath);
if ( compiledDetails.content === '' ) {
compiledDetails.content = this.compileFilters(
rawDetails.content,
{ assetKey: assetKey }
);
this.assets.put(compiledPath, compiledDetails.content);
}
compiledDetails.assetKey = assetKey;
return compiledDetails;
}; };
/******************************************************************************/ /******************************************************************************/
@ -851,7 +798,7 @@
/******************************************************************************/ /******************************************************************************/
µBlock.compileFilters = function(rawText, details) { µBlock.compileFilters = function(rawText, details) {
let writer = new this.CompiledLineIO.Writer(); const writer = new this.CompiledLineIO.Writer();
// Populate the writer with information potentially useful to the // Populate the writer with information potentially useful to the
// client compilers. // client compilers.
@ -1007,8 +954,9 @@
/******************************************************************************/ /******************************************************************************/
µBlock.loadRedirectResources = function() { µBlock.loadRedirectResources = async function() {
return this.redirectEngine.resourcesFromSelfie().then(success => { try {
const success = await this.redirectEngine.resourcesFromSelfie();
if ( success === true ) { return true; } if ( success === true ) { return true; }
const fetchPromises = [ const fetchPromises = [
@ -1022,8 +970,7 @@
} }
} }
return Promise.all(fetchPromises); const results = await Promise.all(fetchPromises);
}).then(results => {
if ( Array.isArray(results) === false ) { return results; } if ( Array.isArray(results) === false ) { return results; }
let content = ''; let content = '';
@ -1041,35 +988,34 @@
this.redirectEngine.resourcesFromString(content); this.redirectEngine.resourcesFromString(content);
this.redirectEngine.selfieFromResources(); this.redirectEngine.selfieFromResources();
return true; } catch(ex) {
}).catch(reason => { log.info(ex);
log.info(reason);
return false; return false;
}); }
return true;
}; };
/******************************************************************************/ /******************************************************************************/
µBlock.loadPublicSuffixList = function() { µBlock.loadPublicSuffixList = async function() {
if ( this.hiddenSettings.disableWebAssembly === false ) { if ( this.hiddenSettings.disableWebAssembly === false ) {
publicSuffixList.enableWASM(); publicSuffixList.enableWASM();
} }
return this.assets.get( try {
'compiled/' + this.pslAssetKey const result = await this.assets.get(`compiled/${this.pslAssetKey}`);
).then(details => if ( publicSuffixList.fromSelfie(result.content, this.base64) ) {
publicSuffixList.fromSelfie(details.content, µBlock.base64) return;
).catch(reason => { }
console.info(reason); } catch (ex) {
return false; console.error(ex);
}).then(success => { return;
if ( success ) { return; } }
return this.assets.get(this.pslAssetKey, details => {
if ( details.content !== '' ) { const result = await this.assets.get(this.pslAssetKey);
this.compilePublicSuffixList(details.content); if ( result.content !== '' ) {
} this.compilePublicSuffixList(result.content);
}); }
});
}; };
µBlock.compilePublicSuffixList = function(content) { µBlock.compilePublicSuffixList = function(content) {
@ -1111,7 +1057,7 @@
}); });
}; };
const load = function() { const load = async function() {
return Promise.all([ return Promise.all([
µb.assets.get('selfie/main').then(details => { µb.assets.get('selfie/main').then(details => {
if ( if (
@ -1162,10 +1108,7 @@
}, µb.hiddenSettings.selfieAfter * 60000); }, µb.hiddenSettings.selfieAfter * 60000);
}; };
return { return { load, destroy };
load: load,
destroy: destroy
};
})(); })();
/******************************************************************************/ /******************************************************************************/
@ -1177,97 +1120,82 @@
// necessarily present, i.e. administrators may removed entries which // necessarily present, i.e. administrators may removed entries which
// values are left to the user's choice. // values are left to the user's choice.
µBlock.restoreAdminSettings = function() { µBlock.restoreAdminSettings = async function() {
return new Promise(resolve => { let data;
// >>>> start of executor try {
const json = await vAPI.adminStorage.getItem('adminSettings');
if ( vAPI.adminStorage instanceof Object === false ) { if ( typeof json === 'string' && json !== '' ) {
return resolve(); data = JSON.parse(json);
}
} catch (ex) {
console.error(ex);
} }
vAPI.adminStorage.getItem('adminSettings', json => { if ( data instanceof Object === false ) { return; }
let data;
if ( typeof json === 'string' && json !== '' ) { const bin = {};
try { let binNotEmpty = false;
data = JSON.parse(json);
} catch (ex) { // https://github.com/uBlockOrigin/uBlock-issues/issues/666
console.error(ex); // Allows an admin to set their own 'assets.json' file, with their
// own set of stock assets.
if (
typeof data.assetsBootstrapLocation === 'string' &&
data.assetsBootstrapLocation !== ''
) {
µBlock.assetsBootstrapLocation = data.assetsBootstrapLocation;
}
if ( typeof data.userSettings === 'object' ) {
for ( const name in this.userSettings ) {
if ( this.userSettings.hasOwnProperty(name) === false ) {
continue;
} }
} if ( data.userSettings.hasOwnProperty(name) === false ) {
continue;
if ( data instanceof Object === false ) {
return resolve();
}
const bin = {};
let binNotEmpty = false;
// https://github.com/uBlockOrigin/uBlock-issues/issues/666
// Allows an admin to set their own 'assets.json' file, with their
// own set of stock assets.
if (
typeof data.assetsBootstrapLocation === 'string' &&
data.assetsBootstrapLocation !== ''
) {
µBlock.assetsBootstrapLocation = data.assetsBootstrapLocation;
}
if ( typeof data.userSettings === 'object' ) {
for ( const name in this.userSettings ) {
if ( this.userSettings.hasOwnProperty(name) === false ) {
continue;
}
if ( data.userSettings.hasOwnProperty(name) === false ) {
continue;
}
bin[name] = data.userSettings[name];
binNotEmpty = true;
} }
} bin[name] = data.userSettings[name];
// 'selectedFilterLists' is an array of filter list tokens. Each token
// is a reference to an asset in 'assets.json'.
if ( Array.isArray(data.selectedFilterLists) ) {
bin.selectedFilterLists = data.selectedFilterLists;
binNotEmpty = true; binNotEmpty = true;
} }
}
if ( Array.isArray(data.whitelist) ) { // 'selectedFilterLists' is an array of filter list tokens. Each token
bin.netWhitelist = data.whitelist; // is a reference to an asset in 'assets.json'.
binNotEmpty = true; if ( Array.isArray(data.selectedFilterLists) ) {
} else if ( typeof data.netWhitelist === 'string' ) { bin.selectedFilterLists = data.selectedFilterLists;
bin.netWhitelist = data.netWhitelist.split('\n'); binNotEmpty = true;
binNotEmpty = true; }
}
if ( typeof data.dynamicFilteringString === 'string' ) { if ( Array.isArray(data.whitelist) ) {
bin.dynamicFilteringString = data.dynamicFilteringString; bin.netWhitelist = data.whitelist;
binNotEmpty = true; binNotEmpty = true;
} } else if ( typeof data.netWhitelist === 'string' ) {
bin.netWhitelist = data.netWhitelist.split('\n');
binNotEmpty = true;
}
if ( typeof data.urlFilteringString === 'string' ) { if ( typeof data.dynamicFilteringString === 'string' ) {
bin.urlFilteringString = data.urlFilteringString; bin.dynamicFilteringString = data.dynamicFilteringString;
binNotEmpty = true; binNotEmpty = true;
} }
if ( typeof data.hostnameSwitchesString === 'string' ) { if ( typeof data.urlFilteringString === 'string' ) {
bin.hostnameSwitchesString = data.hostnameSwitchesString; bin.urlFilteringString = data.urlFilteringString;
binNotEmpty = true; binNotEmpty = true;
} }
if ( binNotEmpty ) { if ( typeof data.hostnameSwitchesString === 'string' ) {
vAPI.storage.set(bin); bin.hostnameSwitchesString = data.hostnameSwitchesString;
} binNotEmpty = true;
}
if ( typeof data.userFilters === 'string' ) { if ( binNotEmpty ) {
this.assets.put(this.userFiltersPath, data.userFilters); vAPI.storage.set(bin);
} }
resolve(); if ( typeof data.userFilters === 'string' ) {
}); this.saveUserFilters(data.userFilters);
}
// <<<< end of executor
});
}; };
/******************************************************************************/ /******************************************************************************/