Skip to content

Commit

Permalink
Make asset updater compatible with non-persistent background page
Browse files Browse the repository at this point in the history
Related issue:
uBlockOrigin/uBlock-issues#2969

Additionally, modified default timing values for asset updater and
selfie creation.
  • Loading branch information
gorhill committed Mar 2, 2024
1 parent 80b66c8 commit 96704f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,8 @@ async function diffUpdater() {
terminate();
};
const worker = new Worker('js/diff-updater.js');
}).catch(reason => {
ubolog(`Diff updater: ${reason}`);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const hiddenSettingsDefault = {
allowGenericProceduralFilters: false,
assetFetchTimeout: 30,
autoCommentFilterTemplate: '{{date}} {{origin}}',
autoUpdateAssetFetchPeriod: 15,
autoUpdateDelayAfterLaunch: 105,
autoUpdateAssetFetchPeriod: 5,
autoUpdateDelayAfterLaunch: 37,
autoUpdatePeriod: 1,
benchmarkDatasetURL: 'unset',
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
Expand Down Expand Up @@ -84,7 +84,7 @@ const hiddenSettingsDefault = {
popupPanelHeightMode: 0,
requestJournalProcessPeriod: 1000,
requestStatsDisabled: false,
selfieAfter: 2,
selfieDelayInSeconds: 53,
strictBlockingBypassDuration: 120,
toolbarWarningTimeout: 60,
trustedListPrefixes: 'ublock-',
Expand Down
37 changes: 28 additions & 9 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,6 @@ webRequest.start();
// as possible ensure minimal memory usage baseline.
lz4Codec.relinquish();

// https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future.
io.addObserver(µb.assetObserver.bind(µb));
µb.scheduleAssetUpdater({
updateDelay: µb.userSettings.autoUpdate
? µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000
: 0
});

// Force an update of the context menu according to the currently
// active tab.
contextMenu.update();
Expand All @@ -509,11 +500,39 @@ ubolog(`All ready ${µb.supportStats.allReadyAfter} after launch`);

µb.isReadyResolve();


// https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future.
io.addObserver(µb.assetObserver.bind(µb));
if ( µb.userSettings.autoUpdate ) {
let needEmergencyUpdate = false;
const entries = await io.getUpdateAges({
filters: µb.selectedFilterLists,
internal: [ '*' ],
});
for ( const entry of entries ) {
if ( entry.ageNormalized < 2 ) { continue; }
needEmergencyUpdate = true;
break;
}
const updateDelay = needEmergencyUpdate
? 2000
: µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000;
µb.scheduleAssetUpdater({
auto: true,
updateDelay,
fetchDelay: needEmergencyUpdate ? 1000 : undefined
});
}

// Process alarm queue
while ( µb.alarmQueue.length !== 0 ) {
const what = µb.alarmQueue.shift();
ubolog(`Processing alarm event from suspended state: '${what}'`);
switch ( what ) {
case 'assetUpdater':
µb.scheduleAssetUpdater({ auto: true, updateDelay: 2000, fetchDelay : 1000 });
break;
case 'createSelfie':
µb.selfieManager.create();
break;
Expand Down
37 changes: 10 additions & 27 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,9 +1376,9 @@ onBroadcast(msg => {
ubolog('Filtering engine selfie marked for invalidation');
}
vAPI.alarms.create('createSelfie', {
delayInMinutes: µb.hiddenSettings.selfieAfter + 0.5
delayInMinutes: (µb.hiddenSettings.selfieDelayInSeconds + 17) / 60,
});
createTimer.offon({ min: µb.hiddenSettings.selfieAfter });
createTimer.offon({ sec: µb.hiddenSettings.selfieDelayInSeconds });
};

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

{
let next = 0;
let lastEmergencyUpdate = 0;

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

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

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

const now = Date.now();
let needEmergencyUpdate = false;

// Respect cooldown period before launching an emergency update.
const timeSinceLastEmergencyUpdate = (now - lastEmergencyUpdate) / 3600000;
if ( timeSinceLastEmergencyUpdate > 1 ) {
const entries = await io.getUpdateAges({
filters: µb.selectedFilterLists,
internal: [ '*' ],
});
for ( const entry of entries ) {
if ( entry.ageNormalized < 2 ) { continue; }
needEmergencyUpdate = true;
lastEmergencyUpdate = now;
break;
}
}

// Use the new schedule if and only if it is earlier than the previous
// one.
if ( next !== 0 ) {
updateDelay = Math.min(updateDelay, Math.max(next - now, 0));
}

if ( needEmergencyUpdate ) {
updateDelay = Math.min(updateDelay, 15000);
updateDelay = Math.min(updateDelay, Math.max(next - now, 1));
}

next = now + updateDelay;

const fetchDelay = needEmergencyUpdate
? 2000
: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 || 60000;
const fetchDelay = details.fetchDelay ||
this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 ||
60000;

launchTimer.on(updateDelay, fetchDelay);
vAPI.alarms.create('assetUpdater', {
delayInMinutes: Math.ceil(updateDelay / 60000) + 0.25
});
};
}

Expand Down

0 comments on commit 96704f2

Please sign in to comment.