Bug 959059: [NFC] CheckP2PRegistration should return error if sessionToken doesn't exist. r=allstars.chh
authorGarner Lee <garner.lee@telekom.com>
Wed, 30 Apr 2014 10:18:00 +0200
changeset 181735 e21efd8655aba1ed691d3b456193109b35ea4f7d
parent 181734 574ed4c4e3ddc73ade3716df844b089b70f7d33f
child 181736 236aa1d2236532370f693ab8c3a01295ce69936f
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersallstars
bugs959059
milestone32.0a1
Bug 959059: [NFC] CheckP2PRegistration should return error if sessionToken doesn't exist. r=allstars.chh
dom/system/gonk/Nfc.js
dom/system/gonk/NfcContentHelper.js
--- a/dom/system/gonk/Nfc.js
+++ b/dom/system/gonk/Nfc.js
@@ -302,16 +302,34 @@ XPCOMUtils.defineLazyGetter(this, "gMess
 
     isValidPeerEvent: function isValidPeerEvent(event) {
       // Valid values : 0x01, 0x02 Or 0x03
       return ((event === NFC.NFC_PEER_EVENT_READY) ||
               (event === NFC.NFC_PEER_EVENT_LOST)  ||
               (event === (NFC.NFC_PEER_EVENT_READY | NFC.NFC_PEER_EVENT_LOST)));
     },
 
+    checkP2PRegistration: function checkP2PRegistration(msg) {
+      // Check if the session and application id yeild a valid registered
+      // target.  It should have registered for NFC_PEER_EVENT_READY
+      let isValid = !!this.nfc.sessionTokenMap[this.nfc._currentSessionId] &&
+                    this.isRegisteredP2PTarget(msg.json.appId,
+                                               NFC.NFC_PEER_EVENT_READY);
+      // Remember the current AppId if registered.
+      this.currentPeerAppId = (isValid) ? msg.json.appId : null;
+      let status = (isValid) ? NFC.GECKO_NFC_ERROR_SUCCESS :
+                               NFC.GECKO_NFC_ERROR_GENERIC_FAILURE;
+
+      // Notify the content process immediately of the status
+      msg.target.sendAsyncMessage(msg.name + "Response", {
+        status: status,
+        requestId: msg.json.requestId
+      });
+    },
+
     /**
      * nsIMessageListener interface methods.
      */
 
     receiveMessage: function receiveMessage(msg) {
       debug("Received '" + msg.name + "' message from content process");
       if (msg.name == "child-process-shutdown") {
         // By the time we receive child-process-shutdown, the child process has
@@ -355,29 +373,17 @@ XPCOMUtils.defineLazyGetter(this, "gMess
           return null;
         case "NFC:RegisterPeerTarget":
           this.registerPeerTarget(msg);
           return null;
         case "NFC:UnregisterPeerTarget":
           this.unregisterPeerTarget(msg);
           return null;
         case "NFC:CheckP2PRegistration":
-          // Check if the application id is a valid registered target.
-          // (It should have registered for NFC_PEER_EVENT_READY).
-          let isRegistered = this.isRegisteredP2PTarget(msg.json.appId,
-                                                        NFC.NFC_PEER_EVENT_READY);
-          // Remember the current AppId if registered.
-          this.currentPeerAppId = (isRegistered) ? msg.json.appId : null;
-          let status = (isRegistered) ? NFC.GECKO_NFC_ERROR_SUCCESS :
-                                        NFC.GECKO_NFC_ERROR_GENERIC_FAILURE;
-          // Notify the content process immediately of the status
-          msg.target.sendAsyncMessage(msg.name + "Response", {
-            status: status,
-            requestId: msg.json.requestId
-          });
+          this.checkP2PRegistration(msg);
           return null;
         case "NFC:NotifyUserAcceptedP2P":
           // Notify the 'NFC_PEER_EVENT_READY' since user has acknowledged
           this.notifyPeerEvent(msg.json.appId, NFC.NFC_PEER_EVENT_READY);
           return null;
         case "NFC:NotifySendFileStatus":
           // Upon receiving the status of sendFile operation, send the response
           // to appropriate content process.
--- a/dom/system/gonk/NfcContentHelper.js
+++ b/dom/system/gonk/NfcContentHelper.js
@@ -402,21 +402,23 @@ NfcContentHelper.prototype = {
 
     switch (message.name) {
       case "NFC:ReadNDEFResponse":
         this.handleReadNDEFResponse(result);
         break;
       case "NFC:GetDetailsNDEFResponse":
         this.handleGetDetailsNDEFResponse(result);
         break;
+      case "NFC:CheckP2PRegistrationResponse":
+        this.handleCheckP2PRegistrationResponse(result);
+        break;
       case "NFC:ConnectResponse": // Fall through.
       case "NFC:CloseResponse":
       case "NFC:WriteNDEFResponse":
       case "NFC:MakeReadOnlyNDEFResponse":
-      case "NFC:CheckP2PRegistrationResponse":
       case "NFC:NotifySendFileStatusResponse":
       case "NFC:ConfigResponse":
         if (result.status !== NFC.GECKO_NFC_ERROR_SUCCESS) {
           this.fireRequestError(atob(result.requestId), result.status);
         } else {
           this.fireRequestSuccess(atob(result.requestId), result);
         }
         break;
@@ -463,13 +465,20 @@ NfcContentHelper.prototype = {
       this.fireRequestError(atob(result.requestId), result.status);
       return;
     }
 
     let requestId = atob(result.requestId);
     let result = new GetDetailsNDEFResponse(result);
     this.fireRequestSuccess(requestId, result);
   },
+
+  handleCheckP2PRegistrationResponse: function handleCheckP2PRegistrationResponse(result) {
+    // Privilaged status API. Always fire success to avoid using exposed props.
+    // The receiver must check the boolean mapped status code to handle.
+    let requestId = atob(result.requestId);
+    this.fireRequestSuccess(requestId, result.status == NFC.GECKO_NFC_ERROR_SUCCESS);
+  },
 };
 
 if (NFC_ENABLED) {
   this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NfcContentHelper]);
 }