Bug 1069177 - B2G NFC: rename nsINFCPeerEventListener to nsINfcDOMEventTarget. r=dimi
authorYoshi Huang <allstars.chh@mozilla.com>
Wed, 24 Sep 2014 14:52:04 +0800
changeset 207558 99be50ec39a5c26fb5c2df375bd92cffc1886d56
parent 207557 3efd63648a1bbb14acbc6a63de5074614265ce98
child 207559 6d2c7b4931bbda4a821a09e03d9e503005b609c7
push id11005
push useryhuang@mozilla.com
push dateMon, 29 Sep 2014 09:57:00 +0000
treeherderb2g-inbound@99be50ec39a5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi
bugs1069177
milestone35.0a1
Bug 1069177 - B2G NFC: rename nsINFCPeerEventListener to nsINfcDOMEventTarget. r=dimi
dom/nfc/NfcContentHelper.js
dom/nfc/gonk/Nfc.js
dom/nfc/nsINfcContentHelper.idl
dom/nfc/nsNfc.js
--- a/dom/nfc/NfcContentHelper.js
+++ b/dom/nfc/NfcContentHelper.js
@@ -50,17 +50,17 @@ const NFCCONTENTHELPER_CID =
 const NFC_IPC_MSG_NAMES = [
   "NFC:ReadNDEFResponse",
   "NFC:WriteNDEFResponse",
   "NFC:GetDetailsNDEFResponse",
   "NFC:MakeReadOnlyNDEFResponse",
   "NFC:ConnectResponse",
   "NFC:CloseResponse",
   "NFC:CheckP2PRegistrationResponse",
-  "NFC:PeerEvent",
+  "NFC:DOMEvent",
   "NFC:NotifySendFileStatusResponse",
   "NFC:ConfigResponse"
 ];
 
 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                    "@mozilla.org/childprocessmessagemanager;1",
                                    "nsISyncMessageSender");
 
