Bug 1531303 - Implemented a PermissionTestUtils module. r=johannh
authorPaul Zuehlcke <pzuhlcke@mozilla.com>
Thu, 15 Aug 2019 14:32:46 +0000
changeset 488253 e6b0f70a739a593bad874f550c600aef312c8083
parent 488246 17ef275f8a79eb2c79b618b62587873fe0acd781
child 488254 31b2a7bf293285a063d76cb84e46de08d10d5f5c
push id36440
push userncsoregi@mozilla.com
push dateFri, 16 Aug 2019 03:57:48 +0000
treeherdermozilla-central@a58b7dc85887 [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);
+  },
+};