script/dom/performance/
performanceentry.rs1use 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#[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]
61pub(crate) struct PerformanceEntry {
62 reflector_: Reflector,
63
64 name: DOMString,
66
67 entry_type: EntryType,
69
70 #[no_trace]
72 start_time: Option<CrossProcessInstant>,
73
74 #[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 pub(crate) fn entry_type(&self) -> EntryType {
101 self.entry_type
102 }
103
104 pub(crate) fn name(&self) -> &DOMString {
106 &self.name
107 }
108
109 pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
111 self.start_time
112 }
113
114 pub(crate) fn duration(&self) -> Duration {
116 self.duration
117 }
118}
119
120impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
121 fn Name(&self) -> DOMString {
123 self.name.clone()
124 }
125
126 fn EntryType(&self) -> DOMString {
128 DOMString::from(self.entry_type.as_str())
129 }
130
131 fn StartTime(&self) -> DOMHighResTimeStamp {
133 self.global()
134 .performance()
135 .maybe_to_dom_high_res_time_stamp(self.start_time)
136 }
137
138 fn Duration(&self) -> DOMHighResTimeStamp {
140 self.duration.to_dom_high_res_time_stamp()
141 }
142}