Bug 1333588 - Convert PageActions events to bundle events; r=sebastian
authorJim Chen <nchen@mozilla.com>
Wed, 25 Jan 2017 18:57:31 -0500
changeset 466666 45316394ec1d7a06fa15404ba21c67f1ca4027d6
parent 466665 639e6a9174f466b5f3e0ed894d15dfb8cc3081af
child 466667 edeef4fcdb7b4dfb43a1ae9d31a8f19a46c8e9fc
push id42948
push userbmo:gasolin@mozilla.com
push dateThu, 26 Jan 2017 07:49:21 +0000
reviewerssebastian
bugs1333588
milestone54.0a1
Bug 1333588 - Convert PageActions events to bundle events; r=sebastian Convert events used in PageActions to bundle events. UI thread events are used because the listeners require the UI thread. The observer notifications from Java to Gecko are converted to events as well.
mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
mobile/android/modules/PageActions.jsm
--- a/mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
@@ -4,19 +4,19 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.toolbar;
 
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.util.ResourceDrawableUtils;
+import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
-import org.mozilla.gecko.util.NativeEventListener;
-import org.mozilla.gecko.util.NativeJSObject;
+import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.widget.GeckoPopupMenu;
 
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.util.AttributeSet;
@@ -27,17 +27,17 @@ import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 
 import java.util.Iterator;
 import java.util.List;
 import java.util.UUID;
 import java.util.ArrayList;
 
