Bug 1168292 - Part 2: notify listener with correct tabId. r=dimi
authorYoshi Huang <allstars.chh@mozilla.com>
Wed, 27 May 2015 17:31:43 +0800
changeset 246152 222ada9adef3601f9dec836ac2d606d3d1e25c91
parent 246151 a16ea3be66e5653e9028963567324c4ccedbb03e
child 246153 047f199393d844d6eadc921a954ad12067730bd4
push id60388
push userryanvm@gmail.com
push dateFri, 29 May 2015 13:44:22 +0000
treeherdermozilla-inbound@6b20dd2e20cc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi
bugs1168292
milestone41.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 1168292 - Part 2: notify listener with correct tabId. r=dimi
dom/nfc/gonk/Nfc.js
--- a/dom/nfc/gonk/Nfc.js
+++ b/dom/nfc/gonk/Nfc.js
@@ -209,23 +209,36 @@ XPCOMUtils.defineLazyGetter(this, "gMess
     removePeerTarget: function removePeerTarget(target) {
       Object.keys(this.peerTargets).forEach((appId) => {
         if (this.peerTargets[appId] === target) {
           delete this.peerTargets[appId];
         }
       });
     },
 
+    notifyFocusApp: function notifyFocusApp(options) {
+      let target, tabId;
+      if (this.eventListeners[this.focusApp]) {
+        target = this.eventListeners[this.focusApp];
+        tabId = this.focusApp;
+      } else {
+        target = this.eventListeners[NFC.SYSTEM_APP_ID];
+        tabId = NFC.SYSTEM_APP_ID;
+      }
+      options.tabId = tabId;
+
+      this.notifyDOMEvent(target, options);
+    },
+
     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)) {
@@ -283,17 +296,18 @@ XPCOMUtils.defineLazyGetter(this, "gMess
       let target = this.peerTargets[appId];
       let sessionToken = SessionHelper.getCurrentP2PToken();
       let isValid = (sessionToken != null) && (target != null);
       if (!isValid) {
         debug("Peer already lost or " + appId + " is not a registered PeerReadytarget");
         return;
       }
 
-      this.notifyDOMEvent(target, {event: NFC.PEER_EVENT_READY,
+      this.notifyDOMEvent(target, {tabId: this.focusApp,
+                                   event: NFC.PEER_EVENT_READY,
                                    sessionToken: sessionToken});
     },
 
     notifySendFileStatus: function notifySendFileStatus(message) {
       if (message.data.status) {
         message.data.errorMsg =
             this.nfc.getErrorMessage(NFC.NFC_GECKO_ERROR_SEND_FILE_FAILED);
       }
@@ -308,57 +322,48 @@ XPCOMUtils.defineLazyGetter(this, "gMess
     },
 
     callDefaultLostHandler: function callDefaultLostHandler(message) {
       // message.isP2P is not used.
       gSystemMessenger.broadcastMessage("nfc-manager-tech-lost", message.sessionToken);
     },
 
     onTagFound: function onTagFound(message) {
-      let target = this.eventListeners[this.focusApp] ||
-                   this.eventListeners[NFC.SYSTEM_APP_ID];
-
       message.event = NFC.TAG_EVENT_FOUND;
-
-      this.notifyDOMEvent(target, message);
-
+      this.notifyFocusApp(message);
       delete message.event;
     },
 
     onTagLost: function onTagLost(sessionToken) {
-      let target = this.eventListeners[this.focusApp] ||
-                   this.eventListeners[NFC.SYSTEM_APP_ID];
-
-      this.notifyDOMEvent(target, { event: NFC.TAG_EVENT_LOST,
-                                    sessionToken: sessionToken });
+      this.notifyFocusApp({ event: NFC.TAG_EVENT_LOST,
+                            sessionToken: sessionToken });
     },
 
     onPeerEvent: function onPeerEvent(eventType, sessionToken) {
-      let target = this.eventListeners[this.focusApp] ||
-                   this.eventListeners[NFC.SYSTEM_APP_ID];
-
-      this.notifyDOMEvent(target, { event: eventType,
-                                    sessionToken: sessionToken });
+      this.notifyFocusApp({ event: eventType,
+                            sessionToken: sessionToken });
     },
 
     onRFStateChanged: function onRFStateChanged(rfState) {
       for (let id in this.eventListeners) {
         this.notifyDOMEvent(this.eventListeners[id],
-                            { event: NFC.RF_EVENT_STATE_CHANGED,
+                            { tabId: id,
+                              event: NFC.RF_EVENT_STATE_CHANGED,
                               rfState: rfState });
       }
     },
 
     onFocusChanged: function onFocusChanged(focusApp, focus) {
       let target = this.eventListeners[focusApp];
       if (!target) {
         return;
       }
 
-      this.notifyDOMEvent(target, { event: NFC.FOCUS_CHANGED,
+      this.notifyDOMEvent(target, { tabId: this.focusApp,
+                                    event: NFC.FOCUS_CHANGED,
                                     focus: focus });
     },
 
     /**
      * nsIMessageListener interface methods.
      */
 
     receiveMessage: function receiveMessage(message) {