Path: blob/trunk/py/selenium/webdriver/common/bidi/permissions.py
1864 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.1617from typing import Optional, Union1819from selenium.webdriver.common.bidi.common import command_builder202122class PermissionState:23"""Represents the possible permission states."""2425GRANTED = "granted"26DENIED = "denied"27PROMPT = "prompt"282930class PermissionDescriptor:31"""Represents a permission descriptor."""3233def __init__(self, name: str):34self.name = name3536def to_dict(self) -> dict:37return {"name": self.name}383940class Permissions:41"""42BiDi implementation of the permissions module.43"""4445def __init__(self, conn):46self.conn = conn4748def set_permission(49self,50descriptor: Union[str, PermissionDescriptor],51state: str,52origin: str,53user_context: Optional[str] = None,54) -> None:55"""Sets a permission state for a given permission descriptor.5657Parameters:58-----------59descriptor: The permission name (str) or PermissionDescriptor object.60Examples: "geolocation", "camera", "microphone"61state: The permission state (granted, denied, prompt).62origin: The origin for which the permission is set.63user_context: The user context id (optional).6465Raises:66------67ValueError: If the permission state is invalid.68"""69if state not in [PermissionState.GRANTED, PermissionState.DENIED, PermissionState.PROMPT]:70valid_states = f"{PermissionState.GRANTED}, {PermissionState.DENIED}, {PermissionState.PROMPT}"71raise ValueError(f"Invalid permission state. Must be one of: {valid_states}")7273if isinstance(descriptor, str):74permission_descriptor = PermissionDescriptor(descriptor)75else:76permission_descriptor = descriptor7778params = {79"descriptor": permission_descriptor.to_dict(),80"state": state,81"origin": origin,82}8384if user_context is not None:85params["userContext"] = user_context8687self.conn.execute(command_builder("permissions.setPermission", params))888990