o
    ѷ6i$                     @   sf   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ G dd dZ	G dd	 d	Z
G d
d dZdS )    N)Any)command_builder)UserPromptHandler)Proxyc                   @   s,   e Zd ZdZdZdZdZdZeeeehZdS )ClientWindowStatezRepresents a window state.
fullscreen	maximized	minimizednormalN)	__name__
__module____qualname____doc__
FULLSCREEN	MAXIMIZED	MINIMIZEDNORMALVALID_STATES r   r   k/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/selenium/webdriver/common/bidi/browser.pyr      s    r   c                   @   s   e Zd ZdZdededededededefd	d
ZdefddZdefddZ	defddZ
defddZdefddZdefddZdefddZededd fddZdS )ClientWindowInfoz'Represents a client window information.client_windowstatewidthheightxyactivec                 C   s.   || _ || _|| _|| _|| _|| _|| _d S Nr   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   __init__'   s   

zClientWindowInfo.__init__returnc                 C      | j S )zGets the state of the client window.

        Returns:
            str: The state of the client window (one of the ClientWindowState constants).
        )r   r    r   r   r   	get_state9      zClientWindowInfo.get_statec                 C   r#   )zlGets the client window identifier.

        Returns:
            str: The client window identifier.
        )r   r$   r   r   r   get_client_windowA   r&   z"ClientWindowInfo.get_client_windowc                 C   r#   )zpGets the width of the client window.

        Returns:
            int: The width of the client window.
        )r   r$   r   r   r   	get_widthI   r&   zClientWindowInfo.get_widthc                 C   r#   )zrGets the height of the client window.

        Returns:
            int: The height of the client window.
        )r   r$   r   r   r   
get_heightQ   r&   zClientWindowInfo.get_heightc                 C   r#   )z~Gets the x coordinate of the client window.

        Returns:
            int: The x coordinate of the client window.
        )r   r$   r   r   r   get_xY   r&   zClientWindowInfo.get_xc                 C   r#   )z~Gets the y coordinate of the client window.

        Returns:
            int: The y coordinate of the client window.
        )r   r$   r   r   r   get_ya   r&   zClientWindowInfo.get_yc                 C   r#   )zChecks if the client window is active.

        Returns:
            bool: True if the client window is active, False otherwise.
        )r   r$   r   r   r   	is_activei   r&   zClientWindowInfo.is_activedatac           
   
   C   sP  z|d }t |tstd|d }t |tstd|tjvr+td| dtj |d }t |tr8|dk r?td	| |d
 }t |trL|dk rStd| |d }t |tsftdt|j |d }t |tsytdt|j |d }t |tstd| |||||||dW S  t	t
fy }	 ztd|	 |	d}	~	ww )aJ  Creates a ClientWindowInfo instance from a dictionary.

        Args:
            data: A dictionary containing the client window information.

        Returns:
            ClientWindowInfo: A new instance of ClientWindowInfo.

        Raises:
            ValueError: If required fields are missing or have invalid types.
        clientWindowzclientWindow must be a stringr   zstate must be a stringzInvalid state: z. Must be one of r   r   z*width must be a non-negative integer, got r   z+height must be a non-negative integer, got r   zx must be an integer, got r   zy must be an integer, got r   zactive must be a booleanr   z*Invalid data format for ClientWindowInfo: N)
isinstancestr
ValueErrorr   r   inttyper   boolKeyError	TypeError)
clsr-   r   r   r   r   r   r   r   er   r   r   	from_dictq   sJ   





	zClientWindowInfo.from_dictN)r   r   r   r   r0   r2   r4   r!   r%   r'   r(   r)   r*   r+   r,   classmethoddictr9   r   r   r   r   r   $   s4    
r   c                	   @   s   e Zd ZdZdd Z			ddedB dedB dedB defd	d
Z	de
e fddZdeddfddZde
e fddZdddddedB deejB dB de
e dB ddfddZdS )Browserz*BiDi implementation of the browser module.c                 C   s
   || _ d S r   )conn)r    r=   r   r   r   r!      s   
zBrowser.__init__Naccept_insecure_certsproxyunhandled_prompt_behaviorr"   c                 C   sV   i }|dur
||d< |dur|  |d< |dur| |d< | jtd|}|d S )ao  Creates a new user context.

        Args:
            accept_insecure_certs: Optional flag to accept insecure TLS certificates.
            proxy: Optional proxy configuration for the user context.
            unhandled_prompt_behavior: Optional configuration for handling user prompts.

        Returns:
            str: The ID of the created user context.
        NacceptInsecureCertsr?   unhandledPromptBehaviorzbrowser.createUserContextuserContext)to_bidi_dictto_dictr=   executer   )r    r>   r?   r@   paramsresultr   r   r   create_user_context   s   zBrowser.create_user_contextc                 C   $   | j tdi }dd |d D S )zeGets all user contexts.

        Returns:
            List[str]: A list of user context IDs.
        zbrowser.getUserContextsc                 S   s   g | ]}|d  qS )rC   r   ).0context_infor   r   r   
<listcomp>   s    z-Browser.get_user_contexts.<locals>.<listcomp>userContextsr=   rF   r   r    rH   r   r   r   get_user_contexts      zBrowser.get_user_contextsuser_context_idc                 C   s.   |dkrt dd|i}| jtd| dS )zRemoves a user context.

        Args:
            user_context_id: The ID of the user context to remove.

        Raises:
            ValueError: If the user context ID is "default" or does not exist.
        defaultz&Cannot remove the default user contextrC   zbrowser.removeUserContextN)r1   r=   rF   r   )r    rS   rG   r   r   r   remove_user_context   s   	zBrowser.remove_user_contextc                 C   rJ   )z|Gets all client windows.

        Returns:
            List[ClientWindowInfo]: A list of client window information.
        zbrowser.getClientWindowsc                 S   s   g | ]}t |qS r   )r   r9   )rK   windowr   r   r   rM      s    z.Browser.get_client_windows.<locals>.<listcomp>clientWindowsrO   rP   r   r   r   get_client_windows   rR   zBrowser.get_client_windows)alloweddestination_folderuser_contextsrY   rZ   r[   c                C   sz   i }|du rd|d< n|r|st ddt|d|d< n|r$t dddi|d< |dur2||d	< | jtd
| dS )a  Set the download behavior for the browser or specific user contexts.

        Args:
            allowed: True to allow downloads, False to deny downloads, or None to
                clear download behavior (revert to default).
            destination_folder: Required when allowed is True. Specifies the folder
                to store downloads in.
            user_contexts: Optional list of user context IDs to apply this
                behavior to. If omitted, updates the default behavior.

        Raises:
            ValueError: If allowed=True and destination_folder is missing, or if
                allowed=False and destination_folder is provided.
        NdownloadBehaviorz1destination_folder is required when allowed=True.rY   )r3   destinationFolderz=destination_folder should not be provided when allowed=False.r3   deniedrN   zbrowser.setDownloadBehavior)r1   osfspathr=   rF   r   )r    rY   rZ   r[   rG   r   r   r   set_download_behavior   s   
zBrowser.set_download_behavior)NNN)r   r   r   r   r!   r4   r   r   r0   rI   listrQ   rU   r   rX   r_   PathLikera   r   r   r   r   r<      s<    
	
r<   )r_   typingr   %selenium.webdriver.common.bidi.commonr   &selenium.webdriver.common.bidi.sessionr   selenium.webdriver.common.proxyr   r   r   r<   r   r   r   r   <module>   s    