From 348f75af7fdb8ede8ea09735e854b35a5162040a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 26 Apr 2023 08:00:13 -0400 Subject: [PATCH] Reverse usage of `browser.alarms` Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/2604 Related commit: - https://github.com/gorhill/uBlock/commit/bec6cad2c029ef59de40043dcc998bb3c861cc69 --- platform/chromium/manifest.json | 1 - platform/common/vapi-background.js | 55 ------------------------------ platform/firefox/manifest.json | 1 - platform/opera/manifest.json | 1 - platform/thunderbird/manifest.json | 1 - src/js/storage.js | 4 +-- 6 files changed, 2 insertions(+), 61 deletions(-) diff --git a/platform/chromium/manifest.json b/platform/chromium/manifest.json index e99393f27..c48776edc 100644 --- a/platform/chromium/manifest.json +++ b/platform/chromium/manifest.json @@ -82,7 +82,6 @@ "open_in_tab": true }, "permissions": [ - "alarms", "contextMenus", "privacy", "storage", diff --git a/platform/common/vapi-background.js b/platform/common/vapi-background.js index 8106bbefd..4fcf40418 100644 --- a/platform/common/vapi-background.js +++ b/platform/common/vapi-background.js @@ -126,61 +126,6 @@ if ( webext.storage.local.getBytesInUse instanceof Function ) { /******************************************************************************/ /******************************************************************************/ -vAPI.alarms = { - create(callback) { - this.uniqueIdGenerator += 1; - const name = this.uniqueIdGenerator.toString(36); - const client = new this.Client(name, callback); - this.clientMap.set(name, client); - return client; - }, - Client: class { - constructor(name, callback) { - this.name = name; - this.callback = callback; - } - on(delay) { - const delayInMinutes = this.normalizeDelay(delay); - browser.alarms.get(this.name, alarm => { - if ( alarm ) { return; } - browser.alarms.create(this.name, { delayInMinutes }); - }); - } - offon(delay) { - const delayInMinutes = this.normalizeDelay(delay); - return browser.alarms.create(this.name, { delayInMinutes }); - } - off() { - return browser.alarms.clear(this.name); - } - normalizeDelay(delay) { - let delayInMinutes = 0; - if ( typeof delay === 'number' ) { - delayInMinutes = delay; - } else if ( typeof delay === 'object' ) { - if ( delay.sec !== undefined ) { - delayInMinutes = delay.sec / 60; - } else if ( delay.ms !== undefined ) { - delayInMinutes = delay.ms / 60000; - } - } - return Math.max(delayInMinutes, 1); - } - }, - onAlarm(alarm) { - const client = this.clientMap.get(alarm.name); - if ( client === undefined ) { return; } - client.callback(alarm); - }, - clientMap: new Map(), - uniqueIdGenerator: 1000000, -}; - -browser.alarms.onAlarm.addListener(alarm => vAPI.alarms.onAlarm(alarm)); - -/******************************************************************************/ -/******************************************************************************/ - // https://github.com/gorhill/uMatrix/issues/234 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network diff --git a/platform/firefox/manifest.json b/platform/firefox/manifest.json index 816df87b3..b7ea2e375 100644 --- a/platform/firefox/manifest.json +++ b/platform/firefox/manifest.json @@ -90,7 +90,6 @@ "open_in_tab": true }, "permissions": [ - "alarms", "dns", "menus", "privacy", diff --git a/platform/opera/manifest.json b/platform/opera/manifest.json index e0d437099..d5b02fb3e 100644 --- a/platform/opera/manifest.json +++ b/platform/opera/manifest.json @@ -78,7 +78,6 @@ "name": "uBlock Origin", "options_page": "dashboard.html", "permissions": [ - "alarms", "contextMenus", "privacy", "storage", diff --git a/platform/thunderbird/manifest.json b/platform/thunderbird/manifest.json index 917f99588..69950a027 100644 --- a/platform/thunderbird/manifest.json +++ b/platform/thunderbird/manifest.json @@ -66,7 +66,6 @@ "open_in_tab": true }, "permissions": [ - "alarms", "privacy", "storage", "tabs", diff --git a/src/js/storage.js b/src/js/storage.js index c267d612e..32f840a44 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1283,10 +1283,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { io.remove(/^selfie\//); µb.selfieIsInvalid = true; } - createAlarm.offon(µb.hiddenSettings.selfieAfter); + createTimer.offon({ min: µb.hiddenSettings.selfieAfter }); }; - const createAlarm = vAPI.alarms.create(create); + const createTimer = vAPI.defer.create(create); µb.selfieManager = { load, destroy }; }