2012-03-14 13:39:15 -07:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-03-20 08:59:38 -07:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
2012-03-14 13:39:15 -07:00
|
|
|
|
|
|
|
const DEBUG = true; // set to false to suppress debug messages
|
|
|
|
|
|
|
|
const DOMWIFIMANAGER_CONTRACTID = "@mozilla.org/wifimanager;1";
|
|
|
|
const DOMWIFIMANAGER_CID = Components.ID("{2cf775a7-1837-410c-9e26-323c42e076da}");
|
|
|
|
|
2012-03-16 09:51:05 -07:00
|
|
|
function DOMWifiManager() {
|
2012-03-14 13:39:15 -07:00
|
|
|
}
|
|
|
|
|
2012-03-16 09:51:05 -07:00
|
|
|
DOMWifiManager.prototype = {
|
2012-03-20 08:59:38 -07:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2012-03-14 13:39:15 -07:00
|
|
|
classID: DOMWIFIMANAGER_CID,
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: DOMWIFIMANAGER_CID,
|
|
|
|
contractID: DOMWIFIMANAGER_CONTRACTID,
|
2012-03-16 09:51:05 -07:00
|
|
|
classDescription: "DOMWifiManager",
|
2012-03-14 13:39:15 -07:00
|
|
|
interfaces: [Ci.nsIDOMWifiManager],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT}),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMWifiManager,
|
|
|
|
Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
|
|
|
// nsIDOMGlobalPropertyInitializer implementation
|
|
|
|
init: function(aWindow) {
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(Ci.nsIScriptSecurityManager);
|
|
|
|
|
|
|
|
let perm = (principal == secMan.getSystemPrincipal()) ?
|
|
|
|
Ci.nsIPermissionManager.ALLOW_ACTION :
|
|
|
|
Services.perms.testExactPermission(principal.URI, "wifi-manage");
|
|
|
|
|
|
|
|
// Only pages with perm set can use the wifi manager.
|
|
|
|
this._hasPrivileges = perm == Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
|
|
|
|
// Maintain this state for synchronous APIs.
|
|
|
|
this._currentNetwork = null;
|
|
|
|
this._enabled = true;
|
2012-03-26 09:26:32 -07:00
|
|
|
this._lastConnectionInfo = null;
|
2012-03-14 13:39:15 -07:00
|
|
|
|
2012-03-20 08:59:38 -07:00
|
|
|
const messages = ["WifiManager:setEnabled:Return:OK", "WifiManager:setEnabled:Return:NO",
|
2012-03-14 13:39:15 -07:00
|
|
|
"WifiManager:getNetworks:Return:OK", "WifiManager:getNetworks:Return:NO",
|
|
|
|
"WifiManager:associate:Return:OK", "WifiManager:associate:Return:NO",
|
2012-03-20 08:31:10 -07:00
|
|
|
"WifiManager:onconnecting", "WifiManager:onassociate",
|
2012-03-26 09:26:32 -07:00
|
|
|
"WifiManager:onconnect", "WifiManager:ondisconnect",
|
|
|
|
"WifiManager:connectionInfoUpdate"];
|
2012-03-20 08:59:38 -07:00
|
|
|
this.initHelper(aWindow, messages);
|
2012-03-14 13:39:15 -07:00
|
|
|
this._mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender);
|
|
|
|
|
2012-03-26 09:26:32 -07:00
|
|
|
var state = this._mm.sendSyncMessage("WifiManager:getState")[0];
|
|
|
|
this._currentNetwork = state.network;
|
|
|
|
this._lastConnectionInfo = state.connectionInfo;
|
|
|
|
this._enabled = state.enabled;
|
2012-03-14 13:39:15 -07:00
|
|
|
},
|
|
|
|
|
2012-03-20 08:59:38 -07:00
|
|
|
uninit: function() {
|
|
|
|
this._onConnecting = null;
|
|
|
|
this._onAssociate = null;
|
|
|
|
this._onConnect = null;
|
|
|
|
this._onDisconnect = null;
|
2012-03-26 09:26:32 -07:00
|
|
|
this._onConnectionInfoUpdate = null;
|
2012-03-14 13:39:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_sendMessageForRequest: function(name, data, request) {
|
2012-03-20 08:59:38 -07:00
|
|
|
let id = this.getRequestId(request);
|
2012-03-14 13:39:15 -07:00
|
|
|
this._mm.sendAsyncMessage(name, { data: data, rid: id, mid: this._id });
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let msg = aMessage.json;
|
|
|
|
if (msg.mid && msg.mid != this._id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let request;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "WifiManager:setEnabled:Return:OK":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
this._enabled = msg.data;
|
|
|
|
if (!this._enabled)
|
|
|
|
this._currentNetwork = null;
|
|
|
|
Services.DOMRequest.fireSuccess(request, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:setEnabled:Return:NO":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
Services.DOMRequest.fireError(request, "Unable to initialize wifi");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:getNetworks:Return:OK":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
Services.DOMRequest.fireSuccess(request, msg.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:getNetworks:Return:NO":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
Services.DOMRequest.fireError(request, "Unable to scan for networks");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:associate:Return:OK":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
Services.DOMRequest.fireSuccess(request, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:associate:Return:NO":
|
2012-03-20 08:59:38 -07:00
|
|
|
request = this.takeRequest(msg.rid);
|
2012-03-14 13:39:15 -07:00
|
|
|
Services.DOMRequest.fireError(request, "Unable to add the network");
|
|
|
|
break;
|
|
|
|
|
2012-03-20 08:31:10 -07:00
|
|
|
case "WifiManager:onconnecting":
|
|
|
|
this._currentNetwork = msg.network;
|
|
|
|
this._fireOnConnecting(msg.network);
|
|
|
|
break;
|
|
|
|
|
2012-03-14 13:39:15 -07:00
|
|
|
case "WifiManager:onassociate":
|
|
|
|
this._currentNetwork = msg.network;
|
|
|
|
this._fireOnAssociate(msg.network);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:onconnect":
|
|
|
|
this._currentNetwork = msg.network;
|
|
|
|
this._fireOnConnect(msg.network);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:ondisconnect":
|
|
|
|
this._fireOnDisconnect(this._currentNetwork);
|
|
|
|
this._currentNetwork = null;
|
2012-03-26 09:26:32 -07:00
|
|
|
this._lastConnectionInfo = null;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "WifiManager:connectionInfoUpdate":
|
|
|
|
this._lastConnectionInfo = msg;
|
|
|
|
this._fireConnectionInfoUpdate(msg);
|
2012-03-14 13:39:15 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-20 08:31:10 -07:00
|
|
|
_fireOnConnecting: function onConnecting(network) {
|
|
|
|
if (this._onConnecting)
|
|
|
|
this._onConnecting.handleEvent(new WifiStateChangeEvent(network));
|
|
|
|
},
|
|
|
|
|
2012-03-14 13:39:15 -07:00
|
|
|
_fireOnAssociate: function onAssociate(network) {
|
|
|
|
if (this._onAssociate)
|
|
|
|
this._onAssociate.handleEvent(new WifiStateChangeEvent(network));
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireOnConnect: function onConnect(network) {
|
|
|
|
if (this._onConnect)
|
|
|
|
this._onConnect.handleEvent(new WifiStateChangeEvent(network));
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireOnDisconnect: function onDisconnect(network) {
|
2012-03-26 09:26:32 -07:00
|
|
|
if (this._onDisconnect)
|
2012-03-14 13:39:15 -07:00
|
|
|
this._onDisconnect.handleEvent(new WifiStateChangeEvent(network));
|
2012-03-26 09:26:32 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_fireConnectionInfoUpdate: function connectionInfoUpdate(info) {
|
|
|
|
if (this._onConnectionInfoUpdate) {
|
|
|
|
var evt = new ConnectionInfoUpdate(this._currentNetwork,
|
|
|
|
info.signalStrength,
|
|
|
|
info.relSignalStrength,
|
|
|
|
info.linkSpeed);
|
|
|
|
this._onConnectionInfoUpdate(evt);
|
2012-03-14 13:39:15 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIDOMWifiManager
|
|
|
|
setEnabled: function nsIDOMWifiManager_setEnabled(enabled) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
2012-03-20 08:59:38 -07:00
|
|
|
var request = this.createRequest();
|
2012-03-14 13:39:15 -07:00
|
|
|
this._sendMessageForRequest("WifiManager:setEnabled", enabled, request);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
getNetworks: function nsIDOMWifiManager_getNetworks() {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
2012-03-20 08:59:38 -07:00
|
|
|
var request = this.createRequest();
|
2012-03-14 13:39:15 -07:00
|
|
|
this._sendMessageForRequest("WifiManager:getNetworks", null, request);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
associate: function nsIDOMWifiManager_associate(network) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
2012-03-20 08:59:38 -07:00
|
|
|
var request = this.createRequest();
|
2012-03-14 13:39:15 -07:00
|
|
|
this._sendMessageForRequest("WifiManager:associate", network, request);
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
|
|
|
get enabled() {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
return this._enabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
get connectedNetwork() {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
return this._currentNetwork;
|
|
|
|
},
|
|
|
|
|
2012-03-26 09:26:32 -07:00
|
|
|
get connectionInfo() {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
return this._lastConnectionInfo;
|
|
|
|
},
|
|
|
|
|
2012-03-20 08:31:10 -07:00
|
|
|
set onconnecting(callback) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
this._onConnecting = callback;
|
|
|
|
},
|
|
|
|
|
2012-03-14 13:39:15 -07:00
|
|
|
set onassociate(callback) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
this._onAssociate = callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
set onconnect(callback) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
this._onConnect = callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
set ondisconnect(callback) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
this._onDisconnect = callback;
|
2012-03-26 09:26:32 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
set connectionInfoUpdate(callback) {
|
|
|
|
if (!this._hasPrivileges)
|
|
|
|
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
|
|
|
|
this._onConnectionInfoUpdate = callback;
|
2012-03-14 13:39:15 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function WifiStateChangeEvent(network) {
|
|
|
|
this.network = network;
|
|
|
|
}
|
|
|
|
|
|
|
|
WifiStateChangeEvent.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMWifiStateChangeEvent]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{f28c1ae7-4db7-4a4d-bb06-737eb04ad700}"),
|
|
|
|
contractID: "@mozilla.org/wifi/statechange-event;1",
|
|
|
|
interfaces: [Ci.nsIDOMWifiStateChangeEvent],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Wifi State Change Event"})
|
|
|
|
};
|
|
|
|
|
2012-03-26 09:26:32 -07:00
|
|
|
function ConnectionInfoUpdate(network, signalStrength, relSignalStrength, linkSpeed) {
|
|
|
|
this.network = network;
|
|
|
|
this.signalStrength = signalStrength;
|
|
|
|
this.relSignalStrength = relSignalStrength;
|
|
|
|
this.linkSpeed = linkSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionInfoUpdate.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMWifiConnectionInfoEvent]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{aba4c481-7ea2-464a-b14c-7254a5c99454}"),
|
|
|
|
contractID: "@mozilla.org/wifi/connectioninfo-event;1",
|
|
|
|
interfaces: [Ci.nsIDOMWifiConnectionInfoEvent],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "Wifi Connection Info Event"})
|
|
|
|
};
|
|
|
|
|
2012-03-16 09:51:05 -07:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([DOMWifiManager]);
|
2012-03-14 13:39:15 -07:00
|
|
|
|
|
|
|
let debug;
|
|
|
|
if (DEBUG) {
|
|
|
|
debug = function (s) {
|
2012-03-16 09:51:05 -07:00
|
|
|
dump("-*- DOMWifiManager component: " + s + "\n");
|
2012-03-14 13:39:15 -07:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
debug = function (s) {};
|
|
|
|
}
|