summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/ext-alarms.js
blob: 2171e7dba6e9dd54324b8233b414715942b5f774 (plain)
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
"use strict";

var {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
  EventManager,
} = ExtensionUtils;

// WeakMap[Extension -> Map[name -> Alarm]]
var alarmsMap = new WeakMap();

// WeakMap[Extension -> Set[callback]]
var alarmCallbacksMap = new WeakMap();

// Manages an alarm created by the extension (alarms API).
function Alarm(extension, name, alarmInfo) {
  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 {
    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);
  timer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT);
  this.timer = timer;
}

Alarm.prototype = {
  clear() {
    this.timer.cancel();
    alarmsMap.get(this.extension).delete(this.name);
    this.canceled = true;
  },

  observe(subject, topic, data) {
    if (this.canceled) {
      return;
    }

    for (let callback of alarmCallbacksMap.get(this.extension)) {
      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,
    };
  },
};

/* eslint-disable mozilla/balanced-listeners */
extensions.on("startup", (type, extension) => {
  alarmsMap.set(extension, new Map());
  alarmCallbacksMap.set(extension, new Set());
});

extensions.on("shutdown", (type, extension) => {
  if (alarmsMap.has(extension)) {
    for (let alarm of alarmsMap.get(extension).values()) {
      alarm.clear();
    }
    alarmsMap.delete(extension);
    alarmCallbacksMap.delete(extension);
  }
});
/* eslint-enable mozilla/balanced-listeners */

extensions.registerSchemaAPI("alarms", "addon_parent", context => {
  let {extension} = context;
  return {
    alarms: {
      create: function(name, alarmInfo) {
        name = name || "";
        let alarms = alarmsMap.get(extension);
        if (alarms.has(name)) {
          alarms.get(name).clear();
        }
        let alarm = new Alarm(extension, name, alarmInfo);
        alarms.set(alarm.name, alarm);
      },

      get: function(name) {
        name = name || "";
        let alarms = alarmsMap.get(extension);
        if (alarms.has(name)) {
          return Promise.resolve(alarms.get(name).data);
        }
        return Promise.resolve();
      },

      getAll: function() {
        let result = Array.from(alarmsMap.get(extension).values(), alarm => alarm.data);
        return Promise.resolve(result);
      },

      clear: function(name) {
        name = name || "";
        let alarms = alarmsMap.get(extension);
        if (alarms.has(name)) {
          alarms.get(name).clear();
          return Promise.resolve(true);
        }
        return Promise.resolve(false);
      },

      clearAll: function() {
        let cleared = false;
        for (let alarm of alarmsMap.get(extension).values()) {
          alarm.clear();
          cleared = true;
        }
        return Promise.resolve(cleared);
      },

      onAlarm: new EventManager(context, "alarms.onAlarm", fire => {
        let callback = alarm => {
          fire(alarm.data);
        };

        alarmCallbacksMap.get(extension).add(callback);
        return () => {
          alarmCallbacksMap.get(extension).delete(callback);
        };
      }).api(),
    },
  };
});