Bug 933136 - Part 1: Add Nfc DOM event handlers 'onpeerready' and 'onpeerlost'. r=khuey, sr=smaug
authorSiddartha Pothapragada <Siddartha.Pothapragada@telekom.com>
Sun, 24 Nov 2013 20:40:39 -0800
changeset 174547 404b5d26f2dea26c77300501e94b77167dee293f
parent 174546 c381080acb6bcacd00707945461ca117f3a0cdaa
child 174548 2a7e01f13840b3f6b7cb783f43a6d53ada0a96aa
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey, smaug
bugs933136
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 933136 - Part 1: Add Nfc DOM event handlers 'onpeerready' and 'onpeerlost'. r=khuey, sr=smaug Add new interface 'checkP2PRegistration' to MozNfc and two 'ChromeOnly' interfaces 'eventListenerWasAdded' 'eventListenerWasRemoved' to support basic PeerToPeer use cases.
CLOBBER
dom/nfc/nsNfc.js
dom/webidl/MozNfc.webidl
--- a/CLOBBER
+++ b/CLOBBER
@@ -13,9 +13,9 @@
 #          |               |
 #          O <-- Clobber   O  <-- Clobber
 #
 # Note: The description below will be part of the error message shown to users.
 #
 # Modifying this file will now automatically clobber the buildbot machines \o/
 #
 
-Bug 915533 - Remove unused files under dom/bluetooth.
+Bug 933136 - Windows webidl changes.
--- a/dom/nfc/nsNfc.js
+++ b/dom/nfc/nsNfc.js
@@ -14,24 +14,32 @@ function debug(s) {
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/ObjectWrapper.jsm");
 
+XPCOMUtils.defineLazyServiceGetter(this,
+                                   "appsService",
+                                   "@mozilla.org/AppsService;1",
+                                   "nsIAppsService");
+const NFC_PEER_EVENT_READY = 0x01;
+const NFC_PEER_EVENT_LOST  = 0x02;
+
 /**
  * NFCTag
  */
 function MozNFCTag() {
   debug("In MozNFCTag Constructor");
   this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
                              .getService(Ci.nsINfcContentHelper);
   this.session = null;
+
   // Map WebIDL declared enum map names to integer
   this._techTypesMap = [];
   this._techTypesMap['NFC_A'] = 0;
   this._techTypesMap['NFC_B'] = 1;
   this._techTypesMap['NFC_ISO_DEP'] = 2;
   this._techTypesMap['NFC_F'] = 3;
   this._techTypesMap['NFC_V'] = 4;
   this._techTypesMap['NDEF'] = 5;
@@ -97,82 +105,172 @@ function MozNFCPeer() {
   this.session = null;
 }
 MozNFCPeer.prototype = {
   _nfcContentHelper: null,
   _window: null,
 
   initialize: function(aWindow, aSessionToken) {
     this._window = aWindow;
-    this.setSessionToken(aSessionToken);
+    this.session = aSessionToken;
   },
 
   // ChromeOnly interface
   setSessionToken: function setSessionToken(aSessionToken) {
     debug("Setting session token.");
     this.session = aSessionToken;
     // report to NFC worker:
     return this._nfcContentHelper.setSessionToken(aSessionToken);
   },
 
   // NFCPeer interface:
   sendNDEF: function sendNDEF(records) {
     // Just forward sendNDEF to writeNDEF
-    return this._nfcContentHelper.writeNDEF(this._window, records);
+    return this._nfcContentHelper.writeNDEF(this._window, records, this.session);
   },
 
   classID: Components.ID("{c1b2bcf0-35eb-11e3-aa6e-0800200c9a66}"),
   contractID: "@mozilla.org/nfc/NFCPeer;1",
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
                                          Ci.nsIDOMGlobalPropertyInitializer]),
 };
 
 /**
  * Navigator NFC object
  */
 function mozNfc() {
   debug("In mozNfc Constructor");
+  this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
+                             .getService(Ci.nsINfcContentHelper);
 }
 mozNfc.prototype = {
   _nfcContentHelper: null,
   _window: null,
   _wrap: function _wrap(obj) {
     return ObjectWrapper.wrap(obj, this._window);
   },
 
   init: function init(aWindow) {
     debug("mozNfc init called");
     this._window = aWindow;
+    let origin = this._window.document.nodePrincipal.origin;
+    // Only System Process should listen on 'nfc-p2p-user-accept' event
+    if (origin !== 'app://system.gaiamobile.org') {
+      return;
+    }
+    let self = this;
+    this._window.addEventListener("nfc-p2p-user-accept", function (event) {
+      let appID = appsService.getAppLocalIdByManifestURL(event.detail.manifestUrl);
+      // Notify Chrome process of User's acknowledgement
+      self._nfcContentHelper.notifyUserAcceptedP2P(self._window, appID);
+    });
+  },
+
+  checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
+    // Get the AppID and pass it to ContentHelper
+    let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
+    return this._nfcContentHelper.checkP2PRegistration(this._window, appID);
   },
 
   getNFCTag: function getNFCTag(sessionToken) {
     let obj = new MozNFCTag();
     let nfcTag = this._window.MozNFCTag._create(this._window, obj);
     if (nfcTag) {
       obj.initialize(this._window, sessionToken);
       return nfcTag;
     } else {
       debug("Error: Unable to create NFCTag");
       return null;
     }
   },
 
   getNFCPeer: function getNFCPeer(sessionToken) {
     let obj = new MozNFCPeer();
-    let nfcPeer = this._window.MozNFCTag._create(this._window, obj);
+    let nfcPeer = this._window.MozNFCPeer._create(this._window, obj);
     if (nfcPeer) {
       obj.initialize(this._window, sessionToken);
       return nfcPeer;
     } else {
       debug("Error: Unable to create NFCPeer");
       return null;
     }
   },
 
