2015-12-08 23:41:41 +03:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2015-12-08 23:41:41 +03:00
|
|
|
|
|
|
|
var isParent = Services.appinfo.processType === Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
|
|
|
|
2016-03-23 03:34:41 +03:00
|
|
|
// The default Push service implementation.
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "PushService", function() {
|
2018-01-30 02:20:18 +03:00
|
|
|
const {PushService} = ChromeUtils.import("resource://gre/modules/PushService.jsm",
|
|
|
|
{});
|
2016-03-23 03:34:41 +03:00
|
|
|
PushService.init();
|
|
|
|
return PushService;
|
|
|
|
});
|
|
|
|
|
2016-04-26 20:20:08 +03:00
|
|
|
// 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.
|
2016-03-04 01:37:10 +03:00
|
|
|
const OBSERVER_TOPIC_PUSH = "push-message";
|
|
|
|
const OBSERVER_TOPIC_SUBSCRIPTION_CHANGE = "push-subscription-change";
|
2016-05-02 18:20:14 +03:00
|
|
|
const OBSERVER_TOPIC_SUBSCRIPTION_MODIFIED = "push-subscription-modified";
|
2016-03-04 01:37:10 +03:00
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
/**
|
|
|
|
* `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: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIPushService,
|
|
|
|
Ci.nsIPushQuotaManager,
|
2016-03-28 22:29:25 +03:00
|
|
|
Ci.nsIPushErrorReporter,
|
2015-12-08 23:41:41 +03:00
|
|
|
]),
|
|
|
|
|
2016-03-04 01:37:10 +03:00
|
|
|
pushTopic: OBSERVER_TOPIC_PUSH,
|
|
|
|
subscriptionChangeTopic: OBSERVER_TOPIC_SUBSCRIPTION_CHANGE,
|
2016-05-02 18:20:14 +03:00
|
|
|
subscriptionModifiedTopic: OBSERVER_TOPIC_SUBSCRIPTION_MODIFIED,
|
2016-03-04 01:37:10 +03:00
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
_handleReady() {},
|
|
|
|
|
|
|
|
_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 === "app-startup") {
|
|
|
|
Services.obs.addObserver(this, "sessionstore-windows-restored", true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (topic === "sessionstore-windows-restored") {
|
|
|
|
Services.obs.removeObserver(this, "sessionstore-windows-restored");
|
|
|
|
this._handleReady();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-20 22:06:13 +03:00
|
|
|
if (topic === "android-push-service") {
|
|
|
|
// Load PushService immediately.
|
|
|
|
this._handleReady();
|
|
|
|
return;
|
|
|
|
}
|
2015-12-08 23:41:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_deliverSubscription(request, props) {
|
|
|
|
if (!props) {
|
|
|
|
request.onPushSubscription(Cr.NS_OK, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request.onPushSubscription(Cr.NS_OK, new PushSubscription(props));
|
|
|
|
},
|
2016-03-16 12:53:13 +03:00
|
|
|
|
|
|
|
_deliverSubscriptionError(request, error) {
|
|
|
|
let result = typeof error.result == "number" ?
|
|
|
|
error.result : Cr.NS_ERROR_FAILURE;
|
|
|
|
request.onPushSubscription(result, null);
|
|
|
|
},
|
2015-12-08 23:41:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function PushServiceParent() {
|
|
|
|
PushServiceBase.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PushServiceParent.prototype = Object.create(PushServiceBase.prototype);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(PushServiceParent.prototype, "_mm",
|
2018-03-01 22:19:56 +03:00
|
|
|
"@mozilla.org/parentprocessmessagemanager;1", "nsISupports");
|
2015-12-08 23:41:41 +03:00
|
|
|
|
|
|
|
Object.assign(PushServiceParent.prototype, {
|
|
|
|
_xpcom_factory: XPCOMUtils.generateSingletonFactory(PushServiceParent),
|
|
|
|
|
|
|
|
_messages: [
|
|
|
|
"Push:Register",
|
|
|
|
"Push:Registration",
|
|
|
|
"Push:Unregister",
|
2015-12-08 23:41:48 +03:00
|
|
|
"Push:Clear",
|
2015-12-08 23:41:41 +03:00
|
|
|
"Push:NotificationForOriginShown",
|
|
|
|
"Push:NotificationForOriginClosed",
|
2016-03-28 22:29:25 +03:00
|
|
|
"Push:ReportError",
|
2015-12-08 23:41:41 +03:00
|
|
|
],
|
|
|
|
|
|
|
|
// nsIPushService methods
|
|
|
|
|
|
|
|
subscribe(scope, principal, callback) {
|
2016-03-16 12:53:13 +03:00
|
|
|
this.subscribeWithKey(scope, principal, 0, null, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribeWithKey(scope, principal, keyLen, key, callback) {
|
|
|
|
this._handleRequest("Push:Register", principal, {
|
2015-12-08 23:41:41 +03:00
|
|
|
scope: scope,
|
2016-03-16 12:53:13 +03:00
|
|
|
appServerKey: key,
|
2015-12-08 23:41:41 +03:00
|
|
|
}).then(result => {
|
|
|
|
this._deliverSubscription(callback, result);
|
|
|
|
}, error => {
|
2016-03-16 12:53:13 +03:00
|
|
|
this._deliverSubscriptionError(callback, error);
|
2015-12-08 23:41:41 +03:00
|
|
|
}).catch(Cu.reportError);
|
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribe(scope, principal, callback) {
|
|
|
|
this._handleRequest("Push:Unregister", principal, {
|
|
|
|
scope: 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: scope,
|
|
|
|
}).then(result => {
|
|
|
|
this._deliverSubscription(callback, result);
|
|
|
|
}, error => {
|
2016-03-16 12:53:13 +03:00
|
|
|
this._deliverSubscriptionError(callback, error);
|
2015-12-08 23:41:41 +03:00
|
|
|
}).catch(Cu.reportError);
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:48 +03:00
|
|
|
clearForDomain(domain, callback) {
|
2015-12-16 20:21:22 +03:00
|
|
|
return this._handleRequest("Push:Clear", null, {
|
2015-12-08 23:41:48 +03:00
|
|
|
domain: domain,
|
|
|
|
}).then(result => {
|
|
|
|
callback.onClear(Cr.NS_OK);
|
|
|
|
}, error => {
|
|
|
|
callback.onClear(Cr.NS_ERROR_FAILURE);
|
|
|
|
}).catch(Cu.reportError);
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
// nsIPushQuotaManager methods
|
|
|
|
|
|
|
|
notificationForOriginShown(origin) {
|
2016-03-23 03:34:41 +03:00
|
|
|
this.service.notificationForOriginShown(origin);
|
2015-12-08 23:41:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
notificationForOriginClosed(origin) {
|
2016-03-23 03:34:41 +03:00
|
|
|
this.service.notificationForOriginClosed(origin);
|
2015-12-08 23:41:41 +03:00
|
|
|
},
|
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
// nsIPushErrorReporter methods
|
|
|
|
|
|
|
|
reportDeliveryError(messageId, reason) {
|
|
|
|
this.service.reportDeliveryError(messageId, reason);
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
receiveMessage(message) {
|
|
|
|
if (!this._isValidMessage(message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let {name, principal, target, data} = message;
|
|
|
|
if (name === "Push:NotificationForOriginShown") {
|
|
|
|
this.notificationForOriginShown(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (name === "Push:NotificationForOriginClosed") {
|
|
|
|
this.notificationForOriginClosed(data);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-28 22:29:25 +03:00
|
|
|
if (name === "Push:ReportError") {
|
|
|
|
this.reportDeliveryError(data.messageId, data.reason);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-08 23:41:41 +03:00
|
|
|
return this._handleRequest(name, principal, data).then(result => {
|
2018-03-01 22:19:56 +03:00
|
|
|
target.sendAsyncMessage(this._getResponseName(name, "OK"), {
|
2015-12-08 23:41:41 +03:00
|
|
|
requestID: data.requestID,
|
|
|
|
result: result
|
|
|
|
});
|
|
|
|
}, error => {
|
2018-03-01 22:19:56 +03:00
|
|
|
target.sendAsyncMessage(this._getResponseName(name, "KO"), {
|
2015-12-08 23:41:41 +03:00
|
|
|
requestID: data.requestID,
|
2016-03-16 12:53:13 +03:00
|
|
|
result: error.result,
|
2015-12-08 23:41:41 +03:00
|
|
|
});
|
|
|
|
}).catch(Cu.reportError);
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleReady() {
|
2016-03-23 03:34:41 +03:00
|
|
|
this.service.init();
|
2015-12-08 23:41:41 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_toPageRecord(principal, data) {
|
|
|
|
if (!data.scope) {
|
|
|
|
throw new Error("Invalid page record: missing scope");
|
|
|
|
}
|
2015-12-16 20:21:22 +03:00
|
|
|
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
|
2016-04-24 05:41:59 +03:00
|
|
|
// also do not fire service worker events.
|
2015-12-16 20:21:22 +03:00
|
|
|
data.systemRecord = principal.isSystemPrincipal;
|
2015-12-08 23:41:41 +03:00
|
|
|
|
|
|
|
data.originAttributes =
|
|
|
|
ChromeUtils.originAttributesToSuffix(principal.originAttributes);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleRequest(name, principal, data) {
|
2015-12-08 23:41:48 +03:00
|
|
|
if (name == "Push:Clear") {
|
2016-03-23 03:34:41 +03:00
|
|
|
return this.service.clear(data);
|
2015-12-08 23:41:48 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
let pageRecord;
|
|
|
|
try {
|
|
|
|
pageRecord = this._toPageRecord(principal, data);
|
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === "Push:Register") {
|
2016-03-23 03:34:41 +03:00
|
|
|
return this.service.register(pageRecord);
|
2015-12-08 23:41:41 +03:00
|
|
|
}
|
|
|
|
if (name === "Push:Registration") {
|
2016-03-23 03:34:41 +03:00
|
|
|
return this.service.registration(pageRecord);
|
2015-12-08 23:41:41 +03:00
|
|
|
}
|
|
|
|
if (name === "Push:Unregister") {
|
2016-03-23 03:34:41 +03:00
|
|
|
return this.service.unregister(pageRecord);
|
2015-12-08 23:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(new Error("Invalid request: unknown name"));
|
|
|
|
},
|
|
|
|
|
|
|
|
_getResponseName(requestName, suffix) {
|
|
|
|
let name = requestName.slice("Push:".length);
|
|
|
|
return "PushService:" + name + ":" + suffix;
|
|
|
|
},
|
2016-03-18 03:11:22 +03:00
|
|
|
|
|
|
|
// Methods used for mocking in tests.
|
|
|
|
|
|
|
|
replaceServiceBackend(options) {
|
2016-04-08 21:39:00 +03:00
|
|
|
return this.service.changeTestServer(options.serverURI, options);
|
2016-03-18 03:11:22 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
restoreServiceBackend() {
|
|
|
|
var defaultServerURL = Services.prefs.getCharPref("dom.push.serverURL");
|
2016-04-08 21:39:00 +03:00
|
|
|
return this.service.changeTestServer(defaultServerURL);
|
2016-03-23 03:34:41 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Used to replace the implementation with a mock.
|
|
|
|
Object.defineProperty(PushServiceParent.prototype, "service", {
|
|
|
|
get() {
|
|
|
|
return this._service || PushService;
|
|
|
|
},
|
|
|
|
set(impl) {
|
|
|
|
this._service = impl;
|
2016-03-18 03:11:22 +03:00
|
|
|
},
|
2015-12-08 23:41:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
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",
|
2018-03-01 22:19:56 +03:00
|
|
|
"nsISupports");
|
2015-12-08 23:41:41 +03:00
|
|
|
|
|
|
|
Object.assign(PushServiceContent.prototype, {
|
|
|
|
_xpcom_factory: XPCOMUtils.generateSingletonFactory(PushServiceContent),
|
|
|
|
|
|
|
|
_messages: [
|
|
|
|
"PushService:Register:OK",
|
|
|
|
"PushService:Register:KO",
|
|
|
|
"PushService:Registration:OK",
|
|
|
|
"PushService:Registration:KO",
|
|
|
|
"PushService:Unregister:OK",
|
|
|
|
"PushService:Unregister:KO",
|
2015-12-08 23:41:48 +03:00
|
|
|
"PushService:Clear:OK",
|
|
|
|
"PushService:Clear:KO",
|
2015-12-08 23:41:41 +03:00
|
|
|
],
|
|
|
|
|
|
|
|
// nsIPushService methods
|
|
|
|
|
|
|
|
subscribe(scope, principal, callback) {
|
2016-03-16 12:53:13 +03:00
|
|
|
this.subscribeWithKey(scope, principal, 0, null, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribeWithKey(scope, principal, keyLen, key, callback) {
|
2015-12-08 23:41:41 +03:00
|
|
|
let requestId = this._addRequest(callback);
|
|
|
|
this._mm.sendAsyncMessage("Push:Register", {
|
|
|
|
scope: scope,
|
2016-03-16 12:53:13 +03:00
|
|
|
appServerKey: key,
|
2015-12-08 23:41:41 +03:00
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribe(scope, principal, callback) {
|
|
|
|
let requestId = this._addRequest(callback);
|
|
|
|
this._mm.sendAsyncMessage("Push:Unregister", {
|
|
|
|
scope: scope,
|
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscription(scope, principal, callback) {
|
|
|
|
let requestId = this._addRequest(callback);
|
|
|
|
this._mm.sendAsyncMessage("Push:Registration", {
|
|
|
|
scope: scope,
|
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:48 +03:00
|
|
|
clearForDomain(domain, callback) {
|
|
|
|
let requestId = this._addRequest(callback);
|
|
|
|
this._mm.sendAsyncMessage("Push:Clear", {
|
|
|
|
domain: domain,
|
|
|
|
requestID: requestId,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
// nsIPushQuotaManager methods
|
|
|
|
|
|
|
|
notificationForOriginShown(origin) {
|
|
|
|
this._mm.sendAsyncMessage("Push:NotificationForOriginShown", origin);
|
|
|
|
},
|
|
|
|
|
|
|
|
notificationForOriginClosed(origin) {
|
|
|
|
this._mm.sendAsyncMessage("Push:NotificationForOriginClosed", origin);
|
|
|
|
},
|
|
|
|
|
2016-03-28 22:29:25 +03:00
|
|
|
// nsIPushErrorReporter methods
|
|
|
|
|
|
|
|
reportDeliveryError(messageId, reason) {
|
|
|
|
this._mm.sendAsyncMessage("Push:ReportError", {
|
|
|
|
messageId: messageId,
|
|
|
|
reason: reason,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
_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":
|
2016-03-16 12:53:13 +03:00
|
|
|
this._deliverSubscriptionError(request, data);
|
2015-12-08 23:41:41 +03:00
|
|
|
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;
|
|
|
|
|
2015-12-08 23:41:48 +03:00
|
|
|
case "PushService:Clear:OK":
|
|
|
|
request.onClear(Cr.NS_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "PushService:Clear:KO":
|
|
|
|
request.onClear(Cr.NS_ERROR_FAILURE);
|
|
|
|
break;
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/** `PushSubscription` instances are passed to all subscription callbacks. */
|
|
|
|
function PushSubscription(props) {
|
|
|
|
this._props = props;
|
|
|
|
}
|
|
|
|
|
|
|
|
PushSubscription.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.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;
|
|
|
|
},
|
|
|
|
|
2016-04-24 05:41:59 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
|
|
|
|
2016-07-20 20:47:04 +03:00
|
|
|
/** The private key used to decrypt incoming push messages, in JWK format */
|
|
|
|
get p256dhPrivateKey() {
|
|
|
|
return this._props.p256dhPrivateKey;
|
|
|
|
},
|
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
/**
|
|
|
|
* 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, outKeyLen) {
|
2016-03-16 12:53:13 +03:00
|
|
|
switch (name) {
|
|
|
|
case "p256dh":
|
|
|
|
return this._getRawKey(this._props.p256dhKey, outKeyLen);
|
|
|
|
|
|
|
|
case "auth":
|
|
|
|
return this._getRawKey(this._props.authenticationSecret, outKeyLen);
|
|
|
|
|
|
|
|
case "appServer":
|
|
|
|
return this._getRawKey(this._props.appServerKey, outKeyLen);
|
2015-12-08 23:41:41 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getRawKey(key, outKeyLen) {
|
|
|
|
if (!key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let rawKey = new Uint8Array(key);
|
|
|
|
if (outKeyLen) {
|
|
|
|
outKeyLen.value = rawKey.length;
|
|
|
|
}
|
|
|
|
return rawKey;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([
|
|
|
|
// Export the correct implementation depending on whether we're running in
|
|
|
|
// the parent or content process.
|
|
|
|
isParent ? PushServiceParent : PushServiceContent,
|
|
|
|
]);
|