Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 this.EXPORTED_SYMBOLS = ["sendOrderedBroadcast"];
9 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
11 // For adding observers.
12 Cu.import("resource://gre/modules/Services.jsm");
13 Cu.import("resource://gre/modules/Messaging.jsm");
15 let _callbackId = 1;
17 /**
18 * Send an ordered broadcast to Java.
19 *
20 * Internally calls Context.sendOrderedBroadcast.
21 *
22 * action {String} should be a string with a qualified name (like
23 * org.mozilla.gecko.action) that will be broadcast.
24 *
25 * token {Object} is a piece of arbitrary data that will be given as
26 * a parameter to the callback (possibly null).
27 *
28 * callback {function} should accept three arguments: the data
29 * returned from Java as an Object; the specified token; and the
30 * specified action.
31 *
32 * permission {String} is an optional string with an Android permission
33 * that packages must have to respond to the ordered broadcast. A null
34 * value allows any package to respond. If the parameter is omitted (or
35 * {undefined}), then the intent is restricted to the current package.
36 */
37 function sendOrderedBroadcast(action, token, callback, permission) {
38 let callbackId = _callbackId++;
39 let responseEvent = "OrderedBroadcast:Response:" + callbackId;
41 let observer = {
42 callbackId: callbackId,
43 callback: callback,
45 observe: function observe(subject, topic, data) {
46 if (topic != responseEvent) {
47 return;
48 }
50 // Unregister observer as soon as possible.
51 Services.obs.removeObserver(observer, responseEvent);
53 let msg = JSON.parse(data);
54 if (!msg.action || !msg.token || !msg.token.callbackId)
55 return;
57 let theToken = msg.token.data;
58 let theAction = msg.action;
59 let theData = msg.data ? JSON.parse(msg.data) : null;
61 let theCallback = this.callback;
62 if (!theCallback)
63 return;
65 // This is called from within a notified observer, so we don't
66 // need to take special pains to catch exceptions.
67 theCallback(theData, theToken, theAction);
68 },
69 };
71 Services.obs.addObserver(observer, responseEvent, false);
73 sendMessageToJava({
74 type: "OrderedBroadcast:Send",
75 action: action,
76 responseEvent: responseEvent,
77 token: { callbackId: callbackId, data: token || null },
78 permission: permission,
79 });
80 };