Bug 1322586 - 1. Add "always listen" option for GeckoViewHandler; r=esawin
authorJim Chen <nchen@mozilla.com>
Thu, 20 Jul 2017 17:52:13 -0400
changeset 418781 446be06fd42e76142716c5ac62b38efbb2d7ce44
parent 418780 aa10d14239126225de73ea948a0cefe71e4f283a
child 418782 1ca88db9fe80289d821b960ae42f1b5d91833af5
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1322586
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1322586 - 1. Add "always listen" option for GeckoViewHandler; r=esawin It would be nice to reuse GeckoViewHandler for delegates too (e.g. for the new PermissionDelegate), but delegates need to always listen to their events. This patch adds an "always listen" option for GeckoViewHandler that still keeps track of the current listener/delegate instance, but registers for all events at initialization rather than only when there is a listener. MozReview-Commit-ID: JtskpZMy9T4
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoViewHandler.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoViewHandler.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoViewHandler.java
@@ -15,45 +15,66 @@ import android.util.Log;
 
 /* package */ abstract class GeckoViewHandler<Listener>
     implements BundleEventListener {
 
     private static final String LOGTAG = "GeckoViewHandler";
     private static final boolean DEBUG = false;
 
     private Listener mListener;
+    private final boolean mAlwaysListen;
     private final String mModuleName;
     private final String[] mEvents;
 
+
     GeckoViewHandler(final String module, final GeckoView view,
                      final String[] events) {
+        this(module, view, events, /* alwaysListen */ false);
+    }
+
+    GeckoViewHandler(final String module, final GeckoView view,
+                     final String[] events, final boolean alwaysListen) {
+        mAlwaysListen = alwaysListen;
         mModuleName = module;
         mEvents = events;
+
+        if (alwaysListen) {
+            register(view.getEventDispatcher());
+        }
     }
 
     public Listener getListener() {
         return mListener;
     }
 
     public void setListener(final Listener listener, final GeckoView view) {
         final EventDispatcher eventDispatcher = view.getEventDispatcher();
+        if (mListener == listener) {
+            return;
+        }
 
-        if (mListener != null && mListener != listener) {
-            final GeckoBundle msg = new GeckoBundle(1);
-            msg.putString("module", mModuleName);
-            eventDispatcher.dispatch("GeckoView:Unregister", msg);
-            eventDispatcher.unregisterUiThreadListener(this, mEvents);
+        if (!mAlwaysListen && mListener != null) {
+            unregister(eventDispatcher);
         }
 
         mListener = listener;
 
-        if (mListener == null) {
-            return;
+        if (!mAlwaysListen && mListener != null) {
+            register(eventDispatcher);
         }
+    }
 
+    private void unregister(final EventDispatcher eventDispatcher) {
+        final GeckoBundle msg = new GeckoBundle(1);
+        msg.putString("module", mModuleName);
+        eventDispatcher.dispatch("GeckoView:Unregister", msg);
+        eventDispatcher.unregisterUiThreadListener(this, mEvents);
+    }
+
+    private void register(final EventDispatcher eventDispatcher) {
         final GeckoBundle msg = new GeckoBundle(1);
         msg.putString("module", mModuleName);
         eventDispatcher.dispatch("GeckoView:Register", msg);
         eventDispatcher.registerUiThreadListener(this, mEvents);
     }
 
     @Override
     public void handleMessage(final String event, final GeckoBundle message,