-public class PageActionLayout extends LinearLayout implements NativeEventListener,
+public class PageActionLayout extends LinearLayout implements BundleEventListener,
                                                               View.OnClickListener,
                                                               View.OnLongClickListener {
     private static final String MENU_BUTTON_KEY = "MENU_BUTTON_KEY";
     private static final int DEFAULT_PAGE_ACTIONS_SHOWN = 2;
 
     private final Context mContext;
     private final LinearLayout mLayout;
     private final List<PageAction> mPageActionList;
@@ -56,24 +56,24 @@ public class PageActionLayout extends Li
         setNumberShown(DEFAULT_PAGE_ACTIONS_SHOWN);
         refreshPageActionIcons();
     }
 
     @Override
     protected void onAttachedToWindow() {
         super.onAttachedToWindow();
 
-        EventDispatcher.getInstance().registerGeckoThreadListener(this,
+        EventDispatcher.getInstance().registerUiThreadListener(this,
             "PageActions:Add",
             "PageActions:Remove");
     }
 
     @Override
     protected void onDetachedFromWindow() {
-        EventDispatcher.getInstance().unregisterGeckoThreadListener(this,
+        EventDispatcher.getInstance().unregisterUiThreadListener(this,
             "PageActions:Add",
             "PageActions:Remove");
 
         super.onDetachedFromWindow();
     }
 
     private void setNumberShown(int count) {
         ThreadUtils.assertOnUiThread();
@@ -82,54 +82,46 @@ public class PageActionLayout extends Li
 
         for (int index = 0; index < count; index++) {
             if ((getChildCount() - 1) < index) {
                 mLayout.addView(createImageButton());
             }
         }
     }
 
-    @Override
-    public void handleMessage(final String event, final NativeJSObject message, final EventCallback callback) {
-        // NativeJSObject cannot be used off of the Gecko thread, so convert it to a Bundle.
-        final Bundle bundle = message.toBundle();
-
-        ThreadUtils.postToUiThread(new Runnable() {
-            @Override
-            public void run() {
-                handleUiMessage(event, bundle);
-            }
-        });
-    }
-
-    private void handleUiMessage(final String event, final Bundle message) {
+    @Override // BundleEventListener
+    public void handleMessage(final String event, final GeckoBundle message,
+                              final EventCallback callback) {
         ThreadUtils.assertOnUiThread();
 
-        if (event.equals("PageActions:Add")) {
+        if ("PageActions:Add".equals(event)) {
             final String id = message.getString("id");
             final String title = message.getString("title");
             final String imageURL = message.getString("icon");
             final boolean important = message.getBoolean("important");
 
             addPageAction(id, title, imageURL, new OnPageActionClickListeners() {
                 @Override
-                public void onClick(String id) {
-                    GeckoAppShell.notifyObservers("PageActions:Clicked", id);
+                public void onClick(final String id) {
+                    final GeckoBundle data = new GeckoBundle(1);
+                    data.putString("id", id);
+                    EventDispatcher.getInstance().dispatch("PageActions:Clicked", data);
                 }
 
                 @Override
                 public boolean onLongClick(String id) {
-                    GeckoAppShell.notifyObservers("PageActions:LongClicked", id);
+                    final GeckoBundle data = new GeckoBundle(1);
+                    data.putString("id", id);
+                    EventDispatcher.getInstance().dispatch("PageActions:LongClicked", data);
                     return true;
                 }
             }, important);
-        } else if (event.equals("PageActions:Remove")) {
-            final String id = message.getString("id");
 
-            removePageAction(id);
+        } else if ("PageActions:Remove".equals(event)) {
+            removePageAction(message.getString("id"));
         }
     }
 
     private void addPageAction(final String id, final String title, final String imageData,
             final OnPageActionClickListeners onPageActionClickListeners, boolean important) {
         ThreadUtils.assertOnUiThread();
 
         final PageAction pageAction = new PageAction(id, title, null, onPageActionClickListeners, important);
--- a/mobile/android/modules/PageActions.jsm
+++ b/mobile/android/modules/PageActions.jsm
@@ -34,36 +34,40 @@ function resolveGeckoURI(aURI) {
 var PageActions = {
   _items: { },
 
   _inited: false,
 
   _maybeInit: function() {
     if (!this._inited && Object.keys(this._items).length > 0) {
       this._inited = true;
-      Services.obs.addObserver(this, "PageActions:Clicked", false);
-      Services.obs.addObserver(this, "PageActions:LongClicked", false);
+      EventDispatcher.instance.registerListener(this, [
+        "PageActions:Clicked",
+        "PageActions:LongClicked",
+      ]);
     }
   },
 
   _maybeUninit: function() {
     if (this._inited && Object.keys(this._items).length == 0) {
       this._inited = false;
-      Services.obs.removeObserver(this, "PageActions:Clicked");
-      Services.obs.removeObserver(this, "PageActions:LongClicked");
+      EventDispatcher.instance.unregisterListener(this, [
+        "PageActions:Clicked",
+        "PageActions:LongClicked",
+      ]);
     }
   },
 
-  observe: function(aSubject, aTopic, aData) {
-    let item = this._items[aData];
-    if (aTopic == "PageActions:Clicked") {
+  onEvent: function(event, data, callback) {
+    let item = this._items[data.id];
+    if (event == "PageActions:Clicked") {
       if (item.clickCallback) {
         item.clickCallback();
       }
-    } else if (aTopic == "PageActions:LongClicked") {
+    } else if (event == "PageActions:LongClicked") {
       if (item.longClickCallback) {
         item.longClickCallback();
       }
     }
   },
 
   isShown: function(id) {
     return !!this._items[id];
@@ -74,17 +78,17 @@ var PageActions = {
     if (item && item.clickCallback) {
       item.clickCallback();
     }
   },
 
   add: function(aOptions) {
     let id = aOptions.id || uuidgen.generateUUID().toString()
 
-    Messaging.sendRequest({
+    EventDispatcher.instance.sendRequest({
       type: "PageActions:Add",
       id: id,
       title: aOptions.title,
       icon: resolveGeckoURI(aOptions.icon),
       important: "important" in aOptions ? aOptions.important : false
     });
 
     this._items[id] = {};
@@ -97,17 +101,17 @@ var PageActions = {
       this._items[id].longClickCallback = aOptions.longClickCallback;
     }
 
     this._maybeInit();
     return id;
   },
 
   remove: function(id) {
-    Messaging.sendRequest({
+    EventDispatcher.instance.sendRequest({
       type: "PageActions:Remove",
       id: id
     });
 
     delete this._items[id];
     this._maybeUninit();
   }
 }