|
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; |
|
16 |
|
17 // interface MozWifiP2pGroupOwner implementation. |
|
18 |
|
19 function MozWifiP2pGroupOwner(aGo) { |
|
20 this.groupName = aGo.groupName; |
|
21 this.macAddress = aGo.macAddress; |
|
22 this.ipAddress = aGo.ipAddress; |
|
23 this.passphrase = aGo.passphrase; |
|
24 this.ssid = aGo.ssid; |
|
25 this.wpsCapabilities = aGo.wpsCapabilities; |
|
26 this.freq = aGo.freq; |
|
27 this.isLocal = aGo.isLocal; |
|
28 } |
|
29 |
|
30 MozWifiP2pGroupOwner.prototype = { |
|
31 classID: Components.ID("{a9b81450-349d-11e3-aa6e-0800200c9a66}"), |
|
32 contractID: "@mozilla.org/wifip2pgroupowner;1", |
|
33 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]) |
|
34 }; |
|
35 |
|
36 // interface MozWifiP2pManager implementation. |
|
37 |
|
38 const MOZ_WIFIP2PMANAGER_CONTRACTID = "@mozilla.org/wifip2pmanager;1"; |
|
39 const MOZ_WIFIP2PMANAGER_CID = Components.ID("{8d9125a0-3498-11e3-aa6e-0800200c9a66}"); |
|
40 |
|
41 function MozWifiP2pManager() { |
|
42 this.defineEventHandlerGetterSetter("onstatuschange"); |
|
43 this.defineEventHandlerGetterSetter("onpeerinfoupdate"); |
|
44 this.defineEventHandlerGetterSetter("onenabled"); |
|
45 this.defineEventHandlerGetterSetter("ondisabled"); |
|
46 |
|
47 this.currentPeer = null; |
|
48 this.enabled = false; |
|
49 this.groupOwner = null; |
|
50 } |
|
51 |
|
52 // For smaller, read-only APIs, we expose any property that doesn't begin with |
|
53 // an underscore. |
|
54 function exposeReadOnly(obj) { |
|
55 let exposedProps = {}; |
|
56 for (let i in obj) { |
|
57 if (i[0] === "_") { |
|
58 continue; |
|
59 } |
|
60 exposedProps[i] = "r"; |
|
61 } |
|
62 |
|
63 obj.__exposedProps__ = exposedProps; |
|
64 return obj; |
|
65 } |
|
66 |
|
67 function debug(msg) { |
|
68 if (DEBUG) { |
|
69 dump('-------------- MozWifiP2pManager: ' + msg); |
|
70 } |
|
71 } |
|
72 |
|
73 MozWifiP2pManager.prototype = { |
|
74 __proto__: DOMRequestIpcHelper.prototype, |
|
75 |
|
76 classID: MOZ_WIFIP2PMANAGER_CID, |
|
77 contractID: MOZ_WIFIP2PMANAGER_CONTRACTID, |
|
78 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, |
|
79 Ci.nsISupportsWeakReference, |
|
80 Ci.nsIObserver, |
|
81 Ci.nsISupports]), |
|
82 |
|
83 // |
|
84 // nsIDOMGlobalPropertyInitializer implementation. |
|
85 // |
|
86 |
|
87 init: function(aWindow) { |
|
88 const messages = ["WifiP2pManager:setScanEnabled:Return:OK", |
|
89 "WifiP2pManager:setScanEnabled:Return:NO", |
|
90 "WifiP2pManager:getPeerList:Return:OK", |
|
91 "WifiP2pManager:getPeerList:Return:NO", |
|
92 "WifiP2pManager:connect:Return:OK", |
|
93 "WifiP2pManager:connect:Return:NO", |
|
94 "WifiP2pManager:disconnect:Return:OK", |
|
95 "WifiP2pManager:disconnect:Return:NO", |
|
96 "WifiP2pManager:setPairingConfirmation:Return", |
|
97 "WifiP2pManager:setDeviceName:Return:OK", |
|
98 "WifiP2pManager:setDeviceName:Return:NO", |
|
99 |
|
100 "WifiP2pManager:p2pDown", |
|
101 "WifiP2pManager:p2pUp", |
|
102 "WifiP2pManager:onconnecting", |
|
103 "WifiP2pManager:onconnected", |
|
104 "WifiP2pManager:ondisconnected", |
|
105 "WifiP2pManager:ongroupnstop", |
|
106 "WifiP2pManager:onconnectingfailed", |
|
107 "WifiP2pManager:onwpstimeout", |
|
108 "WifiP2pManager:onwpsfail", |
|
109 "WifiP2pManager:onpeerinfoupdate", |
|
110 ]; |
|
111 |
|
112 this.initDOMRequestHelper(aWindow, messages); |
|
113 this._mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); |
|
114 |
|
115 // Notify the internal a new DOM mananger is created. |
|
116 let state = this._mm.sendSyncMessage("WifiP2pManager:getState")[0]; |
|
117 if (state) { |
|
118 debug('State: ' + JSON.stringify(state)); |
|
119 } else { |
|
120 debug('Failed to get state'); |
|
121 } |
|
122 }, |
|
123 |
|
124 uninit: function() { |
|
125 }, |
|
126 |
|
127 _sendMessageForRequest: function(name, data, request) { |
|
128 let id = this.getRequestId(request); |
|
129 this._mm.sendAsyncMessage(name, { data: data, rid: id, mid: this._id }); |
|
130 }, |
|
131 |
|
132 receiveMessage: function(aMessage) { |
|
133 let msg = aMessage.json; |
|
134 if (msg.mid && msg.mid !== this._id) { |
|
135 return; |
|
136 } |
|
137 |
|
138 let request; |
|
139 switch (aMessage.name) { |
|
140 case "WifiP2pManager:setScanEnabled:Return:OK": |
|
141 request = this.takeRequest(msg.rid); |
|
142 Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); |
|
143 break; |
|
144 |
|
145 case "WifiP2pManager:setScanEnabled:Return:NO": |
|
146 request = this.takeRequest(msg.rid); |
|
147 Services.DOMRequest.fireError(request, "Unable to enable/disable Wifi P2P peer discovery."); |
|
148 break; |
|
149 |
|
150 case "WifiP2pManager:getPeerList:Return:OK": |
|
151 request = this.takeRequest(msg.rid); |
|
152 Services.DOMRequest.fireSuccess(request, msg.data); |
|
153 break; |
|
154 |
|
155 case "WifiP2pManager:getPeerList:Return:NO": |
|
156 request = this.takeRequest(msg.rid); |
|
157 Services.DOMRequest.fireError(request, "Unable to disable Wifi P2P peer discovery."); |
|
158 break; |
|
159 |
|
160 case "WifiP2pManager:connect:Return:OK": |
|
161 request = this.takeRequest(msg.rid); |
|
162 Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); |
|
163 break; |
|
164 |
|
165 case "WifiP2pManager:connect:Return:NO": |
|
166 request = this.takeRequest(msg.rid); |
|
167 Services.DOMRequest.fireError(request, "Unable to connect to Wifi P2P peer."); |
|
168 break; |
|
169 |
|
170 case "WifiP2pManager:disconnect:Return:OK": |
|
171 request = this.takeRequest(msg.rid); |
|
172 Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); |
|
173 break; |
|
174 |
|
175 case "WifiP2pManager:disconnect:Return:NO": |
|
176 request = this.takeRequest(msg.rid); |
|
177 Services.DOMRequest.fireError(request, "Unable to disconnect to Wifi P2P peer."); |
|
178 break; |
|
179 |
|
180 case "WifiP2pManager:setDeviceName:Return:OK": |
|
181 request = this.takeRequest(msg.rid); |
|
182 Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); |
|
183 break; |
|
184 |
|
185 case "WifiP2pManager:setDeviceName:Return:NO": |
|
186 request = this.takeRequest(msg.rid); |
|
187 Services.DOMRequest.fireError(request, "Unable to set device name."); |
|
188 break; |
|
189 |
|
190 case "WifiP2pManager:p2pDown": |
|
191 this.enabled = false; |
|
192 this.currentPeer = null; |
|
193 this._fireEnabledOrDisabled(false); |
|
194 break; |
|
195 |
|
196 case "WifiP2pManager:p2pUp": |
|
197 this.enabled = true; |
|
198 this._fireEnabledOrDisabled(true); |
|
199 break; |
|
200 |
|
201 case "WifiP2pManager:onconnecting": |
|
202 debug('onconnecting with peer: ' + JSON.stringify(msg.peer)); |
|
203 this.currentPeer = msg.peer; |
|
204 this._fireStatusChangeEvent(msg.peer.address); |
|
205 break; |
|
206 |
|
207 case "WifiP2pManager:onconnected": |
|
208 debug('onconnected with peer: ' + JSON.stringify(msg.peer)); |
|
209 this.currentPeer = msg.peer; |
|
210 this.groupOwner = new MozWifiP2pGroupOwner(msg.groupOwner); |
|
211 this._fireStatusChangeEvent(msg.peer.address); |
|
212 break; |
|
213 |
|
214 case "WifiP2pManager:ondisconnected": |
|
215 debug('ondisconnected with peer: ' + JSON.stringify(msg.peer)); |
|
216 this.currentPeer = null; |
|
217 this.groupOwner = null; |
|
218 this._fireStatusChangeEvent(msg.peer.address); |
|
219 break; |
|
220 |
|
221 case "WifiP2pManager:onconnectingfailed": |
|
222 this._fireStatusChangeEvent(null); |
|
223 break; |
|
224 |
|
225 case "WifiP2pManager:onwpstimeout": |
|
226 this._fireStatusChangeEvent(null); |
|
227 break; |
|
228 |
|
229 case "WifiP2pManager:onwpsfail": |
|
230 this._fireStatusChangeEvent(null); |
|
231 break; |
|
232 |
|
233 case "WifiP2pManager:onpeerinfoupdate": |
|
234 this._firePeerInfoUpdateEvent(); |
|
235 break; |
|
236 } |
|
237 }, |
|
238 |
|
239 _firePeerInfoUpdateEvent: function PeerInfoUpdate() { |
|
240 let evt = new this._window.Event("peerinfoupdate"); |
|
241 this.__DOM_IMPL__.dispatchEvent(evt); |
|
242 }, |
|
243 |
|
244 _fireStatusChangeEvent: function WifiP2pStatusChange(peerAddress) { |
|
245 let evt = new this._window.MozWifiP2pStatusChangeEvent("statuschange", |
|
246 { peerAddress: peerAddress }); |
|
247 this.__DOM_IMPL__.dispatchEvent(evt); |
|
248 }, |
|
249 |
|
250 _fireEnabledOrDisabled: function enabledDisabled(enabled) { |
|
251 let evt = new this._window.Event(enabled ? "enabled" : "disabled"); |
|
252 this.__DOM_IMPL__.dispatchEvent(evt); |
|
253 }, |
|
254 |
|
255 // |
|
256 // WifiP2pManager.webidl implementation. |
|
257 // |
|
258 |
|
259 enableScan: function () { |
|
260 let request = this.createRequest(); |
|
261 this._sendMessageForRequest("WifiP2pManager:enableScan", null, request); |
|
262 return request; |
|
263 }, |
|
264 |
|
265 disableScan: function () { |
|
266 let request = this.createRequest(); |
|
267 this._sendMessageForRequest("WifiP2pManager:disableScan", null, request); |
|
268 return request; |
|
269 }, |
|
270 |
|
271 setScanEnabled: function(enabled) { |
|
272 let request = this.createRequest(); |
|
273 this._sendMessageForRequest("WifiP2pManager:setScanEnabled", enabled, request); |
|
274 return request; |
|
275 }, |
|
276 |
|
277 connect: function (address, wpsMethod, goIntent) { |
|
278 let request = this.createRequest(); |
|
279 let connectionInfo = { address: address, wpsMethod: wpsMethod, goIntent: goIntent }; |
|
280 this._sendMessageForRequest("WifiP2pManager:connect", connectionInfo, request); |
|
281 return request; |
|
282 }, |
|
283 |
|
284 disconnect: function (address) { |
|
285 let request = this.createRequest(); |
|
286 this._sendMessageForRequest("WifiP2pManager:disconnect", address, request); |
|
287 return request; |
|
288 }, |
|
289 |
|
290 getPeerList: function () { |
|
291 let request = this.createRequest(); |
|
292 this._sendMessageForRequest("WifiP2pManager:getPeerList", null, request); |
|
293 return request; |
|
294 }, |
|
295 |
|
296 setPairingConfirmation: function (accepted, pin) { |
|
297 let request = this.createRequest(); |
|
298 let result = { accepted: accepted, pin: pin }; |
|
299 this._sendMessageForRequest("WifiP2pManager:setPairingConfirmation", result, request); |
|
300 return request; |
|
301 }, |
|
302 |
|
303 setDeviceName: function(newDeviceName) { |
|
304 let request = this.createRequest(); |
|
305 this._sendMessageForRequest("WifiP2pManager:setDeviceName", newDeviceName, request); |
|
306 return request; |
|
307 }, |
|
308 |
|
309 // Helpers. |
|
310 defineEventHandlerGetterSetter: function(event) { |
|
311 Object.defineProperty(this, event, { |
|
312 get: function() { |
|
313 return this.__DOM_IMPL__.getEventHandler(event); |
|
314 }, |
|
315 |
|
316 set: function(handler) { |
|
317 this.__DOM_IMPL__.setEventHandler(event, handler); |
|
318 } |
|
319 }); |
|
320 }, |
|
321 }; |
|
322 |
|
323 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozWifiP2pManager, MozWifiP2pGroupOwner]); |