script/dom/performance/
performanceentry.rs1use 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#[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#[dom_struct]
66pub(crate) struct PerformanceEntry {
67 reflector_: Reflector,
68
69 name: DOMString,
71
72 entry_type: EntryType,
74
75 #[no_trace]
77 start_time: Option<CrossProcessInstant>,
78
79 #[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 pub(crate) fn entry_type(&self) -> EntryType {
106 self.entry_type
107 }
108
109 pub(crate) fn name(&self) -> &DOMString {
111 &self.name
112 }
113
114 pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
116 self.start_time
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, cx: &mut JSContext) -> DOMHighResTimeStamp {
133 self.global()
134 .performance(cx)
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}