Skip to main content

script/dom/performance/
performancemeasure.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;
9use js::rust::MutableHandleValue;
10use script_bindings::reflector::reflect_dom_object;
11use servo_base::cross_process_instant::CrossProcessInstant;
12use time::Duration;
13
14use crate::dom::bindings::codegen::Bindings::PerformanceMeasureBinding::PerformanceMeasureMethods;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::globalscope::GlobalScope;
18use crate::dom::performance::performanceentry::{EntryType, PerformanceEntry};
19use crate::script_runtime::{CanGc, JSContext};
20
21#[dom_struct]
22pub(crate) struct PerformanceMeasure {
23    entry: PerformanceEntry,
24    #[ignore_malloc_size_of = "Defined in rust-mozjs"]
25    detail: Heap<JSVal>,
26}
27
28impl PerformanceMeasure {
29    fn new_inherited(
30        name: DOMString,
31        start_time: CrossProcessInstant,
32        duration: Duration,
33    ) -> PerformanceMeasure {
34        PerformanceMeasure {
35            entry: PerformanceEntry::new_inherited(
36                name,
37                EntryType::Measure,
38                Some(start_time),
39                duration,
40            ),
41            detail: Default::default(),
42        }
43    }
44
45    pub(crate) fn new(
46        global: &GlobalScope,
47        name: DOMString,
48        start_time: CrossProcessInstant,
49        duration: Duration,
50    ) -> DomRoot<PerformanceMeasure> {
51        reflect_dom_object(
52            Box::new(PerformanceMeasure::new_inherited(
53                name, start_time, duration,
54            )),
55            global,
56            CanGc::deprecated_note(),
57        )
58    }
59
60    pub(crate) fn set_detail(&self, handle: HandleValue<'_>) {
61        self.detail.set(handle.get());
62    }
63}
64
65impl PerformanceMeasureMethods<crate::DomTypeHolder> for PerformanceMeasure {
66    /// <https://w3c.github.io/user-timing/#dom-performancemeasure-detail>
67    fn Detail(&self, _cx: JSContext, mut retval: MutableHandleValue) {
68        retval.set(self.detail.get())
69    }
70}