2016-03-05 02:48:09 +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";
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-07-05 11:44:55 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PushDB",
|
|
|
|
"resource://gre/modules/PushDB.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PushRecord",
|
|
|
|
"resource://gre/modules/PushRecord.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PushCrypto",
|
|
|
|
"resource://gre/modules/PushCrypto.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"EventDispatcher",
|
|
|
|
"resource://gre/modules/Messaging.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"Preferences",
|
|
|
|
"resource://gre/modules/Preferences.jsm"
|
|
|
|
);
|
2016-03-05 02:48:09 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "Log", () => {
|
|
|
|
return ChromeUtils.import(
|
|
|
|
"resource://gre/modules/AndroidLog.jsm",
|
|
|
|
{}
|
|
|
|
).AndroidLog.bind("Push");
|
|
|
|
});
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ["PushServiceAndroidGCM"];
|
2016-03-05 02:48:09 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "console", () => {
|
2019-01-17 21:18:31 +03:00
|
|
|
let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
|
2016-03-05 02:48:09 +03:00
|
|
|
return new ConsoleAPI({
|
|
|
|
dump: Log.i,
|
|
|
|
maxLogLevelPref: "dom.push.loglevel",
|
|
|
|
prefix: "PushServiceAndroidGCM",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const kPUSHANDROIDGCMDB_DB_NAME = "pushAndroidGCM";
|
|
|
|
const kPUSHANDROIDGCMDB_DB_VERSION = 5; // Change this if the IndexedDB format changes
|
|
|
|
const kPUSHANDROIDGCMDB_STORE_NAME = "pushAndroidGCM";
|
|
|
|
|
2016-07-20 20:47:04 +03:00
|
|
|
const FXA_PUSH_SCOPE = "chrome://fxa-push";
|
|
|
|
|
2016-03-05 02:48:09 +03:00
|
|
|
const prefs = new Preferences("dom.push.");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The implementation of WebPush push backed by Android's GCM
|
|
|
|
* delivery.
|
|
|
|
*/
|
2018-02-23 22:50:01 +03:00
|
|
|
var PushServiceAndroidGCM = {
|
2016-03-05 02:48:09 +03:00
|
|
|
_mainPushService: null,
|
|
|
|
_serverURI: null,
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
newPushDB() {
|
2016-03-05 02:48:09 +03:00
|
|
|
return new PushDB(
|
|
|
|
kPUSHANDROIDGCMDB_DB_NAME,
|
|
|
|
kPUSHANDROIDGCMDB_DB_VERSION,
|
|
|
|
kPUSHANDROIDGCMDB_STORE_NAME,
|
|
|
|
"channelID",
|
|
|
|
PushRecordAndroidGCM
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
observe(subject, topic, data) {
|
2016-07-20 20:47:04 +03:00
|
|
|
switch (topic) {
|
|
|
|
case "nsPref:changed":
|
|
|
|
if (data == "dom.push.debug") {
|
|
|
|
// Reconfigure.
|
|
|
|
let debug = !!prefs.get("debug");
|
|
|
|
console.info(
|
|
|
|
"Debug parameter changed; updating configuration with new debug",
|
|
|
|
debug
|
|
|
|
);
|
|
|
|
this._configure(this._serverURI, debug);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "PushServiceAndroidGCM:ReceivedPushMessage":
|
|
|
|
this._onPushMessageReceived(data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-03-05 02:48:09 +03:00
|
|
|
}
|
2016-07-20 20:47:04 +03:00
|
|
|
},
|
2016-03-05 02:48:09 +03:00
|
|
|
|
2016-07-20 20:47:04 +03:00
|
|
|
_onPushMessageReceived(data) {
|
|
|
|
// TODO: Use Messaging.jsm for this.
|
|
|
|
if (this._mainPushService == null) {
|
|
|
|
// Shouldn't ever happen, but let's be careful.
|
|
|
|
console.error("No main PushService! Dropping message.");
|
2016-08-17 07:28:51 +03:00
|
|
|
return;
|
2016-07-20 20:47:04 +03:00
|
|
|
}
|
2016-07-20 20:47:04 +03:00
|
|
|
if (!data) {
|
|
|
|
console.error("No data from Java! Dropping message.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = JSON.parse(data);
|
|
|
|
console.debug("ReceivedPushMessage with data", data);
|
|
|
|
|
2016-10-05 18:56:55 +03:00
|
|
|
let { headers, message } = this._messageAndHeaders(data);
|
2016-07-20 20:47:04 +03:00
|
|
|
|
2016-10-05 18:56:55 +03:00
|
|
|
console.debug("Delivering message to main PushService:", message, headers);
|
2016-07-20 20:47:04 +03:00
|
|
|
this._mainPushService.receivedPushMessage(
|
2016-10-05 18:56:55 +03:00
|
|
|
data.channelID,
|
|
|
|
"",
|
|
|
|
headers,
|
|
|
|
message,
|
|
|
|
record => {
|
2016-07-20 20:47:04 +03:00
|
|
|
// Always update the stored record.
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-10-05 18:56:55 +03:00
|
|
|
_messageAndHeaders(data) {
|
2016-07-20 20:47:04 +03:00
|
|
|
// Default is no data (and no encryption).
|
|
|
|
let message = null;
|
2016-10-05 18:56:55 +03:00
|
|
|
let headers = null;
|
2016-07-20 20:47:04 +03:00
|
|
|
|
2019-02-18 11:15:26 +03:00
|
|
|
if (data.message) {
|
|
|
|
if (data.enc && (data.enckey || data.cryptokey)) {
|
|
|
|
headers = {
|
|
|
|
encryption_key: data.enckey,
|
|
|
|
crypto_key: data.cryptokey,
|
|
|
|
encryption: data.enc,
|
|
|
|
encoding: data.con,
|
|
|
|
};
|
2019-04-09 13:05:51 +03:00
|
|
|
} else if (data.con == "aes128gcm") {
|
2019-02-18 11:15:26 +03:00
|
|
|
headers = {
|
|
|
|
encoding: data.con,
|
|
|
|
};
|
|
|
|
}
|
2016-07-20 20:47:04 +03:00
|
|
|
// Ciphertext is (urlsafe) Base 64 encoded.
|
|
|
|
message = ChromeUtils.base64URLDecode(data.message, {
|
|
|
|
// The Push server may append padding.
|
|
|
|
padding: "ignore",
|
|
|
|
});
|
|
|
|
}
|
2019-02-18 11:15:26 +03:00
|
|
|
|
2016-10-05 18:56:55 +03:00
|
|
|
return { headers, message };
|
2016-07-20 20:47:04 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
_configure(serverURL, debug) {
|
2017-02-02 01:35:44 +03:00
|
|
|
return EventDispatcher.instance.sendRequestForResult({
|
2016-03-05 02:48:09 +03:00
|
|
|
type: "PushServiceAndroidGCM:Configure",
|
|
|
|
endpoint: serverURL.spec,
|
2019-04-09 13:05:51 +03:00
|
|
|
debug,
|
2016-03-05 02:48:09 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
init(options, mainPushService, serverURL) {
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("init()");
|
|
|
|
this._mainPushService = mainPushService;
|
|
|
|
this._serverURI = serverURL;
|
|
|
|
|
|
|
|
prefs.observe("debug", this);
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(this, "PushServiceAndroidGCM:ReceivedPushMessage");
|
2016-03-05 02:48:09 +03:00
|
|
|
|
2016-04-14 02:55:25 +03:00
|
|
|
return this._configure(serverURL, !!prefs.get("debug")).then(() => {
|
2017-02-02 01:35:44 +03:00
|
|
|
EventDispatcher.instance.sendRequestForResult({
|
2019-04-09 13:05:51 +03:00
|
|
|
type: "PushServiceAndroidGCM:Initialized",
|
2016-04-14 02:55:25 +03:00
|
|
|
});
|
|
|
|
});
|
2016-03-05 02:48:09 +03:00
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
uninit() {
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("uninit()");
|
2017-02-02 01:35:44 +03:00
|
|
|
EventDispatcher.instance.sendRequestForResult({
|
2019-04-09 13:05:51 +03:00
|
|
|
type: "PushServiceAndroidGCM:Uninitialized",
|
2016-04-14 02:55:25 +03:00
|
|
|
});
|
|
|
|
|
2016-03-05 02:48:09 +03:00
|
|
|
this._mainPushService = null;
|
2016-08-17 07:28:51 +03:00
|
|
|
Services.obs.removeObserver(
|
|
|
|
this,
|
|
|
|
"PushServiceAndroidGCM:ReceivedPushMessage"
|
|
|
|
);
|
2016-03-05 02:48:09 +03:00
|
|
|
prefs.ignore("debug", this);
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
onAlarmFired() {
|
2016-03-05 02:48:09 +03:00
|
|
|
// No action required.
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
connect(records, broadcastListeners) {
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("connect:", records);
|
|
|
|
// It's possible for the registration or subscriptions backing the
|
|
|
|
// PushService to not be registered with the underlying AndroidPushService.
|
|
|
|
// Expire those that are unrecognized.
|
2017-02-02 01:35:44 +03:00
|
|
|
return EventDispatcher.instance
|
|
|
|
.sendRequestForResult({
|
2016-03-05 02:48:09 +03:00
|
|
|
type: "PushServiceAndroidGCM:DumpSubscriptions",
|
|
|
|
})
|
|
|
|
.then(subscriptions => {
|
2017-02-15 20:47:30 +03:00
|
|
|
subscriptions = JSON.parse(subscriptions);
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("connect:", subscriptions);
|
|
|
|
// subscriptions maps chid => subscription data.
|
|
|
|
return Promise.all(
|
|
|
|
records.map(record => {
|
|
|
|
if (subscriptions.hasOwnProperty(record.keyID)) {
|
|
|
|
console.debug("connect:", "hasOwnProperty", record.keyID);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
console.debug("connect:", "!hasOwnProperty", record.keyID);
|
|
|
|
// Subscription is known to PushService.jsm but not to AndroidPushService. Drop it.
|
|
|
|
return this._mainPushService
|
|
|
|
.dropRegistrationAndNotifyApp(record.keyID)
|
|
|
|
.catch(error => {
|
|
|
|
console.error(
|
|
|
|
"connect: Error dropping registration",
|
|
|
|
record.keyID,
|
|
|
|
error
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
async sendSubscribeBroadcast(serviceId, version) {
|
2018-04-11 17:53:20 +03:00
|
|
|
// Not implemented yet
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
isConnected() {
|
2016-03-05 02:48:09 +03:00
|
|
|
return this._mainPushService != null;
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
disconnect() {
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("disconnect");
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
register(record) {
|
2016-03-28 22:29:25 +03:00
|
|
|
console.debug("register:", record);
|
2016-03-05 02:48:09 +03:00
|
|
|
let ctime = Date.now();
|
2016-03-22 22:09:31 +03:00
|
|
|
let appServerKey = record.appServerKey
|
|
|
|
? ChromeUtils.base64URLEncode(record.appServerKey, {
|
|
|
|
// The Push server requires padding.
|
|
|
|
pad: true,
|
|
|
|
})
|
|
|
|
: null;
|
2016-07-20 20:47:04 +03:00
|
|
|
let message = {
|
2016-03-05 02:48:09 +03:00
|
|
|
type: "PushServiceAndroidGCM:SubscribeChannel",
|
2019-04-09 13:05:51 +03:00
|
|
|
appServerKey,
|
|
|
|
};
|
2016-07-20 20:47:04 +03:00
|
|
|
if (record.scope == FXA_PUSH_SCOPE) {
|
|
|
|
message.service = "fxa";
|
|
|
|
}
|
|
|
|
// Caller handles errors.
|
2017-02-02 01:35:44 +03:00
|
|
|
return EventDispatcher.instance.sendRequestForResult(message).then(data => {
|
2017-02-15 20:47:30 +03:00
|
|
|
data = JSON.parse(data);
|
2016-03-05 02:48:09 +03:00
|
|
|
console.debug("Got data:", data);
|
|
|
|
return PushCrypto.generateKeys().then(
|
|
|
|
exportedKeys =>
|
|
|
|
new PushRecordAndroidGCM({
|
|
|
|
// Straight from autopush.
|
|
|
|
channelID: data.channelID,
|
|
|
|
pushEndpoint: data.endpoint,
|
|
|
|
// Common to all PushRecord implementations.
|
|
|
|
scope: record.scope,
|
|
|
|
originAttributes: record.originAttributes,
|
2019-04-09 13:05:51 +03:00
|
|
|
ctime,
|
2016-07-20 20:47:04 +03:00
|
|
|
systemRecord: record.systemRecord,
|
2016-03-05 02:48:09 +03:00
|
|
|
// Cryptography!
|
|
|
|
p256dhPublicKey: exportedKeys[0],
|
|
|
|
p256dhPrivateKey: exportedKeys[1],
|
|
|
|
authenticationSecret: PushCrypto.generateAuthenticationSecret(),
|
2016-03-22 22:09:31 +03:00
|
|
|
appServerKey: record.appServerKey,
|
2016-03-05 02:48:09 +03:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
unregister(record) {
|
2016-03-28 22:29:25 +03:00
|
|
|
console.debug("unregister: ", record);
|
2017-02-02 01:35:44 +03:00
|
|
|
return EventDispatcher.instance.sendRequestForResult({
|
2016-03-05 02:48:09 +03:00
|
|
|
type: "PushServiceAndroidGCM:UnsubscribeChannel",
|
|
|
|
channelID: record.keyID,
|
|
|
|
});
|
|
|
|
},
|
2016-04-21 23:35:26 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
reportDeliveryError(messageID, reason) {
|
2016-04-21 23:35:26 +03:00
|
|
|
console.warn(
|
|
|
|
"reportDeliveryError: Ignoring message delivery error",
|
|
|
|
messageID,
|
|
|
|
reason
|
|
|
|
);
|
|
|
|
},
|
2016-03-05 02:48:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
function PushRecordAndroidGCM(record) {
|
|
|
|
PushRecord.call(this, record);
|
|
|
|
this.channelID = record.channelID;
|
|
|
|
}
|
|
|
|
|
|
|
|
PushRecordAndroidGCM.prototype = Object.create(PushRecord.prototype, {
|
|
|
|
keyID: {
|
|
|
|
get() {
|
|
|
|
return this.channelID;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|