use serde::{Deserialize, Serialize};
use crate::cdp::Cdp;
use crate::cdp::command::{CdpCommand, Empty};
use crate::common::protocol::string_enum;
use crate::error::WebDriverResult;
string_enum! {
pub enum DownloadBehavior {
Deny = "deny",
Allow = "allow",
AllowAndName = "allowAndName",
Default = "default",
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetVersion;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VersionInfo {
pub protocol_version: String,
pub product: String,
pub revision: String,
pub user_agent: String,
pub js_version: String,
}
impl CdpCommand for GetVersion {
const METHOD: &'static str = "Browser.getVersion";
type Returns = VersionInfo;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct Close;
impl CdpCommand for Close {
const METHOD: &'static str = "Browser.close";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetDownloadBehavior {
pub behavior: DownloadBehavior,
#[serde(skip_serializing_if = "Option::is_none")]
pub browser_context_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub download_path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub events_enabled: Option<bool>,
}
impl CdpCommand for SetDownloadBehavior {
const METHOD: &'static str = "Browser.setDownloadBehavior";
type Returns = Empty;
}
#[derive(Debug)]
pub struct BrowserDomain<'a> {
cdp: &'a Cdp,
}
impl<'a> BrowserDomain<'a> {
pub(crate) fn new(cdp: &'a Cdp) -> Self {
Self {
cdp,
}
}
pub async fn get_version(&self) -> WebDriverResult<VersionInfo> {
self.cdp.send(GetVersion).await
}
pub async fn close(&self) -> WebDriverResult<()> {
self.cdp.send(Close).await?;
Ok(())
}
pub async fn set_download_behavior(
&self,
behavior: DownloadBehavior,
download_path: Option<String>,
) -> WebDriverResult<()> {
self.cdp
.send(SetDownloadBehavior {
behavior,
browser_context_id: None,
download_path,
events_enabled: None,
})
.await?;
Ok(())
}
}