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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
/* 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 file exports XPCOM components for C++ and chrome JavaScript callers to
* interact with the Push service.
*/
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var isParent =
Services.appinfo.processType === Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
// The default Push service implementation.
XPCOMUtils.defineLazyGetter(this, "PushService", function() {
if (Services.prefs.getBoolPref("dom.push.enabled")) {
const { PushService } = ChromeUtils.import(
"resource://gre/modules/PushService.jsm"
);
PushService.init();
return PushService;
}
throw Components.Exception("", Cr.NS_ERROR_NOT_AVAILABLE);
});
// Observer notification topics for push messages and subscription status
// changes. These are duplicated and used in `nsIPushNotifier`. They're exposed
// on `nsIPushService` so that JS callers only need to import this service.
const OBSERVER_TOPIC_PUSH = "push-message";
const OBSERVER_TOPIC_SUBSCRIPTION_CHANGE = "push-subscription-change";
const OBSERVER_TOPIC_SUBSCRIPTION_MODIFIED = "push-subscription-modified";
/**
* `PushServiceBase`, `PushServiceParent`, and `PushServiceContent` collectively
* implement the `nsIPushService` interface. This interface provides calls
* similar to the Push DOM API, but does not require service workers.
*
* Push service methods may be called from the parent or content process. The
* parent process implementation loads `PushService.jsm` at app startup, and
* calls its methods directly. The content implementation forwards calls to
* the parent Push service via IPC.
*
* The implementations share a class and contract ID.
*/
function PushServiceBase() {
this.wrappedJSObject = this;
this._addListeners();
}
PushServiceBase.prototype = {
classID: Components.ID("{daaa8d73-677e-4233-8acd-2c404bd01658}"),
contractID: "@mozilla.org/push/Service;1",
QueryInterface: ChromeUtils.generateQI([
"nsIObserver",
"nsISupportsWeakReference",
"nsIPushService",
"nsIPushQuotaManager",
"nsIPushErrorReporter",
]),
pushTopic: OBSERVER_TOPIC_PUSH,
subscriptionChangeTopic: OBSERVER_TOPIC_SUBSCRIPTION_CHANGE,
subscriptionModifiedTopic: OBSERVER_TOPIC_SUBSCRIPTION_MODIFIED,
ensureReady() {},
_addListeners() {
for (let message of this._messages) {
this._mm.addMessageListener(message, this);
}
},
_isValidMessage(message) {
return this._messages.includes(message.name);
},
observe(subject, topic, data) {
if (topic === "android-push-service") {
// Load PushService immediately.
this.ensureReady();
}
},
_deliverSubscription(request, props) {
if (!props) {
request.onPushSubscription(Cr.NS_OK, null);
return;
}
request.onPushSubscription(Cr.NS_OK, new PushSubscription(props));
},
_deliverSubscriptionError(request, error) {
let result =
typeof error.result == "number" ? error.result : Cr.NS_ERROR_FAILURE;
request.onPushSubscription(result, null);
},
};
/**
* The parent process implementation of `nsIPushService`. This version loads
* `PushService.jsm` at startup and calls its methods directly. It also
* receives and responds to requests from the content process.
*/
let parentInstance;
function PushServiceParent() {
if (parentInstance) {
return parentInstance;
}
parentInstance = this;
PushServiceBase.call(this);
}
PushServiceParent.prototype = Object.create(PushServiceBase.prototype);
XPCOMUtils.defineLazyServiceGetter(
PushServiceParent.prototype,
"_mm",
"@mozilla.org/parentprocessmessagemanager;1",
"nsISupports"
);
Object.assign(PushServiceParent.prototype, {
_messages: [
"Push:Register",
"Push:Registration",
"Push:Unregister",
"Push:Clear",
"Push:NotificationForOriginShown",
"Push:NotificationForOriginClosed",
"Push:ReportError",
],
// nsIPushService methods
subscribe(scope, principal, callback) {
this.subscribeWithKey(scope, principal, [], callback);
},
subscribeWithKey(scope, principal, key, callback) {
this._handleRequest("Push:Register", principal, {
scope,
appServerKey: key,
})
.then(
result => {
this._deliverSubscription(callback, result);
},
error => {
this._deliverSubscriptionError(callback, error);
}
)
.catch(Cu.reportError);
},
unsubscribe(scope, principal, callback) {
this._handleRequest("Push:Unregister", principal, {
scope,
})
.then(
result => {
callback.onUnsubscribe(Cr.NS_OK, result);
},
error => {
callback.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
}
)
.catch(Cu.reportError);
},
getSubscription(scope, principal, callback) {
return this._handleRequest("Push:Registration", principal, {
scope,
})
.then(
result => {
this._deliverSubscription(callback, result);
},
error => {
this._deliverSubscriptionError(callback, error);
}
)
.catch(Cu.reportError);
},
clearForDomain(domain, callback) {
return this._handleRequest("Push:Clear", null, {
domain,
})
.then(
result => {
callback.onClear(Cr.NS_OK);
},
error => {
callback.onClear(Cr.NS_ERROR_FAILURE);
}
)
.catch(Cu.reportError);
},
// nsIPushQuotaManager methods
notificationForOriginShown(origin) {
this.service.notificationForOriginShown(origin);
},
notificationForOriginClosed(origin) {
this.service.notificationForOriginClosed(origin);
},
// nsIPushErrorReporter methods
reportDeliveryError(messageId, reason) {
this.service.reportDeliveryError(messageId, reason);
},
receiveMessage(message) {
if (!this._isValidMessage(message)) {
return;
}
let { name, target, data } = message;
if (name === "Push:NotificationForOriginShown") {
this.notificationForOriginShown(data);
return;
}
if (name === "Push:NotificationForOriginClosed") {
this.notificationForOriginClosed(data);
return;
}
if (name === "Push:ReportError") {
this.reportDeliveryError(data.messageId, data.reason);
return;
}
this._handleRequest(name, data.principal, data)
.then(
result => {
target.sendAsyncMessage(this._getResponseName(name, "OK"), {
requestID: data.requestID,
result,
});
},
error => {
target.sendAsyncMessage(this._getResponseName(name, "KO"), {
requestID: data.requestID,
result: error.result,
});
}
)
.catch(Cu.reportError);
},
ensureReady() {
this.service.init();
},
_toPageRecord(principal, data) {
if (!data.scope) {
throw new Error("Invalid page record: missing scope");
}
if (!principal) {
throw new Error("Invalid page record: missing principal");
}
if (principal.isNullPrincipal || principal.isExpandedPrincipal) {
throw new Error("Invalid page record: unsupported principal");
}
// System subscriptions can only be created by chrome callers, and are
// exempt from the background message quota and permission checks. They
// also do not fire service worker events.
data.systemRecord = principal.isSystemPrincipal;
data.originAttributes = ChromeUtils.originAttributesToSuffix(
principal.originAttributes
);
return data;
},
async _handleRequest(name, principal, data) {
if (name == "Push:Clear") {
return this.service.clear(data);
}
let pageRecord;
try {
pageRecord = this._toPageRecord(principal, data);
} catch (e) {
return Promise.reject(e);
}
if (name === "Push:Register") {
return this.service.register(pageRecord);
}
if (name === "Push:Registration") {
return this.service.registration(pageRecord);
}
if (name === "Push:Unregister") {
return this.service.unregister(pageRecord);
}
return Promise.reject(new Error("Invalid request: unknown name"));
},
_getResponseName(requestName, suffix) {
let name = requestName.slice("Push:".length);
return "PushService:" + name + ":" + suffix;
},
// Methods used for mocking in tests.
replaceServiceBackend(options) {
return this.service.changeTestServer(options.serverURI, options);
},
restoreServiceBackend() {
var defaultServerURL = Services.prefs.getCharPref("dom.push.serverURL");
return this.service.changeTestServer(defaultServerURL);
},
});
// Used to replace the implementation with a mock.
Object.defineProperty(PushServiceParent.prototype, "service", {
get() {
return this._service || PushService;
},
set(impl) {
this._service = impl;
},
});
let contentInstance;
/**
* The content process implementation of `nsIPushService`. This version
* uses the child message manager to forward calls to the parent process.
* The parent Push service instance handles the request, and responds with a
* message containing the result.
*/
function PushServiceContent() {
if (contentInstance) {
return contentInstance;
}
contentInstance = this;
PushServiceBase.apply(this, arguments);
this._requests = new Map();
this._requestId = 0;
}
PushServiceContent.prototype = Object.create(PushServiceBase.prototype);
XPCOMUtils.defineLazyServiceGetter(
PushServiceContent.prototype,
"_mm",
"@mozilla.org/childprocessmessagemanager;1",
"nsISupports"
);
Object.assign(PushServiceContent.prototype, {
_messages: [
"PushService:Register:OK",
"PushService:Register:KO",
"PushService:Registration:OK",
"PushService:Registration:KO",
"PushService:Unregister:OK",
"PushService:Unregister:KO",
"PushService:Clear:OK",
"PushService:Clear:KO",
],
// nsIPushService methods
subscribe(scope, principal, callback) {
this.subscribeWithKey(scope, principal, [], callback);
},
subscribeWithKey(scope, principal, key, callback) {
let requestID = this._addRequest(callback);
this._mm.sendAsyncMessage("Push:Register", {
scope,
appServerKey: key,
requestID,
principal,
});
},
unsubscribe(scope, principal, callback) {
let requestID = this._addRequest(callback);
this._mm.sendAsyncMessage("Push:Unregister", {
scope,
requestID,
principal,
});
},
getSubscription(scope, principal, callback) {
let requestID = this._addRequest(callback);
this._mm.sendAsyncMessage("Push:Registration", {
scope,
requestID,
principal,
});
},
clearForDomain(domain, callback) {
let requestID = this._addRequest(callback);
this._mm.sendAsyncMessage("Push:Clear", {
domain,
requestID,
});
},
// nsIPushQuotaManager methods
notificationForOriginShown(origin) {
this._mm.sendAsyncMessage("Push:NotificationForOriginShown", origin);
},
notificationForOriginClosed(origin) {
this._mm.sendAsyncMessage("Push:NotificationForOriginClosed", origin);
},
// nsIPushErrorReporter methods
reportDeliveryError(messageId, reason) {
this._mm.sendAsyncMessage("Push:ReportError", {
messageId,
reason,
});
},
_addRequest(data) {
let id = ++this._requestId;
this._requests.set(id, data);
return id;
},
_takeRequest(requestId) {
let d = this._requests.get(requestId);
this._requests.delete(requestId);
return d;
},
receiveMessage(message) {
if (!this._isValidMessage(message)) {
return;
}
let { name, data } = message;
let request = this._takeRequest(data.requestID);
if (!request) {
return;
}
switch (name) {
case "PushService:Register:OK":
case "PushService:Registration:OK":
this._deliverSubscription(request, data.result);
break;
case "PushService:Register:KO":
case "PushService:Registration:KO":
this._deliverSubscriptionError(request, data);
break;
case "PushService:Unregister:OK":
if (typeof data.result === "boolean") {
request.onUnsubscribe(Cr.NS_OK, data.result);
} else {
request.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
}
break;
case "PushService:Unregister:KO":
request.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
break;
case "PushService:Clear:OK":
request.onClear(Cr.NS_OK);
break;
case "PushService:Clear:KO":
request.onClear(Cr.NS_ERROR_FAILURE);
break;
default:
break;
}
},
});
/** `PushSubscription` instances are passed to all subscription callbacks. */
function PushSubscription(props) {
this._props = props;
}
PushSubscription.prototype = {
QueryInterface: ChromeUtils.generateQI(["nsIPushSubscription"]),
/** The URL for sending messages to this subscription. */
get endpoint() {
return this._props.endpoint;
},
/** The last time a message was sent to this subscription. */
get lastPush() {
return this._props.lastPush;
},
/** The total number of messages sent to this subscription. */
get pushCount() {
return this._props.pushCount;
},
/** The number of remaining background messages that can be sent to this
* subscription, or -1 of the subscription is exempt from the quota.
*/
get quota() {
return this._props.quota;
},
/**
* Indicates whether this subscription was created with the system principal.
* System subscriptions are exempt from the background message quota and
* permission checks.
*/
get isSystemSubscription() {
return !!this._props.systemRecord;
},
/** The private key used to decrypt incoming push messages, in JWK format */
get p256dhPrivateKey() {
return this._props.p256dhPrivateKey;
},
/**
* Indicates whether this subscription is subject to the background message
* quota.
*/
quotaApplies() {
return this.quota >= 0;
},
/**
* Indicates whether this subscription exceeded the background message quota,
* or the user revoked the notification permission. The caller must request a
* new subscription to continue receiving push messages.
*/
isExpired() {
return this.quota === 0;
},
/**
* Returns a key for encrypting messages sent to this subscription. JS
* callers receive the key buffer as a return value, while C++ callers
* receive the key size and buffer as out parameters.
*/
getKey(name) {
switch (name) {
case "p256dh":
return this._getRawKey(this._props.p256dhKey);
case "auth":
return this._getRawKey(this._props.authenticationSecret);
case "appServer":
return this._getRawKey(this._props.appServerKey);
}
return [];
},
_getRawKey(key) {
if (!key) {
return [];
}
return new Uint8Array(key);
},
};
// Export the correct implementation depending on whether we're running in
// the parent or content process.
let Service = isParent ? PushServiceParent : PushServiceContent;
const EXPORTED_SYMBOLS = ["Service"];
|