Skip to content

Commit 925c8d5

Browse files
committed
Add setting to control suspension on network activity at launch
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.
1 parent 0c03244 commit 925c8d5

10 files changed

+52
-54
lines changed

platform/chromium/vapi-background-ext.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ vAPI.Tabs = class extends vAPI.Tabs {
186186
return { cancel: true };
187187
}
188188

189-
unsuspendAllRequests() {
190-
for ( const tabId of this.suspendedTabIds ) {
191-
vAPI.tabs.reload(tabId);
189+
unsuspendAllRequests(discard = false) {
190+
if ( discard !== true ) {
191+
for ( const tabId of this.suspendedTabIds ) {
192+
vAPI.tabs.reload(tabId);
193+
}
192194
}
193195
this.suspendedTabIds.clear();
194196
}

platform/common/vapi-background.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1220,20 +1220,18 @@ vAPI.Net = class {
12201220
}
12211221
unsuspendAllRequests() {
12221222
}
1223-
suspend(force = false) {
1224-
if ( this.canSuspend() || force ) {
1225-
this.suspendDepth += 1;
1226-
}
1223+
suspend() {
1224+
this.suspendDepth += 1;
12271225
}
1228-
unsuspend(all = false) {
1226+
unsuspend({ all = false, discard = false } = {}) {
12291227
if ( this.suspendDepth === 0 ) { return; }
12301228
if ( all ) {
12311229
this.suspendDepth = 0;
12321230
} else {
12331231
this.suspendDepth -= 1;
12341232
}
12351233
if ( this.suspendDepth !== 0 ) { return; }
1236-
this.unsuspendAllRequests();
1234+
this.unsuspendAllRequests(discard);
12371235
}
12381236
canSuspend() {
12391237
return false;

platform/firefox/vapi-background-ext.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,15 @@ import {
292292
this.pendingRequests.push(pending);
293293
return pending.promise;
294294
}
295-
unsuspendAllRequests() {
295+
unsuspendAllRequests(discard = false) {
296296
const pendingRequests = this.pendingRequests;
297297
this.pendingRequests = [];
298298
for ( const entry of pendingRequests ) {
299-
entry.resolve(this.onBeforeSuspendableRequest(entry.details));
299+
entry.resolve(
300+
discard !== true
301+
? this.onBeforeSuspendableRequest(entry.details)
302+
: undefined
303+
);
300304
}
301305
}
302306
canSuspend() {

src/3p-filters.html

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
</div>
2525

2626
<div>
27-
<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>
28-
<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>
27+
<div class="li">
28+
<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>
2929
</div>
30-
<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>
30+
<div class="li">
31+
<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>
32+
</div>
33+
<div class="li">
34+
<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>
35+
</div>
36+
<div class="li">
37+
<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>
3138
</div>
3239
</div>
3340

src/js/3p-filters.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ const renderFilterLists = function(soft) {
286286
// Re-insert import widget.
287287
uDom('[data-groupkey="custom"] .listEntries').append(importWidget);
288288

289-
uDom.nodeFromId('autoUpdate').checked = listDetails.autoUpdate === true;
289+
uDom.nodeFromId('autoUpdate').checked =
290+
listDetails.autoUpdate === true;
290291
uDom.nodeFromId('listsOfBlockedHostsPrompt').textContent =
291292
vAPI.i18n('3pListsOfBlockedHostsPrompt')
292293
.replace(
@@ -301,6 +302,8 @@ const renderFilterLists = function(soft) {
301302
listDetails.parseCosmeticFilters === true;
302303
uDom.nodeFromId('ignoreGenericCosmeticFilters').checked =
303304
listDetails.ignoreGenericCosmeticFilters === true;
305+
uDom.nodeFromId('suspendUntilListsAreLoaded').checked =
306+
listDetails.suspendUntilListsAreLoaded === true;
304307

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

530533
/******************************************************************************/
531534

532-
const autoUpdateCheckboxChanged = function() {
535+
const userSettingCheckboxChanged = function() {
536+
const target = event.target;
533537
messaging.send('dashboard', {
534538
what: 'userSettings',
535-
name: 'autoUpdate',
536-
value: this.checked,
539+
name: target.id,
540+
value: target.checked,
537541
});
538542
};
539543

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

684688
/******************************************************************************/
685689

686-
uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
690+
uDom('#autoUpdate').on('change', userSettingCheckboxChanged);
687691
uDom('#parseCosmeticFilters').on('change', onFilteringSettingsChanged);
688692
uDom('#ignoreGenericCosmeticFilters').on('change', onFilteringSettingsChanged);
693+
uDom('#suspendUntilListsAreLoaded').on('change', userSettingCheckboxChanged);
689694
uDom('#buttonApply').on('click', ( ) => { buttonApplyHandler(); });
690695
uDom('#buttonUpdate').on('click', ( ) => { buttonUpdateHandler(); });
691696
uDom('#buttonPurgeAll').on('click', ev => {

src/js/background.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ const hiddenSettingsDefault = {
8080
requestJournalProcessPeriod: 1000,
8181
selfieAfter: 2,
8282
strictBlockingBypassDuration: 120,
83-
suspendTabsUntilReady: 'unset',
8483
uiPopupConfig: 'unset',
8584
uiFlavor: 'unset',
8685
uiStyles: 'unset',
@@ -109,6 +108,7 @@ const userSettingsDefault = {
109108
prefetchingDisabled: true,
110109
requestLogMaxEntries: 1000,
111110
showIconBadge: true,
111+
suspendUntilListsAreLoaded: true,
112112
tooltipsDisabled: false,
113113
webrtcIPAddressHidden: false,
114114
};
@@ -214,7 +214,7 @@ const µBlock = { // jshint ignore:line
214214
readyToFilter: false,
215215

216216
supportStats: {
217-
launchToReadiness: '',
217+
allReadyAfter: '',
218218
},
219219

220220
pageStores: new Map(),

src/js/messaging.js

+1
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ const getLists = async function(callback) {
10911091
isUpdating: io.isUpdating(),
10921092
netFilterCount: staticNetFilteringEngine.getFilterCount(),
10931093
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
1094+
suspendUntilListsAreLoaded: µb.userSettings.suspendUntilListsAreLoaded,
10941095
userFiltersPath: µb.userFiltersPath
10951096
};
10961097
const [ lists, metadata ] = await Promise.all([

src/js/start.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ const onNetWhitelistReady = function(netWhitelistRaw, adminExtra) {
192192
// User settings are in memory
193193

194194
const onUserSettingsReady = function(fetched) {
195+
// Terminate suspended state?
196+
if ( fetched.suspendUntilListsAreLoaded === false ) {
197+
vAPI.net.unsuspend({ all: true, discard: true });
198+
ubolog(`Unsuspend network activity listener`);
199+
µb.supportStats.unsuspendAfter = `${Date.now() - vAPI.T0} ms`;
200+
}
201+
195202
// `externalLists` will be deprecated in some future, it is kept around
196203
// for forward compatibility purpose, and should reflect the content of
197204
// `importedLists`.
@@ -282,13 +289,6 @@ const onHiddenSettingsReady = async function() {
282289
ubolog(`Override default webext flavor with ${tokens}`);
283290
}
284291

285-
// Maybe override current network listener suspend state
286-
if ( µb.hiddenSettings.suspendTabsUntilReady === 'no' ) {
287-
vAPI.net.unsuspend(true);
288-
} else if ( µb.hiddenSettings.suspendTabsUntilReady === 'yes' ) {
289-
vAPI.net.suspend(true);
290-
}
291-
292292
// Maybe disable WebAssembly
293293
if ( vAPI.canWASM && µb.hiddenSettings.disableWebAssembly !== true ) {
294294
const wasmModuleFetcher = function(path) {
@@ -506,11 +506,11 @@ browser.runtime.onUpdateAvailable.addListener(details => {
506506
}
507507
});
508508

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

515515
// <<<<< end of private scope
516516
})();

src/js/storage.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,6 @@ import {
239239
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
240240
this.hiddenSettings[key] = hs[key];
241241
}
242-
if ( typeof this.hiddenSettings.suspendTabsUntilReady === 'boolean' ) {
243-
this.hiddenSettings.suspendTabsUntilReady =
244-
this.hiddenSettings.suspendTabsUntilReady
245-
? 'yes'
246-
: 'unset';
247-
}
248242
this.fireDOMEvent('hiddenSettingsChanged');
249243
};
250244

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

813-
vAPI.net.suspend();
807+
if ( vAPI.net.canSuspend() ) {
808+
vAPI.net.suspend();
809+
}
814810
redirectEngine.reset();
815811
staticExtFilteringEngine.reset();
816812
staticNetFilteringEngine.reset();

src/js/traffic.js

+1-16
Original file line numberDiff line numberDiff line change
@@ -1146,22 +1146,7 @@ const webRequest = {
11461146
{ urls: [ 'http://*/*', 'https://*/*' ] },
11471147
[ 'blocking', 'responseHeaders' ]
11481148
);
1149-
vAPI.net.unsuspend(true);
1150-
// Mitigation: force-reload active tabs for environments not
1151-
// supporting suspended network request listeners.
1152-
if (
1153-
vAPI.net.canSuspend() !== true ||
1154-
µb.hiddenSettings.suspendTabsUntilReady === 'no'
1155-
) {
1156-
const tabs = await vAPI.tabs.query({
1157-
active: true,
1158-
url: [ 'https://*/*', 'http://*/*' ],
1159-
windowType: 'normal',
1160-
});
1161-
for ( const tab of tabs ) {
1162-
vAPI.tabs.reload(tab.id);
1163-
}
1164-
}
1149+
vAPI.net.unsuspend({ all: true });
11651150
};
11661151
})(),
11671152

0 commit comments

Comments
 (0)