Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* Copyright © 2013, Deutsche Telekom, Inc. */ |
michael@0 | 6 | |
michael@0 | 7 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | const DEBUG = false; |
michael@0 | 10 | function debug(s) { |
michael@0 | 11 | if (DEBUG) dump("-*- Nfc DOM: " + s + "\n"); |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | const Cc = Components.classes; |
michael@0 | 15 | const Ci = Components.interfaces; |
michael@0 | 16 | const Cu = Components.utils; |
michael@0 | 17 | |
michael@0 | 18 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 19 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 20 | Cu.import("resource://gre/modules/ObjectWrapper.jsm"); |
michael@0 | 21 | |
michael@0 | 22 | XPCOMUtils.defineLazyServiceGetter(this, |
michael@0 | 23 | "appsService", |
michael@0 | 24 | "@mozilla.org/AppsService;1", |
michael@0 | 25 | "nsIAppsService"); |
michael@0 | 26 | const NFC_PEER_EVENT_READY = 0x01; |
michael@0 | 27 | const NFC_PEER_EVENT_LOST = 0x02; |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * NFCTag |
michael@0 | 31 | */ |
michael@0 | 32 | function MozNFCTag() { |
michael@0 | 33 | debug("In MozNFCTag Constructor"); |
michael@0 | 34 | this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
michael@0 | 35 | .getService(Ci.nsINfcContentHelper); |
michael@0 | 36 | this.session = null; |
michael@0 | 37 | |
michael@0 | 38 | // Map WebIDL declared enum map names to integer |
michael@0 | 39 | this._techTypesMap = []; |
michael@0 | 40 | this._techTypesMap['NFC_A'] = 0; |
michael@0 | 41 | this._techTypesMap['NFC_B'] = 1; |
michael@0 | 42 | this._techTypesMap['NFC_ISO_DEP'] = 2; |
michael@0 | 43 | this._techTypesMap['NFC_F'] = 3; |
michael@0 | 44 | this._techTypesMap['NFC_V'] = 4; |
michael@0 | 45 | this._techTypesMap['NDEF'] = 5; |
michael@0 | 46 | this._techTypesMap['NDEF_FORMATABLE'] = 6; |
michael@0 | 47 | this._techTypesMap['MIFARE_CLASSIC'] = 7; |
michael@0 | 48 | this._techTypesMap['MIFARE_ULTRALIGHT'] = 8; |
michael@0 | 49 | this._techTypesMap['NFC_BARCODE'] = 9; |
michael@0 | 50 | this._techTypesMap['P2P'] = 10; |
michael@0 | 51 | } |
michael@0 | 52 | MozNFCTag.prototype = { |
michael@0 | 53 | _nfcContentHelper: null, |
michael@0 | 54 | _window: null, |
michael@0 | 55 | |
michael@0 | 56 | initialize: function(aWindow, aSessionToken) { |
michael@0 | 57 | this._window = aWindow; |
michael@0 | 58 | this.setSessionToken(aSessionToken); |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | // ChromeOnly interface |
michael@0 | 62 | setSessionToken: function setSessionToken(aSessionToken) { |
michael@0 | 63 | debug("Setting session token."); |
michael@0 | 64 | this.session = aSessionToken; |
michael@0 | 65 | // report to NFC worker: |
michael@0 | 66 | this._nfcContentHelper.setSessionToken(aSessionToken); |
michael@0 | 67 | }, |
michael@0 | 68 | |
michael@0 | 69 | _techTypesMap: null, |
michael@0 | 70 | |
michael@0 | 71 | // NFCTag interface: |
michael@0 | 72 | getDetailsNDEF: function getDetailsNDEF() { |
michael@0 | 73 | return this._nfcContentHelper.getDetailsNDEF(this._window, this.session); |
michael@0 | 74 | }, |
michael@0 | 75 | readNDEF: function readNDEF() { |
michael@0 | 76 | return this._nfcContentHelper.readNDEF(this._window, this.session); |
michael@0 | 77 | }, |
michael@0 | 78 | writeNDEF: function writeNDEF(records) { |
michael@0 | 79 | return this._nfcContentHelper.writeNDEF(this._window, records, this.session); |
michael@0 | 80 | }, |
michael@0 | 81 | makeReadOnlyNDEF: function makeReadOnlyNDEF() { |
michael@0 | 82 | return this._nfcContentHelper.makeReadOnlyNDEF(this._window, this.session); |
michael@0 | 83 | }, |
michael@0 | 84 | connect: function connect(enum_tech_type) { |
michael@0 | 85 | let int_tech_type = this._techTypesMap[enum_tech_type]; |
michael@0 | 86 | return this._nfcContentHelper.connect(this._window, int_tech_type, this.session); |
michael@0 | 87 | }, |
michael@0 | 88 | close: function close() { |
michael@0 | 89 | return this._nfcContentHelper.close(this._window, this.session); |
michael@0 | 90 | }, |
michael@0 | 91 | |
michael@0 | 92 | classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"), |
michael@0 | 93 | contractID: "@mozilla.org/nfc/NFCTag;1", |
michael@0 | 94 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 95 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * NFCPeer |
michael@0 | 100 | */ |
michael@0 | 101 | function MozNFCPeer() { |
michael@0 | 102 | debug("In MozNFCPeer Constructor"); |
michael@0 | 103 | this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
michael@0 | 104 | .getService(Ci.nsINfcContentHelper); |
michael@0 | 105 | this.session = null; |
michael@0 | 106 | } |
michael@0 | 107 | MozNFCPeer.prototype = { |
michael@0 | 108 | _nfcContentHelper: null, |
michael@0 | 109 | _window: null, |
michael@0 | 110 | |
michael@0 | 111 | initialize: function(aWindow, aSessionToken) { |
michael@0 | 112 | this._window = aWindow; |
michael@0 | 113 | this.setSessionToken(aSessionToken); |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | // ChromeOnly interface |
michael@0 | 117 | setSessionToken: function setSessionToken(aSessionToken) { |
michael@0 | 118 | debug("Setting session token."); |
michael@0 | 119 | this.session = aSessionToken; |
michael@0 | 120 | // report to NFC worker: |
michael@0 | 121 | return this._nfcContentHelper.setSessionToken(aSessionToken); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | // NFCPeer interface: |
michael@0 | 125 | sendNDEF: function sendNDEF(records) { |
michael@0 | 126 | // Just forward sendNDEF to writeNDEF |
michael@0 | 127 | return this._nfcContentHelper.writeNDEF(this._window, records, this.session); |
michael@0 | 128 | }, |
michael@0 | 129 | |
michael@0 | 130 | sendFile: function sendFile(blob) { |
michael@0 | 131 | let data = { |
michael@0 | 132 | "blob": blob |
michael@0 | 133 | }; |
michael@0 | 134 | return this._nfcContentHelper.sendFile(this._window, |
michael@0 | 135 | Cu.cloneInto(data, this._window), |
michael@0 | 136 | this.session); |
michael@0 | 137 | }, |
michael@0 | 138 | |
michael@0 | 139 | classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"), |
michael@0 | 140 | contractID: "@mozilla.org/nfc/NFCPeer;1", |
michael@0 | 141 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 142 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 143 | }; |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Navigator NFC object |
michael@0 | 147 | */ |
michael@0 | 148 | function mozNfc() { |
michael@0 | 149 | debug("In mozNfc Constructor"); |
michael@0 | 150 | try { |
michael@0 | 151 | this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"] |
michael@0 | 152 | .getService(Ci.nsINfcContentHelper); |
michael@0 | 153 | } catch(e) { |
michael@0 | 154 | debug("No NFC support.") |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | mozNfc.prototype = { |
michael@0 | 158 | _nfcContentHelper: null, |
michael@0 | 159 | _window: null, |
michael@0 | 160 | _wrap: function _wrap(obj) { |
michael@0 | 161 | return Cu.cloneInto(obj, this._window); |
michael@0 | 162 | }, |
michael@0 | 163 | |
michael@0 | 164 | init: function init(aWindow) { |
michael@0 | 165 | debug("mozNfc init called"); |
michael@0 | 166 | this._window = aWindow; |
michael@0 | 167 | }, |
michael@0 | 168 | |
michael@0 | 169 | // Only apps which have nfc-manager permission can call the following interfaces |
michael@0 | 170 | // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus', |
michael@0 | 171 | // 'startPoll', 'stopPoll', and 'powerOff'. |
michael@0 | 172 | checkP2PRegistration: function checkP2PRegistration(manifestUrl) { |
michael@0 | 173 | // Get the AppID and pass it to ContentHelper |
michael@0 | 174 | let appID = appsService.getAppLocalIdByManifestURL(manifestUrl); |
michael@0 | 175 | return this._nfcContentHelper.checkP2PRegistration(this._window, appID); |
michael@0 | 176 | }, |
michael@0 | 177 | |
michael@0 | 178 | notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) { |
michael@0 | 179 | let appID = appsService.getAppLocalIdByManifestURL(manifestUrl); |
michael@0 | 180 | // Notify chrome process of user's acknowledgement |
michael@0 | 181 | this._nfcContentHelper.notifyUserAcceptedP2P(this._window, appID); |
michael@0 | 182 | }, |
michael@0 | 183 | |
michael@0 | 184 | notifySendFileStatus: function notifySendFileStatus(status, requestId) { |
michael@0 | 185 | this._nfcContentHelper.notifySendFileStatus(this._window, |
michael@0 | 186 | status, requestId); |
michael@0 | 187 | }, |
michael@0 | 188 | |
michael@0 | 189 | startPoll: function startPoll() { |
michael@0 | 190 | return this._nfcContentHelper.startPoll(this._window); |
michael@0 | 191 | }, |
michael@0 | 192 | |
michael@0 | 193 | stopPoll: function stopPoll() { |
michael@0 | 194 | return this._nfcContentHelper.stopPoll(this._window); |
michael@0 | 195 | }, |
michael@0 | 196 | |
michael@0 | 197 | powerOff: function powerOff() { |
michael@0 | 198 | return this._nfcContentHelper.powerOff(this._window); |
michael@0 | 199 | }, |
michael@0 | 200 | |
michael@0 | 201 | getNFCTag: function getNFCTag(sessionToken) { |
michael@0 | 202 | let obj = new MozNFCTag(); |
michael@0 | 203 | let nfcTag = this._window.MozNFCTag._create(this._window, obj); |
michael@0 | 204 | if (nfcTag) { |
michael@0 | 205 | obj.initialize(this._window, sessionToken); |
michael@0 | 206 | return nfcTag; |
michael@0 | 207 | } else { |
michael@0 | 208 | debug("Error: Unable to create NFCTag"); |
michael@0 | 209 | return null; |
michael@0 | 210 | } |
michael@0 | 211 | }, |
michael@0 | 212 | |
michael@0 | 213 | getNFCPeer: function getNFCPeer(sessionToken) { |
michael@0 | 214 | let obj = new MozNFCPeer(); |
michael@0 | 215 | let nfcPeer = this._window.MozNFCPeer._create(this._window, obj); |
michael@0 | 216 | if (nfcPeer) { |
michael@0 | 217 | obj.initialize(this._window, sessionToken); |
michael@0 | 218 | return nfcPeer; |
michael@0 | 219 | } else { |
michael@0 | 220 | debug("Error: Unable to create NFCPeer"); |
michael@0 | 221 | return null; |
michael@0 | 222 | } |
michael@0 | 223 | }, |
michael@0 | 224 | |
michael@0 | 225 | // get/set onpeerready |
michael@0 | 226 | get onpeerready() { |
michael@0 | 227 | return this.__DOM_IMPL__.getEventHandler("onpeerready"); |
michael@0 | 228 | }, |
michael@0 | 229 | |
michael@0 | 230 | set onpeerready(handler) { |
michael@0 | 231 | this.__DOM_IMPL__.setEventHandler("onpeerready", handler); |
michael@0 | 232 | }, |
michael@0 | 233 | |
michael@0 | 234 | // get/set onpeerlost |
michael@0 | 235 | get onpeerlost() { |
michael@0 | 236 | return this.__DOM_IMPL__.getEventHandler("onpeerlost"); |
michael@0 | 237 | }, |
michael@0 | 238 | |
michael@0 | 239 | set onpeerlost(handler) { |
michael@0 | 240 | this.__DOM_IMPL__.setEventHandler("onpeerlost", handler); |
michael@0 | 241 | }, |
michael@0 | 242 | |
michael@0 | 243 | eventListenerWasAdded: function(evt) { |
michael@0 | 244 | let eventType = this.getEventType(evt); |
michael@0 | 245 | if (eventType == -1) |
michael@0 | 246 | return; |
michael@0 | 247 | this.registerTarget(eventType); |
michael@0 | 248 | }, |
michael@0 | 249 | |
michael@0 | 250 | eventListenerWasRemoved: function(evt) { |
michael@0 | 251 | let eventType = this.getEventType(evt); |
michael@0 | 252 | if (eventType == -1) |
michael@0 | 253 | return; |
michael@0 | 254 | this.unregisterTarget(eventType); |
michael@0 | 255 | }, |
michael@0 | 256 | |
michael@0 | 257 | registerTarget: function registerTarget(event) { |
michael@0 | 258 | let self = this; |
michael@0 | 259 | let appId = this._window.document.nodePrincipal.appId; |
michael@0 | 260 | this._nfcContentHelper.registerTargetForPeerEvent(this._window, appId, |
michael@0 | 261 | event, function(evt, sessionToken) { |
michael@0 | 262 | self.session = sessionToken; |
michael@0 | 263 | self.firePeerEvent(evt, sessionToken); |
michael@0 | 264 | }); |
michael@0 | 265 | }, |
michael@0 | 266 | |
michael@0 | 267 | unregisterTarget: function unregisterTarget(event) { |
michael@0 | 268 | let appId = this._window.document.nodePrincipal.appId; |
michael@0 | 269 | this._nfcContentHelper.unregisterTargetForPeerEvent(this._window, |
michael@0 | 270 | appId, event); |
michael@0 | 271 | }, |
michael@0 | 272 | |
michael@0 | 273 | getEventType: function getEventType(evt) { |
michael@0 | 274 | let eventType = -1; |
michael@0 | 275 | switch (evt) { |
michael@0 | 276 | case 'peerready': |
michael@0 | 277 | eventType = NFC_PEER_EVENT_READY; |
michael@0 | 278 | break; |
michael@0 | 279 | case 'peerlost': |
michael@0 | 280 | eventType = NFC_PEER_EVENT_LOST; |
michael@0 | 281 | break; |
michael@0 | 282 | default: |
michael@0 | 283 | break; |
michael@0 | 284 | } |
michael@0 | 285 | return eventType; |
michael@0 | 286 | }, |
michael@0 | 287 | |
michael@0 | 288 | firePeerEvent: function firePeerEvent(evt, sessionToken) { |
michael@0 | 289 | let peerEvent = (NFC_PEER_EVENT_READY === evt) ? "peerready" : "peerlost"; |
michael@0 | 290 | let detail = { |
michael@0 | 291 | "detail":sessionToken |
michael@0 | 292 | }; |
michael@0 | 293 | let event = new this._window.CustomEvent(peerEvent, this._wrap(detail)); |
michael@0 | 294 | this.__DOM_IMPL__.dispatchEvent(event); |
michael@0 | 295 | }, |
michael@0 | 296 | |
michael@0 | 297 | classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"), |
michael@0 | 298 | contractID: "@mozilla.org/navigatorNfc;1", |
michael@0 | 299 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, |
michael@0 | 300 | Ci.nsIDOMGlobalPropertyInitializer]), |
michael@0 | 301 | }; |
michael@0 | 302 | |
michael@0 | 303 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]); |