From c6793eff98049f8d9efe99fe9d4ae90beaf79e25 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 3 Nov 2016 23:42:03 -0400 Subject: [PATCH] code review re. #2067 --- src/js/start.js | 4 +-- src/js/traffic.js | 85 ++++++++++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/js/start.js b/src/js/start.js index 9032f7bd0..a07cd4e0a 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -79,12 +79,10 @@ var onAllReady = function() { //quickProfiler.stop(0); - vAPI.onLoadAllCompleted(); µb.contextMenu.update(null); µb.firstInstall = false; - vAPI.net.onBeforeReady = null; - vAPI.messaging.broadcast({ what: 'ublockOrigin-readyState-complete' }); + vAPI.net.onReady(); }; /******************************************************************************/ diff --git a/src/js/traffic.js b/src/js/traffic.js index 23038dcc6..b3a296fe6 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -33,9 +33,59 @@ var exports = {}; /******************************************************************************/ +// https://github.com/gorhill/uBlock/issues/2067 +// Experimental: Suspend tabs until uBO is fully ready. + +vAPI.net.onReady = function() { + if ( µBlock.hiddenSettings.suspendTabsUntilReady !== true ) { + vAPI.onLoadAllCompleted(); + } + var fn = onBeforeReady; + onBeforeReady = null; + if ( fn !== null ) { + fn('ready'); + } +}; + +var onBeforeReady = (function() { + var suspendedTabs = new Set(); + + var forceReloadSuspendedTabs = function() { + var iter = suspendedTabs.values(), + entry; + for (;;) { + entry = iter.next(); + if ( entry.done ) { break; } + vAPI.tabs.reload(entry.value); + } + }; + + return function(tabId) { + if ( + vAPI.isBehindTheSceneTabId(tabId) || + µBlock.hiddenSettings.suspendTabsUntilReady !== true + ) { + return; + } + if ( tabId === 'ready' ) { + forceReloadSuspendedTabs(); + return; + } + suspendedTabs.add(tabId); + return true; + }; +})(); + +/******************************************************************************/ + // Intercept and filter web requests. var onBeforeRequest = function(details) { + var tabId = details.tabId; + if ( onBeforeReady !== null && onBeforeReady(tabId) ) { + return { cancel: true }; + } + // Special handling for root document. // https://github.com/chrisaljoudi/uBlock/issues/1001 // This must be executed regardless of whether the request is @@ -46,7 +96,6 @@ var onBeforeRequest = function(details) { } // Special treatment: behind-the-scene requests - var tabId = details.tabId; if ( vAPI.isBehindTheSceneTabId(tabId) ) { return onBeforeBehindTheSceneRequest(details); } @@ -138,13 +187,6 @@ var onBeforeRequest = function(details) { /******************************************************************************/ var onBeforeRootFrameRequest = function(details) { - if ( - vAPI.net.onBeforeReady instanceof Function && - vAPI.net.onBeforeReady(details) === true - ) { - return { cancel: true }; - } - var tabId = details.tabId, requestURL = details.url, µb = µBlock; @@ -642,33 +684,6 @@ var headerIndexFromName = function(headerName, headers) { /******************************************************************************/ -// https://github.com/gorhill/uBlock/issues/2067 -// Experimental: Suspend tabs until uBO is fully ready. - -vAPI.net.onBeforeReady = function(details) { - if ( µBlock.hiddenSettings.suspendTabsUntilReady !== true ) { - return; - } - var pageURL = details.url; - if ( /^https?:\/\//.test(pageURL) === false ) { - return; - } - if ( - details.tabId === -1 || - details.type !== 'main_frame' || - details.frameId !== 0 - ) { - return; - } - vAPI.tabs.replace( - details.tabId, - vAPI.getURL('document-suspended.html?url=') + encodeURIComponent(pageURL) - ); - return true; -}; - -/******************************************************************************/ - vAPI.net.onBeforeRequest = { urls: [ 'http://*/*',