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