Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/bidi/permissions.py
1864 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
from typing import Optional, Union
19
20
from selenium.webdriver.common.bidi.common import command_builder
21
22
23
class PermissionState:
24
"""Represents the possible permission states."""
25
26
GRANTED = "granted"
27
DENIED = "denied"
28
PROMPT = "prompt"
29
30
31
class PermissionDescriptor:
32
"""Represents a permission descriptor."""
33
34
def __init__(self, name: str):
35
self.name = name
36
37
def to_dict(self) -> dict:
38
return {"name": self.name}
39
40
41
class Permissions:
42
"""
43
BiDi implementation of the permissions module.
44
"""
45
46
def __init__(self, conn):
47
self.conn = conn
48
49
def set_permission(
50
self,
51
descriptor: Union[str, PermissionDescriptor],
52
state: str,
53
origin: str,
54
user_context: Optional[str] = None,
55
) -> None:
56
"""Sets a permission state for a given permission descriptor.
57
58
Parameters:
59
-----------
60
descriptor: The permission name (str) or PermissionDescriptor object.
61
Examples: "geolocation", "camera", "microphone"
62
state: The permission state (granted, denied, prompt).
63
origin: The origin for which the permission is set.
64
user_context: The user context id (optional).
65
66
Raises:
67
------
68
ValueError: If the permission state is invalid.
69
"""
70
if state not in [PermissionState.GRANTED, PermissionState.DENIED, PermissionState.PROMPT]:
71
valid_states = f"{PermissionState.GRANTED}, {PermissionState.DENIED}, {PermissionState.PROMPT}"
72
raise ValueError(f"Invalid permission state. Must be one of: {valid_states}")
73
74
if isinstance(descriptor, str):
75
permission_descriptor = PermissionDescriptor(descriptor)
76
else:
77
permission_descriptor = descriptor
78
79
params = {
80
"descriptor": permission_descriptor.to_dict(),
81
"state": state,
82
"origin": origin,
83
}
84
85
if user_context is not None:
86
params["userContext"] = user_context
87
88
self.conn.execute(command_builder("permissions.setPermission", params))
89
90