From 96704f2fda68102dbebe74413f8ec06fcac7f1a2 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 1 Mar 2024 19:52:55 -0500 Subject: [PATCH] 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. --- src/js/assets.js | 2 ++ src/js/background.js | 6 +++--- src/js/start.js | 37 ++++++++++++++++++++++++++++--------- src/js/storage.js | 37 ++++++++++--------------------------- 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 0eb8ef3c4..e1bc4e616 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -1325,6 +1325,8 @@ async function diffUpdater() { terminate(); }; const worker = new Worker('js/diff-updater.js'); + }).catch(reason => { + ubolog(`Diff updater: ${reason}`); }); } diff --git a/src/js/background.js b/src/js/background.js index d5f8f33bf..b65d5d3c7 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -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', @@ -84,7 +84,7 @@ const hiddenSettingsDefault = { popupPanelHeightMode: 0, requestJournalProcessPeriod: 1000, requestStatsDisabled: false, - selfieAfter: 2, + selfieDelayInSeconds: 53, strictBlockingBypassDuration: 120, toolbarWarningTimeout: 60, trustedListPrefixes: 'ublock-', diff --git a/src/js/start.js b/src/js/start.js index a3052cc08..516ca934d 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -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(); @@ -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; diff --git a/src/js/storage.js b/src/js/storage.js index 40cd4fca5..0070afcfe 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -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); @@ -1543,7 +1543,6 @@ onBroadcast(msg => { { let next = 0; - let lastEmergencyUpdate = 0; const launchTimer = vAPI.defer.create(fetchDelay => { next = 0; @@ -1552,6 +1551,7 @@ onBroadcast(msg => { µb.scheduleAssetUpdater = async function(details = {}) { launchTimer.off(); + vAPI.alarms.clear('assetUpdater'); if ( details.now ) { next = 0; @@ -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 + }); }; }