dom/wifi/DOMWifiP2pManager.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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

mercurial