2015-07-23 18:30:15 +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";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2015-11-10 00:58:32 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "console", () => {
|
|
|
|
let {ConsoleAPI} = Cu.import("resource://gre/modules/Console.jsm", {});
|
|
|
|
return new ConsoleAPI({
|
|
|
|
maxLogLevelPref: "dom.push.loglevel",
|
|
|
|
prefix: "PushClient",
|
|
|
|
});
|
|
|
|
});
|
2015-08-11 22:53:14 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
const kMessages = [
|
|
|
|
"PushService:Register:OK",
|
|
|
|
"PushService:Register:KO",
|
|
|
|
"PushService:Registration:OK",
|
|
|
|
"PushService:Registration:KO",
|
|
|
|
"PushService:Unregister:OK",
|
|
|
|
"PushService:Unregister:KO",
|
|
|
|
];
|
|
|
|
|
|
|
|
this.PushClient = function PushClient() {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("PushClient()");
|
2015-07-23 18:30:15 +03:00
|
|
|
this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsISyncMessageSender);
|
|
|
|
this._requests = {};
|
|
|
|
this.addListeners();
|
|
|
|
};
|
|
|
|
|
|
|
|
PushClient.prototype = {
|
|
|
|
classID: Components.ID("{16042199-bec0-484a-9640-25ecc0c0a149}"),
|
|
|
|
|
|
|
|
contractID: "@mozilla.org/push/PushClient;1",
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIPushClient,
|
|
|
|
Ci.nsIMessageListener,]),
|
|
|
|
|
|
|
|
|
|
|
|
_getRandomId: function() {
|
|
|
|
return Cc["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Ci.nsIUUIDGenerator).generateUUID().toString();
|
|
|
|
},
|
|
|
|
|
|
|
|
addRequest: function(data) {
|
|
|
|
let id = this._getRandomId();
|
|
|
|
this._requests[id] = data;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
|
|
|
takeRequest: function(requestId) {
|
|
|
|
let d = this._requests[requestId];
|
|
|
|
delete this._requests[requestId];
|
|
|
|
return d;
|
|
|
|
},
|
|
|
|
|
|
|
|
addListeners: function() {
|
|
|
|
for (let message of kMessages) {
|
|
|
|
this._cpmm.addWeakMessageListener(message, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribe: function(scope, principal, callback) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("subscribe()", scope);
|
2015-07-23 18:30:15 +03:00
|
|
|
let requestId = this.addRequest(callback);
|
|
|
|
this._cpmm.sendAsyncMessage("Push:Register", {
|
|
|
|
scope: scope,
|
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribe: function(scope, principal, callback) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("unsubscribe()", scope);
|
2015-07-23 18:30:15 +03:00
|
|
|
let requestId = this.addRequest(callback);
|
|
|
|
this._cpmm.sendAsyncMessage("Push:Unregister", {
|
|
|
|
scope: scope,
|
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscription: function(scope, principal, callback) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("getSubscription()", scope);
|
2015-07-23 18:30:15 +03:00
|
|
|
let requestId = this.addRequest(callback);
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("getSubscription: Going to send", scope, principal,
|
|
|
|
requestId);
|
2015-07-23 18:30:15 +03:00
|
|
|
this._cpmm.sendAsyncMessage("Push:Registration", {
|
|
|
|
scope: scope,
|
|
|
|
requestID: requestId,
|
|
|
|
}, null, principal);
|
|
|
|
},
|
|
|
|
|
2015-09-11 17:51:32 +03:00
|
|
|
_deliverPushEndpoint: function(request, registration) {
|
2015-11-10 00:58:50 +03:00
|
|
|
if (!registration) {
|
|
|
|
request.onPushEndpoint(Cr.NS_OK, "", 0, null);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-11 17:51:32 +03:00
|
|
|
if (registration.p256dhKey) {
|
|
|
|
let key = new Uint8Array(registration.p256dhKey);
|
|
|
|
request.onPushEndpoint(Cr.NS_OK,
|
|
|
|
registration.pushEndpoint,
|
|
|
|
key.length,
|
|
|
|
key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onPushEndpoint(Cr.NS_OK, registration.pushEndpoint, 0, null);
|
|
|
|
},
|
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
receiveMessage: function(aMessage) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.debug("receiveMessage()", aMessage);
|
2015-07-23 18:30:15 +03:00
|
|
|
|
|
|
|
let json = aMessage.data;
|
|
|
|
let request = this.takeRequest(json.requestID);
|
|
|
|
|
|
|
|
if (!request) {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("receiveMessage: Unknown request ID", json.requestID);
|
2015-07-23 18:30:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "PushService:Register:OK":
|
|
|
|
case "PushService:Registration:OK":
|
2015-11-10 00:58:50 +03:00
|
|
|
this._deliverPushEndpoint(request, json.result);
|
2015-07-23 18:30:15 +03:00
|
|
|
break;
|
2015-11-10 00:58:50 +03:00
|
|
|
|
|
|
|
case "PushService:Register:KO":
|
2015-07-23 18:30:15 +03:00
|
|
|
case "PushService:Registration:KO":
|
2015-09-11 17:51:32 +03:00
|
|
|
request.onPushEndpoint(Cr.NS_ERROR_FAILURE, "", 0, null);
|
2015-07-23 18:30:15 +03:00
|
|
|
break;
|
2015-11-10 00:58:50 +03:00
|
|
|
|
2015-07-23 18:30:15 +03:00
|
|
|
case "PushService:Unregister:OK":
|
|
|
|
if (typeof json.result !== "boolean") {
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("receiveMessage: Expected boolean for unregister response",
|
|
|
|
json.result);
|
2015-07-23 18:30:15 +03:00
|
|
|
request.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onUnsubscribe(Cr.NS_OK, json.result);
|
|
|
|
break;
|
|
|
|
case "PushService:Unregister:KO":
|
|
|
|
request.onUnsubscribe(Cr.NS_ERROR_FAILURE, false);
|
|
|
|
break;
|
|
|
|
default:
|
2015-11-10 00:58:32 +03:00
|
|
|
console.error("receiveMessage: NOT IMPLEMENTED!", aMessage.name);
|
2015-07-23 18:30:15 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PushClient]);
|