2015-12-03 03:58:53 +03:00
|
|
|
"use strict";
|
|
|
|
|
2016-02-17 07:10:00 +03:00
|
|
|
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2015-08-15 02:55:09 +03:00
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
|
2015-09-15 21:19:45 +03:00
|
|
|
var {
|
2015-06-04 01:34:44 +03:00
|
|
|
EventManager,
|
|
|
|
} = ExtensionUtils;
|
|
|
|
|
2016-03-08 22:03:41 +03:00
|
|
|
// WeakMap[Extension -> Map[name -> Alarm]]
|
2015-09-15 21:19:45 +03:00
|
|
|
var alarmsMap = new WeakMap();
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2015-09-07 03:53:09 +03:00
|
|
|
// WeakMap[Extension -> Set[callback]]
|
2015-09-15 21:19:45 +03:00
|
|
|
var alarmCallbacksMap = new WeakMap();
|
2015-06-04 01:34:44 +03:00
|
|
|
|
|
|
|
// Manages an alarm created by the extension (alarms API).
|
2015-12-03 03:58:53 +03:00
|
|
|
function Alarm(extension, name, alarmInfo) {
|
2015-06-04 01:34:44 +03:00
|
|
|
this.extension = extension;
|
|
|
|
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 {
|
2015-10-16 20:24:57 +03:00
|
|
|
if (!this.delayInMinutes) {
|
|
|
|
this.delayInMinutes = this.periodInMinutes;
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
delay = this.delayInMinutes * 60 * 1000;
|
|
|
|
scheduledTime = Date.now() + delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scheduledTime = scheduledTime;
|
|
|
|
|
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
timer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
this.timer = timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Alarm.prototype = {
|
|
|
|
clear() {
|
|
|
|
this.timer.cancel();
|
2016-03-08 22:03:41 +03:00
|
|
|
alarmsMap.get(this.extension).delete(this.name);
|
2015-06-04 01:34:44 +03:00
|
|
|
this.canceled = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
if (this.canceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:01:12 +03:00
|
|
|
for (let callback of alarmCallbacksMap.get(this.extension)) {
|
|
|
|
callback(this);
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:34:44 +03:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-disable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
extensions.on("startup", (type, extension) => {
|
2016-03-08 22:03:41 +03:00
|
|
|
alarmsMap.set(extension, new Map());
|
2015-09-07 03:53:09 +03:00
|
|
|
alarmCallbacksMap.set(extension, new Set());
|
2015-06-04 01:34:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
extensions.on("shutdown", (type, extension) => {
|
2016-07-13 14:12:59 +03:00
|
|
|
if (alarmsMap.has(extension)) {
|
|
|
|
for (let alarm of alarmsMap.get(extension).values()) {
|
|
|
|
alarm.clear();
|
|
|
|
}
|
|
|
|
alarmsMap.delete(extension);
|
|
|
|
alarmCallbacksMap.delete(extension);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
|
|
|
});
|
2015-12-03 03:58:53 +03:00
|
|
|
/* eslint-enable mozilla/balanced-listeners */
|
2015-06-04 01:34:44 +03:00
|
|
|
|
2016-08-17 01:51:50 +03:00
|
|
|
extensions.registerSchemaAPI("alarms", "addon_parent", context => {
|
2016-08-15 11:04:58 +03:00
|
|
|
let {extension} = context;
|
2015-06-04 01:34:44 +03:00
|
|
|
return {
|
|
|
|
alarms: {
|
2016-03-03 03:06:10 +03:00
|
|
|
create: function(name, alarmInfo) {
|
|
|
|
name = name || "";
|
2016-03-08 22:03:41 +03:00
|
|
|
let alarms = alarmsMap.get(extension);
|
|
|
|
if (alarms.has(name)) {
|
|
|
|
alarms.get(name).clear();
|
|
|
|
}
|
2015-06-04 01:34:44 +03:00
|
|
|
let alarm = new Alarm(extension, name, alarmInfo);
|
2016-03-08 22:03:41 +03:00
|
|
|
alarms.set(alarm.name, alarm);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-03-03 03:06:10 +03:00
|
|
|
get: function(name) {
|
|
|
|
name = name || "";
|
2016-03-08 22:03:41 +03:00
|
|
|
let alarms = alarmsMap.get(extension);
|
|
|
|
if (alarms.has(name)) {
|
|
|
|
return Promise.resolve(alarms.get(name).data);
|
2016-03-03 03:06:10 +03:00
|
|
|
}
|
2016-03-08 22:03:41 +03:00
|
|
|
return Promise.resolve();
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-03-03 03:06:10 +03:00
|
|
|
getAll: function() {
|
2016-03-08 22:03:41 +03:00
|
|
|
let result = Array.from(alarmsMap.get(extension).values(), alarm => alarm.data);
|
2016-03-03 03:06:10 +03:00
|
|
|
return Promise.resolve(result);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-03-03 03:06:10 +03:00
|
|
|
clear: function(name) {
|
|
|
|
name = name || "";
|
2015-06-04 01:34:44 +03:00
|
|
|
let alarms = alarmsMap.get(extension);
|
2016-03-08 22:03:41 +03:00
|
|
|
if (alarms.has(name)) {
|
|
|
|
alarms.get(name).clear();
|
|
|
|
return Promise.resolve(true);
|
2015-06-04 01:34:44 +03:00
|
|
|
}
|
2016-03-08 22:03:41 +03:00
|
|
|
return Promise.resolve(false);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
2016-03-03 03:06:10 +03:00
|
|
|
clearAll: function() {
|
2015-06-04 01:34:44 +03:00
|
|
|
let cleared = false;
|
2016-03-08 22:03:41 +03:00
|
|
|
for (let alarm of alarmsMap.get(extension).values()) {
|
2015-06-04 01:34:44 +03:00
|
|
|
alarm.clear();
|
|
|
|
cleared = true;
|
|
|
|
}
|
2016-03-03 03:06:10 +03:00
|
|
|
return Promise.resolve(cleared);
|
2015-06-04 01:34:44 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onAlarm: new EventManager(context, "alarms.onAlarm", fire => {
|
|
|
|
let callback = alarm => {
|
|
|
|
fire(alarm.data);
|
|
|
|
};
|
|
|
|
|
2015-09-07 03:53:09 +03:00
|
|
|
alarmCallbacksMap.get(extension).add(callback);
|
2015-06-04 01:34:44 +03:00
|
|
|
return () => {
|
2015-09-07 03:53:09 +03:00
|
|
|
alarmCallbacksMap.get(extension).delete(callback);
|
2015-06-04 01:34:44 +03:00
|
|
|
};
|
|
|
|
}).api(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|