Bug 1206245 - Add a function to SitePermissions.jsm to check granted permissions by uri. r=paolo
authorJohann Hofmann <jhofmann@mozilla.com>
Wed, 01 Jun 2016 12:39:52 +0200
changeset 340907 359674b9970a98e42c97184e9d7c59c89bac06d7
parent 340906 cb0b25c9093f75bfb94d99c9c1dc8b61253832dc
child 340908 d0288a0ca5ce65b439a69be3cce5b01dc1d2b42f
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs1206245
milestone49.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 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");
+});