chromiumoxide/
browser.rs

1use hashbrown::HashMap;
2use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
3use std::future::Future;
4use std::time::Duration;
5use std::{
6    io,
7    path::{Path, PathBuf},
8};
9
10use futures::channel::mpsc::{channel, unbounded, Sender};
11use futures::channel::oneshot::channel as oneshot_channel;
12use futures::select;
13use futures::SinkExt;
14
15use crate::async_process::{self, Child, ExitStatus, Stdio};
16use crate::cmd::{to_command_response, CommandMessage};
17use crate::conn::Connection;
18use crate::detection::{self, DetectionOptions};
19use crate::error::{BrowserStderr, CdpError, Result};
20use crate::handler::browser::BrowserContext;
21use crate::handler::viewport::Viewport;
22use crate::handler::{Handler, HandlerConfig, HandlerMessage, REQUEST_TIMEOUT};
23use crate::listeners::{EventListenerRequest, EventStream};
24use crate::page::Page;
25use crate::utils;
26use chromiumoxide_cdp::cdp::browser_protocol::browser::{
27    BrowserContextId, CloseReturns, GetVersionParams, GetVersionReturns,
28};
29use chromiumoxide_cdp::cdp::browser_protocol::browser::{
30    PermissionDescriptor, PermissionSetting, SetPermissionParams,
31};
32use chromiumoxide_cdp::cdp::browser_protocol::network::{Cookie, CookieParam};
33use chromiumoxide_cdp::cdp::browser_protocol::storage::{
34    ClearCookiesParams, GetCookiesParams, SetCookiesParams,
35};
36use chromiumoxide_cdp::cdp::browser_protocol::target::{
37    CreateBrowserContextParams, CreateTargetParams, DisposeBrowserContextParams,
38    GetBrowserContextsParams, GetBrowserContextsReturns, TargetId, TargetInfo,
39};
40
41use chromiumoxide_cdp::cdp::{CdpEventMessage, IntoEventKind};
42use chromiumoxide_types::*;
43use spider_network_blocker::intercept_manager::NetworkInterceptManager;
44
45/// Default `Browser::launch` timeout in MS
46pub const LAUNCH_TIMEOUT: u64 = 20_000;
47
48lazy_static::lazy_static! {
49    /// The request client to get the web socket url.
50    static ref REQUEST_CLIENT: reqwest::Client = reqwest::Client::builder()
51        .timeout(Duration::from_secs(60))
52        .default_headers({
53            let mut m = HeaderMap::new();
54
55            m.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
56
57            m
58        })
59        .tcp_keepalive(Some(Duration::from_secs(5)))
60        .pool_idle_timeout(Some(Duration::from_secs(60)))
61        .pool_max_idle_per_host(10)
62        .build()
63        .expect("client to build");
64}
65
66/// A [`Browser`] is created when chromiumoxide connects to a Chromium instance.
67#[derive(Debug)]
68pub struct Browser {
69    /// The `Sender` to send messages to the connection handler that drives the
70    /// websocket
71    pub(crate) sender: Sender<HandlerMessage>,
72    /// How the spawned chromium instance was configured, if any
73    config: Option<BrowserConfig>,
74    /// The spawned chromium instance
75    child: Option<Child>,
76    /// The debug web socket url of the chromium instance
77    debug_ws_url: String,
78    /// The context of the browser
79    pub browser_context: BrowserContext,
80}
81
82/// Browser connection information.
83#[derive(serde::Deserialize, Debug, Default)]
84pub struct BrowserConnection {
85    #[serde(rename = "Browser")]
86    /// The browser name
87    pub browser: String,
88    #[serde(rename = "Protocol-Version")]
89    /// Browser version
90    pub protocol_version: String,
91    #[serde(rename = "User-Agent")]
92    /// User Agent used by default.
93    pub user_agent: String,
94    #[serde(rename = "V8-Version")]
95    /// The v8 engine version
96    pub v8_version: String,
97    #[serde(rename = "WebKit-Version")]
98    /// Webkit version
99    pub webkit_version: String,
100    #[serde(rename = "webSocketDebuggerUrl")]
101    /// Remote debugging address
102    pub web_socket_debugger_url: String,
103}
104
105impl Browser {
106    /// Connect to an already running chromium instance via the given URL.
107    ///
108    /// If the URL is a http(s) URL, it will first attempt to retrieve the Websocket URL from the `json/version` endpoint.
109    pub async fn connect(url: impl Into<String>) -> Result<(Self, Handler)> {
110        Self::connect_with_config(url, HandlerConfig::default()).await
111    }
112
113    // Connect to an already running chromium instance with a given `HandlerConfig`.
114    ///
115    /// If the URL is a http URL, it will first attempt to retrieve the Websocket URL from the `json/version` endpoint.
116    pub async fn connect_with_config(
117        url: impl Into<String>,
118        config: HandlerConfig,
119    ) -> Result<(Self, Handler)> {
120        let mut debug_ws_url = url.into();
121
122        if debug_ws_url.starts_with("http") {
123            match REQUEST_CLIENT
124                .get(
125                    if debug_ws_url.ends_with("/json/version")
126                        || debug_ws_url.ends_with("/json/version/")
127                    {
128                        debug_ws_url.to_owned()
129                    } else {
130                        format!(
131                            "{}{}json/version",
132                            &debug_ws_url,
133                            if debug_ws_url.ends_with('/') { "" } else { "/" }
134                        )
135                    },
136                )
137                .send()
138                .await
139            {
140                Ok(req) => {
141                    if let Ok(b) = req.bytes().await {
142                        if let Ok(connection) =
143                            crate::serde_json::from_slice::<Box<BrowserConnection>>(&b)
144                        {
145                            if !connection.web_socket_debugger_url.is_empty() {
146                                debug_ws_url = connection.web_socket_debugger_url;
147                            }
148                        }
149                    }
150                }
151                Err(_) => return Err(CdpError::NoResponse),
152            }
153        }
154
155        let conn = Connection::<CdpEventMessage>::connect(&debug_ws_url).await?;
156
157        let (tx, rx) = channel(1000);
158
159        let handler_config = BrowserConfig {
160            ignore_https_errors: config.ignore_https_errors,
161            viewport: config.viewport.clone(),
162            request_timeout: config.request_timeout,
163            request_intercept: config.request_intercept,
164            cache_enabled: config.cache_enabled,
165            ignore_visuals: config.ignore_visuals,
166            ignore_stylesheets: config.ignore_stylesheets,
167            ignore_javascript: config.ignore_javascript,
168            ignore_analytics: config.ignore_analytics,
169            ignore_ads: config.ignore_ads,
170            extra_headers: config.extra_headers.clone(),
171            only_html: config.only_html,
172            service_worker_enabled: config.service_worker_enabled,
173            intercept_manager: config.intercept_manager,
174            max_bytes_allowed: config.max_bytes_allowed,
175            whitelist_patterns: config.whitelist_patterns.clone(),
176            blacklist_patterns: config.blacklist_patterns.clone(),
177            ..Default::default()
178        };
179
180        let fut = Handler::new(conn, rx, config);
181        let browser_context = fut.default_browser_context().clone();
182
183        let browser = Self {
184            sender: tx,
185            config: Some(handler_config),
186            child: None,
187            debug_ws_url,
188            browser_context,
189        };
190
191        Ok((browser, fut))
192    }
193
194    /// Launches a new instance of `chromium` in the background and attaches to
195    /// its debug web socket.
196    ///
197    /// This fails when no chromium executable could be detected.
198    ///
199    /// This fails if no web socket url could be detected from the child
200    /// processes stderr for more than the configured `launch_timeout`
201    /// (20 seconds by default).
202    pub async fn launch(mut config: BrowserConfig) -> Result<(Self, Handler)> {
203        // Canonalize paths to reduce issues with sandboxing
204        config.executable = utils::canonicalize_except_snap(config.executable).await?;
205
206        // Launch a new chromium instance
207        let mut child = config.launch()?;
208
209        /// Faillible initialization to run once the child process is created.
210        ///
211        /// All faillible calls must be executed inside this function. This ensures that all
212        /// errors are caught and that the child process is properly cleaned-up.
213        async fn with_child(
214            config: &BrowserConfig,
215            child: &mut Child,
216        ) -> Result<(String, Connection<CdpEventMessage>)> {
217            let dur = config.launch_timeout;
218            let timeout_fut = Box::pin(tokio::time::sleep(dur));
219
220            // extract the ws:
221            let debug_ws_url = ws_url_from_output(child, timeout_fut).await?;
222            let conn = Connection::<CdpEventMessage>::connect(&debug_ws_url).await?;
223            Ok((debug_ws_url, conn))
224        }
225
226        let (debug_ws_url, conn) = match with_child(&config, &mut child).await {
227            Ok(conn) => conn,
228            Err(e) => {
229                // An initialization error occurred, clean up the process
230                if let Ok(Some(_)) = child.try_wait() {
231                    // already exited, do nothing, may happen if the browser crashed
232                } else {
233                    // the process is still alive, kill it and wait for exit (avoid zombie processes)
234                    child.kill().await.expect("`Browser::launch` failed but could not clean-up the child process (`kill`)");
235                    child.wait().await.expect("`Browser::launch` failed but could not clean-up the child process (`wait`)");
236                }
237                return Err(e);
238            }
239        };
240
241        // Only infaillible calls are allowed after this point to avoid clean-up issues with the
242        // child process.
243
244        let (tx, rx) = channel(1000);
245
246        let handler_config = HandlerConfig {
247            ignore_https_errors: config.ignore_https_errors,
248            viewport: config.viewport.clone(),
249            context_ids: Vec::new(),
250            request_timeout: config.request_timeout,
251            request_intercept: config.request_intercept,
252            cache_enabled: config.cache_enabled,
253            ignore_visuals: config.ignore_visuals,
254            ignore_stylesheets: config.ignore_stylesheets,
255            ignore_javascript: config.ignore_javascript,
256            ignore_analytics: config.ignore_analytics,
257            ignore_ads: config.ignore_ads,
258            extra_headers: config.extra_headers.clone(),
259            only_html: config.only_html,
260            service_worker_enabled: config.service_worker_enabled,
261            created_first_target: false,
262            intercept_manager: config.intercept_manager,
263            max_bytes_allowed: config.max_bytes_allowed,
264            whitelist_patterns: config.whitelist_patterns.clone(),
265            blacklist_patterns: config.blacklist_patterns.clone(),
266        };
267
268        let fut = Handler::new(conn, rx, handler_config);
269        let browser_context = fut.default_browser_context().clone();
270
271        let browser = Self {
272            sender: tx,
273            config: Some(config),
274            child: Some(child),
275            debug_ws_url,
276            browser_context,
277        };
278
279        Ok((browser, fut))
280    }
281
282    /// Request to fetch all existing browser targets.
283    ///
284    /// By default, only targets launched after the browser connection are tracked
285    /// when connecting to a existing browser instance with the devtools websocket url
286    /// This function fetches existing targets on the browser and adds them as pages internally
287    ///
288    /// The pages are not guaranteed to be ready as soon as the function returns
289    /// You should wait a few millis if you need to use a page
290    /// Returns [TargetInfo]
291    pub async fn fetch_targets(&mut self) -> Result<Vec<TargetInfo>> {
292        let (tx, rx) = oneshot_channel();
293
294        self.sender
295            .clone()
296            .send(HandlerMessage::FetchTargets(tx))
297            .await?;
298
299        rx.await?
300    }
301
302    /// Request for the browser to close completely.
303    ///
304    /// If the browser was spawned by [`Browser::launch`], it is recommended to wait for the
305    /// spawned instance exit, to avoid "zombie" processes ([`Browser::wait`],
306    /// [`Browser::wait_sync`], [`Browser::try_wait`]).
307    /// [`Browser::drop`] waits automatically if needed.
308    pub async fn close(&self) -> Result<CloseReturns> {
309        let (tx, rx) = oneshot_channel();
310
311        self.sender
312            .clone()
313            .send(HandlerMessage::CloseBrowser(tx))
314            .await?;
315
316        rx.await?
317    }
318
319    /// Asynchronously wait for the spawned chromium instance to exit completely.
320    ///
321    /// The instance is spawned by [`Browser::launch`]. `wait` is usually called after
322    /// [`Browser::close`]. You can call this explicitly to collect the process and avoid
323    /// "zombie" processes.
324    ///
325    /// This call has no effect if this [`Browser`] did not spawn any chromium instance (e.g.
326    /// connected to an existing browser through [`Browser::connect`])
327    pub async fn wait(&mut self) -> io::Result<Option<ExitStatus>> {
328        if let Some(child) = self.child.as_mut() {
329            Ok(Some(child.wait().await?))
330        } else {
331            Ok(None)
332        }
333    }
334
335    /// If the spawned chromium instance has completely exited, wait for it.
336    ///
337    /// The instance is spawned by [`Browser::launch`]. `try_wait` is usually called after
338    /// [`Browser::close`]. You can call this explicitly to collect the process and avoid
339    /// "zombie" processes.
340    ///
341    /// This call has no effect if this [`Browser`] did not spawn any chromium instance (e.g.
342    /// connected to an existing browser through [`Browser::connect`])
343    pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
344        if let Some(child) = self.child.as_mut() {
345            child.try_wait()
346        } else {
347            Ok(None)
348        }
349    }
350
351    /// Get the spawned chromium instance
352    ///
353    /// The instance is spawned by [`Browser::launch`]. The result is a [`async_process::Child`]
354    /// value. It acts as a compat wrapper for an `async-std` or `tokio` child process.
355    ///
356    /// You may use [`async_process::Child::as_mut_inner`] to retrieve the concrete implementation
357    /// for the selected runtime.
358    ///
359    /// This call has no effect if this [`Browser`] did not spawn any chromium instance (e.g.
360    /// connected to an existing browser through [`Browser::connect`])
361    pub fn get_mut_child(&mut self) -> Option<&mut Child> {
362        self.child.as_mut()
363    }
364
365    /// Has a browser instance launched on system.
366    pub fn has_child(&self) -> bool {
367        self.child.is_some()
368    }
369
370    /// Forcibly kill the spawned chromium instance
371    ///
372    /// The instance is spawned by [`Browser::launch`]. `kill` will automatically wait for the child
373    /// process to exit to avoid "zombie" processes.
374    ///
375    /// This method is provided to help if the browser does not close by itself. You should prefer
376    /// to use [`Browser::close`].
377    ///
378    /// This call has no effect if this [`Browser`] did not spawn any chromium instance (e.g.
379    /// connected to an existing browser through [`Browser::connect`])
380    pub async fn kill(&mut self) -> Option<io::Result<()>> {
381        match self.child.as_mut() {
382            Some(child) => Some(child.kill().await),
383            None => None,
384        }
385    }
386
387    /// If not launched as incognito this creates a new incognito browser
388    /// context. After that this browser exists within the incognito session.
389    /// New pages created while being in incognito mode will also run in the
390    /// incognito context. Incognito contexts won't share cookies/cache with
391    /// other browser contexts.
392    pub async fn start_incognito_context(&mut self) -> Result<&mut Self> {
393        if !self.is_incognito_configured() {
394            let browser_context_id = self
395                .create_browser_context(CreateBrowserContextParams::default())
396                .await?;
397            self.browser_context = BrowserContext::from(browser_context_id);
398            self.sender
399                .clone()
400                .send(HandlerMessage::InsertContext(self.browser_context.clone()))
401                .await?;
402        }
403
404        Ok(self)
405    }
406
407    /// If a incognito session was created with
408    /// `Browser::start_incognito_context` this disposes this context.
409    ///
410    /// # Note This will also dispose all pages that were running within the
411    /// incognito context.
412    pub async fn quit_incognito_context_base(
413        &self,
414        browser_context_id: BrowserContextId,
415    ) -> Result<&Self> {
416        self.dispose_browser_context(browser_context_id.clone())
417            .await?;
418        self.sender
419            .clone()
420            .send(HandlerMessage::DisposeContext(BrowserContext::from(
421                browser_context_id,
422            )))
423            .await?;
424        Ok(self)
425    }
426
427    /// If a incognito session was created with
428    /// `Browser::start_incognito_context` this disposes this context.
429    ///
430    /// # Note This will also dispose all pages that were running within the
431    /// incognito context.
432    pub async fn quit_incognito_context(&mut self) -> Result<&mut Self> {
433        if let Some(id) = self.browser_context.take() {
434            let _ = self.quit_incognito_context_base(id).await;
435        }
436        Ok(self)
437    }
438
439    /// Whether incognito mode was configured from the start
440    fn is_incognito_configured(&self) -> bool {
441        self.config
442            .as_ref()
443            .map(|c| c.incognito)
444            .unwrap_or_default()
445    }
446
447    /// Returns the address of the websocket this browser is attached to
448    pub fn websocket_address(&self) -> &String {
449        &self.debug_ws_url
450    }
451
452    /// Whether the BrowserContext is incognito.
453    pub fn is_incognito(&self) -> bool {
454        self.is_incognito_configured() || self.browser_context.is_incognito()
455    }
456
457    /// The config of the spawned chromium instance if any.
458    pub fn config(&self) -> Option<&BrowserConfig> {
459        self.config.as_ref()
460    }
461
462    /// Create a new browser page
463    pub async fn new_page(&self, params: impl Into<CreateTargetParams>) -> Result<Page> {
464        let (tx, rx) = oneshot_channel();
465        let mut params = params.into();
466
467        if let Some(id) = self.browser_context.id() {
468            if params.browser_context_id.is_none() {
469                params.browser_context_id = Some(id.clone());
470            }
471        }
472
473        let _ = self
474            .sender
475            .clone()
476            .send(HandlerMessage::CreatePage(params, tx))
477            .await;
478
479        rx.await?
480    }
481
482    /// Version information about the browser
483    pub async fn version(&self) -> Result<GetVersionReturns> {
484        Ok(self.execute(GetVersionParams::default()).await?.result)
485    }
486
487    /// Returns the user agent of the browser
488    pub async fn user_agent(&self) -> Result<String> {
489        Ok(self.version().await?.user_agent)
490    }
491
492    /// Call a browser method.
493    pub async fn execute<T: Command>(&self, cmd: T) -> Result<CommandResponse<T::Response>> {
494        let (tx, rx) = oneshot_channel();
495        let method = cmd.identifier();
496        let msg = CommandMessage::new(cmd, tx)?;
497
498        self.sender
499            .clone()
500            .send(HandlerMessage::Command(msg))
501            .await?;
502        let resp = rx.await??;
503        to_command_response::<T>(resp, method)
504    }
505
506    /// Set permission settings for given embedding and embedded origins.
507    /// [PermissionDescriptor](https://chromedevtools.github.io/devtools-protocol/tot/Browser/#type-PermissionDescriptor)
508    /// [PermissionSetting](https://chromedevtools.github.io/devtools-protocol/tot/Browser/#type-PermissionSetting)
509    pub async fn set_permission(
510        &self,
511        permission: PermissionDescriptor,
512        setting: PermissionSetting,
513        origin: Option<impl Into<String>>,
514        embedded_origin: Option<impl Into<String>>,
515        browser_context_id: Option<BrowserContextId>,
516    ) -> Result<&Self> {
517        self.execute(SetPermissionParams {
518            permission,
519            setting,
520            origin: origin.map(Into::into),
521            embedded_origin: embedded_origin.map(Into::into),
522            browser_context_id: browser_context_id.or_else(|| self.browser_context.id.clone()),
523        })
524        .await?;
525        Ok(self)
526    }
527
528    /// Convenience: set a permission for a single origin using the current browser context.
529    pub async fn set_permission_for_origin(
530        &self,
531        origin: impl Into<String>,
532        embedded_origin: Option<impl Into<String>>,
533        permission: PermissionDescriptor,
534        setting: PermissionSetting,
535    ) -> Result<&Self> {
536        self.set_permission(permission, setting, Some(origin), embedded_origin, None)
537            .await
538    }
539
540    /// "Reset" a permission override by setting it back to Prompt.
541    pub async fn reset_permission_for_origin(
542        &self,
543        origin: impl Into<String>,
544        embedded_origin: Option<impl Into<String>>,
545        permission: PermissionDescriptor,
546    ) -> Result<&Self> {
547        self.set_permission_for_origin(
548            origin,
549            embedded_origin,
550            permission,
551            PermissionSetting::Prompt,
552        )
553        .await
554    }
555
556    /// "Grant" all permissions.
557    pub async fn grant_all_permission_for_origin(
558        &self,
559        origin: impl Into<String>,
560        embedded_origin: Option<impl Into<String>>,
561        permission: PermissionDescriptor,
562    ) -> Result<&Self> {
563        self.set_permission_for_origin(
564            origin,
565            embedded_origin,
566            permission,
567            PermissionSetting::Granted,
568        )
569        .await
570    }
571
572    /// "Deny" all permissions.
573    pub async fn deny_all_permission_for_origin(
574        &self,
575        origin: impl Into<String>,
576        embedded_origin: Option<impl Into<String>>,
577        permission: PermissionDescriptor,
578    ) -> Result<&Self> {
579        self.set_permission_for_origin(
580            origin,
581            embedded_origin,
582            permission,
583            PermissionSetting::Denied,
584        )
585        .await
586    }
587
588    /// Return all of the pages of the browser
589    pub async fn pages(&self) -> Result<Vec<Page>> {
590        let (tx, rx) = oneshot_channel();
591        self.sender
592            .clone()
593            .send(HandlerMessage::GetPages(tx))
594            .await?;
595        Ok(rx.await?)
596    }
597
598    /// Return page of given target_id
599    pub async fn get_page(&self, target_id: TargetId) -> Result<Page> {
600        let (tx, rx) = oneshot_channel();
601        self.sender
602            .clone()
603            .send(HandlerMessage::GetPage(target_id, tx))
604            .await?;
605        rx.await?.ok_or(CdpError::NotFound)
606    }
607
608    /// Set listener for browser event
609    pub async fn event_listener<T: IntoEventKind>(&self) -> Result<EventStream<T>> {
610        let (tx, rx) = unbounded();
611        self.sender
612            .clone()
613            .send(HandlerMessage::AddEventListener(
614                EventListenerRequest::new::<T>(tx),
615            ))
616            .await?;
617
618        Ok(EventStream::new(rx))
619    }
620
621    /// Creates a new empty browser context.
622    pub async fn create_browser_context(
623        &mut self,
624        params: CreateBrowserContextParams,
625    ) -> Result<BrowserContextId> {
626        let response = self.execute(params).await?;
627
628        Ok(response.result.browser_context_id)
629    }
630
631    /// Returns all browser contexts created with Target.createBrowserContext method.
632    pub async fn get_browser_contexts(
633        &mut self,
634        params: GetBrowserContextsParams,
635    ) -> Result<GetBrowserContextsReturns> {
636        let response = self.execute(params).await?;
637        Ok(response.result)
638    }
639
640    /// Send a new empty browser context.
641    pub async fn send_new_context(
642        &mut self,
643        browser_context_id: BrowserContextId,
644    ) -> Result<&Self> {
645        self.browser_context = BrowserContext::from(browser_context_id);
646        self.sender
647            .clone()
648            .send(HandlerMessage::InsertContext(self.browser_context.clone()))
649            .await?;
650        Ok(self)
651    }
652
653    /// Deletes a browser context.
654    pub async fn dispose_browser_context(
655        &self,
656        browser_context_id: impl Into<BrowserContextId>,
657    ) -> Result<&Self> {
658        self.execute(DisposeBrowserContextParams::new(browser_context_id))
659            .await?;
660
661        Ok(self)
662    }
663
664    /// Clears cookies.
665    pub async fn clear_cookies(&self) -> Result<&Self> {
666        self.execute(ClearCookiesParams::default()).await?;
667        Ok(self)
668    }
669
670    /// Returns all browser cookies.
671    pub async fn get_cookies(&self) -> Result<Vec<Cookie>> {
672        let mut cmd = GetCookiesParams::default();
673
674        cmd.browser_context_id = self.browser_context.id.clone();
675
676        Ok(self.execute(cmd).await?.result.cookies)
677    }
678
679    /// Sets given cookies.
680    pub async fn set_cookies(&self, mut cookies: Vec<CookieParam>) -> Result<&Self> {
681        for cookie in &mut cookies {
682            if let Some(url) = cookie.url.as_ref() {
683                crate::page::validate_cookie_url(url)?;
684            }
685        }
686
687        let mut cookies_param = SetCookiesParams::new(cookies);
688
689        cookies_param.browser_context_id = self.browser_context.id.clone();
690
691        self.execute(cookies_param).await?;
692        Ok(self)
693    }
694}
695
696impl Drop for Browser {
697    fn drop(&mut self) {
698        if let Some(child) = self.child.as_mut() {
699            if let Ok(Some(_)) = child.try_wait() {
700                // Already exited, do nothing. Usually occurs after using the method close or kill.
701            } else {
702                // We set the `kill_on_drop` property for the child process, so no need to explicitely
703                // kill it here. It can't really be done anyway since the method is async.
704                //
705                // On Unix, the process will be reaped in the background by the runtime automatically
706                // so it won't leave any resources locked. It is, however, a better practice for the user to
707                // do it himself since the runtime doesn't provide garantees as to when the reap occurs, so we
708                // warn him here.
709                tracing::warn!("Browser was not closed manually, it will be killed automatically in the background");
710            }
711        }
712    }
713}
714
715/// Resolve devtools WebSocket URL from the provided browser process
716///
717/// If an error occurs, it returns the browser's stderr output.
718///
719/// The URL resolution fails if:
720/// - [`CdpError::LaunchTimeout`]: `timeout_fut` completes, this corresponds to a timeout
721/// - [`CdpError::LaunchExit`]: the browser process exits (or is killed)
722/// - [`CdpError::LaunchIo`]: an input/output error occurs when await the process exit or reading
723///   the browser's stderr: end of stream, invalid UTF-8, other
724async fn ws_url_from_output(
725    child_process: &mut Child,
726    timeout_fut: impl Future<Output = ()> + Unpin,
727) -> Result<String> {
728    use futures::{AsyncBufReadExt, FutureExt};
729    let mut timeout_fut = timeout_fut.fuse();
730    let stderr = child_process.stderr.take().expect("no stderror");
731    let mut stderr_bytes = Vec::<u8>::new();
732    let mut exit_status_fut = Box::pin(child_process.wait()).fuse();
733    let mut buf = futures::io::BufReader::new(stderr);
734    loop {
735        select! {
736            _ = timeout_fut => return Err(CdpError::LaunchTimeout(BrowserStderr::new(stderr_bytes))),
737            exit_status = exit_status_fut => {
738                return Err(match exit_status {
739                    Err(e) => CdpError::LaunchIo(e, BrowserStderr::new(stderr_bytes)),
740                    Ok(exit_status) => CdpError::LaunchExit(exit_status, BrowserStderr::new(stderr_bytes)),
741                })
742            },
743            read_res = buf.read_until(b'\n', &mut stderr_bytes).fuse() => {
744                match read_res {
745                    Err(e) => return Err(CdpError::LaunchIo(e, BrowserStderr::new(stderr_bytes))),
746                    Ok(byte_count) => {
747                        if byte_count == 0 {
748                            let e = io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected end of stream");
749                            return Err(CdpError::LaunchIo(e, BrowserStderr::new(stderr_bytes)));
750                        }
751                        let start_offset = stderr_bytes.len() - byte_count;
752                        let new_bytes = &stderr_bytes[start_offset..];
753                        match std::str::from_utf8(new_bytes) {
754                            Err(_) => {
755                                let e = io::Error::new(io::ErrorKind::InvalidData, "stream did not contain valid UTF-8");
756                                return Err(CdpError::LaunchIo(e, BrowserStderr::new(stderr_bytes)));
757                            }
758                            Ok(line) => {
759                                if let Some((_, ws)) = line.rsplit_once("listening on ") {
760                                    if ws.starts_with("ws") && ws.contains("devtools/browser") {
761                                        return Ok(ws.trim().to_string());
762                                    }
763                                }
764                            }
765                        }
766                    }
767                }
768            }
769        }
770    }
771}
772
773#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
774pub enum HeadlessMode {
775    /// The "headful" mode.
776    False,
777    /// The old headless mode.
778    #[default]
779    True,
780    /// The new headless mode. See also: https://developer.chrome.com/docs/chromium/new-headless
781    New,
782}
783
784#[derive(Debug, Clone, Default)]
785pub struct BrowserConfig {
786    /// Determines whether to run headless version of the browser. Defaults to
787    /// true.
788    headless: HeadlessMode,
789    /// Determines whether to run the browser with a sandbox.
790    sandbox: bool,
791    /// Launch the browser with a specific window width and height.
792    window_size: Option<(u32, u32)>,
793    /// Launch the browser with a specific debugging port.
794    port: u16,
795    /// Path for Chrome or Chromium.
796    ///
797    /// If unspecified, the create will try to automatically detect a suitable
798    /// binary.
799    executable: std::path::PathBuf,
800
801    /// A list of Chrome extensions to load.
802    ///
803    /// An extension should be a path to a folder containing the extension code.
804    /// CRX files cannot be used directly and must be first extracted.
805    ///
806    /// Note that Chrome does not support loading extensions in headless-mode.
807    /// See https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c5
808    extensions: Vec<String>,
809
810    /// Environment variables to set for the Chromium process.
811    /// Passes value through to std::process::Command::envs.
812    pub process_envs: Option<HashMap<String, String>>,
813
814    /// Data dir for user data
815    pub user_data_dir: Option<PathBuf>,
816
817    /// Whether to launch the `Browser` in incognito mode.
818    incognito: bool,
819
820    /// Timeout duration for `Browser::launch`.
821    launch_timeout: Duration,
822
823    /// Ignore https errors, default is true.
824    ignore_https_errors: bool,
825    pub viewport: Option<Viewport>,
826    /// The duration after a request with no response should time out.
827    request_timeout: Duration,
828
829    /// Additional command line arguments to pass to the browser instance.
830    args: Vec<String>,
831
832    /// Whether to disable DEFAULT_ARGS or not, default is false.
833    disable_default_args: bool,
834
835    /// Whether to enable request interception.
836    pub request_intercept: bool,
837
838    /// Whether to enable cache.
839    pub cache_enabled: bool,
840    /// Whether to enable or disable Service Workers.
841    /// Disabling may reduce background network activity and caching effects.
842    pub service_worker_enabled: bool,
843    /// Whether to ignore image/visual requests during interception.
844    /// Can reduce bandwidth and speed up crawling when visuals are unnecessary.
845    pub ignore_visuals: bool,
846    /// Whether to ignore stylesheet (CSS) requests during interception.
847    /// Useful for content-only crawls.
848    pub ignore_stylesheets: bool,
849    /// Whether to ignore JavaScript requests during interception.
850    /// This still allows critical framework bundles to pass when applicable.
851    pub ignore_javascript: bool,
852    /// Whether to ignore analytics/telemetry requests during interception.
853    pub ignore_analytics: bool,
854    /// Whether to ignore ad network requests during interception.
855    pub ignore_ads: bool,
856    /// Extra headers.
857    pub extra_headers: Option<std::collections::HashMap<String, String>>,
858    /// Only html
859    pub only_html: bool,
860    /// The interception intercept manager.
861    pub intercept_manager: NetworkInterceptManager,
862    /// The max bytes to receive.
863    pub max_bytes_allowed: Option<u64>,
864    /// Whitelist patterns to allow through the network.
865    pub whitelist_patterns: Option<Vec<String>>,
866    /// Blacklist patterns to block through the network.
867    pub blacklist_patterns: Option<Vec<String>>,
868}
869
870#[derive(Debug, Clone)]
871pub struct BrowserConfigBuilder {
872    /// Headless mode configuration for the browser.
873    headless: HeadlessMode,
874    /// Whether to run the browser with a sandbox.
875    sandbox: bool,
876    /// Optional initial browser window size `(width, height)`.
877    window_size: Option<(u32, u32)>,
878    /// DevTools debugging port to bind to.
879    port: u16,
880    /// Optional explicit path to the Chrome/Chromium executable.
881    /// If `None`, auto-detection may be attempted based on `executation_detection`.
882    executable: Option<PathBuf>,
883    /// Controls auto-detection behavior for finding a Chrome/Chromium binary.
884    executation_detection: DetectionOptions,
885    /// List of unpacked extensions (directories) to load at startup.
886    extensions: Vec<String>,
887    /// Environment variables to set on the spawned Chromium process.
888    process_envs: Option<HashMap<String, String>>,
889    /// User data directory to persist browser state, or `None` for ephemeral.
890    user_data_dir: Option<PathBuf>,
891    /// Whether to start the browser in incognito (off-the-record) mode.
892    incognito: bool,
893    /// Maximum time to wait for the browser to launch and become ready.
894    launch_timeout: Duration,
895    /// Whether to ignore HTTPS/TLS errors during navigation and requests.
896    ignore_https_errors: bool,
897    /// Default page viewport configuration applied on startup.
898    viewport: Option<Viewport>,
899    /// Timeout for individual network requests without response progress.
900    request_timeout: Duration,
901    /// Additional command-line flags passed directly to the browser process.
902    args: Vec<String>,
903    /// Disable the default argument set and use only the provided `args`.
904    disable_default_args: bool,
905    /// Enable Network.requestInterception for request filtering/handling.
906    request_intercept: bool,
907    /// Enable the browser cache for navigations and subresources.
908    cache_enabled: bool,
909    /// Enable/disable Service Workers.
910    service_worker_enabled: bool,
911    /// Drop image/visual requests when interception is enabled.
912    ignore_visuals: bool,
913    /// Drop ad network requests when interception is enabled.
914    ignore_ads: bool,
915    /// Drop JavaScript requests when interception is enabled.
916    ignore_javascript: bool,
917    /// Drop stylesheet (CSS) requests when interception is enabled.
918    ignore_stylesheets: bool,
919    /// Drop analytics/telemetry requests when interception is enabled.
920    ignore_analytics: bool,
921    /// If `true`, limit fetching to HTML documents.
922    only_html: bool,
923    /// Extra HTTP headers to include with every request.
924    extra_headers: Option<std::collections::HashMap<String, String>>,
925    /// Network interception manager used to configure filtering behavior.
926    intercept_manager: NetworkInterceptManager,
927    /// Optional upper bound on bytes that may be received (per session/run).
928    max_bytes_allowed: Option<u64>,
929    /// Whitelist patterns to allow through the network.
930    whitelist_patterns: Option<Vec<String>>,
931    /// Blacklist patterns to block through the network.
932    blacklist_patterns: Option<Vec<String>>,
933}
934
935impl BrowserConfig {
936    /// Browser builder default config.
937    pub fn builder() -> BrowserConfigBuilder {
938        BrowserConfigBuilder::default()
939    }
940
941    /// Launch with the executable path.
942    pub fn with_executable(path: impl AsRef<Path>) -> Self {
943        Self::builder()
944            .chrome_executable(path)
945            .build()
946            .expect("path to executable exist")
947    }
948}
949
950impl Default for BrowserConfigBuilder {
951    fn default() -> Self {
952        Self {
953            headless: HeadlessMode::True,
954            sandbox: true,
955            window_size: None,
956            port: 0,
957            executable: None,
958            executation_detection: DetectionOptions::default(),
959            extensions: Vec::new(),
960            process_envs: None,
961            user_data_dir: None,
962            incognito: false,
963            launch_timeout: Duration::from_millis(LAUNCH_TIMEOUT),
964            ignore_https_errors: true,
965            viewport: Some(Default::default()),
966            request_timeout: Duration::from_millis(REQUEST_TIMEOUT),
967            args: Vec::new(),
968            disable_default_args: false,
969            request_intercept: false,
970            cache_enabled: true,
971            ignore_visuals: false,
972            ignore_ads: false,
973            ignore_javascript: false,
974            ignore_analytics: false,
975            ignore_stylesheets: false,
976            only_html: false,
977            extra_headers: Default::default(),
978            service_worker_enabled: true,
979            intercept_manager: NetworkInterceptManager::Unknown,
980            max_bytes_allowed: None,
981            whitelist_patterns: None,
982            blacklist_patterns: None,
983        }
984    }
985}
986
987impl BrowserConfigBuilder {
988    /// Configure window size.
989    pub fn window_size(mut self, width: u32, height: u32) -> Self {
990        self.window_size = Some((width, height));
991        self
992    }
993    /// Configure sandboxing.
994    pub fn no_sandbox(mut self) -> Self {
995        self.sandbox = false;
996        self
997    }
998    /// Configure the launch to start non headless.
999    pub fn with_head(mut self) -> Self {
1000        self.headless = HeadlessMode::False;
1001        self
1002    }
1003    /// Configure the launch with the new headless mode.
1004    pub fn new_headless_mode(mut self) -> Self {
1005        self.headless = HeadlessMode::New;
1006        self
1007    }
1008    /// Configure the launch with headless.
1009    pub fn headless_mode(mut self, mode: HeadlessMode) -> Self {
1010        self.headless = mode;
1011        self
1012    }
1013    /// Configure the launch in incognito.
1014    pub fn incognito(mut self) -> Self {
1015        self.incognito = true;
1016        self
1017    }
1018
1019    pub fn respect_https_errors(mut self) -> Self {
1020        self.ignore_https_errors = false;
1021        self
1022    }
1023
1024    pub fn port(mut self, port: u16) -> Self {
1025        self.port = port;
1026        self
1027    }
1028
1029    pub fn with_max_bytes_allowed(mut self, max_bytes_allowed: Option<u64>) -> Self {
1030        self.max_bytes_allowed = max_bytes_allowed;
1031        self
1032    }
1033
1034    pub fn launch_timeout(mut self, timeout: Duration) -> Self {
1035        self.launch_timeout = timeout;
1036        self
1037    }
1038
1039    pub fn request_timeout(mut self, timeout: Duration) -> Self {
1040        self.request_timeout = timeout;
1041        self
1042    }
1043
1044    /// Configures the viewport of the browser, which defaults to `800x600`.
1045    /// `None` disables viewport emulation (i.e., it uses the browsers default
1046    /// configuration, which fills the available space. This is similar to what
1047    /// Playwright does when you provide `null` as the value of its `viewport`
1048    /// option).
1049    pub fn viewport(mut self, viewport: impl Into<Option<Viewport>>) -> Self {
1050        self.viewport = viewport.into();
1051        self
1052    }
1053
1054    pub fn user_data_dir(mut self, data_dir: impl AsRef<Path>) -> Self {
1055        self.user_data_dir = Some(data_dir.as_ref().to_path_buf());
1056        self
1057    }
1058
1059    pub fn chrome_executable(mut self, path: impl AsRef<Path>) -> Self {
1060        self.executable = Some(path.as_ref().to_path_buf());
1061        self
1062    }
1063
1064    pub fn chrome_detection(mut self, options: DetectionOptions) -> Self {
1065        self.executation_detection = options;
1066        self
1067    }
1068
1069    pub fn extension(mut self, extension: impl Into<String>) -> Self {
1070        self.extensions.push(extension.into());
1071        self
1072    }
1073
1074    pub fn extensions<I, S>(mut self, extensions: I) -> Self
1075    where
1076        I: IntoIterator<Item = S>,
1077        S: Into<String>,
1078    {
1079        for ext in extensions {
1080            self.extensions.push(ext.into());
1081        }
1082        self
1083    }
1084
1085    pub fn env(mut self, key: impl Into<String>, val: impl Into<String>) -> Self {
1086        self.process_envs
1087            .get_or_insert(HashMap::new())
1088            .insert(key.into(), val.into());
1089        self
1090    }
1091
1092    pub fn envs<I, K, V>(mut self, envs: I) -> Self
1093    where
1094        I: IntoIterator<Item = (K, V)>,
1095        K: Into<String>,
1096        V: Into<String>,
1097    {
1098        self.process_envs
1099            .get_or_insert(HashMap::new())
1100            .extend(envs.into_iter().map(|(k, v)| (k.into(), v.into())));
1101        self
1102    }
1103
1104    pub fn arg(mut self, arg: impl Into<String>) -> Self {
1105        self.args.push(arg.into());
1106        self
1107    }
1108
1109    pub fn args<I, S>(mut self, args: I) -> Self
1110    where
1111        I: IntoIterator<Item = S>,
1112        S: Into<String>,
1113    {
1114        for arg in args {
1115            self.args.push(arg.into());
1116        }
1117        self
1118    }
1119
1120    pub fn disable_default_args(mut self) -> Self {
1121        self.disable_default_args = true;
1122        self
1123    }
1124
1125    pub fn enable_request_intercept(mut self) -> Self {
1126        self.request_intercept = true;
1127        self
1128    }
1129
1130    pub fn disable_request_intercept(mut self) -> Self {
1131        self.request_intercept = false;
1132        self
1133    }
1134
1135    pub fn enable_cache(mut self) -> Self {
1136        self.cache_enabled = true;
1137        self
1138    }
1139
1140    pub fn disable_cache(mut self) -> Self {
1141        self.cache_enabled = false;
1142        self
1143    }
1144
1145    /// Set service worker enabled.
1146    pub fn set_service_worker_enabled(mut self, bypass: bool) -> Self {
1147        self.service_worker_enabled = bypass;
1148        self
1149    }
1150
1151    /// Set extra request headers.
1152    pub fn set_extra_headers(
1153        mut self,
1154        headers: Option<std::collections::HashMap<String, String>>,
1155    ) -> Self {
1156        self.extra_headers = headers;
1157        self
1158    }
1159
1160    /// Set whitelist patterns to allow through network interception allowing.
1161    pub fn set_whitelist_patterns(mut self, whitelist_patterns: Option<Vec<String>>) -> Self {
1162        self.whitelist_patterns = whitelist_patterns;
1163        self
1164    }
1165
1166    /// Set blacklist patterns to block through network interception.
1167    pub fn set_blacklist_patterns(mut self, blacklist_patterns: Option<Vec<String>>) -> Self {
1168        self.blacklist_patterns = blacklist_patterns;
1169        self
1170    }
1171
1172    /// Build the browser.
1173    pub fn build(self) -> std::result::Result<BrowserConfig, String> {
1174        let executable = if let Some(e) = self.executable {
1175            e
1176        } else {
1177            detection::default_executable(self.executation_detection)?
1178        };
1179
1180        Ok(BrowserConfig {
1181            headless: self.headless,
1182            sandbox: self.sandbox,
1183            window_size: self.window_size,
1184            port: self.port,
1185            executable,
1186            extensions: self.extensions,
1187            process_envs: self.process_envs,
1188            user_data_dir: self.user_data_dir,
1189            incognito: self.incognito,
1190            launch_timeout: self.launch_timeout,
1191            ignore_https_errors: self.ignore_https_errors,
1192            viewport: self.viewport,
1193            request_timeout: self.request_timeout,
1194            args: self.args,
1195            disable_default_args: self.disable_default_args,
1196            request_intercept: self.request_intercept,
1197            cache_enabled: self.cache_enabled,
1198            ignore_visuals: self.ignore_visuals,
1199            ignore_ads: self.ignore_ads,
1200            ignore_javascript: self.ignore_javascript,
1201            ignore_analytics: self.ignore_analytics,
1202            ignore_stylesheets: self.ignore_stylesheets,
1203            extra_headers: self.extra_headers,
1204            only_html: self.only_html,
1205            intercept_manager: self.intercept_manager,
1206            service_worker_enabled: self.service_worker_enabled,
1207            max_bytes_allowed: self.max_bytes_allowed,
1208            whitelist_patterns: self.whitelist_patterns,
1209            blacklist_patterns: self.blacklist_patterns,
1210        })
1211    }
1212}
1213
1214impl BrowserConfig {
1215    pub fn launch(&self) -> io::Result<Child> {
1216        let mut cmd = async_process::Command::new(&self.executable);
1217
1218        if self.disable_default_args {
1219            cmd.args(&self.args);
1220        } else {
1221            cmd.args(DEFAULT_ARGS).args(&self.args);
1222        }
1223
1224        if !self
1225            .args
1226            .iter()
1227            .any(|arg| arg.contains("--remote-debugging-port="))
1228        {
1229            cmd.arg(format!("--remote-debugging-port={}", self.port));
1230        }
1231
1232        cmd.args(
1233            self.extensions
1234                .iter()
1235                .map(|e| format!("--load-extension={e}")),
1236        );
1237
1238        if let Some(ref user_data) = self.user_data_dir {
1239            cmd.arg(format!("--user-data-dir={}", user_data.display()));
1240        } else {
1241            // If the user did not specify a data directory, this would default to the systems default
1242            // data directory. In most cases, we would rather have a fresh instance of Chromium. Specify
1243            // a temp dir just for chromiumoxide instead.
1244            cmd.arg(format!(
1245                "--user-data-dir={}",
1246                std::env::temp_dir().join("chromiumoxide-runner").display()
1247            ));
1248        }
1249
1250        if let Some((width, height)) = self.window_size {
1251            cmd.arg(format!("--window-size={width},{height}"));
1252        }
1253
1254        if !self.sandbox {
1255            cmd.args(["--no-sandbox", "--disable-setuid-sandbox"]);
1256        }
1257
1258        match self.headless {
1259            HeadlessMode::False => (),
1260            HeadlessMode::True => {
1261                cmd.args(["--headless", "--hide-scrollbars", "--mute-audio"]);
1262            }
1263            HeadlessMode::New => {
1264                cmd.args(["--headless=new", "--hide-scrollbars", "--mute-audio"]);
1265            }
1266        }
1267
1268        if self.incognito {
1269            cmd.arg("--incognito");
1270        }
1271
1272        if let Some(ref envs) = self.process_envs {
1273            cmd.envs(envs);
1274        }
1275        cmd.stderr(Stdio::piped()).spawn()
1276    }
1277}
1278
1279/// Returns the path to Chrome's executable.
1280///
1281/// If the `CHROME` environment variable is set, `default_executable` will
1282/// use it as the default path. Otherwise, the filenames `google-chrome-stable`
1283/// `chromium`, `chromium-browser`, `chrome` and `chrome-browser` are
1284/// searched for in standard places. If that fails,
1285/// `/Applications/Google Chrome.app/...` (on MacOS) or the registry (on
1286/// Windows) is consulted. If all of the above fail, an error is returned.
1287#[deprecated(note = "Use detection::default_executable instead")]
1288pub fn default_executable() -> Result<std::path::PathBuf, String> {
1289    let options = DetectionOptions {
1290        msedge: false,
1291        unstable: false,
1292    };
1293    detection::default_executable(options)
1294}
1295
1296/// These are passed to the Chrome binary by default.
1297/// Via https://github.com/puppeteer/puppeteer/blob/4846b8723cf20d3551c0d755df394cc5e0c82a94/src/node/Launcher.ts#L157
1298static DEFAULT_ARGS: [&str; 26] = [
1299    "--disable-background-networking",
1300    "--enable-features=NetworkService,NetworkServiceInProcess",
1301    "--disable-background-timer-throttling",
1302    "--disable-backgrounding-occluded-windows",
1303    "--disable-breakpad",
1304    "--disable-client-side-phishing-detection",
1305    "--disable-component-extensions-with-background-pages",
1306    "--disable-default-apps",
1307    "--disable-dev-shm-usage",
1308    "--disable-extensions",
1309    "--disable-features=TranslateUI",
1310    "--disable-hang-monitor",
1311    "--disable-ipc-flooding-protection",
1312    "--disable-popup-blocking",
1313    "--disable-prompt-on-repost",
1314    "--disable-renderer-backgrounding",
1315    "--disable-sync",
1316    "--force-color-profile=srgb",
1317    "--metrics-recording-only",
1318    "--no-first-run",
1319    "--enable-automation",
1320    "--password-store=basic",
1321    "--use-mock-keychain",
1322    "--enable-blink-features=IdleDetection",
1323    "--lang=en_US",
1324    "--disable-blink-features=AutomationControlled",
1325];