Skip to content

Commit

Permalink
Move early blocking of requests out of experimental status on Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Dec 23, 2018
1 parent 99cdec5 commit 41548be
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 28 deletions.
41 changes: 41 additions & 0 deletions platform/chromium/vapi-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
})();

/******************************************************************************/
49 changes: 49 additions & 0 deletions platform/firefox/vapi-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
})();

/******************************************************************************/
3 changes: 2 additions & 1 deletion src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ 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
// garbage collection of these resources kicks in. Relinquishing as soon
// as possible ensure minimal memory usage baseline.
µb.lz4Codec.relinquish();

µb.webRequest.start();
initializeTabs();

// https://github.com/chrisaljoudi/uBlock/issues/184
Expand Down
38 changes: 11 additions & 27 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
};
})(),

Expand Down

1 comment on commit 41548be

@gorhill
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to link to issue #1327 in commit message, not #2067.

Please sign in to comment.