Reverse usage of `browser.alarms`

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

Related commit:
- bec6cad2c0
This commit is contained in:
Raymond Hill 2023-04-26 08:00:13 -04:00
parent ef825245b9
commit 348f75af7f
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
6 changed files with 2 additions and 61 deletions

View File

@ -82,7 +82,6 @@
"open_in_tab": true "open_in_tab": true
}, },
"permissions": [ "permissions": [
"alarms",
"contextMenus", "contextMenus",
"privacy", "privacy",
"storage", "storage",

View File

@ -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://github.com/gorhill/uMatrix/issues/234
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network

View File

@ -90,7 +90,6 @@
"open_in_tab": true "open_in_tab": true
}, },
"permissions": [ "permissions": [
"alarms",
"dns", "dns",
"menus", "menus",
"privacy", "privacy",

View File

@ -78,7 +78,6 @@
"name": "uBlock Origin", "name": "uBlock Origin",
"options_page": "dashboard.html", "options_page": "dashboard.html",
"permissions": [ "permissions": [
"alarms",
"contextMenus", "contextMenus",
"privacy", "privacy",
"storage", "storage",

View File

@ -66,7 +66,6 @@
"open_in_tab": true "open_in_tab": true
}, },
"permissions": [ "permissions": [
"alarms",
"privacy", "privacy",
"storage", "storage",
"tabs", "tabs",

View File

@ -1283,10 +1283,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
io.remove(/^selfie\//); io.remove(/^selfie\//);
µb.selfieIsInvalid = true; µ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 }; µb.selfieManager = { load, destroy };
} }