Skip to content

Commit 41548be

Browse files
committed
Move early blocking of requests out of experimental status on Firefox
Related issues: - #2067 - uBlockOrigin/uBlock-issues#128 Related mozbug issue: - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
1 parent 99cdec5 commit 41548be

File tree

4 files changed

+103
-28
lines changed

4 files changed

+103
-28
lines changed

platform/chromium/vapi-webrequest.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,44 @@
148148
})();
149149

150150
/******************************************************************************/
151+
152+
// https://github.com/gorhill/uBlock/issues/2067
153+
// Experimental: Block everything until uBO is fully ready.
154+
155+
vAPI.net.onBeforeReady = (function() {
156+
let pendings;
157+
158+
const handler = function(details) {
159+
if ( pendings === undefined ) { return; }
160+
if ( details.tabId < 0 ) { return; }
161+
162+
pendings.add(details.tabId);
163+
164+
return { cancel: true };
165+
};
166+
167+
return {
168+
experimental: true,
169+
start: function() {
170+
pendings = new Set();
171+
browser.webRequest.onBeforeRequest.addListener(
172+
handler,
173+
{ urls: [ 'http://*/*', 'https://*/*' ] },
174+
[ 'blocking' ]
175+
);
176+
},
177+
// https://github.com/gorhill/uBlock/issues/2067
178+
// Force-reload tabs for which network requests were blocked
179+
// during launch. This can happen only if tabs were "suspended".
180+
stop: function() {
181+
if ( pendings === undefined ) { return; }
182+
browser.webRequest.onBeforeRequest.removeListener(handler);
183+
for ( const tabId of pendings ) {
184+
vAPI.tabs.reload(tabId);
185+
}
186+
pendings = undefined;
187+
},
188+
};
189+
})();
190+
191+
/******************************************************************************/

platform/firefox/vapi-webrequest.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,52 @@
130130
})();
131131

132132
/******************************************************************************/
133+
134+
// Related issues:
135+
// - https://github.com/gorhill/uBlock/issues/2067
136+
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
137+
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
138+
139+
vAPI.net.onBeforeReady = (function() {
140+
let pendings;
141+
142+
const handler = function(details) {
143+
if ( pendings === undefined ) { return; }
144+
if ( details.tabId < 0 ) { return; }
145+
146+
const pending = {
147+
details: Object.assign({}, details),
148+
resolve: undefined,
149+
promise: undefined
150+
};
151+
152+
pending.promise = new Promise(function(resolve) {
153+
pending.resolve = resolve;
154+
});
155+
156+
pendings.push(pending);
157+
158+
return pending.promise;
159+
};
160+
161+
return {
162+
start: function() {
163+
pendings = [];
164+
browser.webRequest.onBeforeRequest.addListener(
165+
handler,
166+
{ urls: [ 'http://*/*', 'https://*/*' ] },
167+
[ 'blocking' ]
168+
);
169+
},
170+
stop: function(resolver) {
171+
if ( pendings === undefined ) { return; }
172+
for ( const pending of pendings ) {
173+
const result = resolver(pending.details);
174+
pending.resolve(result);
175+
}
176+
pendings = undefined;
177+
},
178+
};
179+
})();
180+
181+
/******************************************************************************/

src/js/start.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ vAPI.app.onShutdown = function() {
5353
// - Schedule next update operation.
5454

5555
var onAllReady = function() {
56+
µb.webRequest.start();
57+
5658
// Ensure that the resources allocated for decompression purpose (likely
5759
// large buffers) are garbage-collectable immediately after launch.
5860
// Otherwise I have observed that it may take quite a while before the
5961
// garbage collection of these resources kicks in. Relinquishing as soon
6062
// as possible ensure minimal memory usage baseline.
6163
µb.lz4Codec.relinquish();
6264

63-
µb.webRequest.start();
6465
initializeTabs();
6566

6667
// https://github.com/chrisaljoudi/uBlock/issues/184

src/js/traffic.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -992,28 +992,17 @@ const strictBlockBypasser = {
992992

993993
return {
994994
start: (function() {
995-
const suspendedTabs = new Set();
996-
const onBeforeReady = function(details) {
997-
if ( details.type !== 'main_frame' && details.tabId > 0 ) {
998-
suspendedTabs.add(details.tabId);
999-
console.info('uBO suspend tab %d, block %s', details.tabId, details.url);
1000-
return { cancel: true };
1001-
}
1002-
};
1003-
// https://github.com/gorhill/uBlock/issues/2067
1004-
// Experimental: Block everything until uBO is fully ready.
1005-
// https://github.com/gorhill/uBlock/issues/3130
1006-
// Don't block root frame.
1007-
if ( µBlock.hiddenSettings.suspendTabsUntilReady ) {
1008-
vAPI.net.addListener(
1009-
'onBeforeRequest',
1010-
onBeforeReady,
1011-
{ urls: [ 'http://*/*', 'https://*/*' ] },
1012-
[ 'blocking' ]
1013-
);
995+
if (
996+
vAPI.net.onBeforeReady instanceof Object &&
997+
(
998+
vAPI.net.onBeforeReady.experimental !== true ||
999+
µBlock.hiddenSettings.suspendTabsUntilReady
1000+
)
1001+
) {
1002+
vAPI.net.onBeforeReady.start();
10141003
}
1004+
10151005
return function() {
1016-
vAPI.net.removeListener('onBeforeRequest', onBeforeReady);
10171006
vAPI.net.addListener(
10181007
'onBeforeRequest',
10191008
onBeforeRequest,
@@ -1040,14 +1029,9 @@ return {
10401029
[ 'blocking', 'requestBody' ]
10411030
);
10421031
}
1043-
// https://github.com/gorhill/uBlock/issues/2067
1044-
// Force-reload tabs for which network requests were blocked
1045-
// during launch. This can happen only if tabs were "suspended".
1046-
for ( const tabId of suspendedTabs ) {
1047-
console.info('uBO suspend tab %d, force reload', tabId);
1048-
vAPI.tabs.reload(tabId);
1032+
if ( vAPI.net.onBeforeReady instanceof Object ) {
1033+
vAPI.net.onBeforeReady.stop(onBeforeRequest);
10491034
}
1050-
suspendedTabs.clear();
10511035
};
10521036
})(),
10531037

0 commit comments

Comments
 (0)