Skip to main content

script/dom/performance/
largestcontentfulpaint.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 script_bindings::reflector::reflect_dom_object_with_cx;
8use servo_base::cross_process_instant::CrossProcessInstant;
9use servo_url::ServoUrl;
10use time::Duration;
11
12use super::performanceentry::{EntryType, PerformanceEntry};
13use crate::dom::bindings::codegen::Bindings::LargestContentfulPaintBinding::LargestContentfulPaintMethods;
14use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
15use crate::dom::bindings::reflector::DomGlobal;
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::element::Element;
19use crate::dom::globalscope::GlobalScope;
20
21#[dom_struct]
22pub(crate) struct LargestContentfulPaint {
23    entry: PerformanceEntry,
24    #[no_trace]
25    load_time: CrossProcessInstant,
26    #[no_trace]
27    render_time: CrossProcessInstant,
28    size: usize,
29    url: DOMString,
30    element: Option<Dom<Element>>,
31}
32
33impl LargestContentfulPaint {
34    pub(crate) fn new_inherited(
35        render_time: CrossProcessInstant,
36        size: usize,
37        url: Option<ServoUrl>,
38    ) -> LargestContentfulPaint {
39        LargestContentfulPaint {
40            entry: PerformanceEntry::new_inherited(
41                DOMString::from(""),
42                EntryType::LargestContentfulPaint,
43                Some(render_time),
44                Duration::ZERO,
45            ),
46            load_time: CrossProcessInstant::epoch(),
47            render_time,
48            size,
49            url: url.map(|u| DOMString::from(u.as_str())).unwrap_or_default(),
50            element: None,
51        }
52    }
53
54    pub(crate) fn new(
55        cx: &mut JSContext,
56        global: &GlobalScope,
57        render_time: CrossProcessInstant,
58        size: usize,
59        url: Option<ServoUrl>,
60    ) -> DomRoot<LargestContentfulPaint> {
61        reflect_dom_object_with_cx(
62            Box::new(LargestContentfulPaint::new_inherited(
63                render_time,
64                size,
65                url,
66            )),
67            global,
68            cx,
69        )
70    }
71}
72
73impl LargestContentfulPaintMethods<crate::DomTypeHolder> for LargestContentfulPaint {
74    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-loadtime>
75    fn LoadTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
76        self.global()
77            .performance(cx)
78            .to_dom_high_res_time_stamp(self.load_time)
79    }
80
81    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-rendertime>
82    fn RenderTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
83        self.global()
84            .performance(cx)
85            .to_dom_high_res_time_stamp(self.render_time)
86    }
87
88    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-size>
89    fn Size(&self) -> u32 {
90        self.size as u32
91    }
92
93    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-url>
94    fn Url(&self) -> DOMString {
95        self.url.clone()
96    }
97
98    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-element>
99    fn GetElement(&self) -> Option<DomRoot<Element>> {
100        self.element.as_ref().map(|element| element.as_rooted())
101    }
102}