Bug 1024579 - Create a sync getNetworkInformation and make getNetworkStatus use it. r=nsm
authorGuillermo López <willyaranda@mozilla-hispano.org>
Mon, 16 Jun 2014 00:24:05 +0200
changeset 188942 0f60e633f247a026be70b64733b1adc3912561a7
parent 188941 bac4e07e1dd2efe4d063f0562c059422215e1f56
child 188943 9d948f572d49cfb7821a4205eff43fd7baaaea38
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersnsm
bugs1024579
milestone33.0a1
Bug 1024579 - Create a sync getNetworkInformation and make getNetworkStatus use it. r=nsm
dom/push/src/PushService.jsm
--- a/dom/push/src/PushService.jsm
+++ b/dom/push/src/PushService.jsm
@@ -1497,25 +1497,21 @@ this.PushService = {
    */
   onStopListening: function(aServ, aStatus) {
     debug("UDP Server socket was shutdown. Status: " + aStatus);
     this._udpServer = undefined;
     this._beginWSSetup();
   },
 
   /**
-   * Get mobile network information to decide if the client is capable of being
-   * woken up by UDP (which currently just means having an mcc and mnc along
-   * with an IP).
+   * Returns information about MCC-MNC and the IP of the current connection.
    */
-  _getNetworkState: function(callback) {
-    if (typeof callback !== 'function') {
-      throw new Error("No callback method. Aborting push agent !");
-    }
-    debug("getNetworkState()");
+  _getNetworkInformation: function() {
+    debug("getNetworkInformation()");
+
     try {
       if (!prefs.get("udp.wakeupEnabled")) {
         debug("UDP support disabled, we do not send any carrier info");
         throw new Error("UDP disabled");
       }
 
       let nm = Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
       if (nm.active && nm.active.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE) {
@@ -1529,38 +1525,62 @@ this.PushService = {
         let iccInfo = icc.getIccInfo(clientId);
         if (iccInfo) {
           debug("Running on mobile data");
 
           let ips = {};
           let prefixLengths = {};
           nm.active.getAddresses(ips, prefixLengths);
 
-          this._getMobileNetworkId(function(netid) {
-            debug("Recovered netID = " + netid);
-            callback({
-              mcc: iccInfo.mcc,
-              mnc: iccInfo.mnc,
-              ip:  ips.value[0],
-              netid: netid
-            });
-          });
-          return;
+          return {
+            mcc: iccInfo.mcc,
+            mnc: iccInfo.mnc,
+            ip:  ips.value[0]
+          }
         }
       }
     } catch (e) {
       debug("Error recovering mobile network information: " + e);
     }
 
     debug("Running on wifi");
-    callback({
+    return {
       mcc: 0,
       mnc: 0,
       ip: undefined
-    });
+    };
+  },
+
+  /**
+   * Get mobile network information to decide if the client is capable of being
+   * woken up by UDP (which currently just means having an mcc and mnc along
+   * with an IP, and optionally a netid).
+   */
+  _getNetworkState: function(callback) {
+    debug("getNetworkState()");
+
+    if (typeof callback !== 'function') {
+      throw new Error("No callback method. Aborting push agent !");
+    }
+
+    var networkInfo = this._getNetworkInformation();
+
+    if (networkInfo.ip) {
+      this._getMobileNetworkId(function(netid) {
+        debug("Recovered netID = " + netid);
+        callback({
+          mcc: networkInfo.mcc,
+          mnc: networkInfo.mnc,
+          ip:  networkInfo.ip,
+          netid: netid
+        });
+      });
+    } else {
+      callback(networkInfo);
+    }
   },
 
   // utility function used to add/remove observers in init() and shutdown()
   _getNetworkStateChangeEventName: function() {
     try {
       Cc["@mozilla.org/network/manager;1"].getService(Ci.nsINetworkManager);
       return "network-active-changed";
     } catch (e) {