-
Notifications
You must be signed in to change notification settings - Fork 6.2k
chore: move permissions API into features/ #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,14 +53,16 @@ | |
| * [event: 'targetcreated'](#event-targetcreated-1) | ||
| * [event: 'targetdestroyed'](#event-targetdestroyed-1) | ||
| * [browserContext.browser()](#browsercontextbrowser) | ||
| * [browserContext.clearPermissionOverrides()](#browsercontextclearpermissionoverrides) | ||
| * [browserContext.close()](#browsercontextclose) | ||
| * [browserContext.isIncognito()](#browsercontextisincognito) | ||
| * [browserContext.newPage()](#browsercontextnewpage) | ||
| * [browserContext.overridePermissions(origin, permissions)](#browsercontextoverridepermissionsorigin-permissions) | ||
| * [browserContext.pages()](#browsercontextpages) | ||
| * [browserContext.permissions](#browsercontextpermissions) | ||
| * [browserContext.targets()](#browsercontexttargets) | ||
| * [browserContext.waitForTarget(predicate[, options])](#browsercontextwaitfortargetpredicate-options) | ||
| - [class: Permissions](#class-permissions) | ||
| * [permissions.clearOverrides()](#permissionsclearoverrides) | ||
| * [permissions.override(origin, permissions)](#permissionsoverrideorigin-permissions) | ||
| - [class: Page](#class-page) | ||
| * [event: 'close'](#event-close) | ||
| * [event: 'console'](#event-console) | ||
|
|
@@ -832,18 +834,6 @@ Emitted when a target inside the browser context is destroyed, for example when | |
|
|
||
| The browser this browser context belongs to. | ||
|
|
||
| #### browserContext.clearPermissionOverrides() | ||
| - returns: <[Promise]> | ||
|
|
||
| Clears all permission overrides for the browser context. | ||
|
|
||
| ```js | ||
| const context = browser.defaultBrowserContext(); | ||
| context.overridePermissions('https://example.com', ['clipboard-read']); | ||
| // do stuff .. | ||
| context.clearPermissionOverrides(); | ||
| ``` | ||
|
|
||
| #### browserContext.close() | ||
| - returns: <[Promise]> | ||
|
|
||
|
|
@@ -865,8 +855,48 @@ The default browser context is the only non-incognito browser context. | |
|
|
||
| Creates a new page in the browser context. | ||
|
|
||
| #### browserContext.pages() | ||
| - returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages. Non visible pages, such as `"background_page"`, will not be listed here. You can find them using [target.page()](#targetpage). | ||
|
|
||
| An array of all pages inside the browser context. | ||
|
|
||
| #### browserContext.permissions | ||
| - returns: <[Permissions]> | ||
|
|
||
| #### browserContext.targets() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you were going to drop targets from the API, is there a reason we must keep them? For testing/automation use cases where the browser is fully controlled by a program we shouldn't need this as the program should already be aware of all targets. Oh, on the second look it seems that this place didn't actually change and it's just a misleading diff representation, so we can discuss this separately.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Will remove later... |
||
| - returns: <[Array]<[Target]>> | ||
|
|
||
| An array of all active targets inside the browser context. | ||
|
|
||
| #### browserContext.waitForTarget(predicate[, options]) | ||
| - `predicate` <[function]\([Target]\):[boolean]> A function to be run for every target | ||
| - `options` <[Object]> | ||
| - `timeout` <[number]> Maximum wait time in milliseconds. Pass `0` to disable the timeout. Defaults to 30 seconds. | ||
| - returns: <[Promise]<[Target]>> Promise which resolves to the first target found that matches the `predicate` function. | ||
|
|
||
| This searches for a target in this specific browser context. | ||
|
|
||
| An example of finding a target for a page opened via `window.open`: | ||
| ```js | ||
| await page.evaluate(() => window.open('https://www.example.com/')); | ||
| const newWindowTarget = await browserContext.waitForTarget(target => target.url() === 'https://www.example.com/'); | ||
| ``` | ||
|
|
||
| ### class: Permissions | ||
|
|
||
| #### browserContext.overridePermissions(origin, permissions) | ||
| #### permissions.clearOverrides() | ||
| - returns: <[Promise]> | ||
|
|
||
| Clears all permission overrides for the browser context. | ||
|
|
||
| ```js | ||
| const context = browser.defaultBrowserContext(); | ||
| context.permissions.override('https://example.com', ['clipboard-read']); | ||
| // do stuff .. | ||
| context.permissions.clearOverrides(); | ||
| ``` | ||
|
|
||
| #### permissions.override(origin, permissions) | ||
| - `origin` <[string]> The [origin] to grant permissions to, e.g. "https://example.com". | ||
| - `permissions` <[Array]<[string]>> An array of permissions to grant. All permissions that are not listed here will be automatically denied. Permissions can be one of the following values: | ||
| - `'geolocation'` | ||
|
|
@@ -890,32 +920,7 @@ Creates a new page in the browser context. | |
|
|
||
| ```js | ||
| const context = browser.defaultBrowserContext(); | ||
| await context.overridePermissions('https://html5demos.com', ['geolocation']); | ||
| ``` | ||
|
|
||
|
|
||
| #### browserContext.pages() | ||
| - returns: <[Promise]<[Array]<[Page]>>> Promise which resolves to an array of all open pages. Non visible pages, such as `"background_page"`, will not be listed here. You can find them using [target.page()](#targetpage). | ||
|
|
||
| An array of all pages inside the browser context. | ||
|
|
||
| #### browserContext.targets() | ||
| - returns: <[Array]<[Target]>> | ||
|
|
||
| An array of all active targets inside the browser context. | ||
|
|
||
| #### browserContext.waitForTarget(predicate[, options]) | ||
| - `predicate` <[function]\([Target]\):[boolean]> A function to be run for every target | ||
| - `options` <[Object]> | ||
| - `timeout` <[number]> Maximum wait time in milliseconds. Pass `0` to disable the timeout. Defaults to 30 seconds. | ||
| - returns: <[Promise]<[Target]>> Promise which resolves to the first target found that matches the `predicate` function. | ||
|
|
||
| This searches for a target in this specific browser context. | ||
|
|
||
| An example of finding a target for a page opened via `window.open`: | ||
| ```js | ||
| await page.evaluate(() => window.open('https://www.example.com/')); | ||
| const newWindowTarget = await browserContext.waitForTarget(target => target.url() === 'https://www.example.com/'); | ||
| await context.permissions.override('https://html5demos.com', ['geolocation']); | ||
| ``` | ||
|
|
||
| ### class: Page | ||
|
|
@@ -1814,7 +1819,7 @@ Sets the page's geolocation. | |
| await page.setGeolocation({latitude: 59.95, longitude: 30.31667}); | ||
| ``` | ||
|
|
||
| > **NOTE** Consider using [browserContext.overridePermissions](#browsercontextoverridepermissionsorigin-permissions) to grant permissions for the page to read its geolocation. | ||
| > **NOTE** Consider using [browserContext.permissions.override](#permissionsoverrideorigin-permissions) to grant permissions for the page to read its geolocation. | ||
|
|
||
| #### page.setJavaScriptEnabled(enabled) | ||
| - `enabled` <[boolean]> Whether or not to enable JavaScript on the page. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * Copyright 2017 Google Inc. All rights reserved. | ||
| * Modifications copyright (c) Microsoft Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { Protocol } from '../protocol'; | ||
| import { Connection } from './../Connection'; | ||
|
|
||
| export class Permissions { | ||
| private _client: Connection; | ||
| private _browserContextId: string; | ||
|
|
||
| constructor(connection: Connection, browserContextId: string | null) { | ||
| this._client = connection; | ||
| this._browserContextId = browserContextId; | ||
| } | ||
|
|
||
| async override(origin: string, permissions: string[]) { | ||
| const webPermissionToProtocol = new Map<string, Protocol.Browser.PermissionType>([ | ||
| ['geolocation', 'geolocation'], | ||
| ['midi', 'midi'], | ||
| ['notifications', 'notifications'], | ||
| ['camera', 'videoCapture'], | ||
| ['microphone', 'audioCapture'], | ||
| ['background-sync', 'backgroundSync'], | ||
| ['ambient-light-sensor', 'sensors'], | ||
| ['accelerometer', 'sensors'], | ||
| ['gyroscope', 'sensors'], | ||
| ['magnetometer', 'sensors'], | ||
| ['accessibility-events', 'accessibilityEvents'], | ||
| ['clipboard-read', 'clipboardRead'], | ||
| ['clipboard-write', 'clipboardWrite'], | ||
| ['payment-handler', 'paymentHandler'], | ||
| // chrome-specific permissions we have. | ||
| ['midi-sysex', 'midiSysex'], | ||
| ]); | ||
| const filtered = permissions.map(permission => { | ||
| const protocolPermission = webPermissionToProtocol.get(permission); | ||
| if (!protocolPermission) | ||
| throw new Error('Unknown permission: ' + permission); | ||
| return protocolPermission; | ||
| }); | ||
| await this._client.send('Browser.grantPermissions', {origin, browserContextId: this._browserContextId || undefined, permissions: filtered}); | ||
| } | ||
|
|
||
| async clearOverrides() { | ||
| await this._client.send('Browser.resetPermissions', {browserContextId: this._browserContextId || undefined}); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this and other feature accessors be returns: <?[Permissions]> ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point, I'll apply it to everything in a separate patch.