Skip to main content

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