Skip to content

Commit 96704f2

Browse files
committed
Make asset updater compatible with non-persistent background page
Related issue: uBlockOrigin/uBlock-issues#2969 Additionally, modified default timing values for asset updater and selfie creation.
1 parent 80b66c8 commit 96704f2

4 files changed

Lines changed: 43 additions & 39 deletions

File tree

src/js/assets.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,8 @@ async function diffUpdater() {
13251325
terminate();
13261326
};
13271327
const worker = new Worker('js/diff-updater.js');
1328+
}).catch(reason => {
1329+
ubolog(`Diff updater: ${reason}`);
13281330
});
13291331
}
13301332

src/js/background.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ const hiddenSettingsDefault = {
4949
allowGenericProceduralFilters: false,
5050
assetFetchTimeout: 30,
5151
autoCommentFilterTemplate: '{{date}} {{origin}}',
52-
autoUpdateAssetFetchPeriod: 15,
53-
autoUpdateDelayAfterLaunch: 105,
52+
autoUpdateAssetFetchPeriod: 5,
53+
autoUpdateDelayAfterLaunch: 37,
5454
autoUpdatePeriod: 1,
5555
benchmarkDatasetURL: 'unset',
5656
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
@@ -84,7 +84,7 @@ const hiddenSettingsDefault = {
8484
popupPanelHeightMode: 0,
8585
requestJournalProcessPeriod: 1000,
8686
requestStatsDisabled: false,
87-
selfieAfter: 2,
87+
selfieDelayInSeconds: 53,
8888
strictBlockingBypassDuration: 120,
8989
toolbarWarningTimeout: 60,
9090
trustedListPrefixes: 'ublock-',

src/js/start.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,6 @@ webRequest.start();
476476
// as possible ensure minimal memory usage baseline.
477477
lz4Codec.relinquish();
478478

479-
// https://github.com/chrisaljoudi/uBlock/issues/184
480-
// Check for updates not too far in the future.
481-
io.addObserver(µb.assetObserver.bind(µb));
482-
µb.scheduleAssetUpdater({
483-
updateDelay: µb.userSettings.autoUpdate
484-
? µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000
485-
: 0
486-
});
487-
488479
// Force an update of the context menu according to the currently
489480
// active tab.
490481
contextMenu.update();
@@ -509,11 +500,39 @@ ubolog(`All ready ${µb.supportStats.allReadyAfter} after launch`);
509500

510501
µb.isReadyResolve();
511502

503+
504+
// https://github.com/chrisaljoudi/uBlock/issues/184
505+
// Check for updates not too far in the future.
506+
io.addObserver(µb.assetObserver.bind(µb));
507+
if ( µb.userSettings.autoUpdate ) {
508+
let needEmergencyUpdate = false;
509+
const entries = await io.getUpdateAges({
510+
filters: µb.selectedFilterLists,
511+
internal: [ '*' ],
512+
});
513+
for ( const entry of entries ) {
514+
if ( entry.ageNormalized < 2 ) { continue; }
515+
needEmergencyUpdate = true;
516+
break;
517+
}
518+
const updateDelay = needEmergencyUpdate
519+
? 2000
520+
: µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000;
521+
µb.scheduleAssetUpdater({
522+
auto: true,
523+
updateDelay,
524+
fetchDelay: needEmergencyUpdate ? 1000 : undefined
525+
});
526+
}
527+
512528
// Process alarm queue
513529
while ( µb.alarmQueue.length !== 0 ) {
514530
const what = µb.alarmQueue.shift();
515531
ubolog(`Processing alarm event from suspended state: '${what}'`);
516532
switch ( what ) {
533+
case 'assetUpdater':
534+
µb.scheduleAssetUpdater({ auto: true, updateDelay: 2000, fetchDelay : 1000 });
535+
break;
517536
case 'createSelfie':
518537
µb.selfieManager.create();
519538
break;

src/js/storage.js

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,9 +1376,9 @@ onBroadcast(msg => {
13761376
ubolog('Filtering engine selfie marked for invalidation');
13771377
}
13781378
vAPI.alarms.create('createSelfie', {
1379-
delayInMinutes: µb.hiddenSettings.selfieAfter + 0.5
1379+
delayInMinutes: (µb.hiddenSettings.selfieDelayInSeconds + 17) / 60,
13801380
});
1381-
createTimer.offon({ min: µb.hiddenSettings.selfieAfter });
1381+
createTimer.offon({ sec: µb.hiddenSettings.selfieDelayInSeconds });
13821382
};
13831383

13841384
const createTimer = vAPI.defer.create(create);
@@ -1543,7 +1543,6 @@ onBroadcast(msg => {
15431543

15441544
{
15451545
let next = 0;
1546-
let lastEmergencyUpdate = 0;
15471546

15481547
const launchTimer = vAPI.defer.create(fetchDelay => {
15491548
next = 0;
@@ -1552,6 +1551,7 @@ onBroadcast(msg => {
15521551

15531552
µb.scheduleAssetUpdater = async function(details = {}) {
15541553
launchTimer.off();
1554+
vAPI.alarms.clear('assetUpdater');
15551555

15561556
if ( details.now ) {
15571557
next = 0;
@@ -1570,40 +1570,23 @@ onBroadcast(msg => {
15701570
this.hiddenSettings.autoUpdatePeriod * 3600000;
15711571

15721572
const now = Date.now();
1573-
let needEmergencyUpdate = false;
1574-
1575-
// Respect cooldown period before launching an emergency update.
1576-
const timeSinceLastEmergencyUpdate = (now - lastEmergencyUpdate) / 3600000;
1577-
if ( timeSinceLastEmergencyUpdate > 1 ) {
1578-
const entries = await io.getUpdateAges({
1579-
filters: µb.selectedFilterLists,
1580-
internal: [ '*' ],
1581-
});
1582-
for ( const entry of entries ) {
1583-
if ( entry.ageNormalized < 2 ) { continue; }
1584-
needEmergencyUpdate = true;
1585-
lastEmergencyUpdate = now;
1586-
break;
1587-
}
1588-
}
15891573

15901574
// Use the new schedule if and only if it is earlier than the previous
15911575
// one.
15921576
if ( next !== 0 ) {
1593-
updateDelay = Math.min(updateDelay, Math.max(next - now, 0));
1594-
}
1595-
1596-
if ( needEmergencyUpdate ) {
1597-
updateDelay = Math.min(updateDelay, 15000);
1577+
updateDelay = Math.min(updateDelay, Math.max(next - now, 1));
15981578
}
15991579

16001580
next = now + updateDelay;
16011581

1602-
const fetchDelay = needEmergencyUpdate
1603-
? 2000
1604-
: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 || 60000;
1582+
const fetchDelay = details.fetchDelay ||
1583+
this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 ||
1584+
60000;
16051585

16061586
launchTimer.on(updateDelay, fetchDelay);
1587+
vAPI.alarms.create('assetUpdater', {
1588+
delayInMinutes: Math.ceil(updateDelay / 60000) + 0.25
1589+
});
16071590
};
16081591
}
16091592

0 commit comments

Comments
 (0)