From 4c7f4771e27bd7780202eafd41c2f2ae509b38ba Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 21 Jan 2015 21:46:11 -0500 Subject: [PATCH] #475: whitelist behind-the-scene by default --- src/js/messaging.js | 12 ++++++------ src/js/pagestore.js | 35 +++++++++++++++++++++-------------- src/js/storage.js | 44 +++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index f4e3e4142..0b6e5ab6b 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -277,12 +277,11 @@ var onMessage = function(request, sender, callback) { break; case 'toggleNetFiltering': - µb.toggleNetFilteringSwitch( - request.url, - request.scope, - request.state - ); - µb.updateBadgeAsync(request.tabId); + var pageStore = µb.pageStoreFromTabId(request.tabId); + if ( pageStore ) { + pageStore.toggleNetFilteringSwitch(request.url, request.scope, request.state); + µb.updateBadgeAsync(request.tabId); + } break; default: @@ -912,6 +911,7 @@ var restoreUserData = function(userData) { var onAllRemoved = function() { // Be sure to adjust `countdown` if adding/removing anything below + µb.XAL.keyvalSetOne('version', userData.version); µBlock.saveLocalSettings(onCountdown); µb.XAL.keyvalSetMany(userData.userSettings, onCountdown); µb.XAL.keyvalSetOne('remoteBlacklists', userData.filterLists, onCountdown); diff --git a/src/js/pagestore.js b/src/js/pagestore.js index d91c81b27..b0ebb493f 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -295,18 +295,7 @@ NetFilteringResultCache.prototype.init = function() { /******************************************************************************/ NetFilteringResultCache.prototype.dispose = function() { - for ( var key in this.urls ) { - if ( this.urls.hasOwnProperty(key) === false ) { - continue; - } - this.urls[key].dispose(); - } - this.urls = {}; - this.count = 0; - if ( this.timer !== null ) { - clearTimeout(this.timer); - this.timer = null; - } + this.empty(); this.boundPruneAsyncCallback = null; if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) { netFilteringCacheJunkyard.push(this); @@ -335,8 +324,19 @@ NetFilteringResultCache.prototype.add = function(context, result) { /******************************************************************************/ -NetFilteringResultCache.prototype.fetchAll = function() { - return this.urls; +NetFilteringResultCache.prototype.empty = function() { + for ( var key in this.urls ) { + if ( this.urls.hasOwnProperty(key) === false ) { + continue; + } + this.urls[key].dispose(); + } + this.urls = {}; + this.count = 0; + if ( this.timer !== null ) { + clearTimeout(this.timer); + this.timer = null; + } }; /******************************************************************************/ @@ -608,6 +608,13 @@ PageStore.prototype.getCosmeticFilteringSwitch = function() { /******************************************************************************/ +PageStore.prototype.toggleNetFilteringSwitch = function(url, scope, state) { + µb.toggleNetFilteringSwitch(url, scope, state); + this.netFilteringCache.empty(); +}; + +/******************************************************************************/ + PageStore.prototype.filterRequest = function(context) { if ( this.getNetFilteringSwitch() === false ) { this.cacheResult(context, ''); diff --git a/src/js/storage.js b/src/js/storage.js index ac87a64e3..2bb032f11 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -66,9 +66,7 @@ /******************************************************************************/ µBlock.saveUserSettings = function() { - vAPI.storage.set(this.userSettings, function() { - µBlock.getBytesInUse(); - }); + vAPI.storage.set(this.userSettings); }; /******************************************************************************/ @@ -97,9 +95,7 @@ var bin = { 'netWhitelist': this.stringFromWhitelist(this.netWhitelist) }; - vAPI.storage.set(bin, function() { - µBlock.getBytesInUse(); - }); + vAPI.storage.set(bin); this.netWhitelistModifyTime = Date.now(); }; @@ -128,7 +124,7 @@ }; var bin = { - 'netWhitelist': '', + 'netWhitelist': 'behind-the-scene', 'netExceptionList': '' }; vAPI.storage.get(bin, onWhitelistLoaded); @@ -595,18 +591,10 @@ var µb = this; var fromSelfie = false; - // Filter lists - // Whitelist - var countdown = 2; - // Final initialization steps after all needed assets are in memory. // - Initialize internal state with maybe already existing tabs. // - Schedule next update operation. - var doCountdown = function() { - countdown -= 1; - if ( countdown !== 0 ) { - return; - } + var onAllReady = function() { // https://github.com/gorhill/uBlock/issues/426 // Important: remove barrier to remote fetching, this was useful only // for launch time. @@ -619,6 +607,29 @@ µb.updater.restart(µb.firstUpdateAfter); }; + // To bring older versions up to date + var onVersionReady = function(bin) { + var lastVersion = bin.version || '0.0.0.0'; + // Whitelist behind-the-scene scope by default + if ( lastVersion.localeCompare('0.8.6.0') < 0 ) { + µb.toggleNetFilteringSwitch('http://behind-the-scene/', 'site', false); + } + vAPI.storage.set({ version: vAPI.app.version }); + onAllReady(); + }; + + // Filter lists + // Whitelist + var countdown = 2; + var doCountdown = function() { + countdown -= 1; + if ( countdown !== 0 ) { + return; + } + // 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() { @@ -689,5 +700,4 @@ this.loadUserSettings(onUserSettingsReady); this.loadWhitelist(onWhitelistReady); this.loadLocalSettings(); - this.getBytesInUse(); };