Skip to main content

script/dom/performance/
performancepainttiming.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 paint_api::display_list::PaintTimingInfo;
8use script_bindings::reflector::reflect_dom_object;
9use script_traits::ProgressiveWebMetricType;
10use time::Duration;
11
12use super::performanceentry::{EntryType, PerformanceEntry};
13use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
14use crate::dom::bindings::codegen::Bindings::PerformancePaintTimingBinding::PerformancePaintTimingMethods;
15use crate::dom::bindings::reflector::DomGlobal;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::globalscope::GlobalScope;
19
20#[dom_struct]
21pub(crate) struct PerformancePaintTiming {
22    entry: PerformanceEntry,
23    /// <https://www.w3.org/TR/paint-timing/#paint-timing-info>
24    #[no_trace]
25    paint_timing_info: PaintTimingInfo,
26}
27
28impl PerformancePaintTiming {
29    fn new_inherited(
30        metric_type: ProgressiveWebMetricType,
31        paint_timing_info: PaintTimingInfo,
32    ) -> PerformancePaintTiming {
33        let name = match metric_type {
34            ProgressiveWebMetricType::FirstPaint => DOMString::from_static("first-paint"),
35            ProgressiveWebMetricType::FirstContentfulPaint => {
36                DOMString::from_static("first-contentful-paint")
37            },
38            _ => DOMString::new(),
39        };
40        PerformancePaintTiming {
41            entry: PerformanceEntry::new_inherited(
42                name,
43                EntryType::Paint,
44                // From: <https://www.w3.org/TR/paint-timing/#report-paint-timing>
45                //
46                // Set newEntry’s startTime attribute to the default paint
47                // timestamp given paintTimingInfo.
48                Some(paint_timing_info.default_paint_timestamp()),
49                Duration::ZERO,
50            ),
51            paint_timing_info,
52        }
53    }
54
55    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
56    pub(crate) fn new(
57        cx: &mut JSContext,
58        global: &GlobalScope,
59        metric_type: ProgressiveWebMetricType,
60        paint_timing_info: PaintTimingInfo,
61    ) -> DomRoot<PerformancePaintTiming> {
62        let entry = PerformancePaintTiming::new_inherited(metric_type, paint_timing_info);
63        reflect_dom_object(cx, Box::new(entry), global)
64    }
65}
66
67impl PerformancePaintTimingMethods<crate::DomTypeHolder> for PerformancePaintTiming {
68    /// <https://www.w3.org/TR/paint-timing/#dom-painttimingmixin-painttime>
69    fn PaintTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
70        self.global()
71            .performance(cx)
72            .to_dom_high_res_time_stamp(self.paint_timing_info.paint_time())
73    }
74
75    /// <https://www.w3.org/TR/paint-timing/#dom-painttimingmixin-presentationtime>
76    fn GetPresentationTime(&self, cx: &mut JSContext) -> Option<DOMHighResTimeStamp> {
77        Some(
78            self.global()
79                .performance(cx)
80                .maybe_to_dom_high_res_time_stamp(self.paint_timing_info.presentation_time()),
81        )
82    }
83}