Bug 1323576 - Convert GeckoNetworkManager events to bundle events; r=sebastian
authorJim Chen <nchen@mozilla.com>
Sun, 18 Dec 2016 08:10:18 -0500
changeset 450798 9c1ee7cff4a8cbb08127fbe78b11d975d9e77adc
parent 450797 4f5d03fe0e90e3041e694d63eb0c6a76a6621de5
child 450799 cb27c1609d4dd48372942483a21879502fd58c0c
push id38944
push userbmo:leftysolara@gmail.com
push dateSun, 18 Dec 2016 16:01:15 +0000
reviewerssebastian
bugs1323576
milestone53.0a1
Bug 1323576 - Convert GeckoNetworkManager events to bundle events; r=sebastian Convert "Wifi:Enable" and "Wifi:GetIPAddress" events to GeckoBundle events. Use the UI thread because we do things like starting activities and using system services, which are best done on the UI thread.
dom/presentation/PresentationNetworkHelper.js
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoNetworkManager.java
mobile/android/modules/NetErrorHelper.jsm
--- a/dom/presentation/PresentationNetworkHelper.js
+++ b/dom/presentation/PresentationNetworkHelper.js
@@ -14,15 +14,15 @@ const NETWORKHELPER_CID = Components.ID(
 
 function PresentationNetworkHelper() {}
 
 PresentationNetworkHelper.prototype = {
   classID: NETWORKHELPER_CID,
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationNetworkHelper]),
 
   getWifiIPAddress: function(aListener) {
-    Messaging.sendRequestForResult({type: "Wifi:GetIPAddress"})
+    EventDispatcher.instance.sendRequestForResult({type: "Wifi:GetIPAddress"})
              .then(result => aListener.onGetWifiIPAddress(result),
                    err => aListener.onError(err));
   }
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PresentationNetworkHelper]);
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoNetworkManager.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoNetworkManager.java
@@ -2,19 +2,19 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.annotation.JNITarget;
 import org.mozilla.gecko.annotation.WrapForJNI;
-import org.mozilla.gecko.util.NativeEventListener;
-import org.mozilla.gecko.util.NativeJSObject;
+import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
+import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.NetworkUtils;
 import org.mozilla.gecko.util.NetworkUtils.ConnectionSubType;
 import org.mozilla.gecko.util.NetworkUtils.ConnectionType;
 import org.mozilla.gecko.util.NetworkUtils.NetworkStatus;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -37,17 +37,17 @@ import android.util.Log;
  * connection is due to some security concerns. In short, we don't want to expose exact network type,
  * especially the cellular network type.
  *
  * Specific mobile subtypes are mapped to general 2G, 3G and 4G buckets.
  *
  * Logic is implemented as a state machine, so see the transition matrix to figure out what happens when.
  * This class depends on access to the context, so only use after GeckoAppShell has been initialized.
  */
-public class GeckoNetworkManager extends BroadcastReceiver implements NativeEventListener {
+public class GeckoNetworkManager extends BroadcastReceiver implements BundleEventListener {
     private static final String LOGTAG = "GeckoNetworkManager";
 
     private static final String LINK_DATA_CHANGED = "changed";
 
     private static GeckoNetworkManager instance;
 
     // We hackishly (yet harmlessly, in this case) keep a Context reference passed in via the start method.
     // See context handling notes in handleManagerEvent, and Bug 1277333.
@@ -84,24 +84,24 @@ public class GeckoNetworkManager extends
     private NetworkStatus previousNetworkStatus = NetworkStatus.UNKNOWN;
 
     private enum InfoType {
         MCC,
         MNC
     }
 
     private GeckoNetworkManager() {
-        EventDispatcher.getInstance().registerGeckoThreadListener(this,
+        EventDispatcher.getInstance().registerUiThreadListener(this,
                 "Wifi:Enable",
                 "Wifi:GetIPAddress");
     }
 
     private void onDestroy() {
         handleManagerEvent(ManagerEvent.stop);
-        EventDispatcher.getInstance().unregisterGeckoThreadListener(this,
+        EventDispatcher.getInstance().unregisterUiThreadListener(this,
                 "Wifi:Enable",
                 "Wifi:GetIPAddress");
     }
 
     public static GeckoNetworkManager getInstance() {
         if (instance == null) {
             instance = new GeckoNetworkManager();
         }
@@ -390,36 +390,39 @@ public class GeckoNetworkManager extends
         } catch (Exception ex) {
             // getDhcpInfo() is not documented to require any permissions, but on some devices
             // requires android.permission.ACCESS_WIFI_STATE. Just catch the generic exception
             // here and returning 0. Not logging because this could be noisy.
             return 0;
         }
     }
 
-    @Override
+    @Override // BundleEventListener
     /**
      * Handles native messages, not part of the state machine flow.
      */
-    public void handleMessage(final String event, final NativeJSObject message,
+    public void handleMessage(final String event, final GeckoBundle message,
                               final EventCallback callback) {
         final Context applicationContext = GeckoAppShell.getApplicationContext();
         switch (event) {
             case "Wifi:Enable":
-                final WifiManager mgr = (WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);
+                final WifiManager mgr = (WifiManager)
+                        applicationContext.getSystemService(Context.WIFI_SERVICE);
 
                 if (!mgr.isWifiEnabled()) {
                     mgr.setWifiEnabled(true);
-                } else {
-                    // If Wifi is enabled, maybe you need to select a network
-                    Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
-                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                    applicationContext.startActivity(intent);
+                    break;
                 }
+
+                // If Wifi is enabled, maybe you need to select a network
+                Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
+                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                applicationContext.startActivity(intent);
                 break;
+
             case "Wifi:GetIPAddress":
                 getWifiIPAddress(callback);
                 break;
         }
     }
 
     // This function only works for IPv4; not part of the state machine flow.
     private void getWifiIPAddress(final EventCallback callback) {
--- a/mobile/android/modules/NetErrorHelper.jsm
+++ b/mobile/android/modules/NetErrorHelper.jsm
@@ -138,17 +138,17 @@ handlers.wifi = {
     UITelemetry.addEvent("neterror.1", "button", null, "wifitoggle");
     // Show indeterminate progress while we wait for the network.
     node.disabled = true;
     node.classList.add("inProgress");
 
     this.node = Cu.getWeakReference(node);
     Services.obs.addObserver(this, "network:link-status-changed", true);
 
-    Messaging.sendRequest({
+    EventDispatcher.instance.sendRequest({
       type: "Wifi:Enable"
     });
   },
 
   observe: function(subject, topic, data) {
     let node = this.node.get();
     if (!node) {
       return;