Bug 736104 - Change object key of configure network, and return array to gaia instead of object. r=mrbkap
authorChuck Lee <chulee@mozilla.com>
Thu, 08 Nov 2012 13:34:12 +0800
changeset 113014 94e8b43c6d235ce9e7754ce73ede35eec704732b
parent 113013 32bbe2ea773ae85e8b4179e5285e8c655234fd2f
child 113015 b1501a0a129016d7e7d4ab8d4d663bc15c1c2b92
push id23847
push userphilringnalda@gmail.com
push dateTue, 13 Nov 2012 05:07:25 +0000
treeherdermozilla-central@1b0226622e94 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs736104
milestone19.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 736104 - Change object key of configure network, and return array to gaia instead of object. r=mrbkap
dom/wifi/WifiWorker.js
--- a/dom/wifi/WifiWorker.js
+++ b/dom/wifi/WifiWorker.js
@@ -1321,16 +1321,83 @@ var WifiManager = (function() {
         manager.loopDetectionCount = 0;
       }
     }
   }
 
   return manager;
 })();
 
+// Get unique key for a network, now the key is created by escape(SSID)+Security.
+// So networks of same SSID but different security mode can be identified.
+function getNetworkKey(network)
+{
+  var ssid = "",
+      encryption = "OPEN";
+
+  if ("capabilities" in network) {
+    // manager network object, represents an AP
+    // object structure
+    // {
+    //   .ssid           : SSID of AP
+    //   .capabilities[] : "WPA-PSK" for WPA-PSK
+    //                     "WPA-EAP" for WPA-EAP
+    //                     "WEP" for WEP
+    //                     "" for OPEN
+    //   other keys
+    // }
+
+    var capabilities = network.capabilities;
+    ssid = network.ssid;
+
+    for (let j = 0; j < capabilities.length; j++) {
+      if (capabilities[j] === "WPA-PSK") {
+        encryption = "WPA-PSK";
+        break;
+      } else if (capabilities[j] === "WPA-EAP") {
+        encryption = "WPA-EAP";
+        break;
+      } else if (capabilities[j] === "WEP") {
+        encryption = "WEP";
+        break;
+      }
+    }
+  } else if ("key_mgmt" in network) {
+    // configure network object, represents a network
+    // object structure
+    // {
+    //   .ssid           : SSID of network, quoted
+    //   .key_mgmt       : Encryption type
+    //                     "WPA-PSK" for WPA-PSK
+    //                     "WPA-EAP" for WPA-EAP
+    //                     "NONE" for WEP/OPEN
+    //   .auth_alg       : Encryption algorithm(WEP mode only)
+    //                     "OPEN_SHARED" for WEP
+    //   other keys
+    // }
+    var key_mgmt = network.key_mgmt,
+        auth_alg = network.auth_alg;
+    ssid = dequote(network.ssid);
+
+    if (key_mgmt == "WPA-PSK") {
+      encryption = "WPA-PSK";
+    } else if (key_mgmt == "WPA-EAP") {
+      encryption = "WPA-EAP";
+    } else if (key_mgmt == "NONE" && auth_alg === "OPEN SHARED") {
+      encryption = "WEP";
+    }
+  }
+
+  // ssid here must be dequoted, and it's safer to esacpe it.
+  // encryption won't be empty and always be assigned one of the followings :
+  // "OPEN"/"WEP"/"WPA-PSK"/"WPA-EAP".
+  // So for a invalid network object, the returned key will be "OPEN"
+  return escape(ssid) + encryption;
+}
+
 function getKeyManagement(flags) {
   var types = [];
   if (!flags)
     return types;
 
   if (/\[WPA2?-PSK/.test(flags))
     types.push("WPA-PSK");
   if (/\[WPA2?-EAP/.test(flags))
@@ -1872,61 +1939,62 @@ function WifiWorker() {
         return;
       }
 
       // Now that we have scan results, there's no more need to continue
       // scanning. Ignore any errors from this command.
       WifiManager.setScanMode("inactive", function() {});
       let lines = r.split("\n");
       // NB: Skip the header line.
-      self.networks = Object.create(null);
+      self.networksArray = [];
       for (let i = 1; i < lines.length; ++i) {
         // bssid / frequency / signal level / flags / ssid
         var match = /([\S]+)\s+([\S]+)\s+([\S]+)\s+(\[[\S]+\])?\s+(.*)/.exec(lines[i]);
 
         if (match && match[5]) {
-          let ssid = match[5];
+          let ssid = match[5],
+              bssid = match[1],
+              signalLevel = match[3],
+              flags = match[4];
 
           // If this is the first time that we've seen this SSID in the scan
           // results, add it to the list along with any other information.
           // Also, we use the highest signal strength that we see.
-          let network = self.networks[ssid];
-          if (!network) {
-            network = self.networks[ssid] =
-              new ScanResult(ssid, match[1], match[4], match[3]);
-
-            if (ssid in self.configuredNetworks) {
-              let known = self.configuredNetworks[ssid];
-              network.known = true;
-
-              if ("identity" in known && known.identity)
-                network.identity = dequote(known.identity);
-
-              // Note: we don't hand out passwords here! The * marks that there
-              // is a password that we're hiding.
-              if (("psk" in known && known.psk) ||
-                  ("password" in known && known.password) ||
-                  ("wep_key0" in known && known.wep_key0)) {
-                network.password = "*";
-              }
+          let network = new ScanResult(ssid, bssid, flags, signalLevel);
+          self.networksArray.push(network);
+
+          let networkKey = getNetworkKey(network);
+          if (networkKey in self.configuredNetworks) {
+            let known = self.configuredNetworks[networkKey];
+            network.known = true;
+
+            if ("identity" in known && known.identity)
+              network.identity = dequote(known.identity);
+
+            // Note: we don't hand out passwords here! The * marks that there
+            // is a password that we're hiding.
+            if (("psk" in known && known.psk) ||
+                ("password" in known && known.password) ||
+                ("wep_key0" in known && known.wep_key0)) {
+              network.password = "*";
             }
           }
 
           if (network.bssid === WifiManager.connectionInfo.bssid)
             network.connected = true;
 
           let signal = calculateSignal(Number(match[3]));
           if (signal > network.relSignalStrength)
             network.relSignalStrength = signal;
         } else if (!match) {
           debug("Match didn't find anything for: " + lines[i]);
         }
       }
 
-      self.wantScanResults.forEach(function(callback) { callback(self.networks) });
+      self.wantScanResults.forEach(function(callback) { callback(self.networksArray) });
       self.wantScanResults = [];
     });
   };
 
   // Read the 'wifi.enabled' setting in order to start with a known
   // value at boot time. The handle() will be called after reading.
   //
   // nsISettingsServiceCallback implementation
@@ -2092,17 +2160,19 @@ WifiWorker.prototype = {
         let network = networks[net];
         if (!network.ssid) {
           delete networks[net]; // TODO support these?
           continue;
         }
 
         if (network.priority && network.priority > this._highestPriority)
           this._highestPriority = network.priority;
-        networks[dequote(network.ssid)] = network;
+
+        let networkKey = getNetworkKey(network);
+        networks[networkKey] = network;
         delete networks[net];
       }
 
       this.configuredNetworks = networks;
       callback(true);
     }).bind(this));
   },
 
