Bug 1330439 - 1. Convert PanelInfoManager events to bundle events; r=sebastian
authorJim Chen <nchen@mozilla.com>
Wed, 25 Jan 2017 18:53:58 -0500
changeset 466660 d3c8b390c953ac092935591d83eaa20fe3b41a33
parent 466659 14bd5683877aca3615acb06b92edb9ec4bd31eab
child 466661 bf42119cf8af9501275c0ab54890c02b60115a22
push id42948
push userbmo:gasolin@mozilla.com
push dateThu, 26 Jan 2017 07:49:21 +0000
reviewerssebastian
bugs1330439
milestone54.0a1
Bug 1330439 - 1. Convert PanelInfoManager events to bundle events; r=sebastian Convert the "HomePanels:Data" event to a GeckoBundle/BundleEventListener event. UI thread event is used because the listener invokes the callback on the UI thread.
mobile/android/base/java/org/mozilla/gecko/home/PanelInfoManager.java
mobile/android/modules/Home.jsm
--- a/mobile/android/base/java/org/mozilla/gecko/home/PanelInfoManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/PanelInfoManager.java
@@ -11,48 +11,50 @@ import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
-import org.mozilla.gecko.util.GeckoEventListener;
+import org.mozilla.gecko.util.BundleEventListener;
+import org.mozilla.gecko.util.EventCallback;
+import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.util.Log;
 import android.util.SparseArray;
 
-public class PanelInfoManager implements GeckoEventListener {
+public class PanelInfoManager implements BundleEventListener {
     private static final String LOGTAG = "GeckoPanelInfoManager";
 
     public class PanelInfo {
         private final String mId;
         private final String mTitle;
-        private final JSONObject mJSONData;
+        private final GeckoBundle mBundleData;
 
-        public PanelInfo(String id, String title, JSONObject jsonData) {
+        public PanelInfo(String id, String title, final GeckoBundle bundleData) {
             mId = id;
             mTitle = title;
-            mJSONData = jsonData;
+            mBundleData = bundleData;
         }
 
         public String getId() {
             return mId;
         }
 
         public String getTitle() {
             return mTitle;
         }
 
         public PanelConfig toPanelConfig() {
             try {
-                return new PanelConfig(mJSONData);
-            } catch (Exception e) {
+                return new PanelConfig(mBundleData.toJSONObject());
+            } catch (final JSONException e) {
                 Log.e(LOGTAG, "Failed to convert PanelInfo to PanelConfig", e);
                 return null;
             }
         }
     }
 
     public interface RequestCallback {
         public void onComplete(List<PanelInfo> panelInfos);
@@ -72,17 +74,17 @@ public class PanelInfoManager implements
      * @param callback onComplete will be called on the UI thread.
      */
     public void requestPanelsById(Set<String> ids, RequestCallback callback) {
         final int requestId = sRequestId.getAndIncrement();
 
         synchronized (sCallbacks) {
             // If there are no pending callbacks, register the event listener.
             if (sCallbacks.size() == 0) {
-                EventDispatcher.getInstance().registerGeckoThreadListener(this,
+                EventDispatcher.getInstance().registerUiThreadListener(this,
                     "HomePanels:Data");
             }
             sCallbacks.put(requestId, callback);
         }
 
         final JSONObject message = new JSONObject();
         try {
             message.put("requestId", requestId);
@@ -110,52 +112,42 @@ public class PanelInfoManager implements
      */
     public void requestAvailablePanels(RequestCallback callback) {
         requestPanelsById(null, callback);
     }
 
     /**
      * Handles "HomePanels:Data" events.
      */
-    @Override
-    public void handleMessage(String event, JSONObject message) {
+    @Override // BundleEventListener
+    public void handleMessage(final String event, final GeckoBundle message,
+                              final EventCallback cb) {
         final ArrayList<PanelInfo> panelInfos = new ArrayList<PanelInfo>();
+        final GeckoBundle[] panels = message.getBundleArray("panels");
 
-        try {
-            final JSONArray panels = message.getJSONArray("panels");
-            final int count = panels.length();
-            for (int i = 0; i < count; i++) {
-                final PanelInfo panelInfo = getPanelInfoFromJSON(panels.getJSONObject(i));
-                panelInfos.add(panelInfo);
-            }
+        for (int i = 0; i < panels.length; i++) {
+            panelInfos.add(getPanelInfoFromBundle(panels[i]));
+        }
 
-            final RequestCallback callback;
-            final int requestId = message.getInt("requestId");
+        final RequestCallback callback;
+        final int requestId = message.getInt("requestId");
 
-            synchronized (sCallbacks) {
-                callback = sCallbacks.get(requestId);
-                sCallbacks.delete(requestId);
+        synchronized (sCallbacks) {
+            callback = sCallbacks.get(requestId);
+            sCallbacks.delete(requestId);
 
-                // Unregister the event listener if there are no more pending callbacks.
-                if (sCallbacks.size() == 0) {
-                    EventDispatcher.getInstance().unregisterGeckoThreadListener(this,
-                        "HomePanels:Data");
-                }
+            // Unregister the event listener if there are no more pending callbacks.
+            if (sCallbacks.size() == 0) {
+                EventDispatcher.getInstance().unregisterUiThreadListener(this,
+                    "HomePanels:Data");
             }
+        }
 
-            ThreadUtils.postToUiThread(new Runnable() {
-                @Override
-                public void run() {
-                    callback.onComplete(panelInfos);
-                }
-            });
-        } catch (JSONException e) {
-            Log.e(LOGTAG, "Exception handling " + event + " message", e);
-        }
+        callback.onComplete(panelInfos);
     }
 
-    private PanelInfo getPanelInfoFromJSON(JSONObject jsonPanelInfo) throws JSONException {
-        final String id = jsonPanelInfo.getString("id");
-        final String title = jsonPanelInfo.getString("title");
+    private PanelInfo getPanelInfoFromBundle(final GeckoBundle bundlePanelInfo) {
+        final String id = bundlePanelInfo.getString("id");
+        final String title = bundlePanelInfo.getString("title");
 
-        return new PanelInfo(id, title, jsonPanelInfo);
+        return new PanelInfo(id, title, bundlePanelInfo);
     }
 }
--- a/mobile/android/modules/Home.jsm
+++ b/mobile/android/modules/Home.jsm
@@ -209,17 +209,17 @@ var HomePanels = (function () {
           try {
             panels.push(_generatePanel(id));
           } catch(e) {
             Cu.reportError("Home.panels: Invalid options, panel.id = " + id + ": " + e);
           }
         }
       }
 
-      Messaging.sendRequest({
+      EventDispatcher.instance.sendRequest({
         type: "HomePanels:Data",
         panels: panels,
         requestId: requestId
       });
     },
 
     "HomePanels:Authenticate": function handlePanelsAuthenticate(id) {
       // Generate panel options to get auth handler.