Skip to main content

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