1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Move early blocking of requests out of experimental status on Firefox

Related issues:
- https://github.com/gorhill/uBlock/issues/2067
- https://github.com/uBlockOrigin/uBlock-issues/issues/128

Related mozbug issue:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
This commit is contained in:
Raymond Hill 2018-12-23 17:59:31 -05:00
parent 99cdec5ba6
commit 41548be6be
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 103 additions and 28 deletions

View File

@ -148,3 +148,44 @@
})();
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/2067
// Experimental: Block everything until uBO is fully ready.
vAPI.net.onBeforeReady = (function() {
let pendings;
const handler = function(details) {
if ( pendings === undefined ) { return; }
if ( details.tabId < 0 ) { return; }
pendings.add(details.tabId);
return { cancel: true };
};
return {
experimental: true,
start: function() {
pendings = new Set();
browser.webRequest.onBeforeRequest.addListener(
handler,
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking' ]
);
},
// https://github.com/gorhill/uBlock/issues/2067
// Force-reload tabs for which network requests were blocked
// during launch. This can happen only if tabs were "suspended".
stop: function() {
if ( pendings === undefined ) { return; }
browser.webRequest.onBeforeRequest.removeListener(handler);
for ( const tabId of pendings ) {
vAPI.tabs.reload(tabId);
}
pendings = undefined;
},
};
})();
/******************************************************************************/

View File

@ -130,3 +130,52 @@
})();
/******************************************************************************/
// Related issues:
// - https://github.com/gorhill/uBlock/issues/2067
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
vAPI.net.onBeforeReady = (function() {
let pendings;
const handler = function(details) {
if ( pendings === undefined ) { return; }
if ( details.tabId < 0 ) { return; }
const pending = {
details: Object.assign({}, details),
resolve: undefined,
promise: undefined
};
pending.promise = new Promise(function(resolve) {
pending.resolve = resolve;
});
pendings.push(pending);
return pending.promise;
};
return {
start: function() {
pendings = [];
browser.webRequest.onBeforeRequest.addListener(
handler,
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking' ]
);
},
stop: function(resolver) {
if ( pendings === undefined ) { return; }
for ( const pending of pendings ) {
const result = resolver(pending.details);
pending.resolve(result);
}
pendings = undefined;
},
};
})();
/******************************************************************************/

View File

@ -53,6 +53,8 @@ vAPI.app.onShutdown = function() {
// - Schedule next update operation.
var onAllReady = function() {
µb.webRequest.start();
// Ensure that the resources allocated for decompression purpose (likely
// large buffers) are garbage-collectable immediately after launch.
// Otherwise I have observed that it may take quite a while before the
@ -60,7 +62,6 @@ var onAllReady = function() {
// as possible ensure minimal memory usage baseline.
µb.lz4Codec.relinquish();
µb.webRequest.start();
initializeTabs();
// https://github.com/chrisaljoudi/uBlock/issues/184

View File

@ -992,28 +992,17 @@ const strictBlockBypasser = {
return {
start: (function() {
const suspendedTabs = new Set();
const onBeforeReady = function(details) {
if ( details.type !== 'main_frame' && details.tabId > 0 ) {
suspendedTabs.add(details.tabId);
console.info('uBO suspend tab %d, block %s', details.tabId, details.url);
return { cancel: true };
}
};
// https://github.com/gorhill/uBlock/issues/2067
// Experimental: Block everything until uBO is fully ready.
// https://github.com/gorhill/uBlock/issues/3130
// Don't block root frame.
if ( µBlock.hiddenSettings.suspendTabsUntilReady ) {
vAPI.net.addListener(
'onBeforeRequest',
onBeforeReady,
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking' ]
);
if (
vAPI.net.onBeforeReady instanceof Object &&
(
vAPI.net.onBeforeReady.experimental !== true ||
µBlock.hiddenSettings.suspendTabsUntilReady
)
) {
vAPI.net.onBeforeReady.start();
}
return function() {
vAPI.net.removeListener('onBeforeRequest', onBeforeReady);
vAPI.net.addListener(
'onBeforeRequest',
onBeforeRequest,
@ -1040,14 +1029,9 @@ return {
[ 'blocking', 'requestBody' ]
);
}
// https://github.com/gorhill/uBlock/issues/2067
// Force-reload tabs for which network requests were blocked
// during launch. This can happen only if tabs were "suspended".
for ( const tabId of suspendedTabs ) {
console.info('uBO suspend tab %d, force reload', tabId);
vAPI.tabs.reload(tabId);
if ( vAPI.net.onBeforeReady instanceof Object ) {
vAPI.net.onBeforeReady.stop(onBeforeRequest);
}
suspendedTabs.clear();
};
})(),