Bug 1165841 - Part 1: Add window into nsINfcEventListener. r=dimi, a=jocheng
authorYoshi Huang <allstars.chh@mozilla.com>
Tue, 02 Jun 2015 01:37:00 -0400
changeset 238558 cba58571bc4340e525739f146cacb40c19f1bc54
parent 238557 4cf9b3bb55e09d70cbaecb87b5bd6c334aed393f
child 238559 70ce7b9ea2a124a5022ddb44dd53aca25ab2776e
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 1: Add window into nsINfcEventListener. r=dimi, a=jocheng
dom/nfc/nsINfcContentHelper.idl
dom/nfc/nsNfc.js
--- a/dom/nfc/nsINfcContentHelper.idl
+++ b/dom/nfc/nsINfcContentHelper.idl
@@ -26,17 +26,17 @@ interface nsITagNDEFInfo : nsISupports
 
   readonly attribute long maxNDEFSize;
 
   readonly attribute boolean isReadOnly;
 
   readonly attribute boolean isFormatable;
 };
 
-[scriptable, uuid(151b4024-0443-434f-99ca-47ec247508ce)]
+[scriptable, uuid(86e46a0c-016d-4e5d-9fb5-789eb71848a1)]
 interface nsINfcEventListener : nsISupports
 {
   /**
    * Callback function used to notify tagfound.
    *
    * @param sessionToken
    *        SessionToken received from parent process
    * @param tagInfo
@@ -87,16 +87,21 @@ interface nsINfcEventListener : nsISuppo
 
   /**
    * Callback function used to notify focus changed.
    *
    * @param focus
    *        focus value receveid from parent process.
    */
   void notifyFocusChanged(in boolean focus);
+
+  /**
+   * The window object of this event listener.
+   */
+  readonly attribute nsIDOMWindow window;
 };
 
 [scriptable, uuid(6c913015-9658-46a9-88d9-6ecfda2bd020)]
 interface nsINfcRequestCallback : nsISupports
 {
   DOMString getCallbackId();
 
   void notifySuccess();
--- a/dom/nfc/nsNfc.js
+++ b/dom/nfc/nsNfc.js
@@ -329,26 +329,26 @@ function MozNFCImpl() {
     debug("No NFC support.");
   }
 
   this.eventService = Cc["@mozilla.org/eventlistenerservice;1"]
                         .getService(Ci.nsIEventListenerService);
 }
 MozNFCImpl.prototype = {
   _nfcContentHelper: null,
-  _window: null,
+  window: null,
   _rfState: null,
   _contentObj: null,
   nfcPeer: null,
   nfcTag: null,
   eventService: null,
 
   init: function init(aWindow) {
     debug("MozNFCImpl init called");
-    this._window = aWindow;
+    this.window = aWindow;
     this.defineEventHandlerGetterSetter("ontagfound");
     this.defineEventHandlerGetterSetter("ontaglost");
     this.defineEventHandlerGetterSetter("onpeerready");
     this.defineEventHandlerGetterSetter("onpeerfound");
     this.defineEventHandlerGetterSetter("onpeerlost");
 
     if (this._nfcContentHelper) {
       this._nfcContentHelper.init(aWindow);
@@ -359,45 +359,45 @@ MozNFCImpl.prototype = {
 
   // Only apps which have nfc-manager permission can call the following interfaces
   // 'checkP2PRegistration' , 'notifyUserAcceptedP2P' , 'notifySendFileStatus',
   // 'startPoll', 'stopPoll', and 'powerOff'.
   checkP2PRegistration: function checkP2PRegistration(manifestUrl) {
     // Get the AppID and pass it to ContentHelper
     let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
 
-    let callback = new NfcCallback(this._window);
+    let callback = new NfcCallback(this.window);
     this._nfcContentHelper.checkP2PRegistration(appID, callback);
     return callback.promise;
   },
 
   notifyUserAcceptedP2P: function notifyUserAcceptedP2P(manifestUrl) {
     let appID = appsService.getAppLocalIdByManifestURL(manifestUrl);
     // Notify chrome process of user's acknowledgement
     this._nfcContentHelper.notifyUserAcceptedP2P(appID);
   },
 
   notifySendFileStatus: function notifySendFileStatus(status, requestId) {
     this._nfcContentHelper.notifySendFileStatus(status, requestId);
   },
 
   startPoll: function startPoll() {
-    let callback = new NfcCallback(this._window);
+    let callback = new NfcCallback(this.window);
     this._nfcContentHelper.changeRFState(RFState.DISCOVERY, callback);
     return callback.promise;
   },
 
   stopPoll: function stopPoll() {
-    let callback = new NfcCallback(this._window);
+    let callback = new NfcCallback(this.window);
     this._nfcContentHelper.changeRFState(RFState.LISTEN, callback);
     return callback.promise;
   },
 
   powerOff: function powerOff() {
-    let callback = new NfcCallback(this._window);
+    let callback = new NfcCallback(this.window);
     this._nfcContentHelper.changeRFState(RFState.IDLE, callback);
     return callback.promise;
   },
 
   get enabled() {
     return this._rfState != RFState.IDLE;
   },
 
@@ -412,79 +412,79 @@ MozNFCImpl.prototype = {
     });
   },
 
   eventListenerWasAdded: function(eventType) {
     if (eventType !== "peerready") {
       return;
     }
 
-    let appId = this._window.document.nodePrincipal.appId;
+    let appId = this.window.document.nodePrincipal.appId;
     this._nfcContentHelper.registerTargetForPeerReady(appId);
   },
 
   eventListenerWasRemoved: function(eventType) {
     if (eventType !== "peerready") {
       return;
     }
 
-    let appId = this._window.document.nodePrincipal.appId;
+    let appId = this.window.document.nodePrincipal.appId;
     this._nfcContentHelper.unregisterTargetForPeerReady(appId);
   },
 
   notifyTagFound: function notifyTagFound(sessionToken, tagInfo, ndefInfo, records) {
     if (!this.handleTagFound(sessionToken, tagInfo, ndefInfo, records)) {
       this._nfcContentHelper.callDefaultFoundHandler(sessionToken, false, records);
     };
   },
 
   /**
    * Handles Tag Found event.
    *
    * returns true if the app could process this event, false otherwise.
    */
   handleTagFound: function handleTagFound(sessionToken, tagInfo, ndefInfo, records) {
     if (this.hasDeadWrapper()) {
-      dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
+      dump("this.window or this.__DOM_IMPL__ is a dead wrapper.");
       return false;
     }
 
     if (!this.eventService.hasListenersFor(this.__DOM_IMPL__, "tagfound")) {
       debug("ontagfound is not registered.");
       return false;
     }
 
     if (!this.checkPermissions(["nfc"])) {
       return false;
     }
 
-    let tagImpl = new MozNFCTagImpl(this._window, sessionToken, tagInfo, ndefInfo);
-    let tag = this._window.MozNFCTag._create(this._window, tagImpl);
+    let tagImpl = new MozNFCTagImpl(this.window, sessionToken, tagInfo, ndefInfo);
+    let tag = this.window.MozNFCTag._create(this.window, tagImpl);
 
     tagImpl._contentObj = tag;
     this.nfcTag = tag;
 
     let length = records ? records.length : 0;
     let ndefRecords = records ? [] : null;
     for (let i = 0; i < length; i++) {
       let record = records[i];
-      ndefRecords.push(new this._window.MozNDEFRecord({tnf: record.tnf,
+      ndefRecords.push(new this.window.MozNDEFRecord({tnf: record.tnf,
                                                        type: record.type,
                                                        id: record.id,
                                                        payload: record.payload}));
     }
 
     let eventData = {
       "cancelable": true,
       "tag": tag,
       "ndefRecords": ndefRecords
     };
 
     debug("fire ontagfound " + sessionToken);
-    let tagEvent = new this._window.MozNFCTagEvent("tagfound", eventData);
+    let tagEvent = new this.window.MozNFCTagEvent("tagfound", eventData);
     this.__DOM_IMPL__.dispatchEvent(tagEvent);
 
     // If defaultPrevented is false, means we need to take the default action
     // for this event - redirect this event to System app. Before redirecting to
     // System app, we need revoke the tag object first.
     if (!tagEvent.defaultPrevented) {
       this.notifyTagLost(sessionToken);
     }
@@ -495,34 +495,34 @@ MozNFCImpl.prototype = {
   notifyTagLost: function notifyTagLost(sessionToken) {
     if (!this.handleTagLost(sessionToken)) {
       this._nfcContentHelper.callDefaultLostHandler(sessionToken, false);
     }
   },
 
   handleTagLost: function handleTagLost(sessionToken) {
     if (this.hasDeadWrapper()) {
-      dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
+      dump("this.window or this.__DOM_IMPL__ is a dead wrapper.");
       return false;
     }
 
     if (!this.checkPermissions(["nfc"])) {
       return false;
     }
 
     if (!this.nfcTag) {
       debug("No NFCTag object existing.");
       return false;
     }
 
     this.nfcTag.notifyLost();
     this.nfcTag = null;
 
     debug("fire ontaglost " + sessionToken);
-    let event = new this._window.Event("taglost");
+    let event = new this.window.Event("taglost");
     this.__DOM_IMPL__.dispatchEvent(event);
 
     return true;
   },
 
   notifyPeerFound: function notifyPeerFound(sessionToken, isPeerReady) {
     if (!this.handlePeerFound(sessionToken, isPeerReady)) {
       this._nfcContentHelper.callDefaultFoundHandler(sessionToken, true, null);
@@ -531,48 +531,48 @@ MozNFCImpl.prototype = {
 
   /**
    * Handles Peer Found/Peer Ready event.
    *
    * returns true if the app could process this event, false otherwise.
    */
   handlePeerFound: function handlePeerFound(sessionToken, isPeerReady) {
     if (this.hasDeadWrapper()) {
-      dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
+      dump("this.window or this.__DOM_IMPL__ is a dead wrapper.");
       return false;
     }
 
     if (!isPeerReady &&
         !this.eventService.hasListenersFor(this.__DOM_IMPL__, "peerfound")) {
       debug("onpeerfound is not registered.");
       return false;
     }
 
     let perm = isPeerReady ? ["nfc-share"] : ["nfc"];
     if (!this.checkPermissions(perm)) {
       return false;
     }
 
-    let peerImpl = new MozNFCPeerImpl(this._window, sessionToken);
-    this.nfcPeer = this._window.MozNFCPeer._create(this._window, peerImpl);
+    let peerImpl = new MozNFCPeerImpl(this.window, sessionToken);
+    this.nfcPeer = this.window.MozNFCPeer._create(this.window, peerImpl);
 
     let eventType;
     let eventData = {
       "peer": this.nfcPeer
     };
 
     if (isPeerReady) {
       eventType = "peerready";
     } else {
       eventData.cancelable = true;
       eventType = "peerfound";
     }
 
     debug("fire on" + eventType + " " + sessionToken);
-    let event = new this._window.MozNFCPeerEvent(eventType, eventData);
+    let event = new this.window.MozNFCPeerEvent(eventType, eventData);
     this.__DOM_IMPL__.dispatchEvent(event);
 
     // For peerready we don't take the default action.
     if (isPeerReady) {
       return true;
     }
 
     // If defaultPrevented is false, means we need to take the default action
@@ -588,34 +588,34 @@ MozNFCImpl.prototype = {
   notifyPeerLost: function notifyPeerLost(sessionToken) {
     if (!this.handlePeerLost(sessionToken)) {
       this._nfcContentHelper.callDefaultLostHandler(sessionToken, true);
     }
   },
 
   handlePeerLost: function handlePeerLost(sessionToken) {
     if (this.hasDeadWrapper()) {
-      dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
+      dump("this.window or this.__DOM_IMPL__ is a dead wrapper.");
       return false;
     }
 
     if (!this.checkPermissions(["nfc", "nfc-share"])) {
       return false;
     }
 
     if (!this.nfcPeer) {
       debug("No NFCPeer object existing.");
       return false;
     }
 
     this.nfcPeer.notifyLost();
     this.nfcPeer = null;
 
     debug("fire onpeerlost");
-    let event = new this._window.Event("peerlost");
+    let event = new this.window.Event("peerlost");
     this.__DOM_IMPL__.dispatchEvent(event);
 
     return true;
   },
 
   notifyRFStateChanged: function notifyRFStateChanged(rfState) {
     this._rfState = rfState;
   },
@@ -632,32 +632,32 @@ MozNFCImpl.prototype = {
 
     if (this.nfcPeer) {
       debug("losing focus, call peerlost.");
       this.notifyPeerLost(this.nfcPeer.session);
     }
   },
 
   checkPermissions: function checkPermissions(perms) {
-    let principal = this._window.document.nodePrincipal;
+    let principal = this.window.document.nodePrincipal;
     for (let perm of perms) {
       let permValue =
         Services.perms.testExactPermissionFromPrincipal(principal, perm);
       if (permValue == Ci.nsIPermissionManager.ALLOW_ACTION) {
         return true;
       } else {
         debug("doesn't have " + perm + " permission.");
       }
     }
 
     return false;
   },
 
   hasDeadWrapper: function hasDeadWrapper() {
-    return Cu.isDeadWrapper(this._window) || Cu.isDeadWrapper(this.__DOM_IMPL__);
+    return Cu.isDeadWrapper(this.window) || Cu.isDeadWrapper(this.__DOM_IMPL__);
   },
 
   classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
   contractID: "@mozilla.org/nfc/manager;1",
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
                                          Ci.nsIDOMGlobalPropertyInitializer,
                                          Ci.nsINfcEventListener]),
 };