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 base::cross_process_instant::CrossProcessInstant;
6use dom_struct::dom_struct;
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#[dom_struct]
60pub(crate) struct PerformanceEntry {
61    reflector_: Reflector,
62    name: DOMString,
63    entry_type: EntryType,
64    #[no_trace]
65    start_time: Option<CrossProcessInstant>,
66    /// The duration of this [`PerformanceEntry`]. This is a [`time::Duration`],
67    /// because it can be negative and `std::time::Duration` cannot be.
68    #[no_trace]
69    #[ignore_malloc_size_of = "No MallocSizeOf support for `time` crate"]
70    duration: Duration,
71}
72
73impl PerformanceEntry {
74    pub(crate) fn new_inherited(
75        name: DOMString,
76        entry_type: EntryType,
77        start_time: Option<CrossProcessInstant>,
78        duration: Duration,
79    ) -> PerformanceEntry {
80        PerformanceEntry {
81            reflector_: Reflector::new(),
82            name,
83            entry_type,
84            start_time,
85            duration,
86        }
87    }
88
89    pub(crate) fn entry_type(&self) -> EntryType {
90        self.entry_type
91    }
92
93    pub(crate) fn name(&self) -> &DOMString {
94        &self.name
95    }
96
97    pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
98        self.start_time
99    }
100
101    pub(crate) fn duration(&self) -> Duration {
102        self.duration
103    }
104}
105
106impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
107    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-name>
108    fn Name(&self) -> DOMString {
109        self.name.clone()
110    }
111
112    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype>
113    fn EntryType(&self) -> DOMString {
114        DOMString::from(self.entry_type.as_str())
115    }
116
117    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-starttime>
118    fn StartTime(&self) -> DOMHighResTimeStamp {
119        self.global()
120            .performance()
121            .maybe_to_dom_high_res_time_stamp(self.start_time)
122    }
123
124    /// <https://w3c.github.io/performance-timeline/#dom-performanceentry-duration>
125    fn Duration(&self) -> DOMHighResTimeStamp {
126        self.duration.to_dom_high_res_time_stamp()
127    }
128}