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