michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict" michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = ["sendMessageToJava"]; michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: function sendMessageToJava(aMessage, aCallback) { michael@0: if (aCallback) { michael@0: let id = uuidgen.generateUUID().toString(); michael@0: let obs = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: let data = JSON.parse(aData); michael@0: if (data.__guid__ != id) { michael@0: return; michael@0: } michael@0: michael@0: Services.obs.removeObserver(obs, aMessage.type + ":Response", false); michael@0: michael@0: if (data.status === "cancel") { michael@0: // No Java-side listeners handled our callback. michael@0: return; michael@0: } michael@0: michael@0: aCallback(data.status === "success" ? data.response : null, michael@0: data.status === "error" ? data.response : null); michael@0: } michael@0: } michael@0: michael@0: aMessage.__guid__ = id; michael@0: Services.obs.addObserver(obs, aMessage.type + ":Response", false); michael@0: } michael@0: michael@0: return Services.androidBridge.handleGeckoMessage(aMessage); michael@0: }