script/dom/performance/
performanceentry.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use servo_base::cross_process_instant::CrossProcessInstant;
7use strum::VariantArray;
8use time::Duration;
9
10use super::performance::ToDOMHighResTimeStamp;
11use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
12use crate::dom::bindings::codegen::Bindings::PerformanceEntryBinding::PerformanceEntryMethods;
13use crate::dom::bindings::reflector::{DomGlobal, Reflector};
14use crate::dom::bindings::str::DOMString;
15
16/// All supported entry types, in alphabetical order.
17#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq, VariantArray)]
18pub(crate) enum EntryType {
19    LargestContentfulPaint,
20    Mark,
21    Measure,
22    Navigation,
23    Paint,
24    Resource,
25    VisibilityState,
26}
27
28impl EntryType {
29    pub(crate) fn as_str(&self) -> &'static str {
30        match self {
31            EntryType::Measure => "measure",
32            EntryType::Mark => "mark",
33            EntryType::LargestContentfulPaint => "largest-contentful-paint",
34            EntryType::Paint => "paint",
35            EntryType::Navigation => "navigation",
36            EntryType::Resource => "resource",
37            EntryType::VisibilityState => "visibility-state",
38        }
39    }
40}
41
42impl<'a> TryFrom<&'a str> for EntryType {
43    type Error = ();
44
45    fn try_from(value: &'a str) -> Result<EntryType, ()> {
46        Ok(match value {
47            "measure" => EntryType::Measure,
48            "mark" => EntryType::Mark,
49            "largest-contentful-paint" => EntryType::LargestContentfulPaint,
50            "paint" => EntryType::Paint,
51            "navigation" => EntryType::Navigation,
52            "resource" => EntryType::Resource,
53            "visibility-state" => EntryType::VisibilityState,
54            _ => return Err(()),
55        })
56    }
57}
58
59/// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry>
60#[dom_struct]
61pub(crate) struct PerformanceEntry {
62    reflector_: Reflector,
63
64    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-name>
65    name: DOMString,
66
67    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-entrytype>
68    entry_type: EntryType,
69
70    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-starttime>
71    #[no_trace]
72    start_time: Option<CrossProcessInstant>,
73
74    /// The duration of this [`PerformanceEntry`]. This is a [`time::Duration`],
75    /// because it can be negative and `std::time::Duration` cannot be.
76    ///
77    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-duration>
78    #[no_trace]
79    #[ignore_malloc_size_of = "No MallocSizeOf support for `time` crate"]
80    duration: Duration,
81}
82
83impl PerformanceEntry {
84    pub(crate) fn new_inherited(
85        name: DOMString,
86        entry_type: EntryType,
87        start_time: Option<CrossProcessInstant>,
88        duration: Duration,
89    ) -> PerformanceEntry {
90        PerformanceEntry {
91            reflector_: Reflector::new(),
92            name,
93            entry_type,
94            start_time,
95            duration,
96        }
97    }
98
99    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-name>
100    pub(crate) fn entry_type(&self) -> EntryType {
101        self.entry_type
102    }
103
104    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-entrytype>
105    pub(crate) fn name(&self) -> &DOMString {
106        &self.name
107    }
108
109    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-starttime>
110    pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
111        self.start_time
112    }
113
114    /// <https://www.w3.org/TR/performance-timeline/#dom-performanceentry-duration>
115    pub(crate) fn duration(&self) -> Duration {
116        self.duration
117    }
118}
119
120impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
121    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-name>
122    fn Name(&self) -> DOMString {
123        self.name.clone()
124    }
125
126    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
127    fn EntryType(&self) -> DOMString {
128        DOMString::from(self.entry_type.as_str())
129    }
130
131    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-starttime>
132    fn StartTime(&self) -> DOMHighResTimeStamp {
133        self.global()
134            .performance()
135            .maybe_to_dom_high_res_time_stamp(self.start_time)
136    }
137
138    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-duration>
139    fn Duration(&self) -> DOMHighResTimeStamp {
140        self.duration.to_dom_high_res_time_stamp()
141    }
142}