script/dom/performance/
performanceentry.rs1use 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#[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#[dom_struct]
62pub(crate) struct PerformanceEntry {
63 reflector_: Reflector,
64
65 name: DOMString,
67
68 entry_type: EntryType,
70
71 #[no_trace]
73 start_time: Option<CrossProcessInstant>,
74
75 #[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 pub(crate) fn entry_type(&self) -> EntryType {
102 self.entry_type
103 }
104
105 pub(crate) fn name(&self) -> &DOMString {
107 &self.name
108 }
109
110 pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
112 self.start_time
113 }
114
115 pub(crate) fn duration(&self) -> Duration {
117 self.duration
118 }
119}
120
121impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
122 fn Name(&self) -> DOMString {
124 self.name.clone()
125 }
126
127 fn EntryType(&self) -> DOMString {
129 DOMString::from(self.entry_type.as_str())
130 }
131
132 fn StartTime(&self) -> DOMHighResTimeStamp {
134 self.global()
135 .performance()
136 .maybe_to_dom_high_res_time_stamp(self.start_time)
137 }
138
139 fn Duration(&self) -> DOMHighResTimeStamp {
141 self.duration.to_dom_high_res_time_stamp()
142 }
143}