@@ -91,17 +91,17 @@ NfcContentHelper.prototype = {
   classID:   NFCCONTENTHELPER_CID,
   classInfo: XPCOMUtils.generateCI({
     classID:          NFCCONTENTHELPER_CID,
     classDescription: "NfcContentHelper",
     interfaces:       [Ci.nsINfcContentHelper]
   }),
 
   _requestMap: null,
-  peerEventListener: null,
+  eventTarget: null,
 
   encodeNDEFRecords: function encodeNDEFRecords(records) {
     let encodedRecords = [];
     for (let i = 0; i < records.length; i++) {
       let record = records[i];
       encodedRecords.push({
         tnf: record.tnf,
         type: record.type || undefined,
@@ -252,18 +252,19 @@ NfcContentHelper.prototype = {
     }
 
     cpmm.sendAsyncMessage("NFC:NotifySendFileStatus", {
       status: status,
       requestId: requestId
     });
   },
 
-  registerPeerEventListener: function registerPeerEventListener(listener) {
-    this.peerEventListener = listener;
+  registerEventTarget: function registerEventTarget(target) {
+    this.eventTarget = target;
+    cpmm.sendAsyncMessage("NFC:AddEventTarget");
   },
 
   registerTargetForPeerReady: function registerTargetForPeerReady(window, appId) {
     if (window == null) {
       throw Components.Exception("Can't get window object",
                                   Cr.NS_ERROR_UNEXPECTED);
     }
 
@@ -407,23 +408,23 @@ NfcContentHelper.prototype = {
       case "NFC:NotifySendFileStatusResponse":
       case "NFC:ConfigResponse":
         if (result.errorMsg) {
           this.fireRequestError(atob(result.requestId), result.errorMsg);
         } else {
           this.fireRequestSuccess(atob(result.requestId), result);
         }
         break;
-      case "NFC:PeerEvent":
+      case "NFC:DOMEvent":
         switch (result.event) {
           case NFC.NFC_PEER_EVENT_READY:
-            this.peerEventListener.notifyPeerReady(result.sessionToken);
+            this.eventTarget.notifyPeerReady(result.sessionToken);
             break;
           case NFC.NFC_PEER_EVENT_LOST:
-            this.peerEventListener.notifyPeerLost(result.sessionToken);
+            this.eventTarget.notifyPeerLost(result.sessionToken);
             break;
         }
         break;
     }
   },
 
   handleReadNDEFResponse: function handleReadNDEFResponse(result) {
     let requester = this._requestMap[result.requestId];
--- a/dom/nfc/gonk/Nfc.js
+++ b/dom/nfc/gonk/Nfc.js
@@ -42,16 +42,20 @@ if (DEBUG) {
 } else {
   debug = function (s) {};
 }
 
 const NFC_CONTRACTID = "@mozilla.org/nfc;1";
 const NFC_CID =
   Components.ID("{2ff24790-5e74-11e1-b86c-0800200c9a66}");
 
+const NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES = [
+  "NFC:AddEventTarget"
+];
+
 const NFC_IPC_MSG_NAMES = [
   "NFC:CheckSessionToken"
 ];
 
 const NFC_IPC_READ_PERM_MSG_NAMES = [
   "NFC:ReadNDEF",
   "NFC:GetDetailsNDEF",
   "NFC:Connect",
@@ -90,16 +94,18 @@ XPCOMUtils.defineLazyGetter(this, "gMess
                                            Ci.nsIObserver]),
 
     nfc: null,
 
     // Manage registered Peer Targets
     peerTargets: {},
     currentPeer: null,
 
+    eventTargets: [],
+
     init: function init(nfc) {
       this.nfc = nfc;
 
       Services.obs.addObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN, false);
       this._registerMessageListeners();
     },
 
     _shutdown: function _shutdown() {
@@ -108,16 +114,20 @@ XPCOMUtils.defineLazyGetter(this, "gMess
 
       Services.obs.removeObserver(this, NFC.TOPIC_XPCOM_SHUTDOWN);
       this._unregisterMessageListeners();
     },
 
     _registerMessageListeners: function _registerMessageListeners() {
       ppmm.addMessageListener("child-process-shutdown", this);
 
+      for (let message of NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES) {
+        ppmm.addMessageListener(message, this);
+      }
+
       for (let message of NFC_IPC_MSG_NAMES) {
         ppmm.addMessageListener(message, this);
       }
 
       for (let message of NFC_IPC_READ_PERM_MSG_NAMES) {
         ppmm.addMessageListener(message, this);
       }
 
@@ -128,16 +138,20 @@ XPCOMUtils.defineLazyGetter(this, "gMess
       for (let message of NFC_IPC_MANAGER_PERM_MSG_NAMES) {
         ppmm.addMessageListener(message, this);
       }
     },
 
     _unregisterMessageListeners: function _unregisterMessageListeners() {
       ppmm.removeMessageListener("child-process-shutdown", this);
 
+      for (let message of NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES) {
+        ppmm.removeMessageListener(message, this);
+      }
+
       for (let message of NFC_IPC_MSG_NAMES) {
         ppmm.removeMessageListener(message, this);
       }
 
       for (let message of NFC_IPC_READ_PERM_MSG_NAMES) {
         ppmm.removeMessageListener(message, this);
       }
 
@@ -170,26 +184,38 @@ XPCOMUtils.defineLazyGetter(this, "gMess
           if (this.currentPeer === target) {
             this.currentPeer = null;
           }
           delete this.peerTargets[appId];
         }
       });
     },
 
-    notifyPeerEvent: function notifyPeerEvent(target, event, sessionToken) {
+    notifyDOMEvent: function notifyDOMEvent(target, options) {
       if (!target) {
         dump("invalid target");
         return;
       }
 
-      target.sendAsyncMessage("NFC:PeerEvent", {
-        event: event,
-        sessionToken: sessionToken
-      });
+      target.sendAsyncMessage("NFC:DOMEvent", options);
+    },
+
+    addEventTarget: function addEventTarget(target) {
+      if (this.eventTargets.indexOf(target) != -1) {
+        return;
+      }
+
+      this.eventTargets.push(target);
+    },
+
+    removeEventTarget: function removeEventTarget(target) {
+      let index = this.eventTargets.indexOf(target);
+      if (index != -1) {
+        delete this.eventTargets[index];
+      }
     },
 
     checkP2PRegistration: function checkP2PRegistration(message) {
       let target = this.peerTargets[message.data.appId];
       let sessionToken = SessionHelper.getCurrentP2PToken();
       let isValid = (sessionToken != null) && (target != null);
       let respMsg = { requestId: message.data.requestId };
       if (!isValid) {
@@ -205,45 +231,49 @@ XPCOMUtils.defineLazyGetter(this, "gMess
       let isValid = (sessionToken != null) && (target != null);
       if (!isValid) {
         debug("Peer already lost or " + appId + " is not a registered PeerReadytarget");
         return;
       }
 
       // Remember the target that receives onpeerready.
       this.currentPeer = target;
-      this.notifyPeerEvent(target, NFC.NFC_PEER_EVENT_READY, sessionToken);
+      this.notifyDOMEvent(target, {event: NFC.NFC_PEER_EVENT_READY,
+                                   sessionToken: sessionToken});
     },
 
     onPeerLost: function onPeerLost(sessionToken) {
       if (!this.currentPeer) {
         // The target is already killed.
         return;
       }
 
       // For peerlost, the message is delievered to the target which
       // onpeerready has been called before.
-      this.notifyPeerEvent(this.currentPeer, NFC.NFC_PEER_EVENT_LOST, sessionToken);
+      this.notifyDOMEvent(this.currentPeer, {event: NFC.NFC_PEER_EVENT_LOST,
+                                             sessionToken: sessionToken});
       this.currentPeer = null;
     },
 
     /**
      * nsIMessageListener interface methods.
      */
 
     receiveMessage: function receiveMessage(message) {
       DEBUG && debug("Received message from content process: " + JSON.stringify(message));
 
       if (message.name == "child-process-shutdown") {
         this.removePeerTarget(message.target);
         this.nfc.removeTarget(message.target);
+        this.removeEventTarget(msg.target);
         return null;
       }
 
-      if (NFC_IPC_MSG_NAMES.indexOf(message.name) != -1) {
+      if (NFC_IPC_MSG_NAMES.indexOf(message.name) != -1 ||
+          NFC_IPC_ADD_EVENT_TARGET_MSG_NAMES.indexOf(message.name) != -1 ) {
         // Do nothing.
       } else if (NFC_IPC_READ_PERM_MSG_NAMES.indexOf(message.name) != -1) {
         if (!message.target.assertPermission("nfc-read")) {
           debug("Nfc message " + message.name +
                 " from a content process with no 'nfc-read' privileges.");
           return null;
         }
       } else if (NFC_IPC_WRITE_PERM_MSG_NAMES.indexOf(message.name) != -1) {
@@ -259,16 +289,19 @@ XPCOMUtils.defineLazyGetter(this, "gMess
           return null;
         }
       } else {
         debug("Ignoring unknown message type: " + message.name);
         return null;
       }
 
       switch (message.name) {
+        case "NFC:AddEventTarget":
+          this.addEventTarget(message.target);
+          return null;
         case "NFC:CheckSessionToken":
           if (!SessionHelper.isValidToken(message.data.sessionToken)) {
             debug("Received invalid Session Token: " + message.data.sessionToken);
             return NFC.NFC_ERROR_BAD_SESSION_ID;
           }
           return NFC.NFC_SUCCESS;
         case "NFC:RegisterPeerReadyTarget":
           this.registerPeerReadyTarget(message.target, message.data.appId);
@@ -467,16 +500,17 @@ Nfc.prototype = {
       case "InitializedNotification":
         // Do nothing.
         break;
       case "TechDiscoveredNotification":
         message.type = "techDiscovered";
         // Update the upper layers with a session token (alias)
         message.sessionToken =
           SessionHelper.registerSession(message.sessionId, message.techList);
+
         // Do not expose the actual session to the content
         delete message.sessionId;
 
         gSystemMessenger.broadcastMessage("nfc-manager-tech-discovered", message);
         break;
       case "TechLostNotification":
         message.type = "techLost";
 
--- a/dom/nfc/nsINfcContentHelper.idl
+++ b/dom/nfc/nsINfcContentHelper.idl
@@ -2,18 +2,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 #include "nsIDOMDOMRequest.idl"
 
 interface nsIVariant;
 
-[scriptable, uuid(57fc2998-1058-4fd5-8dd9-0e303218d5fd)]
-interface nsINfcPeerEventListener : nsISupports
+[scriptable, uuid(e81cc1ac-6f0b-4581-a9fb-7ee47ed0158e)]
+interface nsINfcDOMEventTarget : nsISupports
 {
   /**
    * Callback function used to notify peerready.
    *
    * @param sessionToken
    *        SessionToken received from Chrome process
    */
    void notifyPeerReady(in DOMString sessionToken);
@@ -22,17 +22,17 @@ interface nsINfcPeerEventListener : nsIS
    * Callback function used to notify peerlost.
    *
    * @param sessionToken
    *        SessionToken received from Chrome process
    */
    void notifyPeerLost(in DOMString sessionToken);
 };
 
-[scriptable, uuid(9a41d969-3375-4933-814e-2da781c8f691)]
+[scriptable, uuid(7eaf4c31-e1d1-422e-aa55-181f4eb156b0)]
 interface nsINfcContentHelper : nsISupports
 {
   const long NFC_EVENT_PEER_READY = 0x01;
   const long NFC_EVENT_PEER_LOST  = 0x02;
 
   boolean checkSessionToken(in DOMString sessionToken);
 
   nsIDOMDOMRequest getDetailsNDEF(in nsIDOMWindow window, in DOMString sessionToken);
@@ -60,21 +60,21 @@ interface nsINfcContentHelper : nsISuppo
   * Returns DOMRequest, if initiation of send file operation is successful
   * then 'onsuccess' is called else 'onerror'
   */
   nsIDOMDOMRequest sendFile(in nsIDOMWindow window,
                             in jsval blob,
                             in DOMString sessionToken);
 
   /**
-   * Register the peer event listener.
+   * Register the event target.
    *
-   * @param listener  An instance of the nsINfcPeerEventListener.
+   * @param target  An instance of the nsINfcDOMEventTarget.
    */
-  void registerPeerEventListener(in nsINfcPeerEventListener listener);
+  void registerEventTarget(in nsINfcDOMEventTarget target);
 
  /**
   * Register the given application id with Chrome process
   *
   * @param window
   *        Current window
   *
   * @param appId
--- a/dom/nfc/nsNfc.js
+++ b/dom/nfc/nsNfc.js
@@ -133,17 +133,17 @@ function mozNfc() {
   debug("In mozNfc Constructor");
   try {
     this._nfcContentHelper = Cc["@mozilla.org/nfc/content-helper;1"]
                                .getService(Ci.nsINfcContentHelper);
   } catch(e) {
     debug("No NFC support.")
   }
 
-  this._nfcContentHelper.registerPeerEventListener(this);
+  this._nfcContentHelper.registerEventTarget(this);
 }
 mozNfc.prototype = {
   _nfcContentHelper: null,
   _window: null,
   nfcObject: null,
 
   init: function init(aWindow) {
     debug("mozNfc init called");
@@ -284,12 +284,12 @@ mozNfc.prototype = {
   hasDeadWrapper: function hasDeadWrapper() {
     return Cu.isDeadWrapper(this._window) || Cu.isDeadWrapper(this.__DOM_IMPL__);
   },
 
   classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
   contractID: "@mozilla.org/navigatorNfc;1",
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
                                          Ci.nsIDOMGlobalPropertyInitializer,
-                                         Ci.nsINfcPeerEventListener]),
+                                         Ci.nsINfcDOMEventTarget]),
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozNFCTag, MozNFCPeer, mozNfc]);