Backed out changeset 88fbb17e3c20 (bug 1865637) for causing animation related mochite...
[gecko.git] / remote / marionette / permissions.sys.mjs
blob280310340504a2d48b5844fab2b2730dcbcb1e8b
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   assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
9   error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
10   MarionettePrefs: "chrome://remote/content/marionette/prefs.sys.mjs",
11 });
13 /** @namespace */
14 export const permissions = {};
16 /**
17  * Set a permission's state.
18  * Note: Currently just a shim to support testdriver's set_permission.
19  *
20  * @param {object} descriptor
21  *     Descriptor with the `name` property.
22  * @param {string} state
23  *     State of the permission. It can be `granted`, `denied` or `prompt`.
24  * @param {boolean} oneRealm
25  *     Currently ignored
26  * @param {browsingContext=} thirdPartyBrowsingContext
27  *     3rd party browsing context object
28  * @param {browsingContext=} topLevelBrowsingContext
29  *     Top level browsing context object
30  * @throws {UnsupportedOperationError}
31  *     If `marionette.setpermission.enabled` is not set or
32  *     an unsupported permission is used.
33  */
34 permissions.set = function (
35   descriptor,
36   state,
37   oneRealm,
38   thirdPartyBrowsingContext,
39   topLevelBrowsingContext
40 ) {
41   if (!lazy.MarionettePrefs.setPermissionEnabled) {
42     throw new lazy.error.UnsupportedOperationError(
43       "'Set Permission' is not available"
44     );
45   }
47   // This is not a real implementation of the permissions API.
48   // Instead the purpose of this implementation is to have web-platform-tests
49   // that use `set_permission()` not fail.
50   // Each test needs the corresponding testing pref to make it actually work.
51   const { name } = descriptor;
53   if (state === "prompt" && name !== "storage-access") {
54     throw new lazy.error.UnsupportedOperationError(
55       "'Set Permission' doesn't support prompt"
56     );
57   }
58   if (["clipboard-write", "clipboard-read"].includes(name)) {
59     if (
60       Services.prefs.getBoolPref("dom.events.testing.asyncClipboard", false)
61     ) {
62       return;
63     }
64     throw new lazy.error.UnsupportedOperationError(
65       "'Set Permission' expected dom.events.testing.asyncClipboard to be set"
66     );
67   } else if (name === "notifications") {
68     if (Services.prefs.getBoolPref("notification.prompt.testing", false)) {
69       return;
70     }
71     throw new lazy.error.UnsupportedOperationError(
72       "'Set Permission' expected notification.prompt.testing to be set"
73     );
74   } else if (name === "storage-access") {
75     // Check if browsing context has a window global object
76     lazy.assert.open(thirdPartyBrowsingContext);
77     lazy.assert.open(topLevelBrowsingContext);
79     const thirdPartyURI =
80       thirdPartyBrowsingContext.currentWindowGlobal.documentURI;
81     const topLevelURI = topLevelBrowsingContext.currentWindowGlobal.documentURI;
83     const thirdPartyPrincipalSite = Services.eTLD.getSite(thirdPartyURI);
85     const topLevelPrincipal =
86       Services.scriptSecurityManager.createContentPrincipal(topLevelURI, {});
88     switch (state) {
89       case "granted": {
90         Services.perms.addFromPrincipal(
91           topLevelPrincipal,
92           "3rdPartyFrameStorage^" + thirdPartyPrincipalSite,
93           Services.perms.ALLOW_ACTION
94         );
95         return;
96       }
97       case "denied": {
98         Services.perms.addFromPrincipal(
99           topLevelPrincipal,
100           "3rdPartyFrameStorage^" + thirdPartyPrincipalSite,
101           Services.perms.DENY_ACTION
102         );
103         return;
104       }
105       case "prompt": {
106         Services.perms.removeFromPrincipal(
107           topLevelPrincipal,
108           "3rdPartyFrameStorage^" + thirdPartyPrincipalSite
109         );
110         return;
111       }
112       default:
113         throw new lazy.error.UnsupportedOperationError(
114           `'Set Permission' did not work for storage-access'`
115         );
116     }
117   } else if (name === "screen-wake-lock") {
118     if (Services.prefs.getBoolPref("dom.screenwakelock.testing", false)) {
119       return;
120     }
121     throw new lazy.error.UnsupportedOperationError(
122       "'Set Permission' expected dom.screenwakelock.testing to be set"
123     );
124   }
126   throw new lazy.error.UnsupportedOperationError(
127     `'Set Permission' doesn't support '${name}'`
128   );