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::context::JSContext;
7use js::gc::HandleValue;
8use js::jsapi::Heap;
9use js::jsval::JSVal;
10use js::rust::MutableHandleValue;
11use script_bindings::reflector::reflect_dom_object_with_cx;
12use servo_base::cross_process_instant::CrossProcessInstant;
13use time::Duration;
14
15use crate::dom::bindings::codegen::Bindings::PerformanceMeasureBinding::PerformanceMeasureMethods;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::performance::performanceentry::{EntryType, PerformanceEntry};
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        cx: &mut JSContext,
47        global: &GlobalScope,
48        name: DOMString,
49        start_time: CrossProcessInstant,
50        duration: Duration,
51    ) -> DomRoot<PerformanceMeasure> {
52        reflect_dom_object_with_cx(
53            Box::new(PerformanceMeasure::new_inherited(
54                name, start_time, duration,
55            )),
56            global,
57            cx,
58        )
59    }
60
61    pub(crate) fn set_detail(&self, handle: HandleValue<'_>) {
62        self.detail.set(handle.get());
63    }
64}
65
66impl PerformanceMeasureMethods<crate::DomTypeHolder> for PerformanceMeasure {
67    /// <https://w3c.github.io/user-timing/#dom-performancemeasure-detail>
68    fn Detail(&self, mut retval: MutableHandleValue) {
69        retval.set(self.detail.get())
70    }
71}