Avoid using `!` toolbar icon badge when inconsequential

uBO will now verify that at least one unprocessed network requests
at launch should have been blocked in order to warn users of
unprocessed network requests through the `!` toolbar icon badge.

For example, with default filter lists, there is nothing to block
on `wikipedia.org`, and hence in this case it's not useful to
present the user with the `!` badge.

Therefore uBO will not show the badge *only* when at least one
unprocessed network requests should have been blocked had uBO been
ready when it was fired by the browser.

Related commit:
- https://github.com/gorhill/uBlock/commit/769b8da664be
This commit is contained in:
Raymond Hill 2023-03-30 12:30:44 -04:00
parent 3d1c696e20
commit 1835e90125
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 38 additions and 19 deletions

View File

@ -1191,7 +1191,7 @@ vAPI.Net = class {
this.deferredSuspendableListener = undefined; this.deferredSuspendableListener = undefined;
this.listenerMap = new WeakMap(); this.listenerMap = new WeakMap();
this.suspendDepth = 0; this.suspendDepth = 0;
this.unprocessedTabs = new Set(); this.unprocessedTabs = new Map();
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
details => { details => {
@ -1252,6 +1252,17 @@ vAPI.Net = class {
this.onUnprocessedRequest(details); this.onUnprocessedRequest(details);
} }
setSuspendableListener(listener) { setSuspendableListener(listener) {
for ( const [ tabId, requests ] of this.unprocessedTabs ) {
let i = requests.length;
while ( i-- ) {
const r = listener(requests[i]);
if ( r === undefined || r.cancel === false ) {
requests.splice(i, 1);
}
}
if ( requests.length !== 0 ) { continue; }
this.unprocessedTabs.delete(tabId);
}
if ( this.unprocessedTabs.size !== 0 ) { if ( this.unprocessedTabs.size !== 0 ) {
this.deferredSuspendableListener = listener; this.deferredSuspendableListener = listener;
listener = details => { listener = details => {
@ -1285,11 +1296,16 @@ vAPI.Net = class {
return actualListener; return actualListener;
} }
onUnprocessedRequest(details) { onUnprocessedRequest(details) {
if ( details.tabId === -1 ) { return; } const { tabId } = details;
if ( tabId === -1 ) { return; }
if ( this.unprocessedTabs.size === 0 ) { if ( this.unprocessedTabs.size === 0 ) {
vAPI.setDefaultIcon('-loading', '!'); vAPI.setDefaultIcon('-loading', '!');
} }
this.unprocessedTabs.add(details.tabId); let requests = this.unprocessedTabs.get(tabId);
if ( requests === undefined ) {
this.unprocessedTabs.set(tabId, (requests = []));
}
requests.push(Object.assign({}, details));
} }
hasUnprocessedRequest(tabId) { hasUnprocessedRequest(tabId) {
return this.unprocessedTabs.size !== 0 && return this.unprocessedTabs.size !== 0 &&

View File

@ -103,6 +103,7 @@ const initializeTabs = async ( ) => {
const tabs = await vAPI.tabs.query({ url: '<all_urls>' }); const tabs = await vAPI.tabs.query({ url: '<all_urls>' });
for ( const tab of tabs ) { for ( const tab of tabs ) {
if ( tab.discarded === true ) { continue; } if ( tab.discarded === true ) { continue; }
if ( tab.status === 'unloaded' ) { continue; }
const { id, url } = tab; const { id, url } = tab;
µb.tabContextManager.commit(id, url); µb.tabContextManager.commit(id, url);
µb.bindTabToPageStore(id, 'tabCommitted', tab); µb.bindTabToPageStore(id, 'tabCommitted', tab);
@ -118,7 +119,8 @@ const initializeTabs = async ( ) => {
tabIds.push(id); tabIds.push(id);
} }
} }
const results = await Promise.all(toCheck); // We do not want to block on content scripts injection
Promise.all(toCheck).then(results => {
for ( let i = 0; i < results.length; i++ ) { for ( let i = 0; i < results.length; i++ ) {
const result = results[i]; const result = results[i];
if ( result.length === 0 || result[0] !== true ) { continue; } if ( result.length === 0 || result[0] !== true ) { continue; }
@ -133,6 +135,7 @@ const initializeTabs = async ( ) => {
} }
} }
} }
});
}; };
/******************************************************************************/ /******************************************************************************/
@ -462,6 +465,9 @@ if ( selfieIsValid !== true ) {
// This can be used to defer filtering decision-making. // This can be used to defer filtering decision-making.
µb.readyToFilter = true; µb.readyToFilter = true;
// Initialize internal state with maybe already existing tabs.
await initializeTabs();
// Start network observers. // Start network observers.
webRequest.start(); webRequest.start();
@ -472,9 +478,6 @@ webRequest.start();
// as possible ensure minimal memory usage baseline. // as possible ensure minimal memory usage baseline.
lz4Codec.relinquish(); lz4Codec.relinquish();
// Initialize internal state with maybe already existing tabs.
initializeTabs();
// https://github.com/chrisaljoudi/uBlock/issues/184 // https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future. // Check for updates not too far in the future.
io.addObserver(µb.assetObserver.bind(µb)); io.addObserver(µb.assetObserver.bind(µb));