use serde::{Deserialize, Serialize};
use crate::bidi::BiDi;
use crate::bidi::command::{BidiCommand, Empty};
use crate::bidi::error::BidiError;
use crate::bidi::ids::{ClientWindowId, UserContextId};
use crate::common::protocol::string_enum;
#[derive(Debug, Clone, Default, Serialize)]
pub struct Close;
impl BidiCommand for Close {
const METHOD: &'static str = "browser.close";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateUserContext {
#[serde(skip_serializing_if = "Option::is_none")]
pub accept_insecure_certs: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub proxy: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unhandled_prompt_behavior: Option<serde_json::Value>,
}
impl BidiCommand for CreateUserContext {
const METHOD: &'static str = "browser.createUserContext";
type Returns = UserContextInfo;
}
#[derive(Debug, Clone, Deserialize)]
pub struct UserContextInfo {
#[serde(rename = "userContext")]
pub user_context: UserContextId,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetUserContexts;
impl BidiCommand for GetUserContexts {
const METHOD: &'static str = "browser.getUserContexts";
type Returns = GetUserContextsResult;
}
#[derive(Debug, Clone, Deserialize)]
pub struct GetUserContextsResult {
#[serde(rename = "userContexts")]
pub user_contexts: Vec<UserContextInfo>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoveUserContext {
pub user_context: UserContextId,
}
impl BidiCommand for RemoveUserContext {
const METHOD: &'static str = "browser.removeUserContext";
type Returns = Empty;
}
string_enum! {
pub enum ClientWindowState {
Fullscreen = "fullscreen",
Maximized = "maximized",
Minimized = "minimized",
Normal = "normal",
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientWindowInfo {
pub active: bool,
pub client_window: ClientWindowId,
pub height: u32,
pub width: u32,
pub x: i32,
pub y: i32,
pub state: ClientWindowState,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetClientWindows;
impl BidiCommand for GetClientWindows {
const METHOD: &'static str = "browser.getClientWindows";
type Returns = GetClientWindowsResult;
}
#[derive(Debug, Clone, Deserialize)]
pub struct GetClientWindowsResult {
#[serde(rename = "clientWindows")]
pub client_windows: Vec<ClientWindowInfo>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetClientWindowState {
pub client_window: ClientWindowId,
pub state: ClientWindowState,
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub y: Option<i32>,
}
impl BidiCommand for SetClientWindowState {
const METHOD: &'static str = "browser.setClientWindowState";
type Returns = ClientWindowInfo;
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum DownloadBehavior {
Allowed {
#[serde(rename = "destinationFolder")]
destination_folder: String,
},
Denied,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetDownloadBehavior {
pub download_behavior: Option<DownloadBehavior>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_contexts: Option<Vec<UserContextId>>,
}
impl BidiCommand for SetDownloadBehavior {
const METHOD: &'static str = "browser.setDownloadBehavior";
type Returns = Empty;
}
#[derive(Debug)]
pub struct BrowserModule<'a> {
bidi: &'a BiDi,
}
impl<'a> BrowserModule<'a> {
pub(crate) fn new(bidi: &'a BiDi) -> Self {
Self {
bidi,
}
}
pub async fn close(&self) -> Result<(), BidiError> {
self.bidi.send(Close).await?;
Ok(())
}
pub async fn create_user_context(&self) -> Result<UserContextInfo, BidiError> {
self.bidi.send(CreateUserContext::default()).await
}
pub async fn get_user_contexts(&self) -> Result<GetUserContextsResult, BidiError> {
self.bidi.send(GetUserContexts).await
}
pub async fn remove_user_context(&self, user_context: UserContextId) -> Result<(), BidiError> {
self.bidi
.send(RemoveUserContext {
user_context,
})
.await?;
Ok(())
}
pub async fn get_client_windows(&self) -> Result<GetClientWindowsResult, BidiError> {
self.bidi.send(GetClientWindows).await
}
pub async fn set_client_window_state(
&self,
client_window: ClientWindowId,
state: ClientWindowState,
) -> Result<ClientWindowInfo, BidiError> {
self.bidi
.send(SetClientWindowState {
client_window,
state,
width: None,
height: None,
x: None,
y: None,
})
.await
}
pub async fn set_download_behavior(
&self,
download_behavior: Option<DownloadBehavior>,
) -> Result<(), BidiError> {
self.bidi
.send(SetDownloadBehavior {
download_behavior,
user_contexts: None,
})
.await?;
Ok(())
}
}