This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDOMWifiManager.js
245 lines (199 loc) · 8.29 KB
/
DOMWifiManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* -*- 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");
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
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}");
function DOMWifiManager() {
}
DOMWifiManager.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
classID: DOMWIFIMANAGER_CID,
classInfo: XPCOMUtils.generateCI({classID: DOMWIFIMANAGER_CID,
contractID: DOMWIFIMANAGER_CONTRACTID,
classDescription: "DOMWifiManager",
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;
const messages = ["WifiManager:setEnabled:Return:OK", "WifiManager:setEnabled:Return:NO",
"WifiManager:getNetworks:Return:OK", "WifiManager:getNetworks:Return:NO",
"WifiManager:associate:Return:OK", "WifiManager:associate:Return:NO",
"WifiManager:onconnecting", "WifiManager:onassociate",
"WifiManager:onconnect", "WifiManager:ondisconnect"];
this.initHelper(aWindow, messages);
this._mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender);
var state = this._mm.sendSyncMessage("WifiManager:getState");
this._currentNetwork = state[0].network;
this._enabled = state[0].enabled;
},
uninit: function() {
this._onConnecting = null;
this._onAssociate = null;
this._onConnect = null;
this._onDisconnect = null;
},
_sendMessageForRequest: function(name, data, request) {
let id = this.getRequestId(request);
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":
request = this.takeRequest(msg.rid);
this._enabled = msg.data;
if (!this._enabled)
this._currentNetwork = null;
Services.DOMRequest.fireSuccess(request, true);
break;
case "WifiManager:setEnabled:Return:NO":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireError(request, "Unable to initialize wifi");
break;
case "WifiManager:getNetworks:Return:OK":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireSuccess(request, msg.data);
break;
case "WifiManager:getNetworks:Return:NO":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireError(request, "Unable to scan for networks");
break;
case "WifiManager:associate:Return:OK":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireSuccess(request, true);
break;
case "WifiManager:associate:Return:NO":
request = this.takeRequest(msg.rid);
Services.DOMRequest.fireError(request, "Unable to add the network");
break;
case "WifiManager:onconnecting":
this._currentNetwork = msg.network;
this._fireOnConnecting(msg.network);
break;
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;
break;
}
},
_fireOnConnecting: function onConnecting(network) {
if (this._onConnecting)
this._onConnecting.handleEvent(new WifiStateChangeEvent(network));
},
_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) {
if (this._onDisconnect) {
this._onDisconnect.handleEvent(new WifiStateChangeEvent(network));
}
},
// nsIDOMWifiManager
setEnabled: function nsIDOMWifiManager_setEnabled(enabled) {
if (!this._hasPrivileges)
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
var request = this.createRequest();
this._sendMessageForRequest("WifiManager:setEnabled", enabled, request);
return request;
},
getNetworks: function nsIDOMWifiManager_getNetworks() {
if (!this._hasPrivileges)
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
var request = this.createRequest();
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);
var request = this.createRequest();
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;
},
set onconnecting(callback) {
if (!this._hasPrivileges)
throw new Components.Exception("Denied", Cr.NS_ERROR_FAILURE);
this._onConnecting = callback;
},
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;
}
};
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"})
};
const NSGetFactory = XPCOMUtils.generateNSGetFactory([DOMWifiManager]);
let debug;
if (DEBUG) {
debug = function (s) {
dump("-*- DOMWifiManager component: " + s + "\n");
};
} else {
debug = function (s) {};
}