@@ -2350,19 +2420,19 @@ WifiWorker.prototype = {
     }
 
     this._reloadConfiguredNetworks((function(ok) {
       if (!ok) {
         this._sendMessage(message, false, "Failed", msg);
         return;
       }
 
-      var networks = {};
-      for (let ssid in this.configuredNetworks) {
-        networks[ssid] = netToDOM(this.configuredNetworks[ssid]);
+      var networks = [];
+      for (let networkKey in this.configuredNetworks) {
+        networks.push(netToDOM(this.configuredNetworks[networkKey]));
       }
 
       this._sendMessage(message, true, networks, msg);
     }).bind(this));
   },
 
   _notifyAfterStateChange: function(success, newState) {
     if (!this._stateRequests.length)
@@ -2482,20 +2552,21 @@ WifiWorker.prototype = {
 
       if (self._highestPriority >= MAX_PRIORITY)
         self._reprioritizeNetworks(selectAndConnect);
       else
         WifiManager.saveConfig(selectAndConnect);
     }
 
     let ssid = privnet.ssid;
+    let networkKey = getNetworkKey(privnet);
     let configured;
 
-    if (ssid in this.configuredNetworks)
-      configured = this.configuredNetworks[ssid];
+    if (networkKey in this.configuredNetworks)
+      configured = this.configuredNetworks[networkKey];
 
     netFromDOM(privnet, configured);
 
     privnet.priority = ++this._highestPriority;
     if (configured) {
       privnet.netId = configured.netId;
       WifiManager.updateNetwork(privnet, (function(ok) {
         if (!ok) {
@@ -2512,38 +2583,40 @@ WifiWorker.prototype = {
       // configuration.
       privnet.disabled = 0;
       WifiManager.addNetwork(privnet, (function(ok) {
         if (!ok) {
           this._sendMessage(message, false, "Network is misconfigured", msg);
           return;
         }
 
-        this.configuredNetworks[ssid] = privnet;
+        this.configuredNetworks[networkKey] = privnet;
         networkReady();
       }).bind(this));
     }
   },
 
   forget: function(msg) {
     const message = "WifiManager:forget:Return";
     let network = msg.data;
     if (!WifiManager.enabled) {
       this._sendMessage(message, false, "Wifi is disabled", msg);
       return;
     }
 
     let ssid = network.ssid;
-    if (!(ssid in this.configuredNetworks)) {
+    let networkKey = getNetworkKey(network);
+
+    if (!(networkKey in this.configuredNetworks)) {
       this._sendMessage(message, false, "Trying to forget an unknown network", msg);
       return;
     }
 
     let self = this;
-    let configured = this.configuredNetworks[ssid];
+    let configured = this.configuredNetworks[networkKey];
     this._reconnectOnDisconnect = (this.currentNetwork &&
                                    (this.currentNetwork.ssid === ssid));
     WifiManager.removeNetwork(configured.netId, function(ok) {
       if (!ok) {
         self._sendMessage(message, false, "Unable to remove the network", msg);
         self._reconnectOnDisconnect = false;
         return;
       }