From d0113278aa7dcb7b58adc5e2928ddf9f48bd0af1 Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 11 Mar 2015 14:52:20 -0400 Subject: [PATCH] code review --- src/js/assets.js | 30 ++++++++++++++++++++++-------- src/js/start.js | 6 ++++-- src/js/storage.js | 13 ++++++++++--- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 056410416..79a05c380 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -67,7 +67,7 @@ var exports = { autoUpdateDelay: 4 * oneDay, // https://github.com/gorhill/uBlock/issues/426 - allowRemoteFetch: true + remoteFetchBarrier: 0 }; /******************************************************************************/ @@ -358,7 +358,7 @@ var getRepoMetadata = function(callback) { return; } - if ( exports.allowRemoteFetch && lastRepoMetaIsRemote === false ) { + if ( exports.remoteFetchBarrier === 0 && lastRepoMetaIsRemote === false ) { lastRepoMetaTimestamp = 0; } if ( (Date.now() - lastRepoMetaTimestamp) >= refreshRepoMetaPeriod ) { @@ -370,7 +370,7 @@ var getRepoMetadata = function(callback) { } lastRepoMetaTimestamp = Date.now(); - lastRepoMetaIsRemote = exports.allowRemoteFetch; + lastRepoMetaIsRemote = exports.remoteFetchBarrier === 0; var localChecksums; var repoChecksums; @@ -528,7 +528,7 @@ var readLocalFile = function(path, callback) { var readRepoFile = function(path, callback) { // https://github.com/gorhill/uBlock/issues/426 - if ( exports.allowRemoteFetch === false ) { + if ( exports.remoteFetchBarrier !== 0 ) { readLocalFile(path, callback); return; } @@ -668,7 +668,10 @@ var readRepoCopyAsset = function(path, callback) { // - Auto-update enabled AND (not in cache OR in cache but obsolete) var timestamp = entries[path]; var inCache = typeof timestamp === 'number'; - if ( exports.allowRemoteFetch && exports.autoUpdate && stringIsNotEmpty(homeURL) ) { + if ( + exports.remoteFetchBarrier === 0 && + exports.autoUpdate && stringIsNotEmpty(homeURL) + ) { if ( inCache === false || cacheIsObsolete(timestamp) ) { //console.log('µBlock> readRepoCopyAsset("%s") / onCacheMetaReady(): not cached or obsolete', path); getTextFileFromURL(homeURL, onHomeFileLoaded, onHomeFileError); @@ -696,7 +699,11 @@ var readRepoCopyAsset = function(path, callback) { } // Repo copy changed: fetch from home URL - if ( exports.allowRemoteFetch && exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) { + if ( + exports.remoteFetchBarrier === 0 && + exports.autoUpdate && + assetEntry.localChecksum !== assetEntry.repoChecksum + ) { //console.log('µBlock> readRepoCopyAsset("%s") / onRepoMetaReady(): repo has newer version', path); if ( stringIsNotEmpty(homeURL) ) { getTextFileFromURL(homeURL, onHomeFileLoaded, onHomeFileError); @@ -793,7 +800,11 @@ var readRepoOnlyAsset = function(path, callback) { } // Asset added or changed: load from repo URL and then cache result - if ( exports.allowRemoteFetch && exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) { + if ( + exports.remoteFetchBarrier === 0 && + exports.autoUpdate && + assetEntry.localChecksum !== assetEntry.repoChecksum + ) { //console.log('µBlock> readRepoOnlyAsset("%s") / onRepoMetaReady(): repo has newer version', path); getTextFileFromURL(repositoryURL, onRepoFileLoaded, onRepoFileError); return; @@ -872,7 +883,9 @@ var readExternalAsset = function(path, callback) { // - Auto-update enabled AND in cache but obsolete var timestamp = entries[path]; var notInCache = typeof timestamp !== 'number'; - var updateCache = exports.allowRemoteFetch && exports.autoUpdate && cacheIsObsolete(timestamp); + var updateCache = exports.remoteFetchBarrier === 0 && + exports.autoUpdate && + cacheIsObsolete(timestamp); if ( notInCache || updateCache ) { getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError); return; @@ -1150,6 +1163,7 @@ var onOneUpdated = function(details) { var path = details.path; if ( details.error ) { + manualUpdateNotify(false, updatedCount / (updatedCount + toUpdateCount)); //console.debug('µBlock.assetUpdater/onOneUpdated: "%s" failed', path); return; } diff --git a/src/js/start.js b/src/js/start.js index 16a370ee3..2892d4987 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -51,13 +51,16 @@ var onAllReady = function() { // Important: remove barrier to remote fetching, this was useful only // for launch time. - µb.assets.allowRemoteFetch = true; + µb.assets.remoteFetchBarrier -= 1; //quickProfiler.stop(0); vAPI.onLoadAllCompleted(); }; +// Forbid remote fetching of assets +µb.assets.remoteFetchBarrier += 1; + /******************************************************************************/ // Filtering engines dependencies: @@ -126,7 +129,6 @@ var onUserSettingsReady = function(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.assets.autoUpdateDelay = µb.updateAssetsEvery; diff --git a/src/js/storage.js b/src/js/storage.js index 04e6c0cbf..0e216971b 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -282,7 +282,14 @@ callback = this.noopFunc; } + // Never fetch from remote servers when we load filter lists: this has to + // be as fast as possible. + µb.assets.remoteFetchBarrier += 1; + var onDone = function() { + // Remove barrier to remote fetching + µb.assets.remoteFetchBarrier -= 1; + µb.staticNetFilteringEngine.freeze(); µb.cosmeticFilteringEngine.freeze(); vAPI.storage.set({ 'remoteBlacklists': µb.remoteBlacklists }); @@ -683,12 +690,12 @@ var µb = this; var updatedCount = details.updatedCount; - // Assets are supposed to have been all updated, avoid fetching from + // Assets are supposed to have been all updated, prevent fetching from // remote servers. - µb.assets.allowRemoteFetch = false; + µb.assets.remoteFetchBarrier += 1; var onFiltersReady = function() { - µb.assets.allowRemoteFetch = true; + µb.assets.remoteFetchBarrier -= 1; }; var onPSLReady = function() {