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.
vAPI.Net = class extends vAPI.Net {
constructor() {
super();
this.suspendedTabIds = new Set();
}
normalizeDetails(details) {
// Chromium 63+ supports the `initiator` property, which contains
// the URL of the origin from which the network request was made.
@ -184,21 +179,22 @@ vAPI.Tabs = class extends vAPI.Tabs {
// https://github.com/uBlockOrigin/uBlock-issues/issues/2063
// Do not interfere with root document
suspendOneRequest(details) {
this.suspendedTabIds.add(details.tabId);
this.onBeforeSuspendableRequest(details);
if ( details.type === 'main_frame' ) { return; }
return {
redirectUrl: vAPI.getURL(`web_accessible_resources/empty?secret=${vAPI.warSecret()}`)
};
return { cancel: true };
}
unsuspendAllRequests(discard = false) {
if ( discard !== true ) {
for ( const tabId of this.suspendedTabIds ) {
if ( discard === true ) { return; }
const toReload = [];
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.suspendDepth = 0;
this.unprocessedTabs = new Map();
this.lastUnprocessedRequestTime = Number.MAX_SAFE_INTEGER;
browser.webRequest.onBeforeRequest.addListener(
details => {
@ -1316,7 +1317,7 @@ vAPI.Net = class {
let i = requests.length;
while ( i-- ) {
const r = listener(requests[i]);
if ( r === undefined || r.cancel === false ) {
if ( r === undefined || r.cancel !== true ) {
requests.splice(i, 1);
}
}
@ -1366,18 +1367,26 @@ vAPI.Net = class {
this.unprocessedTabs.set(tabId, (requests = []));
}
requests.push(Object.assign({}, details));
this.lastUnprocessedRequestTime = Date.now();
}
hasUnprocessedRequest(tabId) {
if ( (Date.now() - this.lastUnprocessedRequestTime) > 60000 ) {
this.removeUnprocessedRequest();
}
return this.unprocessedTabs.size !== 0 &&
this.unprocessedTabs.has(tabId);
}
// https://github.com/uBlockOrigin/uBlock-issues/issues/2589
// - Aggressively clear the unprocessed-request status of all tabs as
// soon as there is a call to clear for one tab.
removeUnprocessedRequest() {
if ( this.unprocessedTabs.size === 0 ) { return true; }
removeUnprocessedRequest(tabId) {
if ( this.deferredSuspendableListener === undefined ) {
this.unprocessedTabs.clear();
if ( this.deferredSuspendableListener === undefined ) { return true; }
return true;
}
if ( tabId !== undefined ) {
this.unprocessedTabs.delete(tabId);
} else {
this.unprocessedTabs.clear();
}
if ( this.unprocessedTabs.size !== 0 ) { return false; }
this.suspendableListener = this.deferredSuspendableListener;
this.deferredSuspendableListener = undefined;
return true;