mobile/android/modules/OrderedBroadcast.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/modules/OrderedBroadcast.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["sendOrderedBroadcast"];
    1.11 +
    1.12 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.13 +
    1.14 +// For adding observers.
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +Cu.import("resource://gre/modules/Messaging.jsm");
    1.17 +
    1.18 +let _callbackId = 1;
    1.19 +
    1.20 +/**
    1.21 + * Send an ordered broadcast to Java.
    1.22 + *
    1.23 + * Internally calls Context.sendOrderedBroadcast.
    1.24 + *
    1.25 + * action {String} should be a string with a qualified name (like
    1.26 + * org.mozilla.gecko.action) that will be broadcast.
    1.27 + *
    1.28 + * token {Object} is a piece of arbitrary data that will be given as
    1.29 + * a parameter to the callback (possibly null).
    1.30 + *
    1.31 + * callback {function} should accept three arguments: the data
    1.32 + * returned from Java as an Object; the specified token; and the
    1.33 + * specified action.
    1.34 + *
    1.35 + * permission {String} is an optional string with an Android permission
    1.36 + * that packages must have to respond to the ordered broadcast. A null
    1.37 + * value allows any package to respond. If the parameter is omitted (or
    1.38 + * {undefined}), then the intent is restricted to the current package.
    1.39 + */
    1.40 +function sendOrderedBroadcast(action, token, callback, permission) {
    1.41 +  let callbackId = _callbackId++;
    1.42 +  let responseEvent = "OrderedBroadcast:Response:" + callbackId;
    1.43 +
    1.44 +  let observer = {
    1.45 +    callbackId: callbackId,
    1.46 +    callback: callback,
    1.47 +
    1.48 +    observe: function observe(subject, topic, data) {
    1.49 +      if (topic != responseEvent) {
    1.50 +        return;
    1.51 +      }
    1.52 +
    1.53 +      // Unregister observer as soon as possible.
    1.54 +      Services.obs.removeObserver(observer, responseEvent);
    1.55 +
    1.56 +      let msg = JSON.parse(data);
    1.57 +      if (!msg.action || !msg.token || !msg.token.callbackId)
    1.58 +        return;
    1.59 +
    1.60 +      let theToken = msg.token.data;
    1.61 +      let theAction = msg.action;
    1.62 +      let theData = msg.data ? JSON.parse(msg.data) : null;
    1.63 +
    1.64 +      let theCallback = this.callback;
    1.65 +      if (!theCallback)
    1.66 +        return;
    1.67 +
    1.68 +      // This is called from within a notified observer, so we don't
    1.69 +      // need to take special pains to catch exceptions.
    1.70 +      theCallback(theData, theToken, theAction);
    1.71 +    },
    1.72 +  };
    1.73 +
    1.74 +  Services.obs.addObserver(observer, responseEvent, false);
    1.75 +
    1.76 +  sendMessageToJava({
    1.77 +    type: "OrderedBroadcast:Send",
    1.78 +    action: action,
    1.79 +    responseEvent: responseEvent,
    1.80 +    token: { callbackId: callbackId, data: token || null },
    1.81 +    permission: permission,
    1.82 +  });
    1.83 +};

mercurial