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 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#[dom_struct]
63pub(crate) struct PerformanceEntry {
64 reflector_: Reflector,
65
66 name: DOMString,
68
69 entry_type: EntryType,
71
72 #[no_trace]
74 start_time: Option<CrossProcessInstant>,
75
76 #[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 pub(crate) fn entry_type(&self) -> EntryType {
103 self.entry_type
104 }
105
106 pub(crate) fn name(&self) -> &DOMString {
108 &self.name
109 }
110
111 pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
113 self.start_time
114 }
115}
116
117impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
118 fn Name(&self) -> DOMString {
120 self.name.clone()
121 }
122
123 fn EntryType(&self) -> DOMString {
125 DOMString::from(self.entry_type.as_str())
126 }
127
128 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 fn Duration(&self) -> DOMHighResTimeStamp {
137 self.duration.to_dom_high_res_time_stamp()
138 }
139}