dom/nfc/nsNfc.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/nfc/nsNfc.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,303 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/* Copyright © 2013, Deutsche Telekom, Inc. */
     1.9 +
    1.10 +"use strict";
    1.11 +
    1.12 +const DEBUG = false;
    1.13 +function debug(s) {
    1.14 +  if (DEBUG) dump("-*- Nfc DOM: " + s + "\n");
    1.15 +}
    1.16 +
    1.17 +const Cc = Components.classes;
    1.18 +const Ci = Components.interfaces;
    1.19 +const Cu = Components.utils;
    1.20 +
    1.21 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.22 +Cu.import("resource://gre/modules/Services.jsm");
    1.23 +Cu.import("resource://gre/modules/ObjectWrapper.jsm");
    1.24 +
    1.25 +XPCOMUtils.defineLazyServiceGetter(this,
    1.26 +                                   "appsService",
    1.27 +                                   "@mozilla.org/AppsService;1",
    1.28 +                                   "nsIAppsService");
    1.29 +const NFC_PEER_EVENT_READY = 0x01;
    1.30 +const NFC_PEER_EVENT_LOST  = 0x02;
    1.31 +
    1.32 +/**
    1.33 + * NFCTag
    1.34 + */
    1.35 +function MozNFCTag() {
    1.36 +  debug("In MozNFCTag Constructor");
    1.37 +  this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
    1.38 +                             .getService(Ci.nsINfcContentHelper);
    1.39 +  this.session = null;
    1.40 +
    1.41 +  // Map WebIDL declared enum map names to integer
    1.42 +  this._techTypesMap = [];
    1.43 +  this._techTypesMap['NFC_A'] = 0;
    1.44 +  this._techTypesMap['NFC_B'] = 1;
    1.45 +  this._techTypesMap['NFC_ISO_DEP'] = 2;
    1.46 +  this._techTypesMap['NFC_F'] = 3;
    1.47 +  this._techTypesMap['NFC_V'] = 4;
    1.48 +  this._techTypesMap['NDEF'] = 5;
    1.49 +  this._techTypesMap['NDEF_FORMATABLE'] = 6;
    1.50 +  this._techTypesMap['MIFARE_CLASSIC'] = 7;
    1.51 +  this._techTypesMap['MIFARE_ULTRALIGHT'] = 8;
    1.52 +  this._techTypesMap['NFC_BARCODE'] = 9;
    1.53 +  this._techTypesMap['P2P'] = 10;
    1.54 +}
    1.55 +MozNFCTag.prototype = {
    1.56 +  _nfcContentHelper: null,
    1.57 +  _window: null,
    1.58 +
    1.59 +  initialize: function(aWindow, aSessionToken) {
    1.60 +    this._window = aWindow;
    1.61 +    this.setSessionToken(aSessionToken);
    1.62 +  },
    1.63 +
    1.64 +  // ChromeOnly interface
    1.65 +  setSessionToken: function setSessionToken(aSessionToken) {
    1.66 +    debug("Setting session token.");
    1.67 +    this.session = aSessionToken;
    1.68 +    // report to NFC worker:
    1.69 +    this._nfcContentHelper.setSessionToken(aSessionToken);
    1.70 +  },
    1.71 +
    1.72 +  _techTypesMap: null,
    1.73 +
    1.74 +  // NFCTag interface:
    1.75 +  getDetailsNDEF: function getDetailsNDEF() {
    1.76 +    return this._nfcContentHelper.getDetailsNDEF(this._window, this.session);
    1.77 +  },
    1.78 +  readNDEF: function readNDEF() {
    1.79 +    return this._nfcContentHelper.readNDEF(this._window, this.session);
    1.80 +  },
    1.81 +  writeNDEF: function writeNDEF(records) {
    1.82 +    return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
    1.83 +  },
    1.84 +  makeReadOnlyNDEF: function makeReadOnlyNDEF() {
    1.85 +    return this._nfcContentHelper.makeReadOnlyNDEF(this._window, this.session);
    1.86 +  },
    1.87 +  connect: function connect(enum_tech_type) {
    1.88 +    let int_tech_type = this._techTypesMap[enum_tech_type];
    1.89 +    return this._nfcContentHelper.connect(this._window, int_tech_type, this.session);
    1.90 +  },
    1.91 +  close: function close() {
    1.92 +    return this._nfcContentHelper.close(this._window, this.session);
    1.93 +  },
    1.94 +
    1.95 +  classID: Components.ID("{4e1e2e90-3137-11e3-aa6e-0800200c9a66}"),
    1.96 +  contractID: "@mozilla.org/nfc/NFCTag;1",
    1.97 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
    1.98 +                                         Ci.nsIDOMGlobalPropertyInitializer]),
    1.99 +};
   1.100 +
   1.101 +/**
   1.102 + * NFCPeer
   1.103 + */
   1.104 +function MozNFCPeer() {
   1.105 +  debug("In MozNFCPeer Constructor");
   1.106 +  this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
   1.107 +                             .getService(Ci.nsINfcContentHelper);
   1.108 +  this.session = null;
   1.109 +}
   1.110 +MozNFCPeer.prototype = {
   1.111 +  _nfcContentHelper: null,
   1.112 +  _window: null,
   1.113 +
   1.114 +  initialize: function(aWindow, aSessionToken) {
   1.115 +    this._window = aWindow;
   1.116 +    this.setSessionToken(aSessionToken);
   1.117 +  },
   1.118 +
   1.119 +  // ChromeOnly interface
   1.120 +  setSessionToken: function setSessionToken(aSessionToken) {
   1.121 +    debug("Setting session token.");
   1.122 +    this.session = aSessionToken;
   1.123 +    // report to NFC worker:
   1.124 +    return this._nfcContentHelper.setSessionToken(aSessionToken);
   1.125 +  },
   1.126 +
   1.127 +  // NFCPeer interface:
   1.128 +  sendNDEF: function sendNDEF(records) {
   1.129 +    // Just forward sendNDEF to writeNDEF
   1.130 +    return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
   1.131 +  },
   1.132 +
   1.133 +  sendFile: function sendFile(blob) {
   1.134 +    let data = {
   1.135 +      "blob": blob
   1.136 +    };
   1.137 +    return this._nfcContentHelper.sendFile(this._window,
   1.138 +                                           Cu.cloneInto(data, this._window),
   1.139 +                                           this.session);
   1.140 +  },
   1.141 +
   1.142 +  classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"),
   1.143 +  contractID: "@mozilla.org/nfc/NFCPeer;1",
   1.144 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
   1.145 +                                         Ci.nsIDOMGlobalPropertyInitializer]),
   1.146 +};
   1.147 +
   1.148 +/**
   1.149 + * Navigator NFC object
   1.150 + */
   1.151 +function mozNfc() {
   1.152 +  debug("In mozNfc Constructor");
   1.153 +  try {
   1.154 +    this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
   1.155 +                               .getService(Ci.nsINfcContentHelper);
   1.156 +  } catch(e) {
   1.157 +    debug("No NFC support.")
   1.158 +  }
   1.159 +}
   1.160 +mozNfc.prototype = {
   1.161 +  _nfcContentHelper: null,
   1.162 +  _window: null,
   1.163 +  _wrap: function _wrap(obj) {
   1.164 +    return Cu.cloneInto(obj, this._window);
   1.165 +  },
   1.166 +
   1.167 +  init: function init(aWindow) {
   1.168 +    debug("mozNfc init called");
   1.169 +    this._window = aWindow;
   1.170 +  },
   1.171 +
   1.172 +  // Only apps which have nfc-manager permission can call the following interfaces
   1.173 +  // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
   1.174 +  // 'startPoll', 'stopPoll', and 'powerOff'.
   1.175 +  checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
   1.176 +    // Get the AppID and pass it to ContentHelper
   1.177 +    let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
   1.178 +    return this._nfcContentHelper.checkP2PRegistration(this._window, appID);
   1.179 +  },
   1.180 +
   1.181 +  notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) {
   1.182 +    let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
   1.183 +    // Notify chrome process of user's acknowledgement
   1.184 +    this._nfcContentHelper.notifyUserAcceptedP2P(this._window, appID);
   1.185 +  },
   1.186 +
   1.187 +  notifySendFileStatus: function notifySendFileStatus(status, requestId) {
   1.188 +    this._nfcContentHelper.notifySendFileStatus(this._window,
   1.189 +                                                status, requestId);
   1.190 +  },
   1.191 +
   1.192 +  startPoll: function startPoll() {
   1.193 +    return this._nfcContentHelper.startPoll(this._window);
   1.194 +  },
   1.195 +
   1.196 +  stopPoll: function stopPoll() {
   1.197 +    return this._nfcContentHelper.stopPoll(this._window);
   1.198 +  },
   1.199 +
   1.200 +  powerOff: function powerOff() {
   1.201 +    return this._nfcContentHelper.powerOff(this._window);
   1.202 +  },
   1.203 +
   1.204 +  getNFCTag: function getNFCTag(sessionToken) {
   1.205 +    let obj = new MozNFCTag();
   1.206 +    let nfcTag = this._window.MozNFCTag._create(this._window, obj);
   1.207 +    if (nfcTag) {
   1.208 +      obj.initialize(this._window, sessionToken);
   1.209 +      return nfcTag;
   1.210 +    } else {
   1.211 +      debug("Error: Unable to create NFCTag");
   1.212 +      return null;
   1.213 +    }
   1.214 +  },
   1.215 +
   1.216 +  getNFCPeer: function getNFCPeer(sessionToken) {
   1.217 +    let obj = new MozNFCPeer();
   1.218 +    let nfcPeer = this._window.MozNFCPeer._create(this._window, obj);
   1.219 +    if (nfcPeer) {
   1.220 +      obj.initialize(this._window, sessionToken);
   1.221 +      return nfcPeer;
   1.222 +    } else {
   1.223 +      debug("Error: Unable to create NFCPeer");
   1.224 +      return null;
   1.225 +    }
   1.226 +  },
   1.227 +
   1.228 +  // get/set onpeerready
   1.229 +  get onpeerready() {
   1.230 +    return this.__DOM_IMPL__.getEventHandler("onpeerready");
   1.231 +  },
   1.232 +
   1.233 +  set onpeerready(handler) {
   1.234 +    this.__DOM_IMPL__.setEventHandler("onpeerready", handler);
   1.235 +  },
   1.236 +
   1.237 +  // get/set onpeerlost
   1.238 +  get onpeerlost() {
   1.239 +    return this.__DOM_IMPL__.getEventHandler("onpeerlost");
   1.240 +  },
   1.241 +
   1.242 +  set onpeerlost(handler) {
   1.243 +    this.__DOM_IMPL__.setEventHandler("onpeerlost", handler);
   1.244 +  },
   1.245 +
   1.246 +  eventListenerWasAdded: function(evt) {
   1.247 +    let eventType = this.getEventType(evt);
   1.248 +    if (eventType == -1)
   1.249 +      return;
   1.250 +    this.registerTarget(eventType);
   1.251 +  },
   1.252 +
   1.253 +  eventListenerWasRemoved: function(evt) {
   1.254 +    let eventType = this.getEventType(evt);
   1.255 +    if (eventType == -1)
   1.256 +      return;
   1.257 +    this.unregisterTarget(eventType);
   1.258 +  },
   1.259 +
   1.260 +  registerTarget: function registerTarget(event) {
   1.261 +    let self = this;
   1.262 +    let appId = this._window.document.nodePrincipal.appId;
   1.263 +    this._nfcContentHelper.registerTargetForPeerEvent(this._window, appId,
   1.264 +      event, function(evt, sessionToken) {
   1.265 +        self.session = sessionToken;
   1.266 +        self.firePeerEvent(evt, sessionToken);
   1.267 +    });
   1.268 +  },
   1.269 +
   1.270 +  unregisterTarget: function unregisterTarget(event) {
   1.271 +    let appId = this._window.document.nodePrincipal.appId;
   1.272 +    this._nfcContentHelper.unregisterTargetForPeerEvent(this._window,
   1.273 +                                                        appId, event);
   1.274 +  },
   1.275 +
   1.276 +  getEventType: function getEventType(evt) {
   1.277 +    let eventType = -1;
   1.278 +    switch (evt) {
   1.279 +      case 'peerready':
   1.280 +        eventType = NFC_PEER_EVENT_READY;
   1.281 +        break;
   1.282 +      case 'peerlost':
   1.283 +        eventType = NFC_PEER_EVENT_LOST;
   1.284 +        break;
   1.285 +      default:
   1.286 +        break;
   1.287 +    }
   1.288 +    return eventType;
   1.289 +  },
   1.290 +
   1.291 +  firePeerEvent: function firePeerEvent(evt, sessionToken) {
   1.292 +    let peerEvent = (NFC_PEER_EVENT_READY === evt) ? "peerready" : "peerlost";
   1.293 +    let detail = {
   1.294 +      "detail":sessionToken
   1.295 +    };
   1.296 +    let event = new this._window.CustomEvent(peerEvent, this._wrap(detail));
   1.297 +    this.__DOM_IMPL__.dispatchEvent(event);
   1.298 +  },
   1.299 +
   1.300 +  classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
   1.301 +  contractID: "@mozilla.org/navigatorNfc;1",
   1.302 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
   1.303 +                                         Ci.nsIDOMGlobalPropertyInitializer]),
   1.304 +};
   1.305 +
   1.306 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);

mercurial