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