Skip to main content

script/dom/performance/
performancemark.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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    /// <https://w3c.github.io/user-timing/#dom-performancemark-detail>
74    fn Detail(&self, mut retval: MutableHandleValue) {
75        retval.set(self.detail.get())
76    }
77
78    /// <https://w3c.github.io/user-timing/#the-performancemark-constructor>
79    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        // The PerformanceMark constructor must run the following steps:
87        // Step 1. If the current global object is a Window object and markName uses the same name
88        // as a read only attribute in the PerformanceTiming interface, throw a SyntaxError.
89        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        // Step 2 - 4. Note: These are handled by the PerformanceMark default constructor below.
96
97        // Step 5. Set entry’s startTime attribute as follows:
98        let start_time = match mark_options.startTime {
99            // Step 5.1. If markOptions’s startTime member exists, then:
100            Some(start_time) => {
101                // Step 5.1.1. If markOptions’s startTime is negative, throw a TypeError.
102                if start_time.is_sign_negative() {
103                    return Err(Error::Type(c"startTime must not be negative".to_owned()));
104                }
105                // Step 5.1.2. Otherwise, set entry’s startTime to the value of markOptions’s startTime.
106                global.performance(cx).time_origin() +
107                    Duration::microseconds(start_time.mul_add(1000.0, 0.0) as i64)
108            },
109            // Step 5.2. Otherwise, set it to the value that would be returned by the Performance object’s now() method.
110            None => CrossProcessInstant::now(),
111        };
112
113        // Step 6. Set entry’s duration attribute to 0.
114        let entry = PerformanceMark::new_with_proto(
115            cx,
116            global,
117            proto,
118            mark_name,
119            start_time,
120            Duration::ZERO,
121        );
122
123        // Step 7. If markOptions’s detail is null, set entry’s detail to null.
124        rooted!(&in(cx) let mut detail = NullValue());
125
126        // Step 8 Otherwise:
127        if !mark_options.detail.get().is_null_or_undefined() {
128            // Step 8.1. Let record be the result of calling the StructuredSerialize algorithm on markOptions’s detail.
129            let record = structuredclone::write(cx, mark_options.detail.handle(), None)?;
130
131            // Step 8.2. Set entry’s detail to the result of calling the StructuredDeserialize algorithm on record and the current realm.
132            structuredclone::read(cx, global, record, detail.handle_mut())?;
133        }
134        entry.set_detail(detail.handle());
135
136        Ok(entry)
137    }
138}