-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spec: https://www.w3.org/TR/permissions/#automation-webdriver-bidi Tests: web-platform-tests/wpt#43802 --------- Co-authored-by: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com>
- Loading branch information
1 parent
3db89f0
commit 29c7b0b
Showing
12 changed files
with
317 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/bidiMapper/domains/permissions/PermissionsProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright 2024 Google LLC. | ||
* 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 type {CdpClient} from '../../../cdp/CdpClient.js'; | ||
import { | ||
InvalidArgumentException, | ||
type EmptyResult, | ||
type Permissions, | ||
} from '../../../protocol/protocol.js'; | ||
|
||
export class PermissionsProcessor { | ||
#browserCdpClient: CdpClient; | ||
|
||
constructor(browserCdpClient: CdpClient) { | ||
this.#browserCdpClient = browserCdpClient; | ||
} | ||
|
||
async setPermissions( | ||
params: Permissions.SetPermissionParameters | ||
): Promise<EmptyResult> { | ||
try { | ||
await this.#browserCdpClient.sendCommand('Browser.setPermission', { | ||
origin: params.origin, | ||
permission: { | ||
name: params.descriptor.name, | ||
}, | ||
setting: params.state, | ||
}); | ||
} catch (err) { | ||
if ( | ||
(err as Error).message === | ||
`Permission can't be granted to opaque origins.` | ||
) { | ||
// Return success if the origin is not valid (does not match any | ||
// existing origins). | ||
return {}; | ||
} | ||
throw new InvalidArgumentException((err as Error).message); | ||
} | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/protocol-parser/generated/webdriver-bidi-permissions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright 2024 Google LLC. | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* THIS FILE IS AUTOGENERATED. Run `npm run bidi-types` to regenerate. | ||
* @see https://github.com/w3c/webdriver-bidi/blob/master/index.bs | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck Some types may be circular. | ||
|
||
import z from 'zod'; | ||
|
||
export const PermissionsCommandSchema = z.lazy( | ||
() => Permissions.SetPermissionSchema | ||
); | ||
export namespace Permissions { | ||
export const PermissionDescriptorSchema = z.lazy(() => | ||
z.object({ | ||
name: z.string(), | ||
}) | ||
); | ||
} | ||
export namespace Permissions { | ||
export const PermissionStateSchema = z.lazy(() => | ||
z.enum(['granted', 'denied', 'prompt']) | ||
); | ||
} | ||
export namespace Permissions { | ||
export const SetPermissionSchema = z.lazy(() => | ||
z.object({ | ||
method: z.literal('permissions.setPermission'), | ||
params: Permissions.SetPermissionParametersSchema, | ||
}) | ||
); | ||
} | ||
export namespace Permissions { | ||
export const SetPermissionParametersSchema = z.lazy(() => | ||
z.object({ | ||
descriptor: Permissions.PermissionDescriptorSchema, | ||
state: Permissions.PermissionStateSchema, | ||
origin: z.string(), | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2024 Google LLC. | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* THIS FILE IS AUTOGENERATED. Run `npm run bidi-types` to regenerate. | ||
* @see https://github.com/w3c/webdriver-bidi/blob/master/index.bs | ||
*/ | ||
export type PermissionsCommand = Permissions.SetPermission; | ||
export namespace Permissions { | ||
export type PermissionDescriptor = { | ||
name: string; | ||
}; | ||
} | ||
export namespace Permissions { | ||
export const enum PermissionState { | ||
Granted = 'granted', | ||
Denied = 'denied', | ||
Prompt = 'prompt', | ||
} | ||
} | ||
export namespace Permissions { | ||
export type SetPermission = { | ||
method: 'permissions.setPermission'; | ||
params: Permissions.SetPermissionParameters; | ||
}; | ||
} | ||
export namespace Permissions { | ||
export type SetPermissionParameters = { | ||
descriptor: Permissions.PermissionDescriptor; | ||
state: Permissions.PermissionState; | ||
origin: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2024 Google LLC. | ||
# 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. | ||
|
||
from urllib.parse import urlparse | ||
|
||
from test_helpers import execute_command | ||
|
||
|
||
def get_origin(url): | ||
""" Return the origin for the given url.""" | ||
parts = urlparse(url) | ||
return parts.scheme + '://' + parts.netloc | ||
|
||
|
||
async def query_permission(websocket, context_id, name): | ||
""" Queries a permission via the script.callFunction command.""" | ||
|
||
result = await execute_command( | ||
websocket, { | ||
"method": "script.callFunction", | ||
"params": { | ||
"functionDeclaration": """() => { | ||
return navigator.permissions.query({ name: '%s' }) | ||
.then(val => val.state, err => err.message) | ||
}""" % name, | ||
"target": { | ||
"context": context_id | ||
}, | ||
"awaitPromise": True, | ||
} | ||
}) | ||
|
||
return result['result']['value'] | ||
|
||
|
||
async def set_permission(websocket, origin, descriptor, state): | ||
""" Set a permission via the permissions.setPermission command.""" | ||
return await execute_command( | ||
websocket, { | ||
'method': 'permissions.setPermission', | ||
'params': { | ||
'origin': origin, | ||
'descriptor': descriptor, | ||
'state': state | ||
} | ||
}) |
Oops, something went wrong.