summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/ext-notifications.js
blob: 1df96a2ace9a44d9a617bff33da55d81c66de0d5 (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
156
157
158
159
160
161
"use strict";

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

Cu.import("resource://gre/modules/ExtensionUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
                                  "resource://devtools/shared/event-emitter.js");

var {
  EventManager,
  ignoreEvent,
} = ExtensionUtils;

// WeakMap[Extension -> Map[id -> Notification]]
var notificationsMap = new WeakMap();

// Manages a notification popup (notifications API) created by the extension.
function Notification(extension, id, options) {
  this.extension = extension;
  this.id = id;
  this.options = options;

  let imageURL;
  if (options.iconUrl) {
    imageURL = this.extension.baseURI.resolve(options.iconUrl);
  }

  try {
    let svc = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
    svc.showAlertNotification(imageURL,
                              options.title,
                              options.message,
                              true, // textClickable
                              this.id,
                              this,
                              this.id);
  } catch (e) {
    // This will fail if alerts aren't available on the system.
  }
}

Notification.prototype = {
  clear() {
    try {
      let svc = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
      svc.closeAlert(this.id);
    } catch (e) {
      // This will fail if the OS doesn't support this function.
    }
    notificationsMap.get(this.extension).delete(this.id);
  },

  observe(subject, topic, data) {
    let notifications = notificationsMap.get(this.extension);

    let emitAndDelete = event => {
      notifications.emit(event, data);
      notifications.delete(this.id);
    };

    // Don't try to emit events if the extension has been unloaded
    if (!notifications) {
      return;
    }

    if (topic === "alertclickcallback") {
      emitAndDelete("clicked");
    }
    if (topic === "alertfinished") {
      emitAndDelete("closed");
    }
  },
};

/* eslint-disable mozilla/balanced-listeners */
extensions.on("startup", (type, extension) => {
  let map = new Map();
  EventEmitter.decorate(map);
  notificationsMap.set(extension, map);
});

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

var nextId = 0;

extensions.registerSchemaAPI("notifications", "addon_parent", context => {
  let {extension} = context;
  return {
    notifications: {
      create: function(notificationId, options) {
        if (!notificationId) {
          notificationId = String(nextId++);
        }

        let notifications = notificationsMap.get(extension);
        if (notifications.has(notificationId)) {
          notifications.get(notificationId).clear();
        }

        // FIXME: Lots of options still aren't supported, especially
        // buttons.
        let notification = new Notification(extension, notificationId, options);
        notificationsMap.get(extension).set(notificationId, notification);

        return Promise.resolve(notificationId);
      },

      clear: function(notificationId) {
        let notifications = notificationsMap.get(extension);
        if (notifications.has(notificationId)) {
          notifications.get(notificationId).clear();
          return Promise.resolve(true);
        }
        return Promise.resolve(false);
      },

      getAll: function() {
        let result = {};
        notificationsMap.get(extension).forEach((value, key) => {
          result[key] = value.options;
        });
        return Promise.resolve(result);
      },

      onClosed: new EventManager(context, "notifications.onClosed", fire => {
        let listener = (event, notificationId) => {
          // FIXME: Support the byUser argument.
          fire(notificationId, true);
        };

        notificationsMap.get(extension).on("closed", listener);
        return () => {
          notificationsMap.get(extension).off("closed", listener);
        };
      }).api(),

      onClicked: new EventManager(context, "notifications.onClicked", fire => {
        let listener = (event, notificationId) => {
          fire(notificationId, true);
        };

        notificationsMap.get(extension).on("clicked", listener);
        return () => {
          notificationsMap.get(extension).off("clicked", listener);
        };
      }).api(),

      // Intend to implement this later: https://bugzilla.mozilla.org/show_bug.cgi?id=1190681
      onButtonClicked: ignoreEvent(context, "notifications.onButtonClicked"),
    },
  };
});