Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/bidi/permissions.py
4133 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
19
from selenium.webdriver.common.bidi.common import command_builder
20
21
22
class PermissionState:
23
"""Represents the possible permission states."""
24
25
GRANTED = "granted"
26
DENIED = "denied"
27
PROMPT = "prompt"
28
29
30
class PermissionDescriptor:
31
"""Represents a permission descriptor."""
32
33
def __init__(self, name: str):
34
self.name = name
35
36
def to_dict(self) -> dict:
37
return {"name": self.name}
38
39
40
class Permissions:
41
"""BiDi implementation of the permissions module."""
42
43
def __init__(self, conn):
44
self.conn = conn
45
46
def set_permission(
47
self,
48
descriptor: str | PermissionDescriptor,
49
state: str,
50
origin: str,
51
user_context: str | None = None,
52
) -> None:
53
"""Sets a permission state for a given permission descriptor.
54
55
Args:
56
descriptor: The permission name (str) or PermissionDescriptor object.
57
Examples: "geolocation", "camera", "microphone".
58
state: The permission state (granted, denied, prompt).
59
origin: The origin for which the permission is set.
60
user_context: The user context id (optional).
61
62
Raises:
63
ValueError: If the permission state is invalid.
64
"""
65
if state not in [PermissionState.GRANTED, PermissionState.DENIED, PermissionState.PROMPT]:
66
valid_states = f"{PermissionState.GRANTED}, {PermissionState.DENIED}, {PermissionState.PROMPT}"
67
raise ValueError(f"Invalid permission state. Must be one of: {valid_states}")
68
69
if isinstance(descriptor, str):
70
permission_descriptor = PermissionDescriptor(descriptor)
71
else:
72
permission_descriptor = descriptor
73
74
params = {
75
"descriptor": permission_descriptor.to_dict(),
76
"state": state,
77
"origin": origin,
78
}
79
80
if user_context is not None:
81
params["userContext"] = user_context
82
83
self.conn.execute(command_builder("permissions.setPermission", params))
84
85