summaryrefslogtreecommitdiffstats
path: root/remote/shared/Permissions.sys.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'remote/shared/Permissions.sys.mjs')
-rw-r--r--remote/shared/Permissions.sys.mjs90
1 files changed, 90 insertions, 0 deletions
diff --git a/remote/shared/Permissions.sys.mjs b/remote/shared/Permissions.sys.mjs
new file mode 100644
index 0000000000..50996bf701
--- /dev/null
+++ b/remote/shared/Permissions.sys.mjs
@@ -0,0 +1,90 @@
+/* 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/. */
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
+});
+
+/** @namespace */
+export const permissions = {};
+
+/**
+ * Get a permission type for the "storage-access" permission.
+ *
+ * @param {nsIURI} uri
+ * The URI to use for building the permission type.
+ *
+ * @returns {string} permissionType
+ * The permission type for the "storage-access" permission.
+ */
+permissions.getStorageAccessPermissionsType = function (uri) {
+ const thirdPartyPrincipalSite = Services.eTLD.getSite(uri);
+ return "3rdPartyFrameStorage^" + thirdPartyPrincipalSite;
+};
+
+/**
+ * Set a permission given a permission descriptor, a permission state,
+ * an origin.
+ *
+ * @param {PermissionDescriptor} descriptor
+ * The descriptor of the permission which will be updated.
+ * @param {string} state
+ * State of the permission. It can be `granted`, `denied` or `prompt`.
+ * @param {string} origin
+ * The origin which is used as a target for permission update.
+ *
+ * @throws {UnsupportedOperationError}
+ * If <var>state</var> has unsupported value.
+ */
+permissions.set = function (descriptor, state, origin) {
+ const principal =
+ Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin);
+
+ switch (state) {
+ case "granted": {
+ Services.perms.addFromPrincipal(
+ principal,
+ descriptor.type,
+ Services.perms.ALLOW_ACTION
+ );
+ return;
+ }
+ case "denied": {
+ Services.perms.addFromPrincipal(
+ principal,
+ descriptor.type,
+ Services.perms.DENY_ACTION
+ );
+ return;
+ }
+ case "prompt": {
+ Services.perms.removeFromPrincipal(principal, descriptor.type);
+ return;
+ }
+ default:
+ throw new lazy.error.UnsupportedOperationError(
+ "Unrecognized permission keyword for 'Set Permission' operation"
+ );
+ }
+};
+
+/**
+ * Validate the permission.
+ *
+ * @param {string} permissionName
+ * The name of the permission which will be validated.
+ *
+ * @throws {UnsupportedOperationError}
+ * If <var>permissionName</var> is not supported.
+ */
+permissions.validatePermission = function (permissionName) {
+ // Bug 1609427: PermissionDescriptor for "camera" and "microphone" are not yet implemented.
+ if (["camera", "microphone"].includes(permissionName)) {
+ throw new lazy.error.UnsupportedOperationError(
+ `"descriptor.name" "${permissionName}" is currently unsupported`
+ );
+ }
+};