script/dom/performance/
performancemark.rs1use dom_struct::dom_struct;
6use js::gc::HandleValue;
7use js::jsapi::Heap;
8use js::jsval::{JSVal, NullValue};
9use js::rust::{HandleObject, MutableHandleValue};
10use script_bindings::codegen::GenericBindings::PerformanceBinding::PerformanceMarkOptions;
11use script_bindings::reflector::reflect_dom_object_with_proto;
12use servo_base::cross_process_instant::CrossProcessInstant;
13use time::Duration;
14
15use crate::dom::PERFORMANCE_TIMING_ATTRIBUTES;
16use crate::dom::bindings::codegen::Bindings::PerformanceMarkBinding::PerformanceMarkMethods;
17use crate::dom::bindings::error::{Error, Fallible};
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::bindings::str::DOMString;
21use crate::dom::bindings::structuredclone;
22use crate::dom::bindings::trace::RootedTraceableBox;
23use crate::dom::globalscope::GlobalScope;
24use crate::dom::performance::performanceentry::{EntryType, PerformanceEntry};
25use crate::dom::window::Window;
26
27#[dom_struct]
28pub(crate) struct PerformanceMark {
29 entry: PerformanceEntry,
30 #[ignore_malloc_size_of = "Defined in rust-mozjs"]
31 detail: Heap<JSVal>,
32}
33
34impl PerformanceMark {
35 fn new_inherited(
36 name: DOMString,
37 start_time: CrossProcessInstant,
38 duration: Duration,
39 ) -> PerformanceMark {
40 PerformanceMark {
41 entry: PerformanceEntry::new_inherited(
42 name,
43 EntryType::Mark,
44 Some(start_time),
45 duration,
46 ),
47 detail: Default::default(),
48 }
49 }
50
51 fn set_detail(&self, handle: HandleValue<'_>) {
52 self.detail.set(handle.get());
53 }
54
55 pub(crate) fn new_with_proto(
56 cx: &mut js::context::JSContext,
57 global: &GlobalScope,
58 proto: Option<HandleObject>,
59 name: DOMString,
60 start_time: CrossProcessInstant,
61 duration: Duration,
62 ) -> DomRoot<PerformanceMark> {
63 reflect_dom_object_with_proto(
64 cx,
65 Box::new(PerformanceMark::new_inherited(name, start_time, duration)),
66 global,
67 proto,
68 )
69 }
70}
71
72impl PerformanceMarkMethods<crate::DomTypeHolder> for PerformanceMark {
73 fn Detail(&self, mut retval: MutableHandleValue) {
75 retval.set(self.detail.get())
76 }
77
78 fn Constructor(
80 cx: &mut js::context::JSContext,
81 global: &GlobalScope,
82 proto: Option<HandleObject>,
83 mark_name: DOMString,
84 mark_options: RootedTraceableBox<PerformanceMarkOptions>,
85 ) -> Fallible<DomRoot<PerformanceMark>> {
86 if global.is::<Window>() && PERFORMANCE_TIMING_ATTRIBUTES.contains(&&*mark_name.str()) {
90 return Err(Error::Syntax(Some(
91 "Read-only attribute cannot be used as a mark name".to_owned(),
92 )));
93 }
94
95 let start_time = match mark_options.startTime {
99 Some(start_time) => {
101 if start_time.is_sign_negative() {
103 return Err(Error::Type(c"startTime must not be negative".to_owned()));
104 }
105 global.performance(cx).time_origin() +
107 Duration::microseconds(start_time.mul_add(1000.0, 0.0) as i64)
108 },
109 None => CrossProcessInstant::now(),
111 };
112
113 let entry = PerformanceMark::new_with_proto(
115 cx,
116 global,
117 proto,
118 mark_name,
119 start_time,
120 Duration::ZERO,
121 );
122
123 rooted!(&in(cx) let mut detail = NullValue());
125
126 if !mark_options.detail.get().is_null_or_undefined() {
128 let record = structuredclone::write(cx, mark_options.detail.handle(), None)?;
130
131 structuredclone::read(cx, global, record, detail.handle_mut())?;
133 }
134 entry.set_detail(detail.handle());
135
136 Ok(entry)
137 }
138}