use serde::{Deserialize, Serialize};
use crate::cdp::Cdp;
use crate::cdp::command::{CdpCommand, CdpEvent, Empty};
use crate::cdp::ids::{BrowserContextId, SessionId, TargetId};
use crate::error::WebDriverResult;
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachToTarget {
pub target_id: TargetId,
pub flatten: bool,
}
impl AttachToTarget {
pub fn flat(target_id: TargetId) -> Self {
Self {
target_id,
flatten: true,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachToTargetResult {
pub session_id: SessionId,
}
impl CdpCommand for AttachToTarget {
const METHOD: &'static str = "Target.attachToTarget";
type Returns = AttachToTargetResult;
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DetachFromTarget {
#[serde(skip_serializing_if = "Option::is_none")]
pub session_id: Option<SessionId>,
}
impl CdpCommand for DetachFromTarget {
const METHOD: &'static str = "Target.detachFromTarget";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetAutoAttach {
pub auto_attach: bool,
pub wait_for_debugger_on_start: bool,
pub flatten: bool,
}
impl CdpCommand for SetAutoAttach {
const METHOD: &'static str = "Target.setAutoAttach";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetTargets;
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TargetInfo {
pub target_id: TargetId,
pub r#type: String,
pub title: String,
pub url: String,
pub attached: bool,
pub browser_context_id: Option<BrowserContextId>,
pub opener_id: Option<TargetId>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTargetsResult {
pub target_infos: Vec<TargetInfo>,
}
impl CdpCommand for GetTargets {
const METHOD: &'static str = "Target.getTargets";
type Returns = GetTargetsResult;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateTarget {
pub url: String,
#[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 browser_context_id: Option<BrowserContextId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateTargetResult {
pub target_id: TargetId,
}
impl CdpCommand for CreateTarget {
const METHOD: &'static str = "Target.createTarget";
type Returns = CreateTargetResult;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloseTarget {
pub target_id: TargetId,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CloseTargetResult {
pub success: bool,
}
impl CdpCommand for CloseTarget {
const METHOD: &'static str = "Target.closeTarget";
type Returns = CloseTargetResult;
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachedToTarget {
pub session_id: SessionId,
pub target_info: TargetInfo,
pub waiting_for_debugger: bool,
}
impl CdpEvent for AttachedToTarget {
const METHOD: &'static str = "Target.attachedToTarget";
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DetachedFromTarget {
pub session_id: SessionId,
pub target_id: Option<TargetId>,
}
impl CdpEvent for DetachedFromTarget {
const METHOD: &'static str = "Target.detachedFromTarget";
}
#[derive(Debug)]
pub struct TargetDomain<'a> {
cdp: &'a Cdp,
}
impl<'a> TargetDomain<'a> {
pub(crate) fn new(cdp: &'a Cdp) -> Self {
Self {
cdp,
}
}
pub async fn get_targets(&self) -> WebDriverResult<Vec<TargetInfo>> {
let r = self.cdp.send(GetTargets).await?;
Ok(r.target_infos)
}
pub async fn create_target(&self, url: impl Into<String>) -> WebDriverResult<TargetId> {
let r = self
.cdp
.send(CreateTarget {
url: url.into(),
width: None,
height: None,
browser_context_id: None,
background: None,
})
.await?;
Ok(r.target_id)
}
pub async fn close_target(&self, target_id: TargetId) -> WebDriverResult<bool> {
Ok(self
.cdp
.send(CloseTarget {
target_id,
})
.await?
.success)
}
}