script/dom/
performanceentry.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 base::cross_process_instant::CrossProcessInstant;
6use dom_struct::dom_struct;
7use time::Duration;
8
9use super::performance::ToDOMHighResTimeStamp;
10use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
11use crate::dom::bindings::codegen::Bindings::PerformanceEntryBinding::PerformanceEntryMethods;
12use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::globalscope::GlobalScope;
16use crate::script_runtime::CanGc;
17
18#[dom_struct]
19pub(crate) struct PerformanceEntry {
20    reflector_: Reflector,
21    name: DOMString,
22    entry_type: DOMString,
23    #[no_trace]
24    start_time: Option<CrossProcessInstant>,
25    /// The duration of this [`PerformanceEntry`]. This is a [`time::Duration`],
26    /// because it can be negative and `std::time::Duration` cannot be.
27    #[no_trace]
28    #[ignore_malloc_size_of = "No MallocSizeOf support for `time` crate"]
29    duration: Duration,
30}
31
32impl PerformanceEntry {
33    pub(crate) fn new_inherited(
34        name: DOMString,
35        entry_type: DOMString,
36        start_time: Option<CrossProcessInstant>,
37        duration: Duration,
38    ) -> PerformanceEntry {
39        PerformanceEntry {
40            reflector_: Reflector::new(),
41            name,
42            entry_type,
43            start_time,
44            duration,
45        }
46    }
47
48    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
49    pub(crate) fn new(
50        global: &GlobalScope,
51        name: DOMString,
52        entry_type: DOMString,
53        start_time: CrossProcessInstant,
54        duration: Duration,
55        can_gc: CanGc,
56    ) -> DomRoot<PerformanceEntry> {
57        let entry = PerformanceEntry::new_inherited(name, entry_type, Some(start_time), duration);
58        reflect_dom_object(Box::new(entry), global, can_gc)
59    }
60
61    pub(crate) fn entry_type(&self) -> &DOMString {
62        &self.entry_type
63    }
64
65    pub(crate) fn name(&self) -> &DOMString {
66        &self.name
67    }
68
69    pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
70        self.start_time
71    }
72
73    pub(crate) fn duration(&self) -> Duration {
74        self.duration
75    }
76}
77
78impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
79    // https://w3c.github.io/performance-timeline/#dom-performanceentry-name
80    fn Name(&self) -> DOMString {
81        self.name.clone()
82    }
83
84    // https://w3c.github.io/performance-timeline/#dom-performanceentry-entrytype
85    fn EntryType(&self) -> DOMString {
86        self.entry_type.clone()
87    }
88
89    // https://w3c.github.io/performance-timeline/#dom-performanceentry-starttime
90    fn StartTime(&self) -> DOMHighResTimeStamp {
91        self.global()
92            .performance()
93            .maybe_to_dom_high_res_time_stamp(self.start_time)
94    }
95
96    // https://w3c.github.io/performance-timeline/#dom-performanceentry-duration
97    fn Duration(&self) -> DOMHighResTimeStamp {
98        self.duration.to_dom_high_res_time_stamp()
99    }
100}