#![allow(clippy::all, missing_docs, unused_imports)]
use serde::{Deserialize, Serialize};
use super::*;
use crate::playwright::connection::{
Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionInitializer {}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionSendParams {
#[serde(rename = "method")]
pub method: String,
#[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionSendResult {
#[serde(rename = "result")]
pub result: serde_json::Value,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionEventEventParams {
#[serde(rename = "method")]
pub method: String,
#[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum CDPSessionEvent {
Event(CDPSessionEventEventParams),
Close,
Unknown {
method: String,
params: serde_json::Value,
},
}
impl ProtocolEvent for CDPSessionEvent {
fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
match method {
"event" => Ok(Self::Event(serde_json::from_value(params)?)),
"close" => Ok(Self::Close),
_ => Ok(Self::Unknown { method: method.to_string(), params }),
}
}
}
#[derive(Debug, Clone)]
pub struct CDPSession {
channel: Channel,
}
impl ChannelType for CDPSession {
const INTERFACE: &'static str = "CDPSession";
type Initializer = CDPSessionInitializer;
type Event = CDPSessionEvent;
fn accepts(interface: &str) -> bool {
matches!(interface, "CDPSession")
}
fn from_channel(channel: Channel) -> Self {
Self { channel }
}
fn channel(&self) -> &Channel {
&self.channel
}
}
impl CDPSession {
pub async fn send(&self, params: CDPSessionSendParams) -> Result<CDPSessionSendResult, ProtocolError> {
self.channel.send("send", ¶ms).await
}
pub async fn detach(&self) -> Result<(), ProtocolError> {
self.channel.send_no_result("detach", &serde_json::json!({})).await
}
}