Bug 991970 - Part 3: add permission check. r=smaug
authorYoshi Huang <allstars.chh@mozilla.com>
Tue, 28 Oct 2014 14:58:47 +0800
changeset 212867 627bfb5807ea1093e8a6afa998d921d8770f002a
parent 212866 66002b73a68b445e4bf1a6ff54b13bac08f913f0
child 212868 236c6e9e724e121f38bd7b2711521d92fcd1271b
push id27734
push userryanvm@gmail.com
push dateWed, 29 Oct 2014 20:15:52 +0000
treeherdermozilla-central@8345ae427a3f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs991970
milestone36.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 991970 - Part 3: add permission check. r=smaug From 6462cdf51f04b81479932f1b7b1d809970820620 Mon Sep 17 00:00:00 2001 --- dom/nfc/nsNfc.js | 23 +++++++++++++++++++++++ dom/webidl/MozNFC.webidl | 1 + 2 files changed, 24 insertions(+)
dom/nfc/nsNfc.js
dom/webidl/MozNFC.webidl
--- a/dom/nfc/nsNfc.js
+++ b/dom/nfc/nsNfc.js
@@ -219,16 +219,20 @@ mozNfc.prototype = {
   },
 
   notifyTagFound: function notifyTagFound(sessionToken, event, records) {
     if (this.hasDeadWrapper()) {
       dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
       return;
     }
 
+    if (!this.checkPermissions(["nfc-read", "nfc-write"])) {
+      return;
+    }
+
     let tag = new MozNFCTag(this._window, sessionToken);
     let tagContentObj = this._window.MozNFCTag._create(this._window, 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,
@@ -248,16 +252,20 @@ mozNfc.prototype = {
   },
 
   notifyTagLost: function notifyTagLost(sessionToken) {
     if (this.hasDeadWrapper()) {
       dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
       return;
     }
 
+    if (!this.checkPermissions(["nfc-read", "nfc-write"])) {
+      return;
+    }
+
     debug("fire ontaglost " + sessionToken);
     let event = new this._window.Event("taglost");
     this.__DOM_IMPL__.dispatchEvent(event);
   },
 
   notifyPeerReady: function notifyPeerReady(sessionToken) {
     if (this.hasDeadWrapper()) {
       dump("this._window or this.__DOM_IMPL__ is a dead wrapper.");
@@ -292,16 +300,31 @@ mozNfc.prototype = {
 
     this.session = null;
 
     debug("fire onpeerlost");
     let event = new this._window.Event("peerlost");
     this.__DOM_IMPL__.dispatchEvent(event);
   },
 
+  checkPermissions: function checkPermissions(perms) {
+    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__);
   },
 
   classID: Components.ID("{6ff2b290-2573-11e3-8224-0800200c9a66}"),
   contractID: "@mozilla.org/navigatorNfc;1",
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports,
                                          Ci.nsIDOMGlobalPropertyInitializer,
--- a/dom/webidl/MozNFC.webidl
+++ b/dom/webidl/MozNFC.webidl
@@ -77,16 +77,17 @@ interface MozNFC : EventTarget {
    * Ths event will be fired when a NFCTag is detected.
    */
   [CheckPermissions="nfc-read nfc-write"]
   attribute EventHandler ontagfound;
 
   /**
    * This event will be fired if the tag detected in ontagfound has been removed.
    */
+  [CheckPermissions="nfc-read nfc-write"]
   attribute EventHandler ontaglost;
 };
 
 // Mozilla Only
 partial interface MozNFC {
   [ChromeOnly]
   void eventListenerWasAdded(DOMString aType);
   [ChromeOnly]