servo-script 0.1.3

A component of the servo web-engine.
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use dom_struct::dom_struct;
use servo_base::cross_process_instant::CrossProcessInstant;
use servo_url::ServoUrl;
use time::Duration;

use super::performanceentry::{EntryType, PerformanceEntry};
use crate::dom::bindings::codegen::Bindings::LargestContentfulPaintBinding::LargestContentfulPaintMethods;
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;

#[dom_struct]
pub(crate) struct LargestContentfulPaint {
    entry: PerformanceEntry,
    #[no_trace]
    load_time: CrossProcessInstant,
    #[no_trace]
    render_time: CrossProcessInstant,
    size: usize,
    url: DOMString,
    element: Option<DomRoot<Element>>,
}

impl LargestContentfulPaint {
    pub(crate) fn new_inherited(
        render_time: CrossProcessInstant,
        size: usize,
        url: Option<ServoUrl>,
    ) -> LargestContentfulPaint {
        LargestContentfulPaint {
            entry: PerformanceEntry::new_inherited(
                DOMString::from(""),
                EntryType::LargestContentfulPaint,
                Some(render_time),
                Duration::ZERO,
            ),
            load_time: CrossProcessInstant::epoch(),
            render_time,
            size,
            url: url.map(|u| DOMString::from(u.as_str())).unwrap_or_default(),
            element: None,
        }
    }

    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
    pub(crate) fn new(
        global: &GlobalScope,
        render_time: CrossProcessInstant,
        size: usize,
        url: Option<ServoUrl>,
        can_gc: CanGc,
    ) -> DomRoot<LargestContentfulPaint> {
        let entry = LargestContentfulPaint::new_inherited(render_time, size, url);
        reflect_dom_object(Box::new(entry), global, can_gc)
    }
}

impl LargestContentfulPaintMethods<crate::DomTypeHolder> for LargestContentfulPaint {
    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-loadtime>
    fn LoadTime(&self) -> DOMHighResTimeStamp {
        self.global()
            .performance()
            .to_dom_high_res_time_stamp(self.load_time)
    }

    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-rendertime>
    fn RenderTime(&self) -> DOMHighResTimeStamp {
        self.global()
            .performance()
            .to_dom_high_res_time_stamp(self.render_time)
    }

    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-size>
    fn Size(&self) -> u32 {
        self.size as u32
    }

    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-url>
    fn Url(&self) -> DOMString {
        self.url.clone()
    }

    /// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-element>
    fn GetElement(&self) -> Option<DomRoot<Element>> {
        self.element.clone()
    }
}