diff --git a/src/js/start.js b/src/js/start.js index fee730fc7..9ec14e7a6 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -19,7 +19,7 @@ Home: https://github.com/gorhill/uBlock */ -/* global vAPI, µBlock */ +/* global publicSuffixList, vAPI, µBlock */ /******************************************************************************/ @@ -31,13 +31,15 @@ quickProfiler.start('start.js'); /******************************************************************************/ +var µb = µBlock; + +/******************************************************************************/ + // Final initialization steps after all needed assets are in memory. // - Initialize internal state with maybe already existing tabs. // - Schedule next update operation. var onAllReady = function() { - var µb = µBlock; - // https://github.com/gorhill/uBlock/issues/184 // Check for updates not too far in the future. µb.assetUpdater.onStart.addEventListener(µb.updateStartHandler.bind(µb)); @@ -56,12 +58,18 @@ var onAllReady = function() { /******************************************************************************/ +// Filtering engines dependencies: +// - PSL + +var onPSLReady = function() { + µb.loadFilterLists(onAllReady); +}; + +/******************************************************************************/ + // To bring older versions up to date -var onVersionReady = function(bin) { - var µb = µBlock; - var lastVersion = bin.version || '0.0.0.0'; - +var onVersionReady = function(lastVersion) { // Whitelist some key scopes by default if ( lastVersion.localeCompare('0.8.6.0') < 0 ) { µb.netWhitelist = µb.whitelistFromString( @@ -71,33 +79,25 @@ var onVersionReady = function(bin) { ); µb.saveWhitelist(); } - - vAPI.storage.set({ version: vAPI.app.version }); - onAllReady(); -}; - -/******************************************************************************/ - -// Filter lists -// Whitelist - -var countdown = 2; -var doCountdown = function() { - countdown -= 1; - if ( countdown !== 0 ) { - return; + if ( lastVersion !== vAPI.app.version ) { + vAPI.storage.set({ version: vAPI.app.version }); } - // Last step: do whatever is necessary when version changes - vAPI.storage.get('version', onVersionReady); }; /******************************************************************************/ -// Filters are in memory. -// Filter engines need PSL to be ready. - -var onFiltersReady = function() { - doCountdown(); +var onSelfieReady = function(selfie) { + if ( selfie === null || selfie.magic !== µb.systemSettings.selfieMagic ) { + return false; + } + if ( publicSuffixList.fromSelfie(selfie.publicSuffixList) !== true ) { + return false; + } + //console.log('start.js/onSelfieReady: selfie looks good'); + µb.remoteBlacklists = selfie.filterLists; + µb.staticNetFilteringEngine.fromSelfie(selfie.staticNetFilteringEngine); + µb.cosmeticFilteringEngine.fromSelfie(selfie.cosmeticFilteringEngine); + return true; }; /******************************************************************************/ @@ -107,45 +107,25 @@ var onFiltersReady = function() { // Whitelist parser needs PSL to be ready. // gorhill 2014-12-15: not anymore -var onWhitelistReady = function() { - doCountdown(); -}; - -/******************************************************************************/ - -// Load order because dependencies: -// User settings -> PSL -> [filter lists] - -var onPSLReady = function() { - µBlock.loadFilterLists(onFiltersReady); -}; - -/******************************************************************************/ - -// If no selfie available, take the long way, i.e. load and parse -// raw data. - -var onSelfieReady = function(success) { - if ( success === true ) { - onFiltersReady(); - return; - } - µBlock.loadPublicSuffixList(onPSLReady); +var onNetWhitelistReady = function(netWhitelistRaw) { + µb.netWhitelist = µb.whitelistFromString(netWhitelistRaw); + µb.netWhitelistModifyTime = Date.now(); }; /******************************************************************************/ // User settings are in memory -var onUserSettingsReady = function(userSettings) { - var µb = µBlock; +var onUserSettingsReady = function(fetched) { + var userSettings = µb.userSettings; + + fromFetch(userSettings, fetched); // https://github.com/gorhill/uBlock/issues/426 // Important: block remote fetching for when loading assets at launch // time. µb.assets.allowRemoteFetch = false; µb.assets.autoUpdate = userSettings.autoUpdate; - µb.fromSelfie(onSelfieReady); // https://github.com/gorhill/uBlock/issues/540 // Disabling local mirroring for the time being @@ -163,32 +143,87 @@ var onUserSettingsReady = function(userSettings) { /******************************************************************************/ -// Housekeeping, as per system setting changes - -var onSystemSettingsReady = function(system) { - var µb = µBlock; - - var mustSaveSystemSettings = false; - if ( system.compiledMagic !== µb.systemSettings.compiledMagic ) { - µb.assets.purge(/^cache:\/\/compiled-/); - mustSaveSystemSettings = true; - } - if ( system.selfieMagic !== µb.systemSettings.selfieMagic ) { - µb.destroySelfie(); - mustSaveSystemSettings = true; - } - if ( mustSaveSystemSettings ) { - µb.saveSystemSettings(); - } - - µb.loadUserSettings(onUserSettingsReady); - µb.loadWhitelist(onWhitelistReady); - µb.loadLocalSettings(); +var onLocalSettingsReady = function(fetched) { + fromFetch(µb.localSettings, fetched); }; /******************************************************************************/ -µBlock.loadSystemSettings(onSystemSettingsReady); +// Housekeeping, as per system setting changes + +var onSystemSettingsReady = function(fetched) { + var mustSaveSystemSettings = false; + if ( fetched.compiledMagic !== µb.systemSettings.compiledMagic ) { + µb.assets.purge(/^cache:\/\/compiled-/); + mustSaveSystemSettings = true; + } + if ( fetched.selfieMagic !== µb.systemSettings.selfieMagic ) { + fetched.selfie = null; + µb.destroySelfie(); + mustSaveSystemSettings = true; + } + if ( mustSaveSystemSettings ) { + vAPI.storage.set(µb.systemSettings, µb.noopFunc); + } +}; + +/******************************************************************************/ + +var onFirstFetchReady = function(fetched) { + + // Order is important -- do not change: + onSystemSettingsReady(fetched); + onLocalSettingsReady(fetched); + onUserSettingsReady(fetched); + onNetWhitelistReady(fetched.netWhitelist); + onVersionReady(fetched.version); + + // If we have a selfie, skip loading PSL, filters + if ( onSelfieReady(fetched.selfie) ) { + onAllReady(); + return; + } + + µb.loadPublicSuffixList(onPSLReady); +}; + +/******************************************************************************/ + +var fetchableProps = { + 'compiledMagic': '', + 'netWhitelist': '', + 'selfie': null, + 'selfieMagic': '', + 'version': '0.0.0.0' +}; + +var toFetch = function(from, fetched) { + for ( var k in from ) { + if ( from.hasOwnProperty(k) === false ) { + continue; + } + fetched[k] = from[k]; + } +}; + +var fromFetch = function(to, fetched) { + for ( var k in to ) { + if ( to.hasOwnProperty(k) === false ) { + continue; + } + if ( fetched.hasOwnProperty(k) === false ) { + continue; + } + to[k] = fetched[k]; + } +}; + +/******************************************************************************/ + +toFetch(µb.localSettings, fetchableProps); +toFetch(µb.userSettings, fetchableProps); + +vAPI.storage.get(fetchableProps, onFirstFetchReady); /******************************************************************************/ diff --git a/src/js/storage.js b/src/js/storage.js index b4e8a6911..9b21396d0 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -43,38 +43,13 @@ /******************************************************************************/ -µBlock.loadLocalSettings = function() { - var settingsLoaded = function(store) { - µBlock.localSettings = store; - }; - - vAPI.storage.get(this.localSettings, settingsLoaded); -}; - -/******************************************************************************/ - -µBlock.saveSystemSettings = function() { - vAPI.storage.set(this.systemSettings, this.noopFunc); -}; - -/******************************************************************************/ - -µBlock.loadSystemSettings = function(callback) { - vAPI.storage.get({ - compiledMagic: '', - selfieMagic: '' - }, callback); -}; - -/******************************************************************************/ - // Save local settings regularly. Not critical. µBlock.asyncJobs.add( 'autoSaveLocalSettings', null, µBlock.saveLocalSettings.bind(µBlock), - 2 * 60 * 1000, + 4 * 60 * 1000, true ); @@ -86,19 +61,6 @@ /******************************************************************************/ -µBlock.loadUserSettings = function(callback) { - var settingsLoaded = function(store) { - µBlock.userSettings = store; - if ( typeof callback === 'function' ) { - callback(µBlock.userSettings); - } - }; - - vAPI.storage.get(this.userSettings, settingsLoaded); -}; - -/******************************************************************************/ - µBlock.savePermanentFirewallRules = function() { this.userSettings.dynamicFilteringString = this.permanentFirewall.toString(); this.XAL.keyvalSetOne('dynamicFilteringString', this.userSettings.dynamicFilteringString); @@ -116,25 +78,6 @@ /******************************************************************************/ -µBlock.loadWhitelist = function(callback) { - var onWhitelistLoaded = function(store) { - var µb = µBlock; - µb.netWhitelist = µb.whitelistFromString(store.netWhitelist); - µb.netWhitelistModifyTime = Date.now(); - - if ( typeof callback === 'function' ) { - callback(); - } - }; - - var bin = { - 'netWhitelist': this.netWhitelistDefault - }; - vAPI.storage.get(bin, onWhitelistLoaded); -}; - -/******************************************************************************/ - µBlock.saveUserFilters = function(content, callback) { return this.assets.put(this.userFiltersPath, content, callback); }; @@ -310,12 +253,13 @@ µb.staticNetFilteringEngine.freeze(); µb.cosmeticFilteringEngine.freeze(); vAPI.storage.set({ 'remoteBlacklists': µb.remoteBlacklists }); + + //quickProfiler.stop(0); + vAPI.messaging.broadcast({ what: 'allFilterListsReloaded' }); callback(); µb.toSelfieAsync(); - - //quickProfiler.stop(0); }; var applyCompiledFilters = function(path, compiled) { @@ -687,35 +631,6 @@ /******************************************************************************/ -µBlock.fromSelfie = function(callback) { - var µb = this; - - if ( typeof callback !== 'function' ) { - callback = this.noopFunc; - } - - var onSelfieReady = function(store) { - var selfie = store.selfie; - if ( typeof selfie !== 'object' || selfie.magic !== µb.systemSettings.selfieMagic ) { - callback(false); - return; - } - if ( publicSuffixList.fromSelfie(selfie.publicSuffixList) !== true ) { - callback(false); - return; - } - //console.log('µBlock.fromSelfie> selfie looks good'); - µb.remoteBlacklists = selfie.filterLists; - µb.staticNetFilteringEngine.fromSelfie(selfie.staticNetFilteringEngine); - µb.cosmeticFilteringEngine.fromSelfie(selfie.cosmeticFilteringEngine); - callback(true); - }; - - vAPI.storage.get('selfie', onSelfieReady); -}; - -/******************************************************************************/ - µBlock.destroySelfie = function() { vAPI.storage.remove('selfie'); this.asyncJobs.remove('toSelfie'); diff --git a/src/lib/publicsuffixlist.js b/src/lib/publicsuffixlist.js index 1e8a94c1c..fa1803704 100644 --- a/src/lib/publicsuffixlist.js +++ b/src/lib/publicsuffixlist.js @@ -42,7 +42,7 @@ var selfieMagic = 'iscjsfsaolnm'; // This value dictate how the search will be performed: // < this.cutoffLength = indexOf() // >= this.cutoffLength = binary search -var cutoffLength = 480; +var cutoffLength = 256; var mustPunycode = /[^a-z0-9.-]/; /******************************************************************************/