permissions.sys.mjs (3714B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { RootBiDiModule } from "chrome://remote/content/webdriver-bidi/modules/RootBiDiModule.sys.mjs"; 6 7 const lazy = {}; 8 9 ChromeUtils.defineESModuleGetters(lazy, { 10 assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs", 11 error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs", 12 permissions: "chrome://remote/content/shared/Permissions.sys.mjs", 13 pprint: "chrome://remote/content/shared/Format.sys.mjs", 14 UserContextManager: 15 "chrome://remote/content/shared/UserContextManager.sys.mjs", 16 }); 17 18 class PermissionsModule extends RootBiDiModule { 19 constructor(messageHandler) { 20 super(messageHandler); 21 } 22 23 destroy() {} 24 25 /** 26 * An object that holds the information about permission descriptor 27 * for Webdriver BiDi permissions.setPermission command. 28 * 29 * @typedef PermissionDescriptor 30 * 31 * @property {string} name 32 * The name of the permission. 33 */ 34 35 /** 36 * Set to a given permission descriptor a given state on a provided origin. 37 * 38 * @param {object=} options 39 * @param {PermissionDescriptor} options.descriptor 40 * The descriptor of the permission which will be updated. 41 * @param {PermissionState} options.state 42 * The state which will be set to the permission. 43 * @param {string} options.origin 44 * The origin which is used as a target for permission update. 45 * @param {string=} options.userContext 46 * The id of the user context which should be used as a target 47 * for permission update. 48 * 49 * @throws {InvalidArgumentError} 50 * Raised if an argument is of an invalid type or value. 51 * @throws {UnsupportedOperationError} 52 * Raised when unsupported permissions are set. 53 */ 54 async setPermission(options = {}) { 55 const { 56 descriptor, 57 state, 58 origin, 59 userContext: userContextId = null, 60 } = options; 61 62 lazy.permissions.validateDescriptor(descriptor); 63 64 const permissionName = descriptor.name; 65 66 if (permissionName === "storage-access") { 67 // TODO: Bug 1895457. Add support for "storage-access" permission. 68 throw new lazy.error.UnsupportedOperationError( 69 `"descriptor.name" "${permissionName}" is currently unsupported` 70 ); 71 } 72 73 lazy.permissions.validateState(state); 74 75 lazy.assert.string( 76 origin, 77 lazy.pprint`Expected "origin" to be a string, got ${origin}` 78 ); 79 lazy.assert.that( 80 origin => URL.canParse(origin), 81 lazy.pprint`Expected "origin" to be a valid URL, got ${origin}` 82 )(origin); 83 84 let userContext; 85 if (userContextId !== null) { 86 lazy.assert.string( 87 userContextId, 88 lazy.pprint`Expected "userContext" to be a string, got ${userContextId}` 89 ); 90 91 if (!lazy.UserContextManager.hasUserContextId(userContextId)) { 92 throw new lazy.error.NoSuchUserContextError( 93 `User Context with id ${userContextId} was not found` 94 ); 95 } 96 97 userContext = lazy.UserContextManager.getInternalIdById(userContextId); 98 } 99 100 const activeWindow = Services.wm.getMostRecentBrowserWindow(); 101 let typedDescriptor; 102 try { 103 typedDescriptor = activeWindow.navigator.permissions.parseSetParameters({ 104 descriptor, 105 state, 106 }); 107 } catch (err) { 108 throw new lazy.error.InvalidArgumentError( 109 `The conversion of "descriptor" was not successful: ${err.message}` 110 ); 111 } 112 113 lazy.permissions.set(typedDescriptor, state, origin, userContext); 114 } 115 } 116 117 export const permissions = PermissionsModule;