use std::sync::Arc;
use serde::Serialize;
use serde_json::Value;
use super::command::CdpCommand;
use super::domains;
use super::transport::Transport;
use super::transport::http::HttpTransport;
use crate::error::WebDriverResult;
use crate::session::handle::SessionHandle;
#[derive(Debug, Clone)]
pub struct Cdp {
transport: HttpTransport,
}
impl Cdp {
pub fn new(handle: Arc<SessionHandle>) -> Self {
Self {
transport: HttpTransport::new(handle),
}
}
pub fn handle(&self) -> &Arc<SessionHandle> {
self.transport.handle()
}
pub async fn send<C: CdpCommand>(&self, params: C) -> WebDriverResult<C::Returns> {
let raw = self.send_raw(C::METHOD, serde_json::to_value(params)?).await?;
Ok(serde_json::from_value(raw)?)
}
pub async fn send_raw<P: Serialize>(&self, method: &str, params: P) -> WebDriverResult<Value> {
let value = serde_json::to_value(params)?;
self.transport.send_raw(method, value).await
}
#[cfg(feature = "cdp-events")]
pub async fn connect(&self) -> WebDriverResult<super::CdpSession> {
super::CdpSession::connect(self.handle().clone()).await
}
pub fn browser(&self) -> domains::browser::BrowserDomain<'_> {
domains::browser::BrowserDomain::new(self)
}
pub fn page(&self) -> domains::page::PageDomain<'_> {
domains::page::PageDomain::new(self)
}
pub fn network(&self) -> domains::network::NetworkDomain<'_> {
domains::network::NetworkDomain::new(self)
}
pub fn fetch(&self) -> domains::fetch::FetchDomain<'_> {
domains::fetch::FetchDomain::new(self)
}
pub fn runtime(&self) -> domains::runtime::RuntimeDomain<'_> {
domains::runtime::RuntimeDomain::new(self)
}
pub fn dom(&self) -> domains::dom::DomDomain<'_> {
domains::dom::DomDomain::new(self)
}
pub fn emulation(&self) -> domains::emulation::EmulationDomain<'_> {
domains::emulation::EmulationDomain::new(self)
}
pub fn input(&self) -> domains::input::InputDomain<'_> {
domains::input::InputDomain::new(self)
}
pub fn target(&self) -> domains::target::TargetDomain<'_> {
domains::target::TargetDomain::new(self)
}
pub fn storage(&self) -> domains::storage::StorageDomain<'_> {
domains::storage::StorageDomain::new(self)
}
pub fn log(&self) -> domains::log::LogDomain<'_> {
domains::log::LogDomain::new(self)
}
pub fn performance(&self) -> domains::performance::PerformanceDomain<'_> {
domains::performance::PerformanceDomain::new(self)
}
}