browser_commander/playwright/protocol/
cdp_session.rs1#![allow(clippy::all, missing_docs, unused_imports)]
5
6use serde::{Deserialize, Serialize};
7
8use super::*;
9use crate::playwright::connection::{
10 Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
11};
12
13#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct CDPSessionInitializer {}
16
17#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
19pub struct CDPSessionSendParams {
20 #[serde(rename = "method")]
21 pub method: String,
22 #[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
23 pub params: Option<serde_json::Value>,
24}
25
26#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28pub struct CDPSessionSendResult {
29 #[serde(rename = "result")]
30 pub result: serde_json::Value,
31}
32
33#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
35pub struct CDPSessionEventEventParams {
36 #[serde(rename = "method")]
37 pub method: String,
38 #[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
39 pub params: Option<serde_json::Value>,
40}
41
42#[derive(Debug, Clone, PartialEq)]
44pub enum CDPSessionEvent {
45 Event(CDPSessionEventEventParams),
47 Close,
49 Unknown {
51 method: String,
53 params: serde_json::Value,
55 },
56}
57
58impl ProtocolEvent for CDPSessionEvent {
59 fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
60 match method {
61 "event" => Ok(Self::Event(serde_json::from_value(params)?)),
62 "close" => Ok(Self::Close),
63 _ => Ok(Self::Unknown { method: method.to_string(), params }),
64 }
65 }
66}
67
68#[derive(Debug, Clone)]
70pub struct CDPSession {
71 channel: Channel,
72}
73
74impl ChannelType for CDPSession {
75 const INTERFACE: &'static str = "CDPSession";
76 type Initializer = CDPSessionInitializer;
77 type Event = CDPSessionEvent;
78
79 fn accepts(interface: &str) -> bool {
80 matches!(interface, "CDPSession")
81 }
82
83 fn from_channel(channel: Channel) -> Self {
84 Self { channel }
85 }
86
87 fn channel(&self) -> &Channel {
88 &self.channel
89 }
90}
91
92impl CDPSession {
93 pub async fn send(&self, params: CDPSessionSendParams) -> Result<CDPSessionSendResult, ProtocolError> {
95 self.channel.send("send", ¶ms).await
96 }
97
98 pub async fn detach(&self) -> Result<(), ProtocolError> {
100 self.channel.send_no_result("detach", &serde_json::json!({})).await
101 }
102}