Path: blob/trunk/py/selenium/webdriver/common/bidi/permissions.py
4133 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.161718from selenium.webdriver.common.bidi.common import command_builder192021class PermissionState:22"""Represents the possible permission states."""2324GRANTED = "granted"25DENIED = "denied"26PROMPT = "prompt"272829class PermissionDescriptor:30"""Represents a permission descriptor."""3132def __init__(self, name: str):33self.name = name3435def to_dict(self) -> dict:36return {"name": self.name}373839class Permissions:40"""BiDi implementation of the permissions module."""4142def __init__(self, conn):43self.conn = conn4445def set_permission(46self,47descriptor: str | PermissionDescriptor,48state: str,49origin: str,50user_context: str | None = None,51) -> None:52"""Sets a permission state for a given permission descriptor.5354Args:55descriptor: The permission name (str) or PermissionDescriptor object.56Examples: "geolocation", "camera", "microphone".57state: The permission state (granted, denied, prompt).58origin: The origin for which the permission is set.59user_context: The user context id (optional).6061Raises:62ValueError: If the permission state is invalid.63"""64if state not in [PermissionState.GRANTED, PermissionState.DENIED, PermissionState.PROMPT]:65valid_states = f"{PermissionState.GRANTED}, {PermissionState.DENIED}, {PermissionState.PROMPT}"66raise ValueError(f"Invalid permission state. Must be one of: {valid_states}")6768if isinstance(descriptor, str):69permission_descriptor = PermissionDescriptor(descriptor)70else:71permission_descriptor = descriptor7273params = {74"descriptor": permission_descriptor.to_dict(),75"state": state,76"origin": origin,77}7879if user_context is not None:80params["userContext"] = user_context8182self.conn.execute(command_builder("permissions.setPermission", params))838485