no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / remote / marionette / permissions.sys.mjs
blob5238bf834700921bf8a3aeb0bc4ca5000fde35ae
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
9   MarionettePrefs: "chrome://remote/content/marionette/prefs.sys.mjs",
10 });
12 /** @namespace */
13 export const permissions = {};
15 function mapToInternalPermissionParameters(browsingContext, permissionType) {
16   const currentURI = browsingContext.currentWindowGlobal.documentURI;
18   // storage-access is quite special...
19   if (permissionType === "storage-access") {
20     const thirdPartyPrincipalSite = Services.eTLD.getSite(currentURI);
22     const topLevelURI = browsingContext.top.currentWindowGlobal.documentURI;
23     const topLevelPrincipal =
24       Services.scriptSecurityManager.createContentPrincipal(topLevelURI, {});
26     return {
27       name: "3rdPartyFrameStorage^" + thirdPartyPrincipalSite,
28       principal: topLevelPrincipal,
29     };
30   }
32   const currentPrincipal =
33     Services.scriptSecurityManager.createContentPrincipal(currentURI, {});
35   return {
36     name: permissionType,
37     principal: currentPrincipal,
38   };
41 /**
42  * Set a permission's state.
43  * Note: Currently just a shim to support testdriver's set_permission.
44  *
45  * @param {object} permissionType
46  *     The Gecko internal permission type
47  * @param {string} state
48  *     State of the permission. It can be `granted`, `denied` or `prompt`.
49  * @param {boolean} oneRealm
50  *     Currently ignored
51  * @param {browsingContext=} browsingContext
52  *     Current browsing context object
53  * @throws {UnsupportedOperationError}
54  *     If `marionette.setpermission.enabled` is not set or
55  *     an unsupported permission is used.
56  */
57 permissions.set = function (permissionType, state, oneRealm, browsingContext) {
58   if (!lazy.MarionettePrefs.setPermissionEnabled) {
59     throw new lazy.error.UnsupportedOperationError(
60       "'Set Permission' is not available"
61     );
62   }
64   const { name, principal } = mapToInternalPermissionParameters(
65     browsingContext,
66     permissionType
67   );
69   switch (state) {
70     case "granted": {
71       Services.perms.addFromPrincipal(
72         principal,
73         name,
74         Services.perms.ALLOW_ACTION
75       );
76       return;
77     }
78     case "denied": {
79       Services.perms.addFromPrincipal(
80         principal,
81         name,
82         Services.perms.DENY_ACTION
83       );
84       return;
85     }
86     case "prompt": {
87       Services.perms.removeFromPrincipal(principal, name);
88       return;
89     }
90     default:
91       throw new lazy.error.UnsupportedOperationError(
92         "Unrecognized permission keyword for 'Set Permission' operation"
93       );
94   }