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/. */
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",
14 export const permissions = {};
17 * Set a permission's state.
18 * Note: Currently just a shim to support testdriver's set_permission.
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
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.
34 permissions.set = function (
38 thirdPartyBrowsingContext,
39 topLevelBrowsingContext
41 if (!lazy.MarionettePrefs.setPermissionEnabled) {
42 throw new lazy.error.UnsupportedOperationError(
43 "'Set Permission' is not available"
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"
58 if (["clipboard-write", "clipboard-read"].includes(name)) {
60 Services.prefs.getBoolPref("dom.events.testing.asyncClipboard", false)
64 throw new lazy.error.UnsupportedOperationError(
65 "'Set Permission' expected dom.events.testing.asyncClipboard to be set"
67 } else if (name === "notifications") {
68 if (Services.prefs.getBoolPref("notification.prompt.testing", false)) {
71 throw new lazy.error.UnsupportedOperationError(
72 "'Set Permission' expected notification.prompt.testing to be set"
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);
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, {});
90 Services.perms.addFromPrincipal(
92 "3rdPartyFrameStorage^" + thirdPartyPrincipalSite,
93 Services.perms.ALLOW_ACTION
98 Services.perms.addFromPrincipal(
100 "3rdPartyFrameStorage^" + thirdPartyPrincipalSite,
101 Services.perms.DENY_ACTION
106 Services.perms.removeFromPrincipal(
108 "3rdPartyFrameStorage^" + thirdPartyPrincipalSite
113 throw new lazy.error.UnsupportedOperationError(
114 `'Set Permission' did not work for storage-access'`
117 } else if (name === "screen-wake-lock") {
118 if (Services.prefs.getBoolPref("dom.screenwakelock.testing", false)) {
121 throw new lazy.error.UnsupportedOperationError(
122 "'Set Permission' expected dom.screenwakelock.testing to be set"
126 throw new lazy.error.UnsupportedOperationError(
127 `'Set Permission' doesn't support '${name}'`