chromiumoxide_cdp/
lib.rs

1use std::fmt;
2
3use crate::cdp::browser_protocol::fetch;
4use crate::cdp::browser_protocol::network::{self, CookieParam, DeleteCookiesParams};
5use crate::cdp::browser_protocol::target::CreateTargetParams;
6use crate::cdp::js_protocol::runtime::{
7    CallFunctionOnParams, EvaluateParams, ExceptionDetails, StackTrace,
8};
9use crate::revision::Revision;
10
11#[allow(clippy::multiple_bound_locations)]
12#[allow(clippy::derive_partial_eq_without_eq)]
13#[allow(unreachable_patterns)]
14pub mod cdp;
15pub mod revision;
16
17// The CDP is not a stable API, it changes from time to time and sometimes
18// in backward incompatible ways.
19//
20// When the CDP changes, the chromium team pushes a commit to the repository
21// https://github.com/ChromeDevTools/devtools-protocol. There you can find
22// valid CDP revisions. That number corresponds to a chromium revision.
23// It is a monotonic version number referring to the chromium master commit position.
24//
25// To map a revision to a chromium version you can use the site
26// https://chromiumdash.appspot.com/commits. We should not necessarily
27// always use the latest revision, as this will mean only the newest chromium
28// browser can be used. Apart from breaking changes, using an older CDP
29// is generally a good idea.
30
31/// Currently built CDP revision. The last stable version was 1354347.
32pub const CURRENT_REVISION: Revision = Revision(1457408);
33
34/// convenience fixups
35impl Default for CreateTargetParams {
36    fn default() -> Self {
37        "about:blank".into()
38    }
39}
40
41/// RequestId conversion
42
43impl From<fetch::RequestId> for network::RequestId {
44    fn from(req: fetch::RequestId) -> Self {
45        let s: String = req.into();
46        s.into()
47    }
48}
49
50impl From<network::RequestId> for fetch::RequestId {
51    fn from(req: network::RequestId) -> Self {
52        let s: String = req.into();
53        s.into()
54    }
55}
56
57impl From<network::InterceptionId> for fetch::RequestId {
58    fn from(req: network::InterceptionId) -> Self {
59        let s: String = req.into();
60        s.into()
61    }
62}
63
64impl From<network::InterceptionId> for network::RequestId {
65    fn from(req: network::InterceptionId) -> Self {
66        let s: String = req.into();
67        s.into()
68    }
69}
70
71impl From<fetch::RequestId> for network::InterceptionId {
72    fn from(req: fetch::RequestId) -> Self {
73        let s: String = req.into();
74        s.into()
75    }
76}
77
78impl From<network::RequestId> for network::InterceptionId {
79    fn from(req: network::RequestId) -> Self {
80        let s: String = req.into();
81        s.into()
82    }
83}
84
85impl DeleteCookiesParams {
86    /// Create a new instance from a `CookieParam`
87    pub fn from_cookie(param: &CookieParam) -> Self {
88        DeleteCookiesParams {
89            name: param.name.clone(),
90            url: param.url.clone(),
91            domain: param.domain.clone(),
92            path: param.path.clone(),
93            partition_key: param.partition_key.clone(),
94        }
95    }
96}
97
98impl From<EvaluateParams> for CallFunctionOnParams {
99    fn from(params: EvaluateParams) -> CallFunctionOnParams {
100        CallFunctionOnParams {
101            function_declaration: params.expression,
102            object_id: None,
103            arguments: None,
104            silent: params.silent,
105            return_by_value: params.return_by_value,
106            generate_preview: params.generate_preview,
107            user_gesture: params.user_gesture,
108            await_promise: params.await_promise,
109            execution_context_id: params.context_id,
110            object_group: params.object_group,
111            throw_on_side_effect: None,
112            unique_context_id: None,
113            serialization_options: None,
114        }
115    }
116}
117
118impl fmt::Display for ExceptionDetails {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        writeln!(
121            f,
122            "{}:{}: {}",
123            self.line_number, self.column_number, self.text
124        )?;
125
126        if let Some(stack) = self.stack_trace.as_ref() {
127            stack.fmt(f)?
128        }
129        Ok(())
130    }
131}
132
133impl std::error::Error for ExceptionDetails {}
134
135impl fmt::Display for StackTrace {
136    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137        if let Some(desc) = self.description.as_ref() {
138            writeln!(f, "{desc}")?;
139        }
140        for frame in &self.call_frames {
141            writeln!(
142                f,
143                "{}@{}:{}:{}",
144                frame.function_name, frame.url, frame.line_number, frame.column_number
145            )?;
146        }
147        Ok(())
148    }
149}