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