Bug 1206245 - Add a function to SitePermissions.jsm to check granted permissions by uri. r?paolo draft
authorJohann Hofmann <jhofmann@mozilla.com>
Wed, 01 Jun 2016 12:39:52 +0200
changeset 373928 01edc1295562c627a45d54e598f3b72d6c478d2e
parent 373501 25321494921c824703a605127fb1f99b1faf5910
child 522500 28170551dbebc018b34531806dc501e19d831a29
push id19876
push usermail@johann-hofmann.com
push dateWed, 01 Jun 2016 13:14:47 +0000
reviewerspaolo
bugs1206245
milestone49.0a1
Bug 1206245 - Add a function to SitePermissions.jsm to check granted permissions by uri. r?paolo MozReview-Commit-ID: KvAuRLRx1nf
browser/modules/SitePermissions.jsm
browser/modules/test/xpcshell/test_SitePermissions.js
--- a/browser/modules/SitePermissions.jsm
+++ b/browser/modules/SitePermissions.jsm
@@ -11,16 +11,34 @@ var gStringBundle =
 
 this.SitePermissions = {
 
   UNKNOWN: Services.perms.UNKNOWN_ACTION,
   ALLOW: Services.perms.ALLOW_ACTION,
   BLOCK: Services.perms.DENY_ACTION,
   SESSION: Components.interfaces.nsICookiePermission.ACCESS_SESSION,
 
+  /* Returns a boolean indicating whether there are any granted
+   * (meaning allowed or session-allowed) permissions for the given URI.
+   * Will return false for invalid URIs (such as file:// URLs).
+   */
+  hasGrantedPermissions: function (aURI) {
+    if (!this.isSupportedURI(aURI)) {
+      return false;
+    }
+
+    for (let permission of this.listPermissions()) {
+      let state = this.get(aURI, permission);
+      if (state === this.ALLOW || state === this.SESSION) {
+        return true;
+      }
+    }
+    return false;
+  },
+
   /* Checks whether a UI for managing permissions should be exposed for a given
    * URI. This excludes file URIs, for instance, as they don't have a host,
    * even though nsIPermissionManager can still handle them.
    */
   isSupportedURI: function (aURI) {
     return aURI.schemeIs("http") || aURI.schemeIs("https");
   },
 
--- a/browser/modules/test/xpcshell/test_SitePermissions.js
+++ b/browser/modules/test/xpcshell/test_SitePermissions.js
@@ -1,13 +1,51 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 "use strict";
 
 Components.utils.import("resource:///modules/SitePermissions.jsm");
+Components.utils.import("resource://gre/modules/Services.jsm");
 
 add_task(function* testPermissionsListing() {
   Assert.deepEqual(SitePermissions.listPermissions().sort(),
     ["camera","cookie","desktop-notification","geo","image",
      "indexedDB","install","microphone","pointerLock","popup"],
     "Correct list of all permissions");
 });
+
+add_task(function* testHasGrantedPermissions() {
+  let uri = Services.io.newURI("https://example.com", null, null)
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), false);
+
+  // check that ALLOW states return true
+  SitePermissions.set(uri, "camera", SitePermissions.ALLOW);
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), true);
+
+  // removing the ALLOW state should revert to false
+  SitePermissions.remove(uri, "camera");
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), false);
+
+  // check that SESSION states return true
+  SitePermissions.set(uri, "microphone", SitePermissions.SESSION);
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), true);
+
+  // removing the SESSION state should revert to false
+  SitePermissions.remove(uri, "microphone");
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), false);
+
+  SitePermissions.set(uri, "pointerLock", SitePermissions.BLOCK);
+
+  // check that a combination of ALLOW and BLOCK states returns true
+  SitePermissions.set(uri, "geo", SitePermissions.ALLOW);
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), true);
+
+  // check that a combination of SESSION and BLOCK states returns true
+  SitePermissions.set(uri, "geo", SitePermissions.SESSION);
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), true);
+
+  // check that only BLOCK states will not return true
+  SitePermissions.remove(uri, "geo");
+  Assert.equal(SitePermissions.hasGrantedPermissions(uri), false);
+
+  SitePermissions.remove(uri, "pointerLock");
+});