Skip to main content

chromiumoxide/handler/
commandfuture.rs

1use futures::channel::{
2    mpsc,
3    oneshot::{self, channel as oneshot_channel},
4};
5use pin_project_lite::pin_project;
6use std::future::Future;
7use std::marker::PhantomData;
8use std::pin::Pin;
9use std::task::{Context, Poll};
10
11use crate::cmd::{to_command_response, CommandMessage};
12use crate::error::Result;
13use crate::handler::target::TargetMessage;
14use chromiumoxide_cdp::cdp::browser_protocol::target::SessionId;
15use chromiumoxide_types::{Command, CommandResponse, MethodId, Response};
16
17pin_project! {
18
19    pub struct CommandFuture<T, M = Result<Response>> {
20        #[pin]
21        rx_command: oneshot::Receiver<M>,
22        #[pin]
23        target_sender: mpsc::Sender<TargetMessage>,
24        // We need delay to be pinned because it's a future
25        // and we need to be able to poll it
26        // it is used to timeout the command if page was closed while waiting for response
27        #[pin]
28        delay: futures_timer::Delay,
29
30        message: Option<TargetMessage>,
31
32        method: MethodId,
33
34        _marker: PhantomData<T>
35    }
36}
37
38impl<T: Command> CommandFuture<T> {
39    /// A new command future.
40    pub fn new(
41        cmd: T,
42        target_sender: mpsc::Sender<TargetMessage>,
43        session: Option<SessionId>,
44        request_timeout: std::time::Duration,
45    ) -> Result<Self> {
46        let (tx, rx_command) = oneshot_channel::<Result<Response>>();
47        let method = cmd.identifier();
48
49        let message = Some(TargetMessage::Command(CommandMessage::with_session(
50            cmd, tx, session,
51        )?));
52
53        let delay = futures_timer::Delay::new(request_timeout);
54
55        Ok(Self {
56            target_sender,
57            rx_command,
58            message,
59            delay,
60            method,
61            _marker: PhantomData,
62        })
63    }
64}
65
66impl<T> Future for CommandFuture<T>
67where
68    T: Command,
69{
70    type Output = Result<CommandResponse<T::Response>>;
71
72    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
73        let mut this = self.project();
74
75        if this.message.is_some() {
76            match this.target_sender.poll_ready(cx) {
77                Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
78                Poll::Ready(Ok(_)) => {
79                    let message = this.message.take().expect("existence checked above");
80                    this.target_sender.start_send(message)?;
81
82                    cx.waker().wake_by_ref();
83                    Poll::Pending
84                }
85                Poll::Pending => Poll::Pending,
86            }
87        } else if this.delay.poll(cx).is_ready() {
88            Poll::Ready(Err(crate::error::CdpError::Timeout))
89        } else {
90            match this.rx_command.as_mut().poll(cx) {
91                Poll::Ready(Ok(Ok(response))) => {
92                    Poll::Ready(to_command_response::<T>(response, this.method.clone()))
93                }
94                Poll::Ready(Ok(Err(e))) => Poll::Ready(Err(e)),
95                Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
96                Poll::Pending => Poll::Pending,
97            }
98        }
99    }
100}