chromiumoxide/
conn.rs

1use std::collections::VecDeque;
2use std::marker::PhantomData;
3use std::pin::Pin;
4use std::task::ready;
5
6use futures::stream::Stream;
7use futures::task::{Context, Poll};
8use futures::{SinkExt, StreamExt};
9use tokio_tungstenite::tungstenite::Message as WsMessage;
10use tokio_tungstenite::MaybeTlsStream;
11use tokio_tungstenite::{tungstenite::protocol::WebSocketConfig, WebSocketStream};
12
13use chromiumoxide_cdp::cdp::browser_protocol::target::SessionId;
14use chromiumoxide_types::{CallId, EventMessage, Message, MethodCall, MethodId};
15
16use crate::error::CdpError;
17use crate::error::Result;
18
19type ConnectStream = MaybeTlsStream<tokio::net::TcpStream>;
20
21/// Exchanges the messages with the websocket
22#[must_use = "streams do nothing unless polled"]
23#[derive(Debug)]
24pub struct Connection<T: EventMessage> {
25    /// Queue of commands to send.
26    pending_commands: VecDeque<MethodCall>,
27    /// The websocket of the chromium instance
28    ws: WebSocketStream<ConnectStream>,
29    /// The identifier for a specific command
30    next_id: usize,
31    /// A flush is required.
32    needs_flush: bool,
33    /// The message that is currently being proceessed
34    pending_flush: Option<MethodCall>,
35    /// The phantom marker.
36    _marker: PhantomData<T>,
37}
38
39lazy_static::lazy_static! {
40    /// Nagle's algorithm disabled?
41    static ref DISABLE_NAGLE: bool = match std::env::var("DISABLE_NAGLE") {
42        Ok(disable_nagle) => disable_nagle == "true",
43        _ => true
44    };
45    /// Websocket config defaults
46    static ref WEBSOCKET_DEFAULTS: bool = match std::env::var("WEBSOCKET_DEFAULTS") {
47        Ok(d) => d == "true",
48        _ => false
49    };
50}
51
52impl<T: EventMessage + Unpin> Connection<T> {
53    pub async fn connect(debug_ws_url: impl AsRef<str>) -> Result<Self> {
54        let mut config = WebSocketConfig::default();
55
56        if *WEBSOCKET_DEFAULTS == false {
57            config.max_message_size = None;
58            config.max_frame_size = None;
59        }
60
61        let (ws, _) = tokio_tungstenite::connect_async_with_config(
62            debug_ws_url.as_ref(),
63            Some(config),
64            *DISABLE_NAGLE,
65        )
66        .await?;
67
68        Ok(Self {
69            pending_commands: Default::default(),
70            ws,
71            next_id: 0,
72            needs_flush: false,
73            pending_flush: None,
74            _marker: Default::default(),
75        })
76    }
77}
78
79impl<T: EventMessage> Connection<T> {
80    fn next_call_id(&mut self) -> CallId {
81        let id = CallId::new(self.next_id);
82        self.next_id = self.next_id.wrapping_add(1);
83        id
84    }
85
86    /// Queue in the command to send over the socket and return the id for this
87    /// command
88    pub fn submit_command(
89        &mut self,
90        method: MethodId,
91        session_id: Option<SessionId>,
92        params: serde_json::Value,
93    ) -> serde_json::Result<CallId> {
94        let id = self.next_call_id();
95        let call = MethodCall {
96            id,
97            method,
98            session_id: session_id.map(Into::into),
99            params,
100        };
101        self.pending_commands.push_back(call);
102        Ok(id)
103    }
104
105    /// flush any processed message and start sending the next over the conn
106    /// sink
107    fn start_send_next(&mut self, cx: &mut Context<'_>) -> Result<()> {
108        if self.needs_flush {
109            if let Poll::Ready(Ok(())) = self.ws.poll_flush_unpin(cx) {
110                self.needs_flush = false;
111            }
112        }
113        if self.pending_flush.is_none() && !self.needs_flush {
114            if let Some(cmd) = self.pending_commands.pop_front() {
115                tracing::trace!("Sending {:?}", cmd);
116                let msg = serde_json::to_string(&cmd)?;
117                self.ws.start_send_unpin(msg.into())?;
118                self.pending_flush = Some(cmd);
119            }
120        }
121        Ok(())
122    }
123}
124
125impl<T: EventMessage + Unpin> Stream for Connection<T> {
126    type Item = Result<Box<Message<T>>>;
127
128    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
129        let pin = self.get_mut();
130
131        // flush pending outgoing messages
132        loop {
133            if let Err(err) = pin.start_send_next(cx) {
134                return Poll::Ready(Some(Err(err)));
135            }
136
137            if let Some(call) = pin.pending_flush.take() {
138                if pin.ws.poll_ready_unpin(cx).is_ready() {
139                    pin.needs_flush = true;
140                    // try another flush in this same poll
141                    continue;
142                } else {
143                    pin.pending_flush = Some(call);
144                }
145            }
146
147            break;
148        }
149
150        // read from the websocket
151        match ready!(pin.ws.poll_next_unpin(cx)) {
152            Some(Ok(WsMessage::Text(text))) => {
153                let ready = decode_message::<T>(text.as_bytes(), Some(&text));
154                Poll::Ready(Some(ready))
155            }
156            Some(Ok(WsMessage::Binary(buf))) => {
157                let ready = decode_message::<T>(&buf, None);
158                Poll::Ready(Some(ready))
159            }
160            Some(Ok(WsMessage::Close(_))) => Poll::Ready(None),
161            // ignore ping and pong
162            Some(Ok(WsMessage::Ping(_))) | Some(Ok(WsMessage::Pong(_))) => {
163                cx.waker().wake_by_ref();
164                Poll::Pending
165            }
166            Some(Ok(msg)) => Poll::Ready(Some(Err(CdpError::UnexpectedWsMessage(msg)))),
167            Some(Err(err)) => Poll::Ready(Some(Err(CdpError::Ws(err)))),
168            None => {
169                // ws connection closed
170                Poll::Ready(None)
171            }
172        }
173    }
174}
175
176/// Shared decode path for both text and binary WS frames.
177/// `raw_text_for_logging` is only provided for textual frames so we can log the original
178/// payload on parse failure if desired.
179#[cfg(not(feature = "serde_stacker"))]
180fn decode_message<T: EventMessage>(
181    bytes: &[u8],
182    raw_text_for_logging: Option<&str>,
183) -> Result<Box<Message<T>>> {
184    match serde_json::from_slice::<Box<Message<T>>>(bytes) {
185        Ok(msg) => {
186            tracing::trace!("Received {:?}", msg);
187            Ok(msg)
188        }
189        Err(err) => {
190            if let Some(txt) = raw_text_for_logging {
191                tracing::error!(
192                    target: "chromiumoxide::conn::raw_ws::parse_errors",
193                    msg_len = txt.len(),
194                    "Failed to parse raw WS message {err}",
195                );
196            } else {
197                tracing::error!(
198                    target: "chromiumoxide::conn::raw_ws::parse_errors",
199                    "Failed to parse binary WS message {err}",
200                );
201            }
202            Err(err.into())
203        }
204    }
205}
206
207/// Shared decode path for both text and binary WS frames.
208/// `raw_text_for_logging` is only provided for textual frames so we can log the original
209/// payload on parse failure if desired.
210#[cfg(feature = "serde_stacker")]
211fn decode_message<T: EventMessage>(
212    bytes: &[u8],
213    raw_text_for_logging: Option<&str>,
214) -> Result<Box<Message<T>>> {
215    use serde::Deserialize;
216    let mut de = serde_json::Deserializer::from_slice(bytes);
217
218    de.disable_recursion_limit();
219
220    let de = serde_stacker::Deserializer::new(&mut de);
221
222    match Box::<Message<T>>::deserialize(de) {
223        Ok(msg) => {
224            tracing::trace!("Received {:?}", msg);
225            Ok(msg)
226        }
227        Err(err) => {
228            if let Some(txt) = raw_text_for_logging {
229                tracing::error!(
230                    target: "chromiumoxide::conn::raw_ws::parse_errors",
231                    msg_len = txt.len(),
232                    "Failed to parse raw WS message {err}",
233                );
234            } else {
235                tracing::error!(
236                    target: "chromiumoxide::conn::raw_ws::parse_errors",
237                    "Failed to parse binary WS message {err}",
238                );
239            }
240            Err(err.into())
241        }
242    }
243}