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
|
/* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
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"
);
XPCOMUtils.defineLazyGetter(this, "Log", () => {
return ChromeUtils.import(
"resource://gre/modules/AndroidLog.jsm",
{}
).AndroidLog.bind("Push");
});
const EXPORTED_SYMBOLS = ["PushServiceAndroidGCM"];
XPCOMUtils.defineLazyGetter(this, "console", () => {
let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
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";
const FXA_PUSH_SCOPE = "chrome://fxa-push";
const prefs = new Preferences("dom.push.");
/**
* The implementation of WebPush push backed by Android's GCM
* delivery.
*/
var PushServiceAndroidGCM = {
_mainPushService: null,
_serverURI: null,
newPushDB() {
return new PushDB(
kPUSHANDROIDGCMDB_DB_NAME,
kPUSHANDROIDGCMDB_DB_VERSION,
kPUSHANDROIDGCMDB_STORE_NAME,
"channelID",
PushRecordAndroidGCM
);
},
observe(subject, topic, data) {
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;
}
},
_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.");
return;
}
if (!data) {
console.error("No data from Java! Dropping message.");
return;
}
data = JSON.parse(data);
console.debug("ReceivedPushMessage with data", data);
let { headers, message } = this._messageAndHeaders(data);
console.debug("Delivering message to main PushService:", message, headers);
this._mainPushService.receivedPushMessage(
data.channelID,
"",
headers,
message,
record => {
// Always update the stored record.
return record;
}
);
},
_messageAndHeaders(data) {
// Default is no data (and no encryption).
let message = null;
let headers = null;
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,
};
} else if (data.con == "aes128gcm") {
headers = {
encoding: data.con,
};
}
// Ciphertext is (urlsafe) Base 64 encoded.
message = ChromeUtils.base64URLDecode(data.message, {
// The Push server may append padding.
padding: "ignore",
});
}
return { headers, message };
},
_configure(serverURL, debug) {
return EventDispatcher.instance.sendRequestForResult({
type: "PushServiceAndroidGCM:Configure",
endpoint: serverURL.spec,
debug,
});
},
init(options, mainPushService, serverURL) {
console.debug("init()");
this._mainPushService = mainPushService;
this._serverURI = serverURL;
prefs.observe("debug", this);
Services.obs.addObserver(this, "PushServiceAndroidGCM:ReceivedPushMessage");
return this._configure(serverURL, !!prefs.get("debug")).then(() => {
EventDispatcher.instance.sendRequestForResult({
type: "PushServiceAndroidGCM:Initialized",
});
});
},
uninit() {
console.debug("uninit()");
EventDispatcher.instance.sendRequestForResult({
type: "PushServiceAndroidGCM:Uninitialized",
});
this._mainPushService = null;
Services.obs.removeObserver(
this,
"PushServiceAndroidGCM:ReceivedPushMessage"
);
prefs.ignore("debug", this);
},
onAlarmFired() {
// No action required.
},
connect(records, broadcastListeners) {
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.
return EventDispatcher.instance
.sendRequestForResult({
type: "PushServiceAndroidGCM:DumpSubscriptions",
})
.then(subscriptions => {
subscriptions = JSON.parse(subscriptions);
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
);
});
})
);
});
},
async sendSubscribeBroadcast(serviceId, version) {
// Not implemented yet
},
isConnected() {
return this._mainPushService != null;
},
disconnect() {
console.debug("disconnect");
},
register(record) {
console.debug("register:", record);
let ctime = Date.now();
let appServerKey = record.appServerKey
? ChromeUtils.base64URLEncode(record.appServerKey, {
// The Push server requires padding.
pad: true,
})
: null;
let message = {
type: "PushServiceAndroidGCM:SubscribeChannel",
appServerKey,
};
if (record.scope == FXA_PUSH_SCOPE) {
message.service = "fxa";
}
// Caller handles errors.
return EventDispatcher.instance.sendRequestForResult(message).then(data => {
data = JSON.parse(data);
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,
ctime,
systemRecord: record.systemRecord,
// Cryptography!
p256dhPublicKey: exportedKeys[0],
p256dhPrivateKey: exportedKeys[1],
authenticationSecret: PushCrypto.generateAuthenticationSecret(),
appServerKey: record.appServerKey,
})
);
});
},
unregister(record) {
console.debug("unregister: ", record);
return EventDispatcher.instance.sendRequestForResult({
type: "PushServiceAndroidGCM:UnsubscribeChannel",
channelID: record.keyID,
});
},
reportDeliveryError(messageID, reason) {
console.warn(
"reportDeliveryError: Ignoring message delivery error",
messageID,
reason
);
},
};
function PushRecordAndroidGCM(record) {
PushRecord.call(this, record);
this.channelID = record.channelID;
}
PushRecordAndroidGCM.prototype = Object.create(PushRecord.prototype, {
keyID: {
get() {
return this.channelID;
},
},
});
|