Skip to main content

paint_api/
largest_contentful_paint_candidate.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
5//! Definitions for Largest Contentful Paint Candidate and Largest Contentful Paint.
6
7use malloc_size_of_derive::MallocSizeOf;
8use serde::{Deserialize, Serialize};
9use servo_base::cross_process_instant::CrossProcessInstant;
10use servo_url::ServoUrl;
11
12/// Largest Contentful Paint Candidate, include image and block-level element containing text
13#[derive(Clone, Debug, Deserialize, Serialize)]
14pub struct LCPCandidate {
15    /// A unique identifier for this candidate.
16    pub id: LCPCandidateID,
17    /// The size of the visual area
18    pub area: usize,
19    /// The candidate's request URL
20    pub url: Option<ServoUrl>,
21}
22
23impl LCPCandidate {
24    pub fn new(id: LCPCandidateID, area: usize, url: Option<ServoUrl>) -> Self {
25        Self { id, area, url }
26    }
27}
28
29/// A unique identifier for an LCP candidate, generated at layout time.
30#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
31pub struct LCPCandidateID(pub u64);
32
33#[derive(Clone, Debug)]
34pub struct LargestContentfulPaint {
35    pub id: LCPCandidateID,
36    pub area: usize,
37    pub paint_time: CrossProcessInstant,
38    pub url: Option<ServoUrl>,
39}
40
41impl LargestContentfulPaint {
42    pub fn from(candidate: LCPCandidate, paint_time: CrossProcessInstant) -> Self {
43        Self {
44            id: candidate.id,
45            area: candidate.area,
46            paint_time,
47            url: candidate.url,
48        }
49    }
50}