1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Make asset updater compatible with non-persistent background page

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/2969

Additionally, modified default timing values for asset updater and
selfie creation.
This commit is contained in:
Raymond Hill 2024-03-01 19:52:55 -05:00
parent 80b66c849a
commit 96704f2fda
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 43 additions and 39 deletions

View File

@ -1325,6 +1325,8 @@ async function diffUpdater() {
terminate(); terminate();
}; };
const worker = new Worker('js/diff-updater.js'); const worker = new Worker('js/diff-updater.js');
}).catch(reason => {
ubolog(`Diff updater: ${reason}`);
}); });
} }

View File

@ -49,8 +49,8 @@ const hiddenSettingsDefault = {
allowGenericProceduralFilters: false, allowGenericProceduralFilters: false,
assetFetchTimeout: 30, assetFetchTimeout: 30,
autoCommentFilterTemplate: '{{date}} {{origin}}', autoCommentFilterTemplate: '{{date}} {{origin}}',
autoUpdateAssetFetchPeriod: 15, autoUpdateAssetFetchPeriod: 5,
autoUpdateDelayAfterLaunch: 105, autoUpdateDelayAfterLaunch: 37,
autoUpdatePeriod: 1, autoUpdatePeriod: 1,
benchmarkDatasetURL: 'unset', benchmarkDatasetURL: 'unset',
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001', blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
@ -84,7 +84,7 @@ const hiddenSettingsDefault = {
popupPanelHeightMode: 0, popupPanelHeightMode: 0,
requestJournalProcessPeriod: 1000, requestJournalProcessPeriod: 1000,
requestStatsDisabled: false, requestStatsDisabled: false,
selfieAfter: 2, selfieDelayInSeconds: 53,
strictBlockingBypassDuration: 120, strictBlockingBypassDuration: 120,
toolbarWarningTimeout: 60, toolbarWarningTimeout: 60,
trustedListPrefixes: 'ublock-', trustedListPrefixes: 'ublock-',

View File

@ -476,15 +476,6 @@ webRequest.start();
// as possible ensure minimal memory usage baseline. // as possible ensure minimal memory usage baseline.
lz4Codec.relinquish(); 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 // Force an update of the context menu according to the currently
// active tab. // active tab.
contextMenu.update(); contextMenu.update();
@ -509,11 +500,39 @@ ubolog(`All ready ${µb.supportStats.allReadyAfter} after launch`);
µb.isReadyResolve(); µ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 // Process alarm queue
while ( µb.alarmQueue.length !== 0 ) { while ( µb.alarmQueue.length !== 0 ) {
const what = µb.alarmQueue.shift(); const what = µb.alarmQueue.shift();
ubolog(`Processing alarm event from suspended state: '${what}'`); ubolog(`Processing alarm event from suspended state: '${what}'`);
switch ( what ) { switch ( what ) {
case 'assetUpdater':
µb.scheduleAssetUpdater({ auto: true, updateDelay: 2000, fetchDelay : 1000 });
break;
case 'createSelfie': case 'createSelfie':
µb.selfieManager.create(); µb.selfieManager.create();
break; break;

View File

@ -1376,9 +1376,9 @@ onBroadcast(msg => {
ubolog('Filtering engine selfie marked for invalidation'); ubolog('Filtering engine selfie marked for invalidation');
} }
vAPI.alarms.create('createSelfie', { 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); const createTimer = vAPI.defer.create(create);
@ -1543,7 +1543,6 @@ onBroadcast(msg => {
{ {
let next = 0; let next = 0;
let lastEmergencyUpdate = 0;
const launchTimer = vAPI.defer.create(fetchDelay => { const launchTimer = vAPI.defer.create(fetchDelay => {
next = 0; next = 0;
@ -1552,6 +1551,7 @@ onBroadcast(msg => {
µb.scheduleAssetUpdater = async function(details = {}) { µb.scheduleAssetUpdater = async function(details = {}) {
launchTimer.off(); launchTimer.off();
vAPI.alarms.clear('assetUpdater');
if ( details.now ) { if ( details.now ) {
next = 0; next = 0;
@ -1570,40 +1570,23 @@ onBroadcast(msg => {
this.hiddenSettings.autoUpdatePeriod * 3600000; this.hiddenSettings.autoUpdatePeriod * 3600000;
const now = Date.now(); 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 // Use the new schedule if and only if it is earlier than the previous
// one. // one.
if ( next !== 0 ) { if ( next !== 0 ) {
updateDelay = Math.min(updateDelay, Math.max(next - now, 0)); updateDelay = Math.min(updateDelay, Math.max(next - now, 1));
}
if ( needEmergencyUpdate ) {
updateDelay = Math.min(updateDelay, 15000);
} }
next = now + updateDelay; next = now + updateDelay;
const fetchDelay = needEmergencyUpdate const fetchDelay = details.fetchDelay ||
? 2000 this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 ||
: this.hiddenSettings.autoUpdateAssetFetchPeriod * 1000 || 60000; 60000;
launchTimer.on(updateDelay, fetchDelay); launchTimer.on(updateDelay, fetchDelay);
vAPI.alarms.create('assetUpdater', {
delayInMinutes: Math.ceil(updateDelay / 60000) + 0.25
});
}; };
} }