code review: avoid redundant PSL selfie

This commit is contained in:
Raymond Hill 2018-04-06 16:02:35 -04:00
parent 79b4706746
commit 4d8974fe80
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
3 changed files with 32 additions and 44 deletions

View file

@ -132,8 +132,8 @@ var µBlock = (function() { // jshint ignore:line
// read-only
systemSettings: {
compiledMagic: 'puuijtkfpspv',
selfieMagic: 'tuqilngsxkwo'
compiledMagic: 1,
selfieMagic: 1
},
restoreBackupSettings: {
@ -177,11 +177,7 @@ var µBlock = (function() { // jshint ignore:line
epickerZap: false,
epickerEprom: null,
scriptlets: {
},
// so that I don't have to care for last comma
dummy: 0
scriptlets: {},
};
})();

View file

@ -19,8 +19,6 @@
Home: https://github.com/gorhill/uBlock
*/
/* global publicSuffixList */
'use strict';
/******************************************************************************/
@ -95,7 +93,12 @@ var onAllReady = function() {
// - PSL
var onPSLReady = function() {
µb.loadFilterLists(onAllReady);
µb.selfieManager.load(function(valid) {
if ( valid === true ) {
return onAllReady();
}
µb.loadFilterLists(onAllReady);
});
};
/******************************************************************************/
@ -161,31 +164,6 @@ var onVersionReady = function(lastVersion) {
/******************************************************************************/
var onSelfieReady = function(selfie) {
if (
selfie instanceof Object === false ||
selfie.magic !== µb.systemSettings.selfieMagic
) {
return false;
}
if ( publicSuffixList.fromSelfie(selfie.publicSuffixList) !== true ) {
return false;
}
if ( selfie.redirectEngine === undefined ) {
return false;
}
µb.availableFilterLists = selfie.availableFilterLists;
µb.staticNetFilteringEngine.fromSelfie(selfie.staticNetFilteringEngine);
µb.redirectEngine.fromSelfie(selfie.redirectEngine);
µb.staticExtFilteringEngine.fromSelfie(selfie.staticExtFilteringEngine);
µb.loadRedirectResources();
return true;
};
/******************************************************************************/
// https://github.com/chrisaljoudi/uBlock/issues/226
// Whitelist in memory.
// Whitelist parser needs PSL to be ready.
@ -243,7 +221,7 @@ var onSystemSettingsReady = function(fetched) {
if ( mustSaveSystemSettings ) {
fetched.selfie = null;
µb.selfieManager.destroy();
vAPI.storage.set(µb.systemSettings, µb.noopFunc);
vAPI.storage.set(µb.systemSettings);
}
};
@ -261,13 +239,8 @@ var onFirstFetchReady = function(fetched) {
onNetWhitelistReady(fetched.netWhitelist);
onVersionReady(fetched.version);
// If we have a selfie, skip loading PSL, filter lists
vAPI.cacheStorage.get('selfie', function(bin) {
if ( bin instanceof Object && onSelfieReady(bin.selfie) ) {
return onAllReady();
}
µb.loadPublicSuffixList(onPSLReady);
});
µb.loadPublicSuffixList(onPSLReady);
µb.loadRedirectResources();
};
/******************************************************************************/

View file

@ -1001,7 +1001,6 @@
timer = null;
var selfie = {
magic: this.systemSettings.selfieMagic,
publicSuffixList: publicSuffixList.toSelfie(),
availableFilterLists: this.availableFilterLists,
staticNetFilteringEngine: this.staticNetFilteringEngine.toSelfie(),
redirectEngine: this.redirectEngine.toSelfie(),
@ -1010,6 +1009,25 @@
vAPI.cacheStorage.set({ selfie: selfie });
}.bind(µBlock);
var load = function(callback) {
vAPI.cacheStorage.get('selfie', function(bin) {
var µb = µBlock;
if (
bin instanceof Object === false ||
bin.selfie instanceof Object === false ||
bin.selfie.magic !== µb.systemSettings.selfieMagic ||
bin.selfie.redirectEngine === undefined
) {
return callback(false);
}
µb.availableFilterLists = bin.selfie.availableFilterLists;
µb.staticNetFilteringEngine.fromSelfie(bin.selfie.staticNetFilteringEngine);
µb.redirectEngine.fromSelfie(bin.selfie.redirectEngine);
µb.staticExtFilteringEngine.fromSelfie(bin.selfie.staticExtFilteringEngine);
callback(true);
});
};
var destroy = function() {
if ( timer !== null ) {
clearTimeout(timer);
@ -1020,6 +1038,7 @@
}.bind(µBlock);
return {
load: load,
destroy: destroy
};
})();