Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 791392 - Expose configured networks to content. r=vchang f=kaze
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbkap committed Sep 27, 2012
1 parent baf6584 commit b2e1fa0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
19 changes: 19 additions & 0 deletions dom/wifi/DOMWifiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ DOMWifiManager.prototype = {
this._lastConnectionInfo = null;

const messages = ["WifiManager:getNetworks:Return:OK", "WifiManager:getNetworks:Return:NO",
"WifiManager:getKnownNetworks:Return:OK", "WifiManager:getKnownNetworks:Return:NO",
"WifiManager:associate:Return:OK", "WifiManager:associate:Return:NO",
"WifiManager:forget:Return:OK", "WifiManager:forget:Return:NO",
"WifiManager:wps:Return:OK", "WifiManager:wps:Return:NO",
Expand Down Expand Up @@ -137,6 +138,16 @@ DOMWifiManager.prototype = {
Services.DOMRequest.fireError(request, "Unable to scan for networks");
break;
case "WifiManager:getKnownNetworks:Return:OK":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data));
break;
case "WifiManager:getKnownNetworks:Return:NO":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireError(request, "Unable to get known networks");
break;
case "WifiManager:associate:Return:OK":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireSuccess(request, true);
Expand Down Expand Up @@ -292,6 +303,14 @@ DOMWifiManager.prototype = {
return request;
},
getKnownNetworks: function nsIDOMWifiManager_getKnownNetworks() {
if (!this._hasPrivileges)
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
var request = this.createRequest();
this._sendMessageForRequest("WifiManager:getKnownNetworks", null, request);
return request;
},
associate: function nsIDOMWifiManager_associate(network) {
if (!this._hasPrivileges)
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
Expand Down
86 changes: 73 additions & 13 deletions dom/wifi/WifiWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,10 +1279,34 @@ function calculateSignal(strength) {
return Math.floor(((strength - MIN_RSSI) / (MAX_RSSI - MIN_RSSI)) * 100);
}

function ScanResult(ssid, bssid, flags, signal) {
function Network(ssid, capabilities, password) {
this.ssid = ssid;
this.capabilities = capabilities;

if (typeof password !== "undefined")
this.password = password;
// TODO connected here as well?

this.__exposedProps__ = Network.api;
}

Network.api = {
ssid: "r",
capabilities: "r",
known: "r",

password: "rw",
keyManagement: "rw",
psk: "rw",
identity: "rw",
wep: "rw"
};

// Note: We never use ScanResult.prototype, so the fact that it's unrelated to
// Network.prototype is OK.
function ScanResult(ssid, bssid, flags, signal) {
Network.call(this, ssid, getKeyManagement(flags));
this.bssid = bssid;
this.capabilities = getKeyManagement(flags);
this.signalStrength = signal;
this.relSignalStrength = calculateSignal(Number(signal));

Expand All @@ -1292,20 +1316,16 @@ function ScanResult(ssid, bssid, flags, signal) {
// XXX This should probably live in the DOM-facing side, but it's hard to do
// there, so we stick this here.
ScanResult.api = {
ssid: "r",
bssid: "r",
capabilities: "r",
signalStrength: "r",
relSignalStrength: "r",
connected: "r",

keyManagement: "rw",
psk: "rw",
identity: "rw",
password: "rw",
wep: "rw"
connected: "r"
};

for (let i in Network.api) {
ScanResult.api[i] = Network.api[i];
}

function quote(s) {
return '"' + s + '"';
}
Expand Down Expand Up @@ -1379,7 +1399,7 @@ function WifiWorker() {

this._mm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
.getService(Ci.nsIMessageListenerManager);
const messages = ["WifiManager:getNetworks",
const messages = ["WifiManager:getNetworks", "WifiManager:getKnownNetworks",
"WifiManager:associate", "WifiManager:forget",
"WifiManager:wps", "WifiManager:getState",
"WifiManager:setPowerSavingMode",
Expand Down Expand Up @@ -1420,7 +1440,22 @@ function WifiWorker() {
// Given a connection status network, takes a network from
// self.configuredNetworks and prepares it for the DOM.
netToDOM = function(net) {
var pub = { ssid: dequote(net.ssid) };
var ssid = dequote(net.ssid);
var capabilities = (net.key_mgmt === "NONE" && net.wep_key0)
? ["WEP"]
: (net.key_mgmt && net.key_mgmt !== "NONE")
? [net.key_mgmt]
: [];
var password;
if (("psk" in net && net.psk) ||
("password" in net && net.password) ||
("wep_key0" in net && net.wep_key0)) {
password = "*";
}

var pub = new Network(ssid, capabilities, password);
if (net.identity)
pub.identity = dequote(net.identity);
if (net.netId)
pub.known = true;
return pub;
Expand Down Expand Up @@ -1997,6 +2032,9 @@ WifiWorker.prototype = {
case "WifiManager:getNetworks":
this.getNetworks(msg);
break;
case "WifiManager:getKnownNetworks":
this.getKnownNetworks(msg);
break;
case "WifiManager:associate":
this.associate(msg);
break;
Expand Down Expand Up @@ -2072,6 +2110,28 @@ WifiWorker.prototype = {
}).bind(this));
},

getKnownNetworks: function(msg) {
const message = "WifiManager:getKnownNetworks:Return";
if (!WifiManager.enabled) {
this._sendMessage(message, false, "Wifi is disabled", msg);
return;
}

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]);
}

this._sendMessage(message, true, networks, msg);
}).bind(this));
},

_notifyAfterStateChange: function(success, newState) {
if (!this._stateRequests.length)
return;
Expand Down
14 changes: 11 additions & 3 deletions dom/wifi/nsIWifi.idl
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@ interface nsIWifi : nsISupports
void shutdown();
};

[scriptable, uuid(65286912-141e-4aec-ba48-cb7d74851fd8)]
[scriptable, uuid(caa76ee3-8ffe-4ea5-bc59-3b53a9df0d07)]
interface nsIDOMWifiManager : nsISupports
{
/**
* Returns the list of currently available networks as well as the list of
* currently configured networks.
* Returns the list of currently available networks.
* onsuccess: We have obtained the current list of networks. request.value
* is an object whose property names are SSIDs and values are
* network objects.
* onerror: We were unable to obtain a list of property names.
*/
nsIDOMDOMRequest getNetworks();

/**
* Returns the list of networks known to the system that will be
* automatically connected to if they're in range.
* onsuccess: request.value is an object whose property names are
* SSIDs and values are network objects.
* onerror: We were unable to obtain a list of known networks.
*/
nsIDOMDOMRequest getKnownNetworks();

/**
* Takes one of the networks returned from getNetworks and tries to
* connect to it.
Expand Down

0 comments on commit b2e1fa0

Please sign in to comment.