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