Skip to main content

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