Better integrate suspend-network with unprocessed-request

Also added additional safeguard against sticky unprocessed-request
status: all unprocessed-request statuses will be cleared after a
minute elapsed following intialization. The idea is that if the
user hasn't care to force a reload of the page, then it's assumed
to be by choice and uBO should stop informing about the status.
This commit is contained in:
Raymond Hill 2023-04-12 16:10:00 -04:00
parent a1619a118d
commit bacf5d1661
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 27 additions and 22 deletions

View File

@ -105,11 +105,6 @@ vAPI.Tabs = class extends vAPI.Tabs {
// Extend base class to normalize as per platform. // Extend base class to normalize as per platform.
vAPI.Net = class extends vAPI.Net { vAPI.Net = class extends vAPI.Net {
constructor() {
super();
this.suspendedTabIds = new Set();
}
normalizeDetails(details) { normalizeDetails(details) {
// Chromium 63+ supports the `initiator` property, which contains // Chromium 63+ supports the `initiator` property, which contains
// the URL of the origin from which the network request was made. // the URL of the origin from which the network request was made.
@ -184,20 +179,21 @@ vAPI.Tabs = class extends vAPI.Tabs {
// https://github.com/uBlockOrigin/uBlock-issues/issues/2063 // https://github.com/uBlockOrigin/uBlock-issues/issues/2063
// Do not interfere with root document // Do not interfere with root document
suspendOneRequest(details) { suspendOneRequest(details) {
this.suspendedTabIds.add(details.tabId); this.onBeforeSuspendableRequest(details);
if ( details.type === 'main_frame' ) { return; } if ( details.type === 'main_frame' ) { return; }
return { return { cancel: true };
redirectUrl: vAPI.getURL(`web_accessible_resources/empty?secret=${vAPI.warSecret()}`)
};
} }
unsuspendAllRequests(discard = false) { unsuspendAllRequests(discard = false) {
if ( discard !== true ) { if ( discard === true ) { return; }
for ( const tabId of this.suspendedTabIds ) { const toReload = [];
vAPI.tabs.reload(tabId); for ( const tabId of this.unprocessedTabs.keys() ) {
} toReload.push(tabId);
}
this.removeUnprocessedRequest();
for ( const tabId of toReload ) {
vAPI.tabs.reload(tabId);
} }
this.suspendedTabIds.clear();
} }
}; };
} }

View File

@ -1252,6 +1252,7 @@ vAPI.Net = class {
this.listenerMap = new WeakMap(); this.listenerMap = new WeakMap();
this.suspendDepth = 0; this.suspendDepth = 0;
this.unprocessedTabs = new Map(); this.unprocessedTabs = new Map();
this.lastUnprocessedRequestTime = Number.MAX_SAFE_INTEGER;
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
details => { details => {
@ -1316,7 +1317,7 @@ vAPI.Net = class {
let i = requests.length; let i = requests.length;
while ( i-- ) { while ( i-- ) {
const r = listener(requests[i]); const r = listener(requests[i]);
if ( r === undefined || r.cancel === false ) { if ( r === undefined || r.cancel !== true ) {
requests.splice(i, 1); requests.splice(i, 1);
} }
} }
@ -1366,18 +1367,26 @@ vAPI.Net = class {
this.unprocessedTabs.set(tabId, (requests = [])); this.unprocessedTabs.set(tabId, (requests = []));
} }
requests.push(Object.assign({}, details)); requests.push(Object.assign({}, details));
this.lastUnprocessedRequestTime = Date.now();
} }
hasUnprocessedRequest(tabId) { hasUnprocessedRequest(tabId) {
if ( (Date.now() - this.lastUnprocessedRequestTime) > 60000 ) {
this.removeUnprocessedRequest();
}
return this.unprocessedTabs.size !== 0 && return this.unprocessedTabs.size !== 0 &&
this.unprocessedTabs.has(tabId); this.unprocessedTabs.has(tabId);
} }
// https://github.com/uBlockOrigin/uBlock-issues/issues/2589 removeUnprocessedRequest(tabId) {
// - Aggressively clear the unprocessed-request status of all tabs as if ( this.deferredSuspendableListener === undefined ) {
// soon as there is a call to clear for one tab. this.unprocessedTabs.clear();
removeUnprocessedRequest() { return true;
if ( this.unprocessedTabs.size === 0 ) { return true; } }
this.unprocessedTabs.clear(); if ( tabId !== undefined ) {
if ( this.deferredSuspendableListener === undefined ) { return true; } this.unprocessedTabs.delete(tabId);
} else {
this.unprocessedTabs.clear();
}
if ( this.unprocessedTabs.size !== 0 ) { return false; }
this.suspendableListener = this.deferredSuspendableListener; this.suspendableListener = this.deferredSuspendableListener;
this.deferredSuspendableListener = undefined; this.deferredSuspendableListener = undefined;
return true; return true;