Skip to main content

browser_commander/playwright/protocol/
cdp_session.rs

1// @generated by scripts/generate-playwright-protocol.mjs from
2// rust/protocol/playwright/*.yml. Do not edit by hand.
3
4#![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/// Initializer the driver sends when it creates a `CDPSession`.
14#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct CDPSessionInitializer {}
16
17/// Parameters of `CDPSession.send` (Send CDP command).
18#[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/// Result of `CDPSession.send` (Send CDP command).
27#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28pub struct CDPSessionSendResult {
29    #[serde(rename = "result")]
30    pub result: serde_json::Value,
31}
32
33/// Payload of the `CDPSession.event` event.
34#[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/// Events a `CDPSession` channel can emit.
43#[derive(Debug, Clone, PartialEq)]
44pub enum CDPSessionEvent {
45    /// `CDPSession.event`
46    Event(CDPSessionEventEventParams),
47    /// `CDPSession.close`
48    Close,
49    /// An event this binding does not know (a newer driver).
50    Unknown {
51        /// Event name.
52        method: String,
53        /// Raw event parameters.
54        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/// Channel for the `CDPSession` interface (`playwright.yml`).
69#[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    /// `CDPSession.send` ("Send CDP command").
94    pub async fn send(&self, params: CDPSessionSendParams) -> Result<CDPSessionSendResult, ProtocolError> {
95        self.channel.send("send", &params).await
96    }
97
98    /// `CDPSession.detach` ("Detach CDP session").
99    pub async fn detach(&self) -> Result<(), ProtocolError> {
100        self.channel.send_no_result("detach", &serde_json::json!({})).await
101    }
102}