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_and_cx;
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;
26use crate::script_runtime::JSContext;
27
28#[dom_struct]
29pub(crate) struct PerformanceMark {
30 entry: PerformanceEntry,
31 #[ignore_malloc_size_of = "Defined in rust-mozjs"]
32 detail: Heap<JSVal>,
33}
34
35impl PerformanceMark {
36 fn new_inherited(
37 name: DOMString,
38 start_time: CrossProcessInstant,
39 duration: Duration,
40 ) -> PerformanceMark {
41 PerformanceMark {
42 entry: PerformanceEntry::new_inherited(
43 name,
44 EntryType::Mark,
45 Some(start_time),
46 duration,
47 ),
48 detail: Default::default(),
49 }
50 }
51
52 fn set_detail(&self, handle: HandleValue<'_>) {
53 self.detail.set(handle.get());
54 }
55
56 pub(crate) fn new_with_proto(
57 cx: &mut js::context::JSContext,
58 global: &GlobalScope,
59 proto: Option<HandleObject>,
60 name: DOMString,
61 start_time: CrossProcessInstant,
62 duration: Duration,
63 ) -> DomRoot<PerformanceMark> {
64 reflect_dom_object_with_proto_and_cx(
65 Box::new(PerformanceMark::new_inherited(name, start_time, duration)),
66 global,
67 proto,
68 cx,
69 )
70 }
71}
72
73impl PerformanceMarkMethods<crate::DomTypeHolder> for PerformanceMark {
74 fn Detail(&self, _cx: JSContext, mut retval: MutableHandleValue) {
76 retval.set(self.detail.get())
77 }
78
79 fn Constructor(
81 cx: &mut js::context::JSContext,
82 global: &GlobalScope,
83 proto: Option<HandleObject>,
84 mark_name: DOMString,
85 mark_options: RootedTraceableBox<PerformanceMarkOptions>,
86 ) -> Fallible<DomRoot<PerformanceMark>> {
87 if global.is::<Window>() && PERFORMANCE_TIMING_ATTRIBUTES.contains(&&*mark_name.str()) {
91 return Err(Error::Syntax(Some(
92 "Read-only attribute cannot be used as a mark name".to_owned(),
93 )));
94 }
95
96 let start_time = match mark_options.startTime {
100 Some(start_time) => {
102 if start_time.is_sign_negative() {
104 return Err(Error::Type(c"startTime must not be negative".to_owned()));
105 }
106 global.performance().time_origin() +
108 Duration::microseconds(start_time.mul_add(1000.0, 0.0) as i64)
109 },
110 None => CrossProcessInstant::now(),
112 };
113
114 let entry = PerformanceMark::new_with_proto(
116 cx,
117 global,
118 proto,
119 mark_name,
120 start_time,
121 Duration::ZERO,
122 );
123
124 rooted!(&in(cx) let mut detail = NullValue());
126
127 if !mark_options.detail.get().is_null_or_undefined() {
129 let record = structuredclone::write(cx, mark_options.detail.handle(), None)?;
131
132 structuredclone::read(cx, global, record, detail.handle_mut())?;
134 }
135 entry.set_detail(detail.handle());
136
137 Ok(entry)
138 }
139}