Bug 1531303 - Implemented a PermissionTestUtils module. r=johannh
☠☠ backed out by b03aef80e76a ☠ ☠
authorPaul Zuehlcke <pzuhlcke@mozilla.com>
Wed, 14 Aug 2019 08:36:41 +0000
changeset 487879 865c2a5d40daeeffd9c0a29782b680588299b70f
parent 487878 e7a2bec5788d25d712b01f4f371c5854f8361542
child 487880 0c75c3bac00be2683556c9dc5cde2e6426335c48
push id92510
push userpzuhlcke@mozilla.com
push dateWed, 14 Aug 2019 08:51:10 +0000
treeherderautoland@0c75c3bac00b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1531303
milestone70.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 1531303 - Implemented a PermissionTestUtils module. r=johannh Differential Revision: https://phabricator.services.mozilla.com/D41028
extensions/permissions/moz.build
extensions/permissions/test/PermissionTestUtils.jsm
--- a/extensions/permissions/moz.build
+++ b/extensions/permissions/moz.build
@@ -1,16 +1,20 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 TEST_DIRS += ['test']
 
+TESTING_JS_MODULES += [
+    'test/PermissionTestUtils.jsm',
+]
+
 UNIFIED_SOURCES += [
     'nsContentBlocker.cpp',
     'nsPermission.cpp',
     'nsPermissionManager.cpp',
 ]
 
 XPCOM_MANIFESTS += [
     'components.conf',
new file mode 100644
--- /dev/null
+++ b/extensions/permissions/test/PermissionTestUtils.jsm
@@ -0,0 +1,112 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Utility module for tests to access the PermissionManager
+ * with uri or origin string parameters.
+ */
+
+"use strict";
+
+let pm = Cc["@mozilla.org/permissionmanager;1"].getService(
+  Ci.nsIPermissionManager
+);
+
+let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
+  Ci.nsIScriptSecurityManager
+);
+
+const EXPORTED_SYMBOLS = ["PermissionTestUtils"];
+
+/**
+ * Convert origin string or uri to principal.
+ * If passed an nsIPrincipal it will be returned without conversion.
+ * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject - Subject to convert to principal
+ * @returns {Ci.nsIPrincipal} Principal created from subject
+ */
+function convertToPrincipal(subject) {
+  if (subject instanceof Ci.nsIPrincipal) {
+    return subject;
+  }
+  if (typeof subject === "string") {
+    return secMan.createContentPrincipalFromOrigin(subject);
+  }
+  if (subject == null || subject instanceof Ci.nsIURI) {
+    return secMan.createContentPrincipal(subject, {});
+  }
+  throw new Error(
+    "subject parameter must be an nsIURI an origin string or a principal."
+  );
+}
+
+let PermissionTestUtils = {
+  /**
+   * Add permission information for a given subject.
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  add(subject, ...args) {
+    return pm.addFromPrincipal(convertToPrincipal(subject), ...args);
+  },
+  /**
+   * Get all custom permissions for a given subject.
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  getAll(subject, ...args) {
+    return pm.getAllForPrincipal(convertToPrincipal(subject), ...args);
+  },
+  /**
+   * Remove permission information for a given subject and permission type
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  remove(subject, ...args) {
+    return pm.removeFromPrincipal(convertToPrincipal(subject), ...args);
+  },
+  /**
+   * Test whether a website has permission to perform the given action.
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  testPermission(subject, ...args) {
+    return pm.testPermissionFromPrincipal(convertToPrincipal(subject), ...args);
+  },
+  /**
+   * Test whether a website has permission to perform the given action.
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  testExactPermission(subject, ...args) {
+    return pm.testExactPermissionFromPrincipal(
+      convertToPrincipal(subject),
+      ...args
+    );
+  },
+  /**
+   * Get the permission object associated with the given subject and action.
+   * Subject can be a principal, uri or origin string.
+   * @see nsIPermissionManager for documentation
+   *
+   * @param {Ci.nsIPrincipal|Ci.nsIURI|string} subject
+   * @param {*} args
+   */
+  getPermissionObject(subject, type, exactHost = false) {
+    return pm.getPermissionObject(convertToPrincipal(subject), type, exactHost);
+  },
+};