-  // get/set onpeerfound/lost onforegrounddispatch
+  // get/set onpeerready
+  get onpeerready() {
+    return this.__DOM_IMPL__.getEventHandler("onpeerready");
+  },
+
+  set onpeerready(handler) {
+    this.__DOM_IMPL__.setEventHandler("onpeerready", handler);
+  },
+
+  // get/set onpeerlost
+  get onpeerlost() {
+    return this.__DOM_IMPL__.getEventHandler("onpeerlost");
+  },
+
+  set onpeerlost(handler) {
+    this.__DOM_IMPL__.setEventHandler("onpeerlost", handler);
+  },
+
+  eventListenerWasAdded: function(evt) {
+    let eventType = this.getEventType(evt);
+    if (eventType == -1)
+      return;
+    this.registerTarget(eventType);
+  },
+
+  eventListenerWasRemoved: function(evt) {
+    let eventType = this.getEventType(evt);
+    if (eventType == -1)
+      return;
+    this.unregisterTarget(eventType);
+  },
+
+  registerTarget: function registerTarget(event) {
+    let self = this;
+    let appId = this._window.document.nodePrincipal.appId;
+    this._nfcContentHelper.registerTargetForPeerEvent(this._window, appId,
+      event, function(evt, sessionToken) {
+        self.session = sessionToken;
+        self.firePeerEvent(evt, sessionToken);
+    });
+  },
+
+  unregisterTarget: function unregisterTarget(event) {
+    let appId = this._window.document.nodePrincipal.appId;
+    this._nfcContentHelper.unregisterTargetForPeerEvent(this._window,
+                                                        appId, event);
+  },
+
+  getEventType: function getEventType(evt) {
+    let eventType = -1;
+    switch (evt) {
+      case 'peerready':
+        eventType = NFC_PEER_EVENT_READY;
+        break;
+      case 'peerlost':
+        eventType = NFC_PEER_EVENT_LOST;
+        break;
+      default:
+        break;
+    }
+    return eventType;
+  },
+
+  firePeerEvent: function firePeerEvent(evt, sessionToken) {
+    let peerEvent = (NFC_PEER_EVENT_READY === evt) ? "peerready" : "peerlost";
+    let detail = {
+      "detail":sessionToken
+    };
+    let event = new this._window.CustomEvent(peerEvent,
+      ObjectWrapper.wrap(detail, this._window));
+    this.__DOM_IMPL__.dispatchEvent(event);
+  },
 
   classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
   contractID: "@mozilla.org/navigatorNfc;1",
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
                                          Ci.nsIDOMGlobalPropertyInitializer]),
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);
--- a/dom/webidl/MozNfc.webidl
+++ b/dom/webidl/MozNfc.webidl
@@ -5,12 +5,27 @@
  /* Copyright © 2013 Deutsche Telekom, Inc. */
 
 [JSImplementation="@mozilla.org/navigatorNfc;1",
  NavigatorProperty="mozNfc"]
 interface MozNfc : EventTarget {
    MozNFCTag getNFCTag(DOMString sessionId);
    MozNFCPeer getNFCPeer(DOMString sessionId);
 
-   /*attribute EventHandler onpeerfound;
+   /**
+    * API to check if the given application's manifest
+    * URL is registered with the Chrome Process or not.
+    *
+    * Returns success if given manifestUrl is registered for 'onpeerready',
+    * otherwise error
+    *
+    * Users of this API should have valid permissions 'nfc-manager'
+    * and 'nfc-write'
+    */
+   DOMRequest checkP2PRegistration(DOMString manifestUrl);
+
+   attribute EventHandler onpeerready;
    attribute EventHandler onpeerlost;
-   attribute EventHandler onforegrounddispatch;*/
+   [ChromeOnly]
+   void eventListenerWasAdded(DOMString aType);
+   [ChromeOnly]
+   void eventListenerWasRemoved(DOMString aType);
 };