Bug 728029 - Make networks objects. r=cjones

--HG--
extra : rebase_source : e21a67df7a93697605e8665916e37396cf0ed4cc
This commit is contained in:
Blake Kaplan 2012-02-16 15:00:53 +01:00
parent 8bbd6b58c6
commit 74ffd06481
2 changed files with 35 additions and 14 deletions

View File

@ -19,7 +19,7 @@
* 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
@ -37,6 +37,14 @@
#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 {
};

View File

@ -795,6 +795,15 @@ var WifiManager = (function() {
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");
@ -806,24 +815,27 @@ function nsWifiWorker() {
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;
}
@ -844,10 +856,9 @@ function nsWifiWorker() {
});
}
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) {
@ -855,10 +866,12 @@ function nsWifiWorker() {
// 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]);
}
});