Skip to content

Commit

Permalink
feat: support user context in permissions (#1805)
Browse files Browse the repository at this point in the history
This PR adds `goog:` prefixed user context to allow use cases required
by Puppeteer. Once permissions spec issue is resolved
w3c/permissions#438 we can remove the prefix.
  • Loading branch information
OrKoN authored Feb 22, 2024
1 parent 2b1dbfe commit a623dc7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/bidiMapper/domains/permissions/PermissionsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ export class PermissionsProcessor {
params: Permissions.SetPermissionParameters
): Promise<EmptyResult> {
try {
const userContextId = (params as {'goog:userContext'?: string})[
'goog:userContext'
];
await this.#browserCdpClient.sendCommand('Browser.setPermission', {
origin: params.origin,
browserContextId:
userContextId && userContextId !== 'default'
? userContextId
: undefined,
permission: {
name: params.descriptor.name,
},
Expand Down
12 changes: 8 additions & 4 deletions src/protocol-parser/protocol-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,13 @@ export namespace Permissions {
export function parseSetPermissionsParams(
params: unknown
): Protocol.Permissions.SetPermissionParameters {
return parseObject(
params,
WebDriverBidiPermissions.Permissions.SetPermissionParametersSchema
) as Protocol.Permissions.SetPermissionParameters;
return {
// TODO: remove once "goog:" attributes are not needed.
...(params as object),
...(parseObject(
params,
WebDriverBidiPermissions.Permissions.SetPermissionParametersSchema
) as Protocol.Permissions.SetPermissionParameters),
};
}
}
9 changes: 7 additions & 2 deletions tests/permissions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ async def query_permission(websocket, context_id, name):
return result['result']['value']


async def set_permission(websocket, origin, descriptor, state):
async def set_permission(websocket,
origin,
descriptor,
state,
user_context=None):
""" Set a permission via the permissions.setPermission command."""
return await execute_command(
websocket, {
'method': 'permissions.setPermission',
'params': {
'origin': origin,
'descriptor': descriptor,
'state': state
'state': state,
'goog:userContext': user_context
}
})
52 changes: 51 additions & 1 deletion tests/permissions/test_set_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pytest
from permissions import get_origin, query_permission, set_permission
from test_helpers import goto_url
from test_helpers import execute_command, goto_url


@pytest.mark.asyncio
Expand All @@ -34,3 +34,53 @@ async def test_permissions_set_permission(websocket, context_id, example_url):
assert resp == {}
assert await query_permission(websocket, context_id,
'geolocation') == 'prompt'


@pytest.mark.asyncio
@pytest.mark.skip(reason="See chromium-bidi/issues#1610")
async def test_permissions_set_permission_in_user_context(
websocket, context_id, example_url):
await goto_url(websocket, context_id, example_url)

user_context = await execute_command(websocket, {
"method": "browser.createUserContext",
"params": {}
})

browsing_context = await execute_command(
websocket, {
"method": "browsingContext.create",
"params": {
"type": "tab",
"userContext": user_context["userContext"]
}
})

origin = get_origin(example_url)

await goto_url(websocket, browsing_context['context'], example_url)

assert await query_permission(websocket, context_id,
'geolocation') == 'prompt'
assert await query_permission(websocket, browsing_context['context'],
'geolocation') == 'prompt'

resp = await set_permission(websocket,
origin, {'name': 'geolocation'},
'granted',
user_context=user_context["userContext"])
assert resp == {}
assert await query_permission(websocket, context_id,
'geolocation') == 'prompt'
assert await query_permission(websocket, browsing_context['context'],
'geolocation') == 'granted'

resp = await set_permission(websocket,
origin, {'name': 'geolocation'},
'prompt',
user_context=user_context["userContext"])
assert resp == {}
assert await query_permission(websocket, context_id,
'geolocation') == 'prompt'
assert await query_permission(websocket, browsing_context['context'],
'geolocation') == 'prompt'

0 comments on commit a623dc7

Please sign in to comment.