use std::cell::RefCell;
use dom_struct::dom_struct;
use js::context::JSContext;
use js::rust::Trace;
use script_bindings::domstring::TracedDOMString;
use script_bindings::inheritance::Castable;
use script_bindings::num::Finite;
use script_bindings::reflector::Reflector;
use servo_base::cross_process_instant::CrossProcessInstant;
use servo_url::ServoUrl;
use strum::VariantArray;
use time::Duration;
use super::performance::ToDOMHighResTimeStamp;
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
use crate::dom::bindings::codegen::Bindings::PerformanceEntryBinding::PerformanceEntryMethods;
use crate::dom::bindings::codegen::Bindings::PerformanceResourceTimingBinding::PerformanceResourceTimingMethods;
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::str::DOMString;
use crate::dom::performance::performanceresourcetiming::PerformanceResourceTiming;
#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq, VariantArray)]
pub(crate) enum EntryType {
ContainerTiming,
LargestContentfulPaint,
Mark,
Measure,
Navigation,
Paint,
Resource,
VisibilityState,
}
impl EntryType {
pub(crate) fn as_str(&self) -> &'static str {
match self {
EntryType::ContainerTiming => "container",
EntryType::Measure => "measure",
EntryType::Mark => "mark",
EntryType::LargestContentfulPaint => "largest-contentful-paint",
EntryType::Paint => "paint",
EntryType::Navigation => "navigation",
EntryType::Resource => "resource",
EntryType::VisibilityState => "visibility-state",
}
}
}
impl<'a> TryFrom<&'a str> for EntryType {
type Error = ();
fn try_from(value: &'a str) -> Result<EntryType, ()> {
Ok(match value {
"container" => EntryType::ContainerTiming,
"measure" => EntryType::Measure,
"mark" => EntryType::Mark,
"largest-contentful-paint" => EntryType::LargestContentfulPaint,
"paint" => EntryType::Paint,
"navigation" => EntryType::Navigation,
"resource" => EntryType::Resource,
"visibility-state" => EntryType::VisibilityState,
_ => return Err(()),
})
}
}
#[derive(MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) enum NameOrUrl {
Url(ServoUrl),
Name(TracedDOMString),
}
#[expect(unsafe_code)]
unsafe impl Trace for NameOrUrl {
unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
match self {
NameOrUrl::Url(_) => {},
NameOrUrl::Name(traced_domstring) => unsafe { traced_domstring.trace(trc) },
}
}
}
impl From<ServoUrl> for NameOrUrl {
fn from(value: ServoUrl) -> Self {
NameOrUrl::Url(value)
}
}
impl From<DOMString> for NameOrUrl {
fn from(value: DOMString) -> Self {
NameOrUrl::Name(value.traced())
}
}
#[dom_struct]
pub(crate) struct PerformanceEntry {
reflector_: Reflector,
name: RefCell<NameOrUrl>,
entry_type: EntryType,
#[no_trace]
start_time: Option<CrossProcessInstant>,
#[no_trace]
duration: Duration,
}
impl PerformanceEntry {
#[cfg_attr(
crown,
expect(
crown::unrooted_must_root,
reason = "Not rooting this value is fine, we do not have a `&mut JSContext`, hence, no GC can happen"
)
)]
pub(crate) fn new_inherited(
name_or_url: NameOrUrl,
entry_type: EntryType,
start_time: Option<CrossProcessInstant>,
duration: Duration,
) -> PerformanceEntry {
PerformanceEntry {
reflector_: Reflector::new(),
name: name_or_url.into(),
entry_type,
start_time,
duration,
}
}
pub(crate) fn entry_type(&self) -> EntryType {
self.entry_type
}
pub(crate) fn start_time(&self) -> Option<CrossProcessInstant> {
self.start_time
}
}
impl PerformanceEntryMethods<crate::DomTypeHolder> for PerformanceEntry {
fn Name(&self) -> DOMString {
{
if let NameOrUrl::Name(name) = &*self.name.borrow() {
return name.clone().root();
}
let mut name = self.name.borrow_mut();
if let NameOrUrl::Url(url) = &*name {
*name = NameOrUrl::Name(url.to_string().into());
}
}
self.Name()
}
fn EntryType(&self) -> DOMString {
DOMString::from(self.entry_type.as_str())
}
fn StartTime(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
self.global()
.performance(cx)
.maybe_to_dom_high_res_time_stamp(self.start_time)
}
fn Duration(&self, cx: &mut JSContext) -> DOMHighResTimeStamp {
if let Some(resource_timing) = self.downcast::<PerformanceResourceTiming>() {
Finite::new(*resource_timing.ResponseEnd(cx) - *self.StartTime(cx))
.unwrap_or(Finite::default())
} else {
self.duration.to_dom_high_res_time_stamp()
}
}
}