Bug 728029 - Make networks objects. r=cjones
authorBlake Kaplan <mrbkap@gmail.com>
Thu, 16 Feb 2012 15:00:53 +0100
changeset 87086 67b6c09fb30fbc5c234ae5222922d5d3aeca7165
parent 87085 5e48ff8f8d5a59e360ba5e1533ab1c12949aac67
child 87087 beb303c4657fd5fdbed363db66807d18c604a65c
push id6195
push usermrbkap@mozilla.com
push dateFri, 17 Feb 2012 12:50:36 +0000
treeherdermozilla-inbound@dbcd8d8bed76 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs728029
milestone13.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 728029 - Make networks objects. r=cjones
dom/wifi/nsIWifi.idl
dom/wifi/nsWifiWorker.js
--- a/dom/wifi/nsIWifi.idl
+++ b/dom/wifi/nsIWifi.idl
@@ -14,17 +14,17 @@
  * The Original Code is Telephony.
  *
  * The Initial Developer of the Original Code is
  * the Mozilla Foundation.
  * Portions created by the Initial Developer are Copyright (C) 2011
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
- *  Philipp von Weitershausen <philipp@weitershausen.de>
+ *  Blake Kaplan <mrbkap@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -32,11 +32,19 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsISupports.idl"
 
+[scriptable, uuid(14c815f0-e9db-41d4-a15e-f3e69140f83b)]
+interface nsIWifiNetwork : nsISupports {
+    readonly attribute DOMString ssid; // can be null
+    readonly attribute DOMString bssid; // can be null
+    readonly attribute DOMString flags; // TODO make this be real flags instead of a string
+    readonly attribute long signal;
+};
+
 [scriptable, uuid(9DCE05BF-659C-4427-A050-0EAC3BB6C1C0)]
 interface nsIWifi : nsISupports {
 };
--- a/dom/wifi/nsWifiWorker.js
+++ b/dom/wifi/nsWifiWorker.js
@@ -790,45 +790,57 @@ var WifiManager = (function() {
   manager.disableNetwork = function(netId, callback) {
     disableNetworkCommand(netId, callback);
   }
   manager.getMacAddress = getMacAddressCommand;
   manager.getScanResults = scanResultsCommand;
   return manager;
 })();
 
+function WifiNetwork(ssid, bssid, flags, signal) {
+  this.ssid = ssid;
+  this.bssid = bssid;
+  this.flags = flags;
+  this.signal = Number(signal);
+}
+
+WifiNetwork.prototype.QueryInterface = XPCOMUtils.generateQI([Ci.nsIWifiNetwork]);
+
 function nsWifiWorker() {
   WifiManager.onsupplicantconnection = function() {
     debug("Connected to supplicant");
     WifiManager.getMacAddress(function (mac) {
       debug("Got mac: " + mac);
     });
   }
   WifiManager.onsupplicantlost = function() {
     debug("Couldn't connect to supplicant");
   }
 
-  var state;
+  var self = this;
+
+  this.state = null;
+  this.networks = Object.create(null);
   WifiManager.onstatechange = function() {
-    debug("State change: " + state + " -> " + this.state);
-    if (state === "SCANNING" && this.state === "INACTIVE") {
+    debug("State change: " + self.state + " -> " + this.state);
+    if (self.state === "SCANNING" && this.state === "INACTIVE") {
       // We're not trying to connect so try to find an open Mozilla network.
       // TODO Remove me in favor of UI and a way to select a network.
 
       debug("Haven't connected to a network, trying a default (for now)");
       var name = "Mozilla";
-      var net = networks[name];
-      if (net && (net[1] && net[1] !== "[IBSS]")) {
+      var net = self.networks[name];
+      if (net && (net.flags && net.flags !== "[IBSS]")) {
         debug("Network Mozilla exists, but is encrypted");
         net = null;
       }
       if (!net) {
         name = "Mozilla Guest";
-        net = networks[name];
-        if (!net || (net[1] && net[1] !== "[IBSS]")) {
+        net = self.networks[name];
+        if (!net || (net.flags && net.flags !== "[IBSS]")) {
           debug("Network Mozilla Guest doesn't exist or is encrypted");
           return;
         }
       }
 
       var config = Object.create(null);
       config["ssid"] = '"' + name + '"';
       config["key_mgmt"] = "NONE";
@@ -839,31 +851,32 @@ function nsWifiWorker() {
         }
 
         WifiManager.enableNetwork(config.netId, false, function(ok) {
           debug((ok ? "Successfully enabled " : "Didn't enable ") + name);
         });
       });
     }
 
-    state = this.state;
+    self.state = this.state;
   }
 
-  var networks = Object.create(null);
   WifiManager.onscanresultsavailable = function() {
     debug("Scan results are available! Asking for them.");
     WifiManager.getScanResults(function(r) {
       let lines = r.split("\n");
       // NB: Skip the header line.
       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)
-          networks[match[5]] = [match[1], match[4]];
-        else
+        var match = /([\S]+)\s+([\S]+)\s+([\S]+)\s+(\[[\S]+\])?\s+(.*)/.exec(lines[i]);
+
+        // TODO Choose bssid based on strength?
+        if (match && match[5])
+          self.networks[match[5]] = new WifiNetwork(match[5], match[1], match[4], match[3]);
+        else if (!match)
           debug("Match didn't find anything for: " + lines[i]);
       }
     });
   }
 
   WifiManager.setWifiEnabled(true, function (ok) {
       if (ok === 0)
         WifiManager.start();