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