From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- remote/marionette/permissions.sys.mjs | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 remote/marionette/permissions.sys.mjs (limited to 'remote/marionette/permissions.sys.mjs') diff --git a/remote/marionette/permissions.sys.mjs b/remote/marionette/permissions.sys.mjs new file mode 100644 index 0000000000..5238bf8347 --- /dev/null +++ b/remote/marionette/permissions.sys.mjs @@ -0,0 +1,95 @@ +/* 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", + MarionettePrefs: "chrome://remote/content/marionette/prefs.sys.mjs", +}); + +/** @namespace */ +export const permissions = {}; + +function mapToInternalPermissionParameters(browsingContext, permissionType) { + const currentURI = browsingContext.currentWindowGlobal.documentURI; + + // storage-access is quite special... + if (permissionType === "storage-access") { + const thirdPartyPrincipalSite = Services.eTLD.getSite(currentURI); + + const topLevelURI = browsingContext.top.currentWindowGlobal.documentURI; + const topLevelPrincipal = + Services.scriptSecurityManager.createContentPrincipal(topLevelURI, {}); + + return { + name: "3rdPartyFrameStorage^" + thirdPartyPrincipalSite, + principal: topLevelPrincipal, + }; + } + + const currentPrincipal = + Services.scriptSecurityManager.createContentPrincipal(currentURI, {}); + + return { + name: permissionType, + principal: currentPrincipal, + }; +} + +/** + * Set a permission's state. + * Note: Currently just a shim to support testdriver's set_permission. + * + * @param {object} permissionType + * The Gecko internal permission type + * @param {string} state + * State of the permission. It can be `granted`, `denied` or `prompt`. + * @param {boolean} oneRealm + * Currently ignored + * @param {browsingContext=} browsingContext + * Current browsing context object + * @throws {UnsupportedOperationError} + * If `marionette.setpermission.enabled` is not set or + * an unsupported permission is used. + */ +permissions.set = function (permissionType, state, oneRealm, browsingContext) { + if (!lazy.MarionettePrefs.setPermissionEnabled) { + throw new lazy.error.UnsupportedOperationError( + "'Set Permission' is not available" + ); + } + + const { name, principal } = mapToInternalPermissionParameters( + browsingContext, + permissionType + ); + + switch (state) { + case "granted": { + Services.perms.addFromPrincipal( + principal, + name, + Services.perms.ALLOW_ACTION + ); + return; + } + case "denied": { + Services.perms.addFromPrincipal( + principal, + name, + Services.perms.DENY_ACTION + ); + return; + } + case "prompt": { + Services.perms.removeFromPrincipal(principal, name); + return; + } + default: + throw new lazy.error.UnsupportedOperationError( + "Unrecognized permission keyword for 'Set Permission' operation" + ); + } +}; -- cgit v1.2.3