From b1ed3fab47a74ef6f6780714c35b44d732c43d01 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Thu, 11 Sep 2014 18:31:33 -0400 Subject: [PATCH] Bug 1063120 - Synchronously respond to Gecko requests if running on Gecko thread; r=bnicholson --- mobile/android/base/EventDispatcher.java | 21 +++++++++++++++++---- mobile/android/base/util/ThreadUtils.java | 7 +++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/mobile/android/base/EventDispatcher.java b/mobile/android/base/EventDispatcher.java index 3fd0ef39303b..4afbd92dae20 100644 --- a/mobile/android/base/EventDispatcher.java +++ b/mobile/android/base/EventDispatcher.java @@ -12,6 +12,7 @@ import org.mozilla.gecko.util.GeckoEventListener; import org.mozilla.gecko.util.NativeEventListener; import org.mozilla.gecko.util.NativeJSContainer; import org.mozilla.gecko.util.NativeJSObject; +import org.mozilla.gecko.util.ThreadUtils; import org.json.JSONException; import org.json.JSONObject; @@ -227,12 +228,18 @@ public final class EventDispatcher { @Deprecated private static void sendResponseHelper(String status, JSONObject message, Object response) { try { + final String topic = message.getString("type") + ":Response"; final JSONObject wrapper = new JSONObject(); wrapper.put(GUID, message.getString(GUID)); wrapper.put("status", status); wrapper.put("response", response); - GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent( - message.getString("type") + ":Response", wrapper.toString())); + + if (ThreadUtils.isOnGeckoThread()) { + GeckoAppShell.notifyGeckoObservers(topic, wrapper.toString()); + } else { + GeckoAppShell.sendEventToGecko( + GeckoEvent.createBroadcastEvent(topic, wrapper.toString())); + } } catch (final JSONException e) { Log.e(LOGTAG, "Unable to send response", e); } @@ -265,12 +272,18 @@ public final class EventDispatcher { sent = true; try { + final String topic = type + ":Response"; final JSONObject wrapper = new JSONObject(); wrapper.put(GUID, guid); wrapper.put("status", status); wrapper.put("response", response); - GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(type + ":Response", - wrapper.toString())); + + if (ThreadUtils.isOnGeckoThread()) { + GeckoAppShell.notifyGeckoObservers(topic, wrapper.toString()); + } else { + GeckoAppShell.sendEventToGecko( + GeckoEvent.createBroadcastEvent(topic, wrapper.toString())); + } } catch (final JSONException e) { Log.e(LOGTAG, "Unable to send response for: " + type, e); } diff --git a/mobile/android/base/util/ThreadUtils.java b/mobile/android/base/util/ThreadUtils.java index 0abb3732cd83..629f2c4f83c7 100644 --- a/mobile/android/base/util/ThreadUtils.java +++ b/mobile/android/base/util/ThreadUtils.java @@ -182,6 +182,13 @@ public final class ThreadUtils { } } + public static boolean isOnGeckoThread() { + if (sGeckoThread != null) { + return isOnThread(sGeckoThread); + } + return false; + } + public static boolean isOnUiThread() { return isOnThread(getUiThread()); }