michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); michael@0: michael@0: const DEBUG = false; michael@0: michael@0: // interface MozWifiP2pGroupOwner implementation. michael@0: michael@0: function MozWifiP2pGroupOwner(aGo) { michael@0: this.groupName = aGo.groupName; michael@0: this.macAddress = aGo.macAddress; michael@0: this.ipAddress = aGo.ipAddress; michael@0: this.passphrase = aGo.passphrase; michael@0: this.ssid = aGo.ssid; michael@0: this.wpsCapabilities = aGo.wpsCapabilities; michael@0: this.freq = aGo.freq; michael@0: this.isLocal = aGo.isLocal; michael@0: } michael@0: michael@0: MozWifiP2pGroupOwner.prototype = { michael@0: classID: Components.ID("{a9b81450-349d-11e3-aa6e-0800200c9a66}"), michael@0: contractID: "@mozilla.org/wifip2pgroupowner;1", michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]) michael@0: }; michael@0: michael@0: // interface MozWifiP2pManager implementation. michael@0: michael@0: const MOZ_WIFIP2PMANAGER_CONTRACTID = "@mozilla.org/wifip2pmanager;1"; michael@0: const MOZ_WIFIP2PMANAGER_CID = Components.ID("{8d9125a0-3498-11e3-aa6e-0800200c9a66}"); michael@0: michael@0: function MozWifiP2pManager() { michael@0: this.defineEventHandlerGetterSetter("onstatuschange"); michael@0: this.defineEventHandlerGetterSetter("onpeerinfoupdate"); michael@0: this.defineEventHandlerGetterSetter("onenabled"); michael@0: this.defineEventHandlerGetterSetter("ondisabled"); michael@0: michael@0: this.currentPeer = null; michael@0: this.enabled = false; michael@0: this.groupOwner = null; michael@0: } michael@0: michael@0: // For smaller, read-only APIs, we expose any property that doesn't begin with michael@0: // an underscore. michael@0: function exposeReadOnly(obj) { michael@0: let exposedProps = {}; michael@0: for (let i in obj) { michael@0: if (i[0] === "_") { michael@0: continue; michael@0: } michael@0: exposedProps[i] = "r"; michael@0: } michael@0: michael@0: obj.__exposedProps__ = exposedProps; michael@0: return obj; michael@0: } michael@0: michael@0: function debug(msg) { michael@0: if (DEBUG) { michael@0: dump('-------------- MozWifiP2pManager: ' + msg); michael@0: } michael@0: } michael@0: michael@0: MozWifiP2pManager.prototype = { michael@0: __proto__: DOMRequestIpcHelper.prototype, michael@0: michael@0: classID: MOZ_WIFIP2PMANAGER_CID, michael@0: contractID: MOZ_WIFIP2PMANAGER_CONTRACTID, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, michael@0: Ci.nsISupportsWeakReference, michael@0: Ci.nsIObserver, michael@0: Ci.nsISupports]), michael@0: michael@0: // michael@0: // nsIDOMGlobalPropertyInitializer implementation. michael@0: // michael@0: michael@0: init: function(aWindow) { michael@0: const messages = ["WifiP2pManager:setScanEnabled:Return:OK", michael@0: "WifiP2pManager:setScanEnabled:Return:NO", michael@0: "WifiP2pManager:getPeerList:Return:OK", michael@0: "WifiP2pManager:getPeerList:Return:NO", michael@0: "WifiP2pManager:connect:Return:OK", michael@0: "WifiP2pManager:connect:Return:NO", michael@0: "WifiP2pManager:disconnect:Return:OK", michael@0: "WifiP2pManager:disconnect:Return:NO", michael@0: "WifiP2pManager:setPairingConfirmation:Return", michael@0: "WifiP2pManager:setDeviceName:Return:OK", michael@0: "WifiP2pManager:setDeviceName:Return:NO", michael@0: michael@0: "WifiP2pManager:p2pDown", michael@0: "WifiP2pManager:p2pUp", michael@0: "WifiP2pManager:onconnecting", michael@0: "WifiP2pManager:onconnected", michael@0: "WifiP2pManager:ondisconnected", michael@0: "WifiP2pManager:ongroupnstop", michael@0: "WifiP2pManager:onconnectingfailed", michael@0: "WifiP2pManager:onwpstimeout", michael@0: "WifiP2pManager:onwpsfail", michael@0: "WifiP2pManager:onpeerinfoupdate", michael@0: ]; michael@0: michael@0: this.initDOMRequestHelper(aWindow, messages); michael@0: this._mm = Cc["@mozilla.org/childprocessmessagemanager;1"].getService(Ci.nsISyncMessageSender); michael@0: michael@0: // Notify the internal a new DOM mananger is created. michael@0: let state = this._mm.sendSyncMessage("WifiP2pManager:getState")[0]; michael@0: if (state) { michael@0: debug('State: ' + JSON.stringify(state)); michael@0: } else { michael@0: debug('Failed to get state'); michael@0: } michael@0: }, michael@0: michael@0: uninit: function() { michael@0: }, michael@0: michael@0: _sendMessageForRequest: function(name, data, request) { michael@0: let id = this.getRequestId(request); michael@0: this._mm.sendAsyncMessage(name, { data: data, rid: id, mid: this._id }); michael@0: }, michael@0: michael@0: receiveMessage: function(aMessage) { michael@0: let msg = aMessage.json; michael@0: if (msg.mid && msg.mid !== this._id) { michael@0: return; michael@0: } michael@0: michael@0: let request; michael@0: switch (aMessage.name) { michael@0: case "WifiP2pManager:setScanEnabled:Return:OK": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); michael@0: break; michael@0: michael@0: case "WifiP2pManager:setScanEnabled:Return:NO": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireError(request, "Unable to enable/disable Wifi P2P peer discovery."); michael@0: break; michael@0: michael@0: case "WifiP2pManager:getPeerList:Return:OK": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireSuccess(request, msg.data); michael@0: break; michael@0: michael@0: case "WifiP2pManager:getPeerList:Return:NO": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireError(request, "Unable to disable Wifi P2P peer discovery."); michael@0: break; michael@0: michael@0: case "WifiP2pManager:connect:Return:OK": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); michael@0: break; michael@0: michael@0: case "WifiP2pManager:connect:Return:NO": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireError(request, "Unable to connect to Wifi P2P peer."); michael@0: break; michael@0: michael@0: case "WifiP2pManager:disconnect:Return:OK": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); michael@0: break; michael@0: michael@0: case "WifiP2pManager:disconnect:Return:NO": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireError(request, "Unable to disconnect to Wifi P2P peer."); michael@0: break; michael@0: michael@0: case "WifiP2pManager:setDeviceName:Return:OK": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireSuccess(request, exposeReadOnly(msg.data)); michael@0: break; michael@0: michael@0: case "WifiP2pManager:setDeviceName:Return:NO": michael@0: request = this.takeRequest(msg.rid); michael@0: Services.DOMRequest.fireError(request, "Unable to set device name."); michael@0: break; michael@0: michael@0: case "WifiP2pManager:p2pDown": michael@0: this.enabled = false; michael@0: this.currentPeer = null; michael@0: this._fireEnabledOrDisabled(false); michael@0: break; michael@0: michael@0: case "WifiP2pManager:p2pUp": michael@0: this.enabled = true; michael@0: this._fireEnabledOrDisabled(true); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onconnecting": michael@0: debug('onconnecting with peer: ' + JSON.stringify(msg.peer)); michael@0: this.currentPeer = msg.peer; michael@0: this._fireStatusChangeEvent(msg.peer.address); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onconnected": michael@0: debug('onconnected with peer: ' + JSON.stringify(msg.peer)); michael@0: this.currentPeer = msg.peer; michael@0: this.groupOwner = new MozWifiP2pGroupOwner(msg.groupOwner); michael@0: this._fireStatusChangeEvent(msg.peer.address); michael@0: break; michael@0: michael@0: case "WifiP2pManager:ondisconnected": michael@0: debug('ondisconnected with peer: ' + JSON.stringify(msg.peer)); michael@0: this.currentPeer = null; michael@0: this.groupOwner = null; michael@0: this._fireStatusChangeEvent(msg.peer.address); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onconnectingfailed": michael@0: this._fireStatusChangeEvent(null); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onwpstimeout": michael@0: this._fireStatusChangeEvent(null); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onwpsfail": michael@0: this._fireStatusChangeEvent(null); michael@0: break; michael@0: michael@0: case "WifiP2pManager:onpeerinfoupdate": michael@0: this._firePeerInfoUpdateEvent(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: _firePeerInfoUpdateEvent: function PeerInfoUpdate() { michael@0: let evt = new this._window.Event("peerinfoupdate"); michael@0: this.__DOM_IMPL__.dispatchEvent(evt); michael@0: }, michael@0: michael@0: _fireStatusChangeEvent: function WifiP2pStatusChange(peerAddress) { michael@0: let evt = new this._window.MozWifiP2pStatusChangeEvent("statuschange", michael@0: { peerAddress: peerAddress }); michael@0: this.__DOM_IMPL__.dispatchEvent(evt); michael@0: }, michael@0: michael@0: _fireEnabledOrDisabled: function enabledDisabled(enabled) { michael@0: let evt = new this._window.Event(enabled ? "enabled" : "disabled"); michael@0: this.__DOM_IMPL__.dispatchEvent(evt); michael@0: }, michael@0: michael@0: // michael@0: // WifiP2pManager.webidl implementation. michael@0: // michael@0: michael@0: enableScan: function () { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:enableScan", null, request); michael@0: return request; michael@0: }, michael@0: michael@0: disableScan: function () { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:disableScan", null, request); michael@0: return request; michael@0: }, michael@0: michael@0: setScanEnabled: function(enabled) { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:setScanEnabled", enabled, request); michael@0: return request; michael@0: }, michael@0: michael@0: connect: function (address, wpsMethod, goIntent) { michael@0: let request = this.createRequest(); michael@0: let connectionInfo = { address: address, wpsMethod: wpsMethod, goIntent: goIntent }; michael@0: this._sendMessageForRequest("WifiP2pManager:connect", connectionInfo, request); michael@0: return request; michael@0: }, michael@0: michael@0: disconnect: function (address) { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:disconnect", address, request); michael@0: return request; michael@0: }, michael@0: michael@0: getPeerList: function () { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:getPeerList", null, request); michael@0: return request; michael@0: }, michael@0: michael@0: setPairingConfirmation: function (accepted, pin) { michael@0: let request = this.createRequest(); michael@0: let result = { accepted: accepted, pin: pin }; michael@0: this._sendMessageForRequest("WifiP2pManager:setPairingConfirmation", result, request); michael@0: return request; michael@0: }, michael@0: michael@0: setDeviceName: function(newDeviceName) { michael@0: let request = this.createRequest(); michael@0: this._sendMessageForRequest("WifiP2pManager:setDeviceName", newDeviceName, request); michael@0: return request; michael@0: }, michael@0: michael@0: // Helpers. michael@0: defineEventHandlerGetterSetter: function(event) { michael@0: Object.defineProperty(this, event, { michael@0: get: function() { michael@0: return this.__DOM_IMPL__.getEventHandler(event); michael@0: }, michael@0: michael@0: set: function(handler) { michael@0: this.__DOM_IMPL__.setEventHandler(event, handler); michael@0: } michael@0: }); michael@0: }, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozWifiP2pManager, MozWifiP2pGroupOwner]);