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