firefox_webdriver/browser/tab/
proxy.rs

1//! Tab-level proxy configuration.
2
3use tracing::debug;
4
5use crate::browser::proxy::ProxyConfig;
6use crate::error::Result;
7use crate::protocol::{Command, ProxyCommand};
8
9use super::Tab;
10
11// ============================================================================
12// Tab - Proxy
13// ============================================================================
14
15impl Tab {
16    /// Sets a proxy for this tab.
17    ///
18    /// Tab-level proxy overrides window-level proxy for this tab only.
19    ///
20    /// # Example
21    ///
22    /// ```ignore
23    /// use firefox_webdriver::ProxyConfig;
24    ///
25    /// tab.set_proxy(ProxyConfig::http("proxy.example.com", 8080)).await?;
26    /// ```
27    pub async fn set_proxy(&self, config: ProxyConfig) -> Result<()> {
28        debug!(tab_id = %self.inner.tab_id, proxy_type = %config.proxy_type.as_str(), "Setting proxy");
29
30        let command = Command::Proxy(ProxyCommand::SetTabProxy {
31            proxy_type: config.proxy_type.as_str().to_string(),
32            host: config.host,
33            port: config.port,
34            username: config.username,
35            password: config.password,
36            proxy_dns: config.proxy_dns,
37        });
38
39        self.send_command(command).await?;
40        Ok(())
41    }
42
43    /// Clears the proxy for this tab.
44    pub async fn clear_proxy(&self) -> Result<()> {
45        let command = Command::Proxy(ProxyCommand::ClearTabProxy);
46        self.send_command(command).await?;
47        Ok(())
48    }
49}