1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
// The ext-* files are imported into the same scopes.
/* import-globals-from ext-toolkit.js */
// Manages an alarm created by the extension (alarms API).
class Alarm {
constructor(api, name, alarmInfo) {
this.api = api;
this.name = name;
this.when = alarmInfo.when;
this.delayInMinutes = alarmInfo.delayInMinutes;
this.periodInMinutes = alarmInfo.periodInMinutes;
this.canceled = false;
let delay, scheduledTime;
if (this.when) {
scheduledTime = this.when;
delay = this.when - Date.now();
} else {
if (!this.delayInMinutes) {
this.delayInMinutes = this.periodInMinutes;
}
delay = this.delayInMinutes * 60 * 1000;
scheduledTime = Date.now() + delay;
}
this.scheduledTime = scheduledTime;
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
delay = delay > 0 ? delay : 0;
timer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT);
this.timer = timer;
}
clear() {
this.timer.cancel();
this.api.alarms.delete(this.name);
this.canceled = true;
}
observe() {
if (this.canceled) {
return;
}
for (let callback of this.api.callbacks) {
callback(this);
}
if (!this.periodInMinutes) {
this.clear();
return;
}
let delay = this.periodInMinutes * 60 * 1000;
this.scheduledTime = Date.now() + delay;
this.timer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT);
}
get data() {
return {
name: this.name,
scheduledTime: this.scheduledTime,
periodInMinutes: this.periodInMinutes,
};
}
}
this.alarms = class extends ExtensionAPIPersistent {
constructor(extension) {
super(extension);
this.alarms = new Map();
this.callbacks = new Set();
}
onShutdown() {
for (let alarm of this.alarms.values()) {
alarm.clear();
}
}
PERSISTENT_EVENTS = {
onAlarm({ fire }) {
let callback = alarm => {
fire.sync(alarm.data);
};
this.callbacks.add(callback);
return {
unregister: () => {
this.callbacks.delete(callback);
},
convert(_fire) {
fire = _fire;
},
};
},
};
getAPI(context) {
const self = this;
return {
alarms: {
create: function (name, alarmInfo) {
name = name || "";
if (self.alarms.has(name)) {
self.alarms.get(name).clear();
}
let alarm = new Alarm(self, name, alarmInfo);
self.alarms.set(alarm.name, alarm);
},
get: function (name) {
name = name || "";
if (self.alarms.has(name)) {
return Promise.resolve(self.alarms.get(name).data);
}
return Promise.resolve();
},
getAll: function () {
let result = Array.from(self.alarms.values(), alarm => alarm.data);
return Promise.resolve(result);
},
clear: function (name) {
name = name || "";
if (self.alarms.has(name)) {
self.alarms.get(name).clear();
return Promise.resolve(true);
}
return Promise.resolve(false);
},
clearAll: function () {
let cleared = false;
for (let alarm of self.alarms.values()) {
alarm.clear();
cleared = true;
}
return Promise.resolve(cleared);
},
onAlarm: new EventManager({
context,
module: "alarms",
event: "onAlarm",
extensionApi: self,
}).api(),
},
};
}
};
|