summaryrefslogtreecommitdiffstats
path: root/remote/webdriver-bidi/modules/root/permissions.sys.mjs
blob: 7c66b113b03547a4d79c8bd3aed9c6f07c968d91 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 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/. */

import { Module } from "chrome://remote/content/shared/messagehandler/Module.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
  error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
  permissions: "chrome://remote/content/shared/Permissions.sys.mjs",
});

export const PermissionState = {
  denied: "denied",
  granted: "granted",
  prompt: "prompt",
};

class PermissionsModule extends Module {
  constructor(messageHandler) {
    super(messageHandler);
  }

  destroy() {}

  /**
   * An object that holds the information about permission descriptor
   * for Webdriver BiDi permissions.setPermission command.
   *
   * @typedef PermissionDescriptor
   *
   * @property {string} name
   *    The name of the permission.
   */

  /**
   * Set to a given permission descriptor a given state on a provided origin.
   *
   * @param {object=} options
   * @param {PermissionDescriptor} options.descriptor
   *     The descriptor of the permission which will be updated.
   * @param {PermissionState} options.state
   *     The state which will be set to the permission.
   * @param {string} options.origin
   *    The origin which is used as a target for permission update.
   * @param {string=} options.userContext [unsupported]
   *    The id of the user context which should be used as a target
   *    for permission update.
   *
   * @throws {InvalidArgumentError}
   *     Raised if an argument is of an invalid type or value.
   * @throws {UnsupportedOperationError}
   *     Raised when unsupported permissions are set or <var>userContext</var>
   *     argument is used.
   */
  async setPermission(options = {}) {
    const {
      descriptor,
      state,
      origin,
      userContext: userContextId = null,
    } = options;

    lazy.assert.object(
      descriptor,
      `Expected "descriptor" to be an object, got ${descriptor}`
    );
    const permissionName = descriptor.name;
    lazy.assert.string(
      permissionName,
      `Expected "descriptor.name" to be a string, got ${permissionName}`
    );

    lazy.permissions.validatePermission(permissionName);

    // Bug 1878741: Allowing this permission causes timing related Android crash.
    if (descriptor.name === "notifications") {
      if (Services.prefs.getBoolPref("notification.prompt.testing", false)) {
        // Okay, do nothing. The notifications module will work without permission.
        return;
      }
      throw new lazy.error.UnsupportedOperationError(
        `Setting "descriptor.name" "notifications" expected "notification.prompt.testing" preference to be set`
      );
    }

    if (permissionName === "storage-access") {
      // TODO: Bug 1895457. Add support for "storage-access" permission.
      throw new lazy.error.UnsupportedOperationError(
        `"descriptor.name" "${permissionName}" is currently unsupported`
      );
    }

    const permissionStateTypes = Object.keys(PermissionState);
    lazy.assert.that(
      state => permissionStateTypes.includes(state),
      `Expected "state" to be one of ${permissionStateTypes}, got ${state}`
    )(state);

    lazy.assert.string(
      origin,
      `Expected "origin" to be a string, got ${origin}`
    );
    lazy.assert.that(
      origin => URL.canParse(origin),
      `Expected "origin" to be a valid URL, got ${origin}`
    )(origin);

    if (userContextId !== null) {
      lazy.assert.string(
        userContextId,
        `Expected "userContext" to be a string, got ${userContextId}`
      );

      // TODO: Bug 1894217. Add support for "userContext" argument.
      throw new lazy.error.UnsupportedOperationError(
        `"userContext" is not supported yet`
      );
    }

    const activeWindow = Services.wm.getMostRecentBrowserWindow();
    let typedDescriptor;
    try {
      typedDescriptor = activeWindow.navigator.permissions.parseSetParameters({
        descriptor,
        state,
      });
    } catch (err) {
      throw new lazy.error.InvalidArgumentError(
        `The conversion of "descriptor" was not successful: ${err.message}`
      );
    }

    lazy.permissions.set(typedDescriptor, state, origin);
  }
}

export const permissions = PermissionsModule;