mobile/android/modules/Messaging.jsm

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:b039dcffe351
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict"
5
6 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
7
8 Cu.import("resource://gre/modules/Services.jsm");
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10
11 this.EXPORTED_SYMBOLS = ["sendMessageToJava"];
12
13 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
14 "@mozilla.org/uuid-generator;1",
15 "nsIUUIDGenerator");
16
17 function sendMessageToJava(aMessage, aCallback) {
18 if (aCallback) {
19 let id = uuidgen.generateUUID().toString();
20 let obs = {
21 observe: function(aSubject, aTopic, aData) {
22 let data = JSON.parse(aData);
23 if (data.__guid__ != id) {
24 return;
25 }
26
27 Services.obs.removeObserver(obs, aMessage.type + ":Response", false);
28
29 if (data.status === "cancel") {
30 // No Java-side listeners handled our callback.
31 return;
32 }
33
34 aCallback(data.status === "success" ? data.response : null,
35 data.status === "error" ? data.response : null);
36 }
37 }
38
39 aMessage.__guid__ = id;
40 Services.obs.addObserver(obs, aMessage.type + ":Response", false);
41 }
42
43 return Services.androidBridge.handleGeckoMessage(aMessage);
44 }

mercurial