Nacre/vendor/servo-script/dom/performance/largestcontentfulpaint.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept
the functions of the timers still waiting. Heavy-web reruns and cargo test still pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 20:38:47 -06:00

152 lines
5.3 KiB
Rust

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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 js::context::JSContext;
use layout_api::LCPCandidate;
use paint_api::display_list::PaintTimingInfo;
use script_bindings::reflector::reflect_dom_object;
use servo_base::cross_process_instant::CrossProcessInstant;
use time::Duration;
use super::performanceentry::{EntryType, PerformanceEntry};
use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use crate::dom::bindings::codegen::Bindings::LargestContentfulPaintBinding::LargestContentfulPaintMethods;
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope;
use crate::dom::node::Node;
#[dom_struct]
pub(crate) struct LargestContentfulPaint {
entry: PerformanceEntry,
#[no_trace]
load_time: Option<CrossProcessInstant>,
#[no_trace]
render_time: CrossProcessInstant,
size: usize,
url: DOMString,
element: Option<Dom<Element>>,
/// <https://www.w3.org/TR/paint-timing/#paint-timing-info>
#[no_trace]
paint_timing_info: PaintTimingInfo,
}
impl LargestContentfulPaint {
pub(crate) fn new_inherited(
candidate: &LCPCandidate,
element: Option<&Element>,
load_time: Option<CrossProcessInstant>,
paint_timing_info: PaintTimingInfo,
) -> LargestContentfulPaint {
// From: <https://www.w3.org/TR/largest-contentful-paint/#sec-largest-contentful-paint-interface>
//
// The renderTime attribute must return the default paint timestamp
// given this’s paint timing info.
let render_time = paint_timing_info.default_paint_timestamp();
LargestContentfulPaint {
entry: PerformanceEntry::new_inherited(
DOMString::new(),
EntryType::LargestContentfulPaint,
Some(render_time),
Duration::ZERO,
),
load_time,
render_time,
size: candidate.area,
url: candidate
.url
.as_ref()
.map(|url| DOMString::from(url.as_str()))
.unwrap_or_default(),
element: element.map(Dom::from_ref),
paint_timing_info,
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
candidate: &LCPCandidate,
element: Option<&Element>,
load_time: Option<CrossProcessInstant>,
paint_timing_info: PaintTimingInfo,
) -> DomRoot<LargestContentfulPaint> {
reflect_dom_object(
cx,
Box::new(LargestContentfulPaint::new_inherited(
candidate,
element,
load_time,
paint_timing_info,
)),
global,
)
}
}
impl LargestContentfulPaintMethods<crate::DomTypeHolder> for LargestContentfulPaint {
/// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-loadtime>
fn LoadTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
self.global()
.performance(cx)
.maybe_to_dom_high_res_time_stamp(self.load_time)
}
/// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-rendertime>
fn RenderTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
self.global()
.performance(cx)
.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-id>
fn Id(&self) -> DOMString {
self.GetElement()
.map(|element| element.Id())
.unwrap_or_default()
}
/// <https://www.w3.org/TR/largest-contentful-paint/#dom-largestcontentfulpaint-element>
fn GetElement(&self) -> Option<DomRoot<Element>> {
self.element
.as_ref()
.filter(|element| {
element
.upcast::<Node>()
.is_connected_with_browsing_context()
})
.map(|element| element.as_rooted())
}
/// <https://www.w3.org/TR/paint-timing/#dom-painttimingmixin-painttime>
fn PaintTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
self.global()
.performance(cx)
.to_dom_high_res_time_stamp(self.paint_timing_info.paint_time())
}
/// <https://www.w3.org/TR/paint-timing/#dom-painttimingmixin-presentationtime>
fn GetPresentationTime(&self, cx: &mut JSContext) -> Option<DOMHighResTimeStamp> {
Some(
self.global()
.performance(cx)
.maybe_to_dom_high_res_time_stamp(self.paint_timing_info.presentation_time()),
)
}
}