Skip to content

Commit

Permalink
Add setting to control suspension on network activity at launch
Browse files Browse the repository at this point in the history
Related discussion:
- a0a9497#commitcomment-62560291

The new setting, when disabled (enabled by default), allows a user
to prevent uBO from waiting for all filter lists to be loaded
before allowing network activity at launch. The setting is enabled
by default, meaning uBO waits for all filter lists to be loaded in
memory before unsuspending network activity. Some users may find
this behavior undesirable, hence the new setting.

This gives the option to potentially speed up page load at launch,
at the cost of potentially not properly filtering network requests
as per filter lists/rules.

For platforms not supporting the suspension of network activity,
the setting will merely prevent whatever mechanism exists on the
platform to mitigate improper filtering of network requests at
launch. For example, in Chromium-based browsers, unchecking the
new setting will prevent the browser from re-loading tabs for
which there was network activity while in "suspended" state at
launch.
  • Loading branch information
gorhill committed Dec 30, 2021
1 parent 0c03244 commit 925c8d5
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 54 deletions.
8 changes: 5 additions & 3 deletions platform/chromium/vapi-background-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ vAPI.Tabs = class extends vAPI.Tabs {
return { cancel: true };
}

unsuspendAllRequests() {
for ( const tabId of this.suspendedTabIds ) {
vAPI.tabs.reload(tabId);
unsuspendAllRequests(discard = false) {
if ( discard !== true ) {
for ( const tabId of this.suspendedTabIds ) {
vAPI.tabs.reload(tabId);
}
}
this.suspendedTabIds.clear();
}
Expand Down
10 changes: 4 additions & 6 deletions platform/common/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,20 +1220,18 @@ vAPI.Net = class {
}
unsuspendAllRequests() {
}
suspend(force = false) {
if ( this.canSuspend() || force ) {
this.suspendDepth += 1;
}
suspend() {
this.suspendDepth += 1;
}
unsuspend(all = false) {
unsuspend({ all = false, discard = false } = {}) {
if ( this.suspendDepth === 0 ) { return; }
if ( all ) {
this.suspendDepth = 0;
} else {
this.suspendDepth -= 1;
}
if ( this.suspendDepth !== 0 ) { return; }
this.unsuspendAllRequests();
this.unsuspendAllRequests(discard);
}
canSuspend() {
return false;
Expand Down
8 changes: 6 additions & 2 deletions platform/firefox/vapi-background-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,15 @@ import {
this.pendingRequests.push(pending);
return pending.promise;
}
unsuspendAllRequests() {
unsuspendAllRequests(discard = false) {
const pendingRequests = this.pendingRequests;
this.pendingRequests = [];
for ( const entry of pendingRequests ) {
entry.resolve(this.onBeforeSuspendableRequest(entry.details));
entry.resolve(
discard !== true
? this.onBeforeSuspendableRequest(entry.details)
: undefined
);
}
}
canSuspend() {
Expand Down
13 changes: 10 additions & 3 deletions src/3p-filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
</div>

<div>
<div class="li"><label><span class="input checkbox"><input type="checkbox" id="autoUpdate"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span data-i18n="3pAutoUpdatePrompt1"></span></label></div>
<div class="li"><label><span class="input checkbox"><input type="checkbox" id="parseCosmeticFilters"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span><span data-i18n="3pParseAllABPHideFiltersPrompt1"></span>&nbsp;<span class="fa-icon info" data-i18n-title="3pParseAllABPHideFiltersInfo">question-circle</span></span></label>
<div class="li">
<label><span class="input checkbox"><input type="checkbox" id="autoUpdate"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span data-i18n="3pAutoUpdatePrompt1"></span></label>
</div>
<div class="li"><label><span class="input checkbox"><input type="checkbox" id="ignoreGenericCosmeticFilters"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span><span data-i18n="3pIgnoreGenericCosmeticFilters"></span>&nbsp;<span class="fa-icon info" data-i18n-title="3pIgnoreGenericCosmeticFiltersInfo">question-circle</span></span></label>
<div class="li">
<label><span class="input checkbox"><input type="checkbox" id="parseCosmeticFilters"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span><span data-i18n="3pParseAllABPHideFiltersPrompt1"></span>&nbsp;<span class="fa-icon info" data-i18n-title="3pParseAllABPHideFiltersInfo">question-circle</span></span></label>
</div>
<div class="li">
<label><span class="input checkbox"><input type="checkbox" id="ignoreGenericCosmeticFilters"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span><span data-i18n="3pIgnoreGenericCosmeticFilters"></span>&nbsp;<span class="fa-icon info" data-i18n-title="3pIgnoreGenericCosmeticFiltersInfo">question-circle</span></span></label>
</div>
<div class="li">
<label><span class="input checkbox"><input type="checkbox" id="suspendUntilListsAreLoaded"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span><span data-i18n="3pSuspendUntilListsAreLoaded"></span></label>
</div>
</div>

Expand Down
15 changes: 10 additions & 5 deletions src/js/3p-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ const renderFilterLists = function(soft) {
// Re-insert import widget.
uDom('[data-groupkey="custom"] .listEntries').append(importWidget);

uDom.nodeFromId('autoUpdate').checked = listDetails.autoUpdate === true;
uDom.nodeFromId('autoUpdate').checked =
listDetails.autoUpdate === true;
uDom.nodeFromId('listsOfBlockedHostsPrompt').textContent =
vAPI.i18n('3pListsOfBlockedHostsPrompt')
.replace(
Expand All @@ -301,6 +302,8 @@ const renderFilterLists = function(soft) {
listDetails.parseCosmeticFilters === true;
uDom.nodeFromId('ignoreGenericCosmeticFilters').checked =
listDetails.ignoreGenericCosmeticFilters === true;
uDom.nodeFromId('suspendUntilListsAreLoaded').checked =
listDetails.suspendUntilListsAreLoaded === true;

// Compute a hash of the settings so that we can keep track of changes
// affecting the loading of filter lists.
Expand Down Expand Up @@ -529,11 +532,12 @@ const buttonPurgeAllHandler = async function(hard) {

/******************************************************************************/

const autoUpdateCheckboxChanged = function() {
const userSettingCheckboxChanged = function() {
const target = event.target;
messaging.send('dashboard', {
what: 'userSettings',
name: 'autoUpdate',
value: this.checked,
name: target.id,
value: target.checked,
});
};

Expand Down Expand Up @@ -683,9 +687,10 @@ self.hasUnsavedData = function() {

/******************************************************************************/

uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
uDom('#autoUpdate').on('change', userSettingCheckboxChanged);
uDom('#parseCosmeticFilters').on('change', onFilteringSettingsChanged);
uDom('#ignoreGenericCosmeticFilters').on('change', onFilteringSettingsChanged);
uDom('#suspendUntilListsAreLoaded').on('change', userSettingCheckboxChanged);
uDom('#buttonApply').on('click', ( ) => { buttonApplyHandler(); });
uDom('#buttonUpdate').on('click', ( ) => { buttonUpdateHandler(); });
uDom('#buttonPurgeAll').on('click', ev => {
Expand Down
4 changes: 2 additions & 2 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const hiddenSettingsDefault = {
requestJournalProcessPeriod: 1000,
selfieAfter: 2,
strictBlockingBypassDuration: 120,
suspendTabsUntilReady: 'unset',
uiPopupConfig: 'unset',
uiFlavor: 'unset',
uiStyles: 'unset',
Expand Down Expand Up @@ -109,6 +108,7 @@ const userSettingsDefault = {
prefetchingDisabled: true,
requestLogMaxEntries: 1000,
showIconBadge: true,
suspendUntilListsAreLoaded: true,
tooltipsDisabled: false,
webrtcIPAddressHidden: false,
};
Expand Down Expand Up @@ -214,7 +214,7 @@ const µBlock = { // jshint ignore:line
readyToFilter: false,

supportStats: {
launchToReadiness: '',
allReadyAfter: '',
},

pageStores: new Map(),
Expand Down
1 change: 1 addition & 0 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ const getLists = async function(callback) {
isUpdating: io.isUpdating(),
netFilterCount: staticNetFilteringEngine.getFilterCount(),
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
suspendUntilListsAreLoaded: µb.userSettings.suspendUntilListsAreLoaded,
userFiltersPath: µb.userFiltersPath
};
const [ lists, metadata ] = await Promise.all([
Expand Down
20 changes: 10 additions & 10 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ const onNetWhitelistReady = function(netWhitelistRaw, adminExtra) {
// User settings are in memory

const onUserSettingsReady = function(fetched) {
// Terminate suspended state?
if ( fetched.suspendUntilListsAreLoaded === false ) {
vAPI.net.unsuspend({ all: true, discard: true });
ubolog(`Unsuspend network activity listener`);
µb.supportStats.unsuspendAfter = `${Date.now() - vAPI.T0} ms`;
}

// `externalLists` will be deprecated in some future, it is kept around
// for forward compatibility purpose, and should reflect the content of
// `importedLists`.
Expand Down Expand Up @@ -282,13 +289,6 @@ const onHiddenSettingsReady = async function() {
ubolog(`Override default webext flavor with ${tokens}`);
}

// Maybe override current network listener suspend state
if ( µb.hiddenSettings.suspendTabsUntilReady === 'no' ) {
vAPI.net.unsuspend(true);
} else if ( µb.hiddenSettings.suspendTabsUntilReady === 'yes' ) {
vAPI.net.suspend(true);
}

// Maybe disable WebAssembly
if ( vAPI.canWASM && µb.hiddenSettings.disableWebAssembly !== true ) {
const wasmModuleFetcher = function(path) {
Expand Down Expand Up @@ -506,11 +506,11 @@ browser.runtime.onUpdateAvailable.addListener(details => {
}
});

µb.supportStats.launchToReadiness = `${Date.now() - vAPI.T0} ms`;
µb.supportStats.allReadyAfter = `${Date.now() - vAPI.T0} ms`;
if ( selfieIsValid ) {
µb.supportStats.launchToReadiness += ' (selfie)';
µb.supportStats.allReadyAfter += ' (selfie)';
}
ubolog(`All ready ${µb.supportStats.launchToReadiness} ms after launch`);
ubolog(`All ready ${µb.supportStats.allReadyAfter} ms after launch`);

// <<<<< end of private scope
})();
10 changes: 3 additions & 7 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ import {
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
this.hiddenSettings[key] = hs[key];
}
if ( typeof this.hiddenSettings.suspendTabsUntilReady === 'boolean' ) {
this.hiddenSettings.suspendTabsUntilReady =
this.hiddenSettings.suspendTabsUntilReady
? 'yes'
: 'unset';
}
this.fireDOMEvent('hiddenSettingsChanged');
};

Expand Down Expand Up @@ -810,7 +804,9 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
const onFilterListsReady = function(lists) {
this.availableFilterLists = lists;

vAPI.net.suspend();
if ( vAPI.net.canSuspend() ) {
vAPI.net.suspend();
}
redirectEngine.reset();
staticExtFilteringEngine.reset();
staticNetFilteringEngine.reset();
Expand Down
17 changes: 1 addition & 16 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,22 +1146,7 @@ const webRequest = {
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking', 'responseHeaders' ]
);
vAPI.net.unsuspend(true);
// Mitigation: force-reload active tabs for environments not
// supporting suspended network request listeners.
if (
vAPI.net.canSuspend() !== true ||
µb.hiddenSettings.suspendTabsUntilReady === 'no'
) {
const tabs = await vAPI.tabs.query({
active: true,
url: [ 'https://*/*', 'http://*/*' ],
windowType: 'normal',
});
for ( const tab of tabs ) {
vAPI.tabs.reload(tab.id);
}
}
vAPI.net.unsuspend({ all: true });
};
})(),

Expand Down

0 comments on commit 925c8d5

Please sign in to comment.