o
    _c.                     @   s   d Z ddlZddlZddlZddlmZmZmZmZm	Z	 ddl
mZ ddlZddlmZ ddlmZ e	r:ddlmZ eeZeed Zeed	 ZG d
d deZG dd deZdedededefddZdededefddZdS )z%Connection/Session management module.    N)	AwaitableCallableDictUnionTYPE_CHECKING)EventEmitter)connect)NetworkError)Optionalz.Connectionz.CDPSessionc                	       s   e Zd ZdZ	d(dedejdeddf fdd	Ze	defd
dZ
d)ddZdededdfddZd*dededefddZdeddfddZdeddfddZdeg df ddfddZdeddfddZd)d d!Zd)d"d#Zd$edd%fd&d'Z  ZS )+
ConnectionzConnection management class.r   urlloopdelayreturnNc                    st   t    || _d| _t | _|d | _|| _t | _|  d| _	t
| jd| jddd| _| j|  | _d| _dS )zMake connection.

        :arg str url: WebSocket url to connect devtool.
        :arg int delay: delay to wait before processing received messages.
        r   i  FN)max_sizer   Zping_intervalZping_timeout)super__init___url_lastIddict
_callbacks_delay_loop	_sessions
_connected
ws_connect_wscreate_task
_recv_loop	_recv_fut_closeCallback)selfr   r   r   	__class__ ?/usr/local/lib/python3.10/dist-packages/pyppeteer/connection.pyr      s   


zConnection.__init__c                 C   s   | j S )zGet connected WebSocket url.)r   r!   r$   r$   r%   r   0   s   zConnection.urlc              
      s   | j 4 I d H E}d| _|| _| jrCz| j I d H }|r%| |I d H  W n tjtfy7   t	d Y nw t
dI d H  | jsW d   I d H  n1 I d H sSw   Y  | jre| j|   d S d S )NTzconnection closedr   )r   r   
connectionrecv_on_message
websocketsConnectionClosedConnectionResetErrorloggerinfoasynciosleepr   r   dispose)r!   r'   respr$   r$   r%   r   5   s(   
(zConnection._recv_loopmsgcallback_idc                    s   | j st| jI d H  | j rz| j|I d H  W d S  tjyK   t	d | j
|d }|rE| sH|d  |  I d H  Y d S Y d S Y d S w )Nzconnection unexpectedly closed)r   r/   r0   r   r'   sendr*   r+   r-   errorr   getdone
set_resultr1   )r!   r3   r4   callbackr$   r$   r%   _async_sendE   s   

zConnection._async_sendmethodparamsc                 C   s   | j r
| js
td|du rt }|  j d7  _ | j }tt|||d}td|  | j	| 
|| | j }|| j|< t |_||_|S )z Send message via the connection.zConnection is closedN   idr<   r=   SEND: )r   r   ConnectionErrorr   jsondumpslogger_connectiondebugr   r   r;   create_futurer   r	   r6   r<   )r!   r<   r=   _idr3   r:   r$   r$   r%   r5   Q   s$   

zConnection.sendc                 C   sL   | j |dd}|dr|t|j|j| d S ||d d S )Nr@   r6   result)r   popr7   set_exception_createProtocolErrorr6   r<   r9   )r!   r3   r:   r$   r$   r%   _on_responseg   s   
zConnection._on_responsec                 C   s   | di }| dd}| d}|dkr)| j |}|r'|| d d S d S |dkrA| j |}|r?|  | j|= d S d S | || d S )Nr=   r<    	sessionId Target.receivedMessageFromTargetmessagezTarget.detachedFromTarget)r7   r   r)   
_on_closedemit)r!   r3   r=   r<   rP   sessionr$   r$   r%   	_on_queryt   s   
zConnection._on_queryr:   c                 C   s
   || _ dS )zSet closed callback.N)r    )r!   r:   r$   r$   r%   setClosedCallback   s   
zConnection.setClosedCallbackrR   c                    sZ   t | jI d H  td|  t|}|d| jv r&| 	| d S | 
| d S )NRECV: r@   )r/   r0   r   rE   rF   rC   loadsr7   r   rN   rV   )r!   rR   r3   r$   r$   r%   r)      s   
zConnection._on_messagec                    s   | j r|    d | _ | j D ]}|t|jd|j d q| j  | j D ]}|	  q+| j  t
| drD| j I d H  | j sP| j  d S d S )NProtocol error : Target closed.r'   )r    r   valuesrL   _rewriteErrorr6   r<   clearr   rS   hasattrr'   closer   r8   cancel)r!   cbrU   r$   r$   r%   	_on_close   s$   




zConnection._on_closec                    s   d| _ |  I dH  dS )zClose all connection.FN)r   rc   r&   r$   r$   r%   r1      s   zConnection.dispose
targetInfo
CDPSessionc                    sH   |  dd|d iI dH }|d}t| |d || j}|| j|< |S )zCreate new session.zTarget.attachToTargetZtargetIdNrP   type)r5   r7   re   r   r   )r!   rd   r2   rP   rU   r$   r$   r%   createSession   s   



zConnection.createSession)r   r   NN)__name__
__module____qualname____doc__strr/   AbstractEventLoopintr   propertyr   r   r;   r   r   r5   rN   rV   r   rW   r)   rc   r1   r   rg   __classcell__r$   r$   r"   r%   r      s(    

	
r   c                
       s   e Zd ZdZdeed f dededejddf
 fdd	Z	dd
ede
defddZdeddfddZdddZdddZdededd fddZ  ZS )re   a|  Chrome Devtools Protocol Session.

    The :class:`CDPSession` instances are used to talk raw Chrome Devtools
    Protocol:

    * protocol methods can be called with :meth:`send` method.
    * protocol events can be subscribed to with :meth:`on` method.

    Documentation on DevTools Protocol can be found
    `here <https://chromedevtools.github.io/devtools-protocol/>`__.
    r'   
targetTyperP   r   r   Nc                    s:   t    d| _i | _|| _|| _|| _t | _|| _	dS )zMake new session.r   N)
r   r   r   r   _connection_targetType
_sessionIdr   r   r   )r!   r'   rs   rP   r   r"   r$   r%   r      s   

zCDPSession.__init__r<   r=   c              
   C   s   | j std| d| j d|  jd7  _| j}tt|||d}td|  | j	
 }|| j|< t |_||_z| j d| j|d W |S  ty~ } z(|| jv rs| j|= | j| }|t|j|jd	  W Y d
}~|S W Y d
}~|S d
}~ww )zSend message to the connected session.

        :arg str method: Protocol method name.
        :arg dict params: Optional method parameters.
        zProtocol Error (z#): Session closed. Most likely the z has been closed.r>   r?   rA   zTarget.sendMessageToTarget)rP   rR   r   N)rt   r	   ru   r   rC   rD   r   logger_sessionrF   r   rG   r   r6   r<   r5   rv   	ExceptionrL   r]   args)r!   r<   r=   rH   r3   r:   e	_callbackr$   r$   r%   r5      sB   





zCDPSession.sendr3   c           	      C   s*  t d|  t|}|d}|rJ| j|}|rD| j|= |dr2|t|j|j	| d S |d}|rF|
 sH|| d S d S d S d S |di }|ddkrk| j|d}|rj||d	 n|dd
kr|d}| j|}|r|  | j|= | |d|d d S )NrX   r@   r6   rJ   r=   r<   rQ   rP   rR   Target.detachFromTarget)rw   rF   rC   rY   r7   r   rL   rM   r6   r<   r8   r9   r   r)   rS   rT   )	r!   r3   objrH   r:   rJ   r=   rU   rP   r$   r$   r%   r)      s>   






zCDPSession._on_messagec                    s.   | j std| j dd| jiI dH  dS )zDetach session from target.

        Once detached, session won't emit any events and can't be used to send
        messages.
        zConnection already closed.r|   rP   N)rt   r	   r5   rv   r&   r$   r$   r%   detach  s   zCDPSession.detachc                 C   s@   | j  D ]}|t|jd|j d q| j   d | _d S )NrZ   r[   )r   r\   rL   r]   r6   r<   r^   rt   )r!   rb   r$   r$   r%   rS   !  s   

zCDPSession._on_closedc                 C   s   t | ||| j}|| j|< |S ri   )re   r   r   )r!   rs   rP   rU   r$   r$   r%   _createSession*  s   
zCDPSession._createSessionri   rh   )rj   rk   rl   rm   r   r   rn   r/   ro   r   r   r   r5   r)   r~   rS   r   rr   r$   r$   r"   r%   re      s     $
 
	re   r6   r<   r}   r   c                 C   sD   d| d|d d  }d|d v r|d|d d  7 }t | |S )NzProtocol error (z): r6   rR   data )r]   )r6   r<   r}   rR   r$   r$   r%   rM   0  s   
rM   rR   c                 C   s   |f| _ | S ri   )ry   )r6   rR   r$   r$   r%   r]   8  s   r]   )rm   r/   rC   loggingtypingr   r   r   r   r   Zpyeer   r*   Zwebsockets.legacy.clientr   r   Zpyppeteer.errorsr	   r
   	getLoggerrj   r-   rE   rw   r   re   rx   rn   rM   r]   r$   r$   r$   r%   <module>   s*   
  x
