mobile/android/modules/Messaging.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/modules/Messaging.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict"
     1.8 +
     1.9 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.10 +
    1.11 +Cu.import("resource://gre/modules/Services.jsm");
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +
    1.14 +this.EXPORTED_SYMBOLS = ["sendMessageToJava"];
    1.15 +
    1.16 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
    1.17 +                                   "@mozilla.org/uuid-generator;1",
    1.18 +                                   "nsIUUIDGenerator");
    1.19 +
    1.20 +function sendMessageToJava(aMessage, aCallback) {
    1.21 +  if (aCallback) {
    1.22 +    let id = uuidgen.generateUUID().toString();
    1.23 +    let obs = {
    1.24 +      observe: function(aSubject, aTopic, aData) {
    1.25 +        let data = JSON.parse(aData);
    1.26 +        if (data.__guid__ != id) {
    1.27 +          return;
    1.28 +        }
    1.29 +
    1.30 +        Services.obs.removeObserver(obs, aMessage.type + ":Response", false);
    1.31 +
    1.32 +        if (data.status === "cancel") {
    1.33 +          // No Java-side listeners handled our callback.
    1.34 +          return;
    1.35 +        }
    1.36 +
    1.37 +        aCallback(data.status === "success" ? data.response : null,
    1.38 +                  data.status === "error"   ? data.response : null);
    1.39 +      }
    1.40 +    }
    1.41 +
    1.42 +    aMessage.__guid__ = id;
    1.43 +    Services.obs.addObserver(obs, aMessage.type + ":Response", false);
    1.44 +  }
    1.45 +
    1.46 +  return Services.androidBridge.handleGeckoMessage(aMessage);
    1.47 +}

mercurial