Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
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
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.gecko;
8 import org.mozilla.gecko.background.common.GlobalConstants;
9 import org.mozilla.gecko.EventDispatcher;
10 import org.mozilla.gecko.util.GeckoEventListener;
12 import org.json.JSONException;
13 import org.json.JSONObject;
15 import android.app.Activity;
17 import android.content.BroadcastReceiver;
18 import android.content.Context;
19 import android.content.Intent;
21 import android.util.Log;
23 /**
24 * Helper class to send Android Ordered Broadcasts.
25 */
26 public final class OrderedBroadcastHelper
27 implements GeckoEventListener
28 {
29 public static final String LOGTAG = "GeckoOrdBroadcast";
31 public static final String SEND_EVENT = "OrderedBroadcast:Send";
33 protected final Context mContext;
35 public OrderedBroadcastHelper(Context context) {
36 mContext = context;
38 EventDispatcher dispatcher = GeckoAppShell.getEventDispatcher();
39 if (dispatcher == null) {
40 Log.e(LOGTAG, "Gecko event dispatcher must not be null", new RuntimeException());
41 return;
42 }
43 dispatcher.registerEventListener(SEND_EVENT, this);
44 }
46 public synchronized void uninit() {
47 EventDispatcher dispatcher = GeckoAppShell.getEventDispatcher();
48 if (dispatcher == null) {
49 Log.e(LOGTAG, "Gecko event dispatcher must not be null", new RuntimeException());
50 return;
51 }
52 dispatcher.unregisterEventListener(SEND_EVENT, this);
53 }
55 @Override
56 public void handleMessage(String event, JSONObject message) {
57 if (!SEND_EVENT.equals(event)) {
58 Log.e(LOGTAG, "OrderedBroadcastHelper got unexpected message " + event);
59 return;
60 }
62 try {
63 final String action = message.getString("action");
64 if (action == null) {
65 Log.e(LOGTAG, "action must not be null");
66 return;
67 }
69 final String responseEvent = message.getString("responseEvent");
70 if (responseEvent == null) {
71 Log.e(LOGTAG, "responseEvent must not be null");
72 return;
73 }
75 // It's fine if the caller-provided token is missing or null.
76 final JSONObject token = (message.has("token") && !message.isNull("token")) ?
77 message.getJSONObject("token") : null;
79 // A missing (undefined) permission means the intent will be limited
80 // to the current package. A null means no permission, so any
81 // package can receive the intent.
82 final String permission = message.has("permission") ?
83 (message.isNull("permission") ? null : message.getString("permission")) :
84 GlobalConstants.PER_ANDROID_PACKAGE_PERMISSION;
86 final BroadcastReceiver resultReceiver = new BroadcastReceiver() {
87 @Override
88 public void onReceive(Context context, Intent intent) {
89 int code = getResultCode();
91 if (code == Activity.RESULT_OK) {
92 String data = getResultData();
94 JSONObject res = new JSONObject();
95 try {
96 res.put("action", action);
97 res.put("token", token);
98 res.put("data", data);
99 } catch (JSONException e) {
100 Log.e(LOGTAG, "Got exception in onReceive handling action " + action, e);
101 return;
102 }
104 GeckoEvent event = GeckoEvent.createBroadcastEvent(responseEvent, res.toString());
105 GeckoAppShell.sendEventToGecko(event);
106 }
107 }
108 };
110 Intent intent = new Intent(action);
111 // OrderedBroadcast.jsm adds its callback ID to the caller's token;
112 // this unwraps that wrapping.
113 if (token != null && token.has("data")) {
114 intent.putExtra("token", token.getString("data"));
115 }
117 mContext.sendOrderedBroadcast(intent,
118 permission,
119 resultReceiver,
120 null,
121 Activity.RESULT_OK,
122 null,
123 null);
124 } catch (JSONException e) {
125 Log.e(LOGTAG, "Got exception in handleMessage handling event " + event, e);
126 return;
127 }
128 }
129 }