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