|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 "use strict"; |
|
8 |
|
9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
|
10 |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 Cu.import("resource://gre/modules/Services.jsm"); |
|
13 Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); |
|
14 |
|
15 const DEBUG = false; // set to false to suppress debug messages |
|
16 |
|
17 const DOMWIFIMANAGER_CONTRACTID = "@mozilla.org/wifimanager;1"; |
|
18 const DOMWIFIMANAGER_CID = Components.ID("{c9b5f09e-25d2-40ca-aef4-c4d13d93c706}"); |
|
19 |
|
20 function MozWifiNetwork() { |
|
21 } |
|
22 |
|
23 MozWifiNetwork.prototype = { |
|
24 |
|
25 init: function(aWindow) { |
|
26 this._window = aWindow; |
|
27 }, |
|
28 |
|
29 __init: function(obj) { |
|
30 for (let key in obj) { |
|
31 this[key] = obj[key]; |
|
32 } |
|
33 }, |
|
34 |
|
35 classID: Components.ID("{c01fd751-43c0-460a-8b64-abf652ec7220}"), |
|
36 contractID: "@mozilla.org/mozwifinetwork;1", |
|
37 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
|
38 Ci.nsIDOMGlobalPropertyInitializer]) |
|
39 }; |
|
40 |
|
41 function MozWifiConnection(obj) { |
|
42 this.status = obj.status; |
|
43 this.network = obj.network; |
|
44 } |
|
45 |
|
46 MozWifiConnection.prototype = { |
|
47 classID: Components.ID("{23579da4-201b-4319-bd42-9b7f337343ac}"), |
|
48 contractID: "@mozilla.org/mozwificonnection;1", |
|
49 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]) |
|
50 }; |
|
51 |
|
52 function MozWifiConnectionInfo(obj) { |
|
53 this.signalStrength = obj.signalStrength; |
|
54 this.relSignalStrength = obj.relSignalStrength; |
|
55 this.linkSpeed = obj.linkSpeed; |
|
56 this.ipAddress = obj.ipAddress; |
|
57 } |
|
58 |
|
59 MozWifiConnectionInfo.prototype = { |
|
60 classID: Components.ID("{83670352-6ed4-4c35-8de9-402296a1959c}"), |
|
61 contractID: "@mozilla.org/mozwificonnectioninfo;1", |
|
62 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]) |
|
63 } |
|
64 |
|
65 function DOMWifiManager() { |
|
66 this.defineEventHandlerGetterSetter("onstatuschange"); |
|
67 this.defineEventHandlerGetterSetter("onconnectionInfoUpdate"); |
|
68 this.defineEventHandlerGetterSetter("onenabled"); |
|
69 this.defineEventHandlerGetterSetter("ondisabled"); |
|
70 } |
|
71 |
|
72 DOMWifiManager.prototype = { |
|
73 __proto__: DOMRequestIpcHelper.prototype, |
|
74 classDescription: "DOMWifiManager", |
|
75 classID: DOMWIFIMANAGER_CID, |
|
76 contractID: DOMWIFIMANAGER_CONTRACTID, |
|
77 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, |
|
78 Ci.nsISupportsWeakReference, |
|
79 Ci.nsIObserver]), |
|
80 |
|
81 // nsIDOMGlobalPropertyInitializer implementation |
|
82 init: function(aWindow) { |
|
83 // Maintain this state for synchronous APIs. |
|
84 this._currentNetwork = null; |
|
85 this._connectionStatus = "disconnected"; |
|
86 this._enabled = false; |
|
87 this._lastConnectionInfo = null; |
|
88 |
|
89 const messages = ["WifiManager:getNetworks:Return:OK", "WifiManager:getNetworks:Return:NO", |
|
90 "WifiManager:getKnownNetworks:Return:OK", "WifiManager:getKnownNetworks:Return:NO", |
|
91 "WifiManager:associate:Return:OK", "WifiManager:associate:Return:NO", |
|
92 "WifiManager:forget:Return:OK", "WifiManager:forget:Return:NO", |
|
93 "WifiManager:wps:Return:OK", "WifiManager:wps:Return:NO", |
|
94 "WifiManager:setPowerSavingMode:Return:OK", "WifiManager:setPowerSavingMode:Return:NO", |
|
95 "WifiManager:setHttpProxy:Return:OK", "WifiManager:setHttpProxy:Return:NO", |
|
96 "WifiManager:setStaticIpMode:Return:OK", "WifiManager:setStaticIpMode:Return:NO", |
|
97 "WifiManager:wifiDown", "WifiManager:wifiUp", |
|
98 "WifiManager:onconnecting", "WifiManager:onassociate", |
|
99 "WifiManager:onconnect", "WifiManager:ondisconnect", |
|
100 "WifiManager:onwpstimeout", "WifiManager:onwpsfail", |
|
101 "WifiManager:onwpsoverlap", "WifiManager:connectionInfoUpdate", |
|
102 "WifiManager:onconnectingfailed"]; |
|
103 this.initDOMRequestHelper(aWindow, messages); |
|
104 this._mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); |
|
105 |
|
106 var state = this._mm.sendSyncMessage("WifiManager:getState")[0]; |
|
107 if (state) { |
|
108 this._currentNetwork = this._convertWifiNetwork(state.network); |
|
109 this._lastConnectionInfo = this._convertConnectionInfo(state.connectionInfo); |
|
110 this._enabled = state.enabled; |
|
111 this._connectionStatus = state.status; |
|
112 this._macAddress = state.macAddress; |
|
113 } else { |
|
114 this._currentNetwork = null; |
|
115 this._lastConnectionInfo = null; |
|
116 this._enabled = false; |
|
117 this._connectionStatus = "disconnected"; |
|
118 this._macAddress = ""; |
|
119 } |
|
120 }, |
|
121 |
|
122 _convertWifiNetworkToJSON: function(aNetwork) { |
|
123 let json = {}; |
|
124 |
|
125 for (let key in aNetwork) { |
|
126 // In WifiWorker.js there are lots of check using "key in network". |
|
127 // So if the value of any property of WifiNetwork is undefined, do not clone it. |
|
128 if (aNetwork[key] != undefined) { |
|
129 json[key] = aNetwork[key]; |
|
130 } |
|
131 } |
|
132 return json; |
|
133 }, |
|
134 |
|
135 _convertWifiNetwork: function(aNetwork) { |
|
136 let network = aNetwork ? new this._window.MozWifiNetwork(aNetwork) : null; |
|
137 return network; |
|
138 }, |
|
139 |
|
140 _convertWifiNetworks: function(aNetworks) { |
|
141 let networks = []; |
|
142 for (let i in aNetworks) { |
|
143 networks.push(this._convertWifiNetwork(aNetworks[i])); |
|
144 } |
|
145 return networks; |
|
146 }, |
|
147 |
|
148 _convertConnection: function(aConn) { |
|
149 let conn = aConn ? new MozWifiConnection(aConn) : null; |
|
150 return conn; |
|
151 }, |
|
152 |
|
153 _convertConnectionInfo: function(aInfo) { |
|
154 let info = aInfo ? new MozWifiConnectionInfo(aInfo) : null; |
|
155 return info; |
|
156 }, |
|
157 |
|
158 _sendMessageForRequest: function(name, data, request) { |
|
159 let id = this.getRequestId(request); |
|
160 this._mm.sendAsyncMessage(name, { data: data, rid: id, mid: this._id }); |
|
161 }, |
|
162 |
|
163 receiveMessage: function(aMessage) { |
|
164 let msg = aMessage.json; |
|
165 if (msg.mid && msg.mid != this._id) |
|
166 return; |
|
167 |
|
168 let request; |
|
169 if (msg.rid) { |
|
170 request = this.takeRequest(msg.rid); |
|
171 if (!request) { |
|
172 return; |
|
173 } |
|
174 } |
|
175 |
|
176 switch (aMessage.name) { |
|
177 case "WifiManager:getNetworks:Return:OK": |
|
178 Services.DOMRequest.fireSuccess(request, this._convertWifiNetworks(msg.data)); |
|
179 break; |
|
180 |
|
181 case "WifiManager:getNetworks:Return:NO": |
|
182 Services.DOMRequest.fireError(request, "Unable to scan for networks"); |
|
183 break; |
|
184 |
|
185 case "WifiManager:getKnownNetworks:Return:OK": |
|
186 Services.DOMRequest.fireSuccess(request, this._convertWifiNetworks(msg.data)); |
|
187 break; |
|
188 |
|
189 case "WifiManager:getKnownNetworks:Return:NO": |
|
190 Services.DOMRequest.fireError(request, "Unable to get known networks"); |
|
191 break; |
|
192 |
|
193 case "WifiManager:associate:Return:OK": |
|
194 Services.DOMRequest.fireSuccess(request, true); |
|
195 break; |
|
196 |
|
197 case "WifiManager:associate:Return:NO": |
|
198 Services.DOMRequest.fireError(request, "Unable to add the network"); |
|
199 break; |
|
200 |
|
201 case "WifiManager:forget:Return:OK": |
|
202 Services.DOMRequest.fireSuccess(request, true); |
|
203 break; |
|
204 |
|
205 case "WifiManager:forget:Return:NO": |
|
206 Services.DOMRequest.fireError(request, msg.data); |
|
207 break; |
|
208 |
|
209 case "WifiManager:wps:Return:OK": |
|
210 Services.DOMRequest.fireSuccess(request, msg.data); |
|
211 break; |
|
212 |
|
213 case "WifiManager:wps:Return:NO": |
|
214 Services.DOMRequest.fireError(request, msg.data); |
|
215 break; |
|
216 |
|
217 case "WifiManager:setPowerSavingMode:Return:OK": |
|
218 Services.DOMRequest.fireSuccess(request, msg.data); |
|
219 break; |
|
220 |
|
221 case "WifiManager:setPowerSavingMode:Return:NO": |
|
222 Services.DOMRequest.fireError(request, msg.data); |
|
223 break; |
|
224 |
|
225 case "WifiManager:setHttpProxy:Return:OK": |
|
226 Services.DOMRequest.fireSuccess(request, msg.data); |
|
227 break; |
|
228 |
|
229 case "WifiManager:setHttpProxy:Return:NO": |
|
230 Services.DOMRequest.fireError(request, msg.data); |
|
231 break; |
|
232 |
|
233 case "WifiManager:setStaticIpMode:Return:OK": |
|
234 Services.DOMRequest.fireSuccess(request, msg.data); |
|
235 break; |
|
236 |
|
237 case "WifiManager:setStaticIpMode:Return:NO": |
|
238 Services.DOMRequest.fireError(request, msg.data); |
|
239 break; |
|
240 |
|
241 case "WifiManager:wifiDown": |
|
242 this._enabled = false; |
|
243 this._currentNetwork = null; |
|
244 this._fireEnabledOrDisabled(false); |
|
245 break; |
|
246 |
|
247 case "WifiManager:wifiUp": |
|
248 this._enabled = true; |
|
249 this._macAddress = msg.macAddress; |
|
250 this._fireEnabledOrDisabled(true); |
|
251 break; |
|
252 |
|
253 case "WifiManager:onconnecting": |
|
254 this._currentNetwork = this._convertWifiNetwork(msg.network); |
|
255 this._connectionStatus = "connecting"; |
|
256 this._fireStatusChangeEvent(); |
|
257 break; |
|
258 |
|
259 case "WifiManager:onassociate": |
|
260 this._currentNetwork = this._convertWifiNetwork(msg.network); |
|
261 this._connectionStatus = "associated"; |
|
262 this._fireStatusChangeEvent(); |
|
263 break; |
|
264 |
|
265 case "WifiManager:onconnect": |
|
266 this._currentNetwork = this._convertWifiNetwork(msg.network); |
|
267 this._connectionStatus = "connected"; |
|
268 this._fireStatusChangeEvent(); |
|
269 break; |
|
270 |
|
271 case "WifiManager:ondisconnect": |
|
272 this._currentNetwork = null; |
|
273 this._connectionStatus = "disconnected"; |
|
274 this._lastConnectionInfo = null; |
|
275 this._fireStatusChangeEvent(); |
|
276 break; |
|
277 |
|
278 case "WifiManager:onwpstimeout": |
|
279 this._currentNetwork = null; |
|
280 this._connectionStatus = "wps-timedout"; |
|
281 this._lastConnectionInfo = null; |
|
282 this._fireStatusChangeEvent(); |
|
283 break; |
|
284 |
|
285 case "WifiManager:onwpsfail": |
|
286 this._currentNetwork = null; |
|
287 this._connectionStatus = "wps-failed"; |
|
288 this._lastConnectionInfo = null; |
|
289 this._fireStatusChangeEvent(); |
|
290 break; |
|
291 |
|
292 case "WifiManager:onwpsoverlap": |
|
293 this._currentNetwork = null; |
|
294 this._connectionStatus = "wps-overlapped"; |
|
295 this._lastConnectionInfo = null; |
|
296 this._fireStatusChangeEvent(); |
|
297 break; |
|
298 |
|
299 case "WifiManager:connectionInfoUpdate": |
|
300 this._lastConnectionInfo = this._convertConnectionInfo(msg); |
|
301 this._fireConnectionInfoUpdate(msg); |
|
302 break; |
|
303 case "WifiManager:onconnectingfailed": |
|
304 this._currentNetwork = null; |
|
305 this._connectionStatus = "connectingfailed"; |
|
306 this._lastConnectionInfo = null; |
|
307 this._fireStatusChangeEvent(); |
|
308 break; |
|
309 } |
|
310 }, |
|
311 |
|
312 _fireStatusChangeEvent: function StatusChangeEvent() { |
|
313 var event = new this._window.MozWifiStatusChangeEvent("statuschange", |
|
314 { network: this._currentNetwork, |
|
315 status: this._connectionStatus |
|
316 }); |
|
317 this.__DOM_IMPL__.dispatchEvent(event); |
|
318 }, |
|
319 |
|
320 _fireConnectionInfoUpdate: function onConnectionInfoUpdate(info) { |
|
321 var evt = new this._window.MozWifiConnectionInfoEvent("connectioninfoupdate", |
|
322 { network: this._currentNetwork, |
|
323 signalStrength: info.signalStrength, |
|
324 relSignalStrength: info.relSignalStrength, |
|
325 linkSpeed: info.linkSpeed, |
|
326 ipAddress: info.ipAddress, |
|
327 }); |
|
328 this.__DOM_IMPL__.dispatchEvent(evt); |
|
329 }, |
|
330 |
|
331 _fireEnabledOrDisabled: function enabledDisabled(enabled) { |
|
332 var evt = new this._window.Event(enabled ? "enabled" : "disabled"); |
|
333 this.__DOM_IMPL__.dispatchEvent(evt); |
|
334 }, |
|
335 |
|
336 getNetworks: function getNetworks() { |
|
337 var request = this.createRequest(); |
|
338 this._sendMessageForRequest("WifiManager:getNetworks", null, request); |
|
339 return request; |
|
340 }, |
|
341 |
|
342 getKnownNetworks: function getKnownNetworks() { |
|
343 var request = this.createRequest(); |
|
344 this._sendMessageForRequest("WifiManager:getKnownNetworks", null, request); |
|
345 return request; |
|
346 }, |
|
347 |
|
348 associate: function associate(network) { |
|
349 var request = this.createRequest(); |
|
350 this._sendMessageForRequest("WifiManager:associate", |
|
351 this._convertWifiNetworkToJSON(network), request); |
|
352 return request; |
|
353 }, |
|
354 |
|
355 forget: function forget(network) { |
|
356 var request = this.createRequest(); |
|
357 this._sendMessageForRequest("WifiManager:forget", |
|
358 this._convertWifiNetworkToJSON(network), request); |
|
359 return request; |
|
360 }, |
|
361 |
|
362 wps: function wps(detail) { |
|
363 var request = this.createRequest(); |
|
364 this._sendMessageForRequest("WifiManager:wps", detail, request); |
|
365 return request; |
|
366 }, |
|
367 |
|
368 setPowerSavingMode: function setPowerSavingMode(enabled) { |
|
369 var request = this.createRequest(); |
|
370 this._sendMessageForRequest("WifiManager:setPowerSavingMode", enabled, request); |
|
371 return request; |
|
372 }, |
|
373 |
|
374 setHttpProxy: function setHttpProxy(network, info) { |
|
375 var request = this.createRequest(); |
|
376 this._sendMessageForRequest("WifiManager:setHttpProxy", |
|
377 { network: this._convertWifiNetworkToJSON(network), info:info}, request); |
|
378 return request; |
|
379 }, |
|
380 |
|
381 setStaticIpMode: function setStaticIpMode(network, info) { |
|
382 var request = this.createRequest(); |
|
383 this._sendMessageForRequest("WifiManager:setStaticIpMode", |
|
384 { network: this._convertWifiNetworkToJSON(network), info: info}, request); |
|
385 return request; |
|
386 }, |
|
387 |
|
388 get enabled() { |
|
389 return this._enabled; |
|
390 }, |
|
391 |
|
392 get macAddress() { |
|
393 return this._macAddress; |
|
394 }, |
|
395 |
|
396 get connection() { |
|
397 let _connection = this._convertConnection({ status: this._connectionStatus, |
|
398 network: this._currentNetwork, |
|
399 }); |
|
400 return _connection; |
|
401 }, |
|
402 |
|
403 get connectionInformation() { |
|
404 return this._lastConnectionInfo; |
|
405 }, |
|
406 |
|
407 defineEventHandlerGetterSetter: function(name) { |
|
408 Object.defineProperty(this, name, { |
|
409 get: function() { |
|
410 return this.__DOM_IMPL__.getEventHandler(name); |
|
411 }, |
|
412 set: function(handler) { |
|
413 this.__DOM_IMPL__.setEventHandler(name, handler); |
|
414 } |
|
415 }); |
|
416 }, |
|
417 }; |
|
418 |
|
419 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ |
|
420 DOMWifiManager, MozWifiNetwork, MozWifiConnection, MozWifiConnectionInfo |
|
421 ]); |
|
422 |
|
423 let debug; |
|
424 if (DEBUG) { |
|
425 debug = function (s) { |
|
426 dump("-*- DOMWifiManager component: " + s + "\n"); |
|
427 }; |
|
428 } else { |
|
429 debug = function (s) {}; |
|
430 } |