From df08b12d48057d46c2e795b02688595efcfa551f Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 4 Apr 2020 11:34:43 -0400 Subject: [PATCH] Fix race condition at browser launch re. cosmetic filtering Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/974 Related feedback: - https://www.reddit.com/r/uBlockOrigin/comments/fuscia/ The race condition was that a content script could query the main process to retrieve cosmetic filters while the cosmetic filters had not been yet fully loaded into memory. The fix ensure that an already injected content script will re-query once the cosmetic filters are fully loaded in memory at browser launch time. --- src/js/background.js | 4 ++++ src/js/messaging.js | 1 + src/js/scriptlets/should-inject-contentscript.js | 2 +- src/js/start.js | 4 ++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/js/background.js b/src/js/background.js index 87143a7ff..c3b074592 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -165,6 +165,10 @@ const µBlock = (( ) => { // jshint ignore:line selectedFilterLists: [], availableFilterLists: {}, + // https://github.com/uBlockOrigin/uBlock-issues/issues/974 + // This can be used to defer filtering decision-making. + readyToFilter: false, + pageStores: new Map(), pageStoresToken: 0, diff --git a/src/js/messaging.js b/src/js/messaging.js index 9cda182ea..b57b482fc 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -509,6 +509,7 @@ vAPI.messaging.listen({ const µb = µBlock; const retrieveContentScriptParameters = function(senderDetails, request) { + if ( µb.readyToFilter !== true ) { return; } const { url, tabId, frameId } = senderDetails; if ( url === undefined || tabId === undefined || frameId === undefined ) { return; diff --git a/src/js/scriptlets/should-inject-contentscript.js b/src/js/scriptlets/should-inject-contentscript.js index 2e8440f34..b7458ad04 100644 --- a/src/js/scriptlets/should-inject-contentscript.js +++ b/src/js/scriptlets/should-inject-contentscript.js @@ -27,7 +27,7 @@ // https://github.com/uBlockOrigin/uBlock-issues/issues/403 // If the content script was not boostrapped, give it another try. -(function() { +(( ) => { try { let status = vAPI.uBO !== true; if ( status === false && vAPI.bootstrap ) { diff --git a/src/js/start.js b/src/js/start.js index 826b74338..82f52a047 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -333,6 +333,10 @@ if ( selfieIsValid !== true ) { // Final initialization steps after all needed assets are in memory. +// https://github.com/uBlockOrigin/uBlock-issues/issues/974 +// This can be used to defer filtering decision-making. +µb.readyToFilter = true; + // Start network observers. µb.webRequest.start();