Bug 1165841 - Part 4: Using eventListeners. r=dimi, a=jocheng
authorYoshi Huang <allstars.chh@mozilla.com>
Tue, 02 Jun 2015 01:39:00 -0400
changeset 238561 b197839c981a06f4f2694ef1c1f777cd95fe4ce8
parent 238560 59fb202660689088f5a8b3a8a4d6ac908c485570
child 238562 5eafee52e9cf77a2dfeb84ec65aa9980e8152e31
push id671
push userryanvm@gmail.com
push dateTue, 02 Jun 2015 15:17:39 +0000
treeherdermozilla-b2g37_v2_2@08da212f538c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi, jocheng
bugs1165841
milestone37.0
Bug 1165841 - Part 4: Using eventListeners. r=dimi, a=jocheng
dom/nfc/NfcContentHelper.js
dom/nfc/gonk/Nfc.js
--- a/dom/nfc/NfcContentHelper.js
+++ b/dom/nfc/NfcContentHelper.js
@@ -67,16 +67,17 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "nsISyncMessageSender");
 
 function NfcContentHelper() {
   Services.obs.addObserver(this, NFC.TOPIC_MOZSETTINGS_CHANGED, false);
   Services.obs.addObserver(this, "xpcom-shutdown", false);
 
   this._requestMap = [];
   this.initDOMRequestHelper(/* window */ null, NFC_IPC_MSG_NAMES);
+  this.eventListeners = {};
 }
 
 NfcContentHelper.prototype = {
   __proto__: DOMRequestIpcHelper.prototype,
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsINfcContentHelper,
                                          Ci.nsINfcBrowserAPI,
                                          Ci.nsISupportsWeakReference,
@@ -85,17 +86,17 @@ NfcContentHelper.prototype = {
   classInfo: XPCOMUtils.generateCI({
     classID:          NFCCONTENTHELPER_CID,
     classDescription: "NfcContentHelper",
     interfaces:       [Ci.nsINfcContentHelper,
                        Ci.nsINfcBrowserAPI]
   }),
 
   _requestMap: null,
-  eventListener: null,
+  eventListeners: null,
 
   queryRFState: function queryRFState() {
     return cpmm.sendSyncMessage("NFC:QueryInfo")[0].rfState;
   },
 
   encodeNDEFRecords: function encodeNDEFRecords(records) {
     if (!Array.isArray(records)) {
       return null;
@@ -202,17 +203,17 @@ NfcContentHelper.prototype = {
       let lock = _window.navigator.mozSettings.createLock();
       var nfcDebug = lock.get(NFC.SETTING_NFC_DEBUG);
       nfcDebug.onsuccess = function _nfcDebug() {
         DEBUG = nfcDebug.result[NFC.SETTING_NFC_DEBUG];
         updateDebug();
       };
     }
 
-    this.eventListener = listener;
+    this.eventListeners[tabId] = listener;
     cpmm.sendAsyncMessage("NFC:AddEventListener", { tabId: tabId });
   },
 
   registerTargetForPeerReady: function registerTargetForPeerReady(appId) {
     cpmm.sendAsyncMessage("NFC:RegisterPeerReadyTarget", { appId: appId });
   },
 
   unregisterTargetForPeerReady: function unregisterTargetForPeerReady(appId) {
@@ -295,54 +296,17 @@ NfcContentHelper.prototype = {
       case "NFC:WriteNDEFResponse": // Fall through.
       case "NFC:MakeReadOnlyResponse":
       case "NFC:FormatResponse":
       case "NFC:NotifySendFileStatusResponse":
       case "NFC:ChangeRFStateResponse":
         this.handleGeneralResponse(result);
         break;
       case "NFC:DOMEvent":
-        switch (result.event) {
-          case NFC.PEER_EVENT_READY:
-            this.eventListener.notifyPeerFound(result.sessionToken, /* isPeerReady */ true);
-            break;
-          case NFC.PEER_EVENT_FOUND:
-            this.eventListener.notifyPeerFound(result.sessionToken);
-            break;
-          case NFC.PEER_EVENT_LOST:
-            this.eventListener.notifyPeerLost(result.sessionToken);
-            break;
-          case NFC.TAG_EVENT_FOUND:
-            let ndefInfo = null;
-            if (result.tagType !== undefined &&
-                result.maxNDEFSize !== undefined &&
-                result.isReadOnly !== undefined &&
-                result.isFormatable !== undefined) {
-              ndefInfo = new TagNDEFInfo(result.tagType,
-                                         result.maxNDEFSize,
-                                         result.isReadOnly,
-                                         result.isFormatable);
-            }
-
-            let tagInfo = new TagInfo(result.techList, result.tagId);
-            this.eventListener.notifyTagFound(result.sessionToken,
-                                              tagInfo,
-                                              ndefInfo,
-                                              result.records);
-            break;
-          case NFC.TAG_EVENT_LOST:
-            this.eventListener.notifyTagLost(result.sessionToken);
-            break;
-          case NFC.RF_EVENT_STATE_CHANGED:
-            this.eventListener.notifyRFStateChanged(result.rfState);
-            break;
-          case NFC.FOCUS_CHANGED:
-            this.eventListener.notifyFocusChanged(result.focus);
-            break;
-        }
+        this.handleDOMEvent(result);
         break;
     }
   },
 
   handle: function handle(name, result) {
     switch (name) {
       case NFC.SETTING_NFC_DEBUG:
         DEBUG = result;
@@ -409,16 +373,63 @@ NfcContentHelper.prototype = {
 
     if (result.errorMsg) {
       callback.notifyError(result.errorMsg);
       return;
     }
 
     callback.notifySuccessWithByteArray(result.response);
   },
+
+  handleDOMEvent: function handleDOMEvent(result) {
+    let listener = this.eventListeners[result.tabId];
+    if (!listener) {
+      debug("no listener for tabId " + result.tabId);
+      return;
+    }
+
+    switch (result.event) {
+      case NFC.PEER_EVENT_READY:
+        listener.notifyPeerFound(result.sessionToken, /* isPeerReady */ true);
+        break;
+      case NFC.PEER_EVENT_FOUND:
+        listener.notifyPeerFound(result.sessionToken);
+        break;
+      case NFC.PEER_EVENT_LOST:
+        listener.notifyPeerLost(result.sessionToken);
+        break;
+      case NFC.TAG_EVENT_FOUND:
+        let ndefInfo = null;
+        if (result.tagType !== undefined &&
+            result.maxNDEFSize !== undefined &&
+            result.isReadOnly !== undefined &&
+            result.isFormatable !== undefined) {
+          ndefInfo = new TagNDEFInfo(result.tagType,
+                                     result.maxNDEFSize,
+                                     result.isReadOnly,
+                                     result.isFormatable);
+        }
+
+        let tagInfo = new TagInfo(result.techList, result.tagId);
+        listener.notifyTagFound(result.sessionToken,
+                                          tagInfo,
+                                          ndefInfo,
+                                          result.records);
+        break;
+      case NFC.TAG_EVENT_LOST:
+        listener.notifyTagLost(result.sessionToken);
+        break;
+      case NFC.RF_EVENT_STATE_CHANGED:
+        listener.notifyRFStateChanged(result.rfState);
+        break;
+      case NFC.FOCUS_CHANGED:
+        listener.notifyFocusChanged(result.focus);
+        break;
+    }
+  }
 };
 
 function TagNDEFInfo(tagType, maxNDEFSize, isReadOnly, isFormatable) {
   this.tagType = tagType;
   this.maxNDEFSize = maxNDEFSize;
   this.isReadOnly = isReadOnly;
   this.isFormatable = isFormatable;
 }
--- a/dom/nfc/gonk/Nfc.js
+++ b/dom/nfc/gonk/Nfc.js
@@ -169,16 +169,17 @@ XPCOMUtils.defineLazyGetter(this, "gMess
     },
 
     notifyDOMEvent: function notifyDOMEvent(target, options) {
       if (!target) {
         dump("invalid target");
         return;
       }
 
+      options.tabId = this.focusApp;
       target.sendAsyncMessage("NFC:DOMEvent", options);
     },
 
     setFocusApp: function setFocusApp(id, isFocus) {
       // if calling setNFCFocus(true) on the browser-element which is already
       // focused, or calling setNFCFocus(false) on the browser-element which has
       // lost focus already, ignore.
       if (isFocus == (id == this.focusApp)) {