summaryrefslogtreecommitdiffstats
path: root/remote/marionette/permissions.sys.mjs
blob: 43fac984228f49b7e41fdfe01493f59ebb02d1f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 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 = {};

/**
 * Set a permission's state.
 * Note: Currently just a shim to support testdriver's set_permission.
 *
 * @param {Object} descriptor
 *     Descriptor with the `name` property.
 * @param {string} state
 *     State of the permission. It can be `granted`, `denied` or `prompt`.
 * @param {boolean} oneRealm
 *     Currently ignored
 *
 * @throws {UnsupportedOperationError}
 *     If `marionette.setpermission.enabled` is not set or
 *     an unsupported permission is used.
 */
permissions.set = function(descriptor, state, oneRealm) {
  if (!lazy.MarionettePrefs.setPermissionEnabled) {
    throw new lazy.error.UnsupportedOperationError(
      "'Set Permission' is not available"
    );
  }

  const { name } = descriptor;
  if (!["clipboard-write", "clipboard-read"].includes(name)) {
    throw new lazy.error.UnsupportedOperationError(
      `'Set Permission' doesn't support '${name}'`
    );
  }

  if (state === "prompt") {
    throw new lazy.error.UnsupportedOperationError(
      "'Set Permission' doesn't support prompt"
    );
  }

  // This is not a real implementation of the permissions API.
  // Instead the purpose of this implementation is to have web-platform-tests
  // that use `set_permission('clipboard-write|read')` not fail.
  // We enable dom.events.testing.asyncClipboard for the whole test suite anyway,
  // so no extra permission is necessary.
  if (!Services.prefs.getBoolPref("dom.events.testing.asyncClipboard", false)) {
    throw new lazy.error.UnsupportedOperationError(
      "'Set Permission' expected dom.events.testing.asyncClipboard to be set"
    );
  }
};