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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* 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";
this.EXPORTED_SYMBOLS = [];
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
"@mozilla.org/system-message-internal;1",
"nsISystemMessagesInternal");
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
"@mozilla.org/AppsService;1",
"nsIAppsService");
XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
"resource://gre/modules/SystemAppProxy.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "notificationStorage",
"@mozilla.org/notificationStorage;1",
"nsINotificationStorage");
XPCOMUtils.defineLazyGetter(this, "ppmm", function() {
return Cc["@mozilla.org/parentprocessmessagemanager;1"]
.getService(Ci.nsIMessageListenerManager);
});
function debug(str) {
//dump("=*= AlertsHelper.jsm : " + str + "\n");
}
const kNotificationIconSize = 128;
const kDesktopNotificationPerm = "desktop-notification";
const kNotificationSystemMessageName = "notification";
const kDesktopNotification = "desktop-notification";
const kDesktopNotificationShow = "desktop-notification-show";
const kDesktopNotificationClick = "desktop-notification-click";
const kDesktopNotificationClose = "desktop-notification-close";
const kTopicAlertClickCallback = "alertclickcallback";
const kTopicAlertShow = "alertshow";
const kTopicAlertFinished = "alertfinished";
const kMozChromeNotificationEvent = "mozChromeNotificationEvent";
const kMozContentNotificationEvent = "mozContentNotificationEvent";
const kMessageAlertNotificationSend = "alert-notification-send";
const kMessageAlertNotificationClose = "alert-notification-close";
const kMessages = [
kMessageAlertNotificationSend,
kMessageAlertNotificationClose
];
var AlertsHelper = {
_listeners: {},
init: function() {
Services.obs.addObserver(this, "xpcom-shutdown", false);
for (let message of kMessages) {
ppmm.addMessageListener(message, this);
}
SystemAppProxy.addEventListener(kMozContentNotificationEvent, this);
},
observe: function(aSubject, aTopic, aData) {
switch (aTopic) {
case "xpcom-shutdown":
Services.obs.removeObserver(this, "xpcom-shutdown");
for (let message of kMessages) {
ppmm.removeMessageListener(message, this);
}
SystemAppProxy.removeEventListener(kMozContentNotificationEvent, this);
break;
}
},
handleEvent: function(evt) {
let detail = evt.detail;
switch(detail.type) {
case kDesktopNotificationShow:
case kDesktopNotificationClick:
case kDesktopNotificationClose:
this.handleNotificationEvent(detail);
break;
default:
debug("FIXME: Unhandled notification event: " + detail.type);
break;
}
},
handleNotificationEvent: function(detail) {
if (!detail || !detail.id) {
return;
}
let uid = detail.id;
let listener = this._listeners[uid];
if (!listener) {
return;
}
let topic;
if (detail.type === kDesktopNotificationClick) {
topic = kTopicAlertClickCallback;
} else if (detail.type === kDesktopNotificationShow) {
topic = kTopicAlertShow;
} else {
/* kDesktopNotificationClose */
topic = kTopicAlertFinished;
}
if (listener.cookie) {
try {
listener.observer.observe(null, topic, listener.cookie);
} catch (e) { }
} else {
if (detail.type === kDesktopNotificationClose && listener.dbId) {
notificationStorage.delete(listener.manifestURL, listener.dbId);
}
}
// we"re done with this notification
if (detail.type === kDesktopNotificationClose) {
delete this._listeners[uid];
}
},
registerListener: function(alertId, cookie, alertListener) {
this._listeners[alertId] = { observer: alertListener, cookie: cookie };
},
registerAppListener: function(uid, listener) {
this._listeners[uid] = listener;
appsService.getManifestFor(listener.manifestURL).then((manifest) => {
let app = appsService.getAppByManifestURL(listener.manifestURL);
let helper = new ManifestHelper(manifest, app.origin, app.manifestURL);
let getNotificationURLFor = function(messages) {
if (!messages) {
return null;
}
for (let i = 0; i < messages.length; i++) {
let message = messages[i];
if (message === kNotificationSystemMessageName) {
return helper.fullLaunchPath();
} else if (typeof message === "object" &&
kNotificationSystemMessageName in message) {
return helper.resolveURL(message[kNotificationSystemMessageName]);
}
}
// No message found...
return null;
}
listener.target = getNotificationURLFor(manifest.messages);
// Bug 816944 - Support notification messages for entry_points.
});
},
deserializeStructuredClone: function(dataString) {
if (!dataString) {
return null;
}
let scContainer = Cc["@mozilla.org/docshell/structured-clone-container;1"].
createInstance(Ci.nsIStructuredCloneContainer);
// The maximum supported structured-clone serialization format version
// as defined in "js/public/StructuredClone.h"
let JS_STRUCTURED_CLONE_VERSION = 4;
scContainer.initFromBase64(dataString, JS_STRUCTURED_CLONE_VERSION);
let dataObj = scContainer.deserializeToVariant();
// We have to check whether dataObj contains DOM objects (supported by
// nsIStructuredCloneContainer, but not by Cu.cloneInto), e.g. ImageData.
// After the structured clone callback systems will be unified, we'll not
// have to perform this check anymore.
try {
let data = Cu.cloneInto(dataObj, {});
} catch(e) { dataObj = null; }
return dataObj;
},
showNotification: function(imageURL, title, text, textClickable, cookie,
uid, dir, lang, dataObj, manifestURL, timestamp,
behavior) {
function send(appName, appIcon) {
SystemAppProxy._sendCustomEvent(kMozChromeNotificationEvent, {
type: kDesktopNotification,
id: uid,
icon: imageURL,
title: title,
text: text,
dir: dir,
lang: lang,
appName: appName,
appIcon: appIcon,
manifestURL: manifestURL,
timestamp: timestamp,
data: dataObj,
mozbehavior: behavior
});
}
if (!manifestURL || !manifestURL.length) {
send(null, null);
return;
}
// If we have a manifest URL, get the icon and title from the manifest
// to prevent spoofing.
appsService.getManifestFor(manifestURL).then((manifest) => {
let app = appsService.getAppByManifestURL(manifestURL);
let helper = new ManifestHelper(manifest, app.origin, manifestURL);
send(helper.name, helper.iconURLForSize(kNotificationIconSize));
});
},
showAlertNotification: function(aMessage) {
let data = aMessage.data;
let currentListener = this._listeners[data.name];
if (currentListener && currentListener.observer) {
currentListener.observer.observe(null, kTopicAlertFinished, currentListener.cookie);
}
let dataObj = this.deserializeStructuredClone(data.dataStr);
this.registerListener(data.name, data.cookie, data.alertListener);
this.showNotification(data.imageURL, data.title, data.text,
data.textClickable, data.cookie, data.name, data.dir,
data.lang, dataObj, null, data.inPrivateBrowsing);
},
closeAlert: function(name) {
SystemAppProxy._sendCustomEvent(kMozChromeNotificationEvent, {
type: kDesktopNotificationClose,
id: name
});
},
receiveMessage: function(aMessage) {
if (!aMessage.target.assertAppHasPermission(kDesktopNotificationPerm)) {
Cu.reportError("Desktop-notification message " + aMessage.name +
" from a content process with no " + kDesktopNotificationPerm +
" privileges.");
return;
}
switch(aMessage.name) {
case kMessageAlertNotificationSend:
this.showAlertNotification(aMessage);
break;
case kMessageAlertNotificationClose:
this.closeAlert(aMessage.data.name);
break;
}
},
}
AlertsHelper.init();
|