Skip to main content

browser_protocol/performancetimeline/
mod.rs

1//! Reporting of performance timeline events, as specified in
2//! <https://w3c.github.io/performance-timeline/#dom-performanceobserver>.
3
4
5use serde::{Serialize, Deserialize};
6use serde_json::Value as JsonValue;
7use std::borrow::Cow;
8
9/// See <https://github.com/WICG/LargestContentfulPaint> and largest_contentful_paint.idl
10
11#[derive(Debug, Clone, Serialize, Deserialize, Default)]
12#[serde(rename_all = "camelCase")]
13pub struct LargestContentfulPaint<'a> {
14    #[serde(rename = "renderTime")]
15    render_time: crate::network::TimeSinceEpoch,
16    #[serde(rename = "loadTime")]
17    load_time: crate::network::TimeSinceEpoch,
18    /// The number of pixels being painted.
19    size: f64,
20    /// The id attribute of the element, if available.
21    #[serde(skip_serializing_if = "Option::is_none", rename = "elementId")]
22    element_id: Option<Cow<'a, str>>,
23    /// The URL of the image (may be trimmed).
24    #[serde(skip_serializing_if = "Option::is_none")]
25    url: Option<Cow<'a, str>>,
26    #[serde(skip_serializing_if = "Option::is_none", rename = "nodeId")]
27    node_id: Option<crate::dom::BackendNodeId>,
28}
29
30impl<'a> LargestContentfulPaint<'a> {
31    /// Creates a builder for this type with the required parameters:
32    /// * `render_time`: 
33    /// * `load_time`: 
34    /// * `size`: The number of pixels being painted.
35    pub fn builder(render_time: crate::network::TimeSinceEpoch, load_time: crate::network::TimeSinceEpoch, size: f64) -> LargestContentfulPaintBuilder<'a> {
36        LargestContentfulPaintBuilder {
37            render_time: render_time,
38            load_time: load_time,
39            size: size,
40            element_id: None,
41            url: None,
42            node_id: None,
43        }
44    }
45    pub fn render_time(&self) -> &crate::network::TimeSinceEpoch { &self.render_time }
46    pub fn load_time(&self) -> &crate::network::TimeSinceEpoch { &self.load_time }
47    /// The number of pixels being painted.
48    pub fn size(&self) -> f64 { self.size }
49    /// The id attribute of the element, if available.
50    pub fn element_id(&self) -> Option<&str> { self.element_id.as_deref() }
51    /// The URL of the image (may be trimmed).
52    pub fn url(&self) -> Option<&str> { self.url.as_deref() }
53    pub fn node_id(&self) -> Option<&crate::dom::BackendNodeId> { self.node_id.as_ref() }
54}
55
56
57pub struct LargestContentfulPaintBuilder<'a> {
58    render_time: crate::network::TimeSinceEpoch,
59    load_time: crate::network::TimeSinceEpoch,
60    size: f64,
61    element_id: Option<Cow<'a, str>>,
62    url: Option<Cow<'a, str>>,
63    node_id: Option<crate::dom::BackendNodeId>,
64}
65
66impl<'a> LargestContentfulPaintBuilder<'a> {
67    /// The id attribute of the element, if available.
68    pub fn element_id(mut self, element_id: impl Into<Cow<'a, str>>) -> Self { self.element_id = Some(element_id.into()); self }
69    /// The URL of the image (may be trimmed).
70    pub fn url(mut self, url: impl Into<Cow<'a, str>>) -> Self { self.url = Some(url.into()); self }
71    pub fn node_id(mut self, node_id: crate::dom::BackendNodeId) -> Self { self.node_id = Some(node_id); self }
72    pub fn build(self) -> LargestContentfulPaint<'a> {
73        LargestContentfulPaint {
74            render_time: self.render_time,
75            load_time: self.load_time,
76            size: self.size,
77            element_id: self.element_id,
78            url: self.url,
79            node_id: self.node_id,
80        }
81    }
82}
83
84
85#[derive(Debug, Clone, Serialize, Deserialize, Default)]
86#[serde(rename_all = "camelCase")]
87pub struct LayoutShiftAttribution {
88    #[serde(rename = "previousRect")]
89    previous_rect: crate::dom::Rect,
90    #[serde(rename = "currentRect")]
91    current_rect: crate::dom::Rect,
92    #[serde(skip_serializing_if = "Option::is_none", rename = "nodeId")]
93    node_id: Option<crate::dom::BackendNodeId>,
94}
95
96impl LayoutShiftAttribution {
97    /// Creates a builder for this type with the required parameters:
98    /// * `previous_rect`: 
99    /// * `current_rect`: 
100    pub fn builder(previous_rect: crate::dom::Rect, current_rect: crate::dom::Rect) -> LayoutShiftAttributionBuilder {
101        LayoutShiftAttributionBuilder {
102            previous_rect: previous_rect,
103            current_rect: current_rect,
104            node_id: None,
105        }
106    }
107    pub fn previous_rect(&self) -> &crate::dom::Rect { &self.previous_rect }
108    pub fn current_rect(&self) -> &crate::dom::Rect { &self.current_rect }
109    pub fn node_id(&self) -> Option<&crate::dom::BackendNodeId> { self.node_id.as_ref() }
110}
111
112
113pub struct LayoutShiftAttributionBuilder {
114    previous_rect: crate::dom::Rect,
115    current_rect: crate::dom::Rect,
116    node_id: Option<crate::dom::BackendNodeId>,
117}
118
119impl LayoutShiftAttributionBuilder {
120    pub fn node_id(mut self, node_id: crate::dom::BackendNodeId) -> Self { self.node_id = Some(node_id); self }
121    pub fn build(self) -> LayoutShiftAttribution {
122        LayoutShiftAttribution {
123            previous_rect: self.previous_rect,
124            current_rect: self.current_rect,
125            node_id: self.node_id,
126        }
127    }
128}
129
130/// See <https://wicg.github.io/layout-instability/#sec-layout-shift> and layout_shift.idl
131
132#[derive(Debug, Clone, Serialize, Deserialize, Default)]
133#[serde(rename_all = "camelCase")]
134pub struct LayoutShift {
135    /// Score increment produced by this event.
136    value: f64,
137    #[serde(rename = "hadRecentInput")]
138    had_recent_input: bool,
139    #[serde(rename = "lastInputTime")]
140    last_input_time: crate::network::TimeSinceEpoch,
141    sources: Vec<LayoutShiftAttribution>,
142}
143
144impl LayoutShift {
145    /// Creates a builder for this type with the required parameters:
146    /// * `value`: Score increment produced by this event.
147    /// * `had_recent_input`: 
148    /// * `last_input_time`: 
149    /// * `sources`: 
150    pub fn builder(value: f64, had_recent_input: bool, last_input_time: crate::network::TimeSinceEpoch, sources: Vec<LayoutShiftAttribution>) -> LayoutShiftBuilder {
151        LayoutShiftBuilder {
152            value: value,
153            had_recent_input: had_recent_input,
154            last_input_time: last_input_time,
155            sources: sources,
156        }
157    }
158    /// Score increment produced by this event.
159    pub fn value(&self) -> f64 { self.value }
160    pub fn had_recent_input(&self) -> bool { self.had_recent_input }
161    pub fn last_input_time(&self) -> &crate::network::TimeSinceEpoch { &self.last_input_time }
162    pub fn sources(&self) -> &[LayoutShiftAttribution] { &self.sources }
163}
164
165
166pub struct LayoutShiftBuilder {
167    value: f64,
168    had_recent_input: bool,
169    last_input_time: crate::network::TimeSinceEpoch,
170    sources: Vec<LayoutShiftAttribution>,
171}
172
173impl LayoutShiftBuilder {
174    pub fn build(self) -> LayoutShift {
175        LayoutShift {
176            value: self.value,
177            had_recent_input: self.had_recent_input,
178            last_input_time: self.last_input_time,
179            sources: self.sources,
180        }
181    }
182}
183
184
185#[derive(Debug, Clone, Serialize, Deserialize, Default)]
186#[serde(rename_all = "camelCase")]
187pub struct TimelineEvent<'a> {
188    /// Identifies the frame that this event is related to. Empty for non-frame targets.
189    #[serde(rename = "frameId")]
190    frame_id: crate::page::FrameId<'a>,
191    /// The event type, as specified in <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
192    /// This determines which of the optional "details" fields is present.
193    #[serde(rename = "type")]
194    type_: Cow<'a, str>,
195    /// Name may be empty depending on the type.
196    name: Cow<'a, str>,
197    /// Time in seconds since Epoch, monotonically increasing within document lifetime.
198    time: crate::network::TimeSinceEpoch,
199    /// Event duration, if applicable.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    duration: Option<f64>,
202    #[serde(skip_serializing_if = "Option::is_none", rename = "lcpDetails")]
203    lcp_details: Option<LargestContentfulPaint<'a>>,
204    #[serde(skip_serializing_if = "Option::is_none", rename = "layoutShiftDetails")]
205    layout_shift_details: Option<LayoutShift>,
206}
207
208impl<'a> TimelineEvent<'a> {
209    /// Creates a builder for this type with the required parameters:
210    /// * `frame_id`: Identifies the frame that this event is related to. Empty for non-frame targets.
211    /// * `type_`: The event type, as specified in <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype> This determines which of the optional "details" fields is present.
212    /// * `name`: Name may be empty depending on the type.
213    /// * `time`: Time in seconds since Epoch, monotonically increasing within document lifetime.
214    pub fn builder(frame_id: crate::page::FrameId<'a>, type_: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>, time: crate::network::TimeSinceEpoch) -> TimelineEventBuilder<'a> {
215        TimelineEventBuilder {
216            frame_id: frame_id,
217            type_: type_.into(),
218            name: name.into(),
219            time: time,
220            duration: None,
221            lcp_details: None,
222            layout_shift_details: None,
223        }
224    }
225    /// Identifies the frame that this event is related to. Empty for non-frame targets.
226    pub fn frame_id(&self) -> &crate::page::FrameId<'a> { &self.frame_id }
227    /// The event type, as specified in <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
228    /// This determines which of the optional "details" fields is present.
229    pub fn type_(&self) -> &str { self.type_.as_ref() }
230    /// Name may be empty depending on the type.
231    pub fn name(&self) -> &str { self.name.as_ref() }
232    /// Time in seconds since Epoch, monotonically increasing within document lifetime.
233    pub fn time(&self) -> &crate::network::TimeSinceEpoch { &self.time }
234    /// Event duration, if applicable.
235    pub fn duration(&self) -> Option<f64> { self.duration }
236    pub fn lcp_details(&self) -> Option<&LargestContentfulPaint<'a>> { self.lcp_details.as_ref() }
237    pub fn layout_shift_details(&self) -> Option<&LayoutShift> { self.layout_shift_details.as_ref() }
238}
239
240
241pub struct TimelineEventBuilder<'a> {
242    frame_id: crate::page::FrameId<'a>,
243    type_: Cow<'a, str>,
244    name: Cow<'a, str>,
245    time: crate::network::TimeSinceEpoch,
246    duration: Option<f64>,
247    lcp_details: Option<LargestContentfulPaint<'a>>,
248    layout_shift_details: Option<LayoutShift>,
249}
250
251impl<'a> TimelineEventBuilder<'a> {
252    /// Event duration, if applicable.
253    pub fn duration(mut self, duration: f64) -> Self { self.duration = Some(duration); self }
254    pub fn lcp_details(mut self, lcp_details: LargestContentfulPaint<'a>) -> Self { self.lcp_details = Some(lcp_details); self }
255    pub fn layout_shift_details(mut self, layout_shift_details: LayoutShift) -> Self { self.layout_shift_details = Some(layout_shift_details); self }
256    pub fn build(self) -> TimelineEvent<'a> {
257        TimelineEvent {
258            frame_id: self.frame_id,
259            type_: self.type_,
260            name: self.name,
261            time: self.time,
262            duration: self.duration,
263            lcp_details: self.lcp_details,
264            layout_shift_details: self.layout_shift_details,
265        }
266    }
267}
268
269/// Previously buffered events would be reported before method returns.
270/// See also: timelineEventAdded
271
272#[derive(Debug, Clone, Serialize, Deserialize, Default)]
273#[serde(rename_all = "camelCase")]
274pub struct EnableParams<'a> {
275    /// The types of event to report, as specified in
276    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
277    /// The specified filter overrides any previous filters, passing empty
278    /// filter disables recording.
279    /// Note that not all types exposed to the web platform are currently supported.
280    #[serde(rename = "eventTypes")]
281    event_types: Vec<Cow<'a, str>>,
282}
283
284impl<'a> EnableParams<'a> {
285    /// Creates a builder for this type with the required parameters:
286    /// * `event_types`: The types of event to report, as specified in <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype> The specified filter overrides any previous filters, passing empty filter disables recording. Note that not all types exposed to the web platform are currently supported.
287    pub fn builder(event_types: Vec<Cow<'a, str>>) -> EnableParamsBuilder<'a> {
288        EnableParamsBuilder {
289            event_types: event_types,
290        }
291    }
292    /// The types of event to report, as specified in
293    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
294    /// The specified filter overrides any previous filters, passing empty
295    /// filter disables recording.
296    /// Note that not all types exposed to the web platform are currently supported.
297    pub fn event_types(&self) -> &[Cow<'a, str>] { &self.event_types }
298}
299
300
301pub struct EnableParamsBuilder<'a> {
302    event_types: Vec<Cow<'a, str>>,
303}
304
305impl<'a> EnableParamsBuilder<'a> {
306    pub fn build(self) -> EnableParams<'a> {
307        EnableParams {
308            event_types: self.event_types,
309        }
310    }
311}
312
313impl<'a> EnableParams<'a> { pub const METHOD: &'static str = "PerformanceTimeline.enable"; }
314
315impl<'a> crate::CdpCommand<'a> for EnableParams<'a> {
316    const METHOD: &'static str = "PerformanceTimeline.enable";
317    type Response = crate::EmptyReturns;
318}