Skip to main content

browser_protocol/headlessexperimental/
mod.rs

1//! This domain provides experimental commands only supported in headless mode.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// Encoding options for a screenshot.
9
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11#[serde(rename_all = "camelCase")]
12pub struct ScreenshotParams<'a> {
13    /// Image compression format (defaults to png).
14    #[serde(skip_serializing_if = "Option::is_none")]
15    format: Option<Cow<'a, str>>,
16    /// Compression quality from range \[0..100\] (jpeg and webp only).
17    #[serde(skip_serializing_if = "Option::is_none")]
18    quality: Option<i64>,
19    /// Optimize image encoding for speed, not for resulting size (defaults to false)
20    #[serde(skip_serializing_if = "Option::is_none", rename = "optimizeForSpeed")]
21    optimize_for_speed: Option<bool>,
22}
23
24impl<'a> ScreenshotParams<'a> {
25    /// Creates a builder for this type.
26    pub fn builder() -> ScreenshotParamsBuilder<'a> {
27        ScreenshotParamsBuilder {
28            format: None,
29            quality: None,
30            optimize_for_speed: None,
31        }
32    }
33    /// Image compression format (defaults to png).
34    pub fn format(&self) -> Option<&str> { self.format.as_deref() }
35    /// Compression quality from range \[0..100\] (jpeg and webp only).
36    pub fn quality(&self) -> Option<i64> { self.quality }
37    /// Optimize image encoding for speed, not for resulting size (defaults to false)
38    pub fn optimize_for_speed(&self) -> Option<bool> { self.optimize_for_speed }
39}
40
41#[derive(Default)]
42pub struct ScreenshotParamsBuilder<'a> {
43    format: Option<Cow<'a, str>>,
44    quality: Option<i64>,
45    optimize_for_speed: Option<bool>,
46}
47
48impl<'a> ScreenshotParamsBuilder<'a> {
49    /// Image compression format (defaults to png).
50    pub fn format(mut self, format: impl Into<Cow<'a, str>>) -> Self { self.format = Some(format.into()); self }
51    /// Compression quality from range \[0..100\] (jpeg and webp only).
52    pub fn quality(mut self, quality: i64) -> Self { self.quality = Some(quality); self }
53    /// Optimize image encoding for speed, not for resulting size (defaults to false)
54    pub fn optimize_for_speed(mut self, optimize_for_speed: bool) -> Self { self.optimize_for_speed = Some(optimize_for_speed); self }
55    pub fn build(self) -> ScreenshotParams<'a> {
56        ScreenshotParams {
57            format: self.format,
58            quality: self.quality,
59            optimize_for_speed: self.optimize_for_speed,
60        }
61    }
62}
63
64/// Sends a BeginFrame to the target and returns when the frame was completed. Optionally captures a
65/// screenshot from the resulting frame. Requires that the target was created with enabled
66/// BeginFrameControl. Designed for use with --run-all-compositor-stages-before-draw, see also
67/// <https://goo.gle/chrome-headless-rendering> for more background.
68
69#[derive(Debug, Clone, Serialize, Deserialize, Default)]
70#[serde(rename_all = "camelCase")]
71pub struct BeginFrameParams<'a> {
72    /// Timestamp of this BeginFrame in Renderer TimeTicks (milliseconds of uptime). If not set,
73    /// the current time will be used.
74    #[serde(skip_serializing_if = "Option::is_none", rename = "frameTimeTicks")]
75    frame_time_ticks: Option<f64>,
76    /// The interval between BeginFrames that is reported to the compositor, in milliseconds.
77    /// Defaults to a 60 frames/second interval, i.e. about 16.666 milliseconds.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    interval: Option<f64>,
80    /// Whether updates should not be committed and drawn onto the display. False by default. If
81    /// true, only side effects of the BeginFrame will be run, such as layout and animations, but
82    /// any visual updates may not be visible on the display or in screenshots.
83    #[serde(skip_serializing_if = "Option::is_none", rename = "noDisplayUpdates")]
84    no_display_updates: Option<bool>,
85    /// If set, a screenshot of the frame will be captured and returned in the response. Otherwise,
86    /// no screenshot will be captured. Note that capturing a screenshot can fail, for example,
87    /// during renderer initialization. In such a case, no screenshot data will be returned.
88    #[serde(skip_serializing_if = "Option::is_none")]
89    screenshot: Option<ScreenshotParams<'a>>,
90}
91
92impl<'a> BeginFrameParams<'a> {
93    /// Creates a builder for this type.
94    pub fn builder() -> BeginFrameParamsBuilder<'a> {
95        BeginFrameParamsBuilder {
96            frame_time_ticks: None,
97            interval: None,
98            no_display_updates: None,
99            screenshot: None,
100        }
101    }
102    /// Timestamp of this BeginFrame in Renderer TimeTicks (milliseconds of uptime). If not set,
103    /// the current time will be used.
104    pub fn frame_time_ticks(&self) -> Option<f64> { self.frame_time_ticks }
105    /// The interval between BeginFrames that is reported to the compositor, in milliseconds.
106    /// Defaults to a 60 frames/second interval, i.e. about 16.666 milliseconds.
107    pub fn interval(&self) -> Option<f64> { self.interval }
108    /// Whether updates should not be committed and drawn onto the display. False by default. If
109    /// true, only side effects of the BeginFrame will be run, such as layout and animations, but
110    /// any visual updates may not be visible on the display or in screenshots.
111    pub fn no_display_updates(&self) -> Option<bool> { self.no_display_updates }
112    /// If set, a screenshot of the frame will be captured and returned in the response. Otherwise,
113    /// no screenshot will be captured. Note that capturing a screenshot can fail, for example,
114    /// during renderer initialization. In such a case, no screenshot data will be returned.
115    pub fn screenshot(&self) -> Option<&ScreenshotParams<'a>> { self.screenshot.as_ref() }
116}
117
118#[derive(Default)]
119pub struct BeginFrameParamsBuilder<'a> {
120    frame_time_ticks: Option<f64>,
121    interval: Option<f64>,
122    no_display_updates: Option<bool>,
123    screenshot: Option<ScreenshotParams<'a>>,
124}
125
126impl<'a> BeginFrameParamsBuilder<'a> {
127    /// Timestamp of this BeginFrame in Renderer TimeTicks (milliseconds of uptime). If not set,
128    /// the current time will be used.
129    pub fn frame_time_ticks(mut self, frame_time_ticks: f64) -> Self { self.frame_time_ticks = Some(frame_time_ticks); self }
130    /// The interval between BeginFrames that is reported to the compositor, in milliseconds.
131    /// Defaults to a 60 frames/second interval, i.e. about 16.666 milliseconds.
132    pub fn interval(mut self, interval: f64) -> Self { self.interval = Some(interval); self }
133    /// Whether updates should not be committed and drawn onto the display. False by default. If
134    /// true, only side effects of the BeginFrame will be run, such as layout and animations, but
135    /// any visual updates may not be visible on the display or in screenshots.
136    pub fn no_display_updates(mut self, no_display_updates: bool) -> Self { self.no_display_updates = Some(no_display_updates); self }
137    /// If set, a screenshot of the frame will be captured and returned in the response. Otherwise,
138    /// no screenshot will be captured. Note that capturing a screenshot can fail, for example,
139    /// during renderer initialization. In such a case, no screenshot data will be returned.
140    pub fn screenshot(mut self, screenshot: ScreenshotParams<'a>) -> Self { self.screenshot = Some(screenshot); self }
141    pub fn build(self) -> BeginFrameParams<'a> {
142        BeginFrameParams {
143            frame_time_ticks: self.frame_time_ticks,
144            interval: self.interval,
145            no_display_updates: self.no_display_updates,
146            screenshot: self.screenshot,
147        }
148    }
149}
150
151/// Sends a BeginFrame to the target and returns when the frame was completed. Optionally captures a
152/// screenshot from the resulting frame. Requires that the target was created with enabled
153/// BeginFrameControl. Designed for use with --run-all-compositor-stages-before-draw, see also
154/// <https://goo.gle/chrome-headless-rendering> for more background.
155
156#[derive(Debug, Clone, Serialize, Deserialize, Default)]
157#[serde(rename_all = "camelCase")]
158pub struct BeginFrameReturns<'a> {
159    /// Whether the BeginFrame resulted in damage and, thus, a new frame was committed to the
160    /// display. Reported for diagnostic uses, may be removed in the future.
161    #[serde(rename = "hasDamage")]
162    has_damage: bool,
163    /// Base64-encoded image data of the screenshot, if one was requested and successfully taken. (Encoded as a base64 string when passed over JSON)
164    #[serde(skip_serializing_if = "Option::is_none", rename = "screenshotData")]
165    screenshot_data: Option<Cow<'a, str>>,
166}
167
168impl<'a> BeginFrameReturns<'a> {
169    /// Creates a builder for this type with the required parameters:
170    /// * `has_damage`: Whether the BeginFrame resulted in damage and, thus, a new frame was committed to the display. Reported for diagnostic uses, may be removed in the future.
171    pub fn builder(has_damage: bool) -> BeginFrameReturnsBuilder<'a> {
172        BeginFrameReturnsBuilder {
173            has_damage: has_damage,
174            screenshot_data: None,
175        }
176    }
177    /// Whether the BeginFrame resulted in damage and, thus, a new frame was committed to the
178    /// display. Reported for diagnostic uses, may be removed in the future.
179    pub fn has_damage(&self) -> bool { self.has_damage }
180    /// Base64-encoded image data of the screenshot, if one was requested and successfully taken. (Encoded as a base64 string when passed over JSON)
181    pub fn screenshot_data(&self) -> Option<&str> { self.screenshot_data.as_deref() }
182}
183
184
185pub struct BeginFrameReturnsBuilder<'a> {
186    has_damage: bool,
187    screenshot_data: Option<Cow<'a, str>>,
188}
189
190impl<'a> BeginFrameReturnsBuilder<'a> {
191    /// Base64-encoded image data of the screenshot, if one was requested and successfully taken. (Encoded as a base64 string when passed over JSON)
192    pub fn screenshot_data(mut self, screenshot_data: impl Into<Cow<'a, str>>) -> Self { self.screenshot_data = Some(screenshot_data.into()); self }
193    pub fn build(self) -> BeginFrameReturns<'a> {
194        BeginFrameReturns {
195            has_damage: self.has_damage,
196            screenshot_data: self.screenshot_data,
197        }
198    }
199}
200
201impl<'a> BeginFrameParams<'a> { pub const METHOD: &'static str = "HeadlessExperimental.beginFrame"; }
202
203impl<'a> crate::CdpCommand<'a> for BeginFrameParams<'a> {
204    const METHOD: &'static str = "HeadlessExperimental.beginFrame";
205    type Response = BeginFrameReturns<'a>;
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize, Default)]
209pub struct DisableParams {}
210
211impl DisableParams { pub const METHOD: &'static str = "HeadlessExperimental.disable"; }
212
213impl<'a> crate::CdpCommand<'a> for DisableParams {
214    const METHOD: &'static str = "HeadlessExperimental.disable";
215    type Response = crate::EmptyReturns;
216}
217
218#[derive(Debug, Clone, Serialize, Deserialize, Default)]
219pub struct EnableParams {}
220
221impl EnableParams { pub const METHOD: &'static str = "HeadlessExperimental.enable"; }
222
223impl<'a> crate::CdpCommand<'a> for EnableParams {
224    const METHOD: &'static str = "HeadlessExperimental.enable";
225    type Response = crate::EmptyReturns;
226}