diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs index bab5629225b..292650fc9aa 100644 --- a/moli-fetch/src/lib.rs +++ b/moli-fetch/src/lib.rs @@ -86,7 +86,7 @@ pub use request_policy::{is_bad_port, should_request_be_blocked_due_to_bad_port} pub use response::{ FetchResponseFilter, NegotiatedHttpVersion, NetworkRequestExtraInfo, NetworkResponseExtraInfo, RawResponse, RedirectInfo, RedirectSource, Response, ResponseBody, ResponseCacheState, - ResponseHead, ResponsePreloadState, + ResponseHead, ResponsePreloadState, resource_timing_content_encoding, }; pub use runtime::PendingStreamingRawResponse; pub use runtime::{ diff --git a/moli-fetch/src/response.rs b/moli-fetch/src/response.rs index 9b94279131b..1fc793e0f48 100644 --- a/moli-fetch/src/response.rs +++ b/moli-fetch/src/response.rs @@ -4,6 +4,46 @@ use url::Url; use crate::{StreamingHtmlResponse, StreamingRawResponse}; +/// Resource Timing exposes registered, supported content codings rather than +/// reflecting an arbitrary response header into the timing entry. +pub fn resource_timing_content_encoding(headers: &[(String, Vec)]) -> &'static str { + use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer}; + + let mut codings = Vec::new(); + for (_, value) in headers + .iter() + .filter(|(name, _)| name.eq_ignore_ascii_case("content-encoding")) + { + let value = crate::decode_header_value(value); + for part in crate::split_http_header_list(&value) { + if part.is_empty() { + codings.push(String::new()); + continue; + } + let mut tokenizer = HeaderFieldTokenizer::new(part); + let Some(coding) = tokenizer.consume_token(HeaderFieldTokenMode::Normal) else { + return ""; + }; + if !tokenizer.is_consumed() { + return ""; + } + codings.push(coding.to_ascii_lowercase()); + } + } + let [coding] = codings.as_slice() else { + return if codings.is_empty() { "" } else { "multiple" }; + }; + let version = curl::Version::get(); + match coding.as_str() { + "" => "", + "gzip" if version.feature_libz() => "gzip", + "deflate" if version.feature_libz() => "deflate", + "br" if version.feature_brotli() => "br", + "zstd" if version.feature_zstd() => "zstd", + _ => "@unknown", + } +} + /// Fetch's response cache state, retained separately from the legacy /// `from_cache` flag so Resource Timing can distinguish a 304 validation from /// a response served without a network request. @@ -819,3 +859,36 @@ pub struct RedirectInfo { pub from_cache: bool, pub negotiated_http_version: Option, } + +#[cfg(test)] +mod resource_timing_tests { + use super::resource_timing_content_encoding; + + #[test] + fn content_encoding_reports_supported_codings_without_reflecting_private_values() { + for (values, expected) in [ + (vec![], ""), + (vec![""], ""), + (vec!["gZip "], "gzip"), + (vec!["deflate"], "deflate"), + (vec!["br"], "br"), + (vec!["gzip, deflate, Apple"], "multiple"), + (vec!["gzip, "], "multiple"), + (vec!["gzip", "deflate"], "multiple"), + (vec!["identity"], "@unknown"), + (vec!["private-user-codec"], "@unknown"), + (vec!["\"gzip\""], ""), + (vec!["bad token"], ""), + ] { + let headers = values + .iter() + .map(|value| ("Content-Encoding".to_owned(), value.as_bytes().to_vec())) + .collect::>(); + assert_eq!( + resource_timing_content_encoding(&headers), + expected, + "{values:?}" + ); + } + } +} diff --git a/moli-page-types/src/lib.rs b/moli-page-types/src/lib.rs index fb5fde8c8df..a6ef2b05532 100644 --- a/moli-page-types/src/lib.rs +++ b/moli-page-types/src/lib.rs @@ -3334,6 +3334,10 @@ pub struct ChildFrameDocumentNetworkSnapshot { pub cache_state: moli_fetch::ResponseCacheState, #[serde(skip)] pub preload_state: moli_fetch::ResponsePreloadState, + /// Body metadata visible to the initiating document, before redirect + /// provenance is discarded. Protocol headers and bytes remain unfiltered. + #[serde(skip)] + pub resource_timing_body_visible: bool, } /// A completed child main-resource request whose Network facts remain diff --git a/moli-protocol/src/domains/page/producer_tests.rs b/moli-protocol/src/domains/page/producer_tests.rs index 196fbfdc155..d744988da7c 100644 --- a/moli-protocol/src/domains/page/producer_tests.rs +++ b/moli-protocol/src/domains/page/producer_tests.rs @@ -1939,6 +1939,7 @@ async fn child_frame_activity_emits_document_network_events_from_prepared_load() from_cache: true, cache_state: moli_fetch::ResponseCacheState::Local, preload_state: Default::default(), + resource_timing_body_visible: false, }), }], "https://example.test".to_owned(), @@ -2071,6 +2072,7 @@ async fn stale_child_document_response_emits_network_without_navigation_or_lifec from_cache: false, cache_state: Default::default(), preload_state: Default::default(), + resource_timing_body_visible: false, }, }], Vec::new(), @@ -2173,6 +2175,7 @@ async fn child_document_network_without_body_records_known_no_data() { from_cache: false, cache_state: Default::default(), preload_state: Default::default(), + resource_timing_body_visible: false, }; let mut background_events = Vec::new(); diff --git a/moli-renderer-v8/src/context_bootstrap/performance_runtime.rs b/moli-renderer-v8/src/context_bootstrap/performance_runtime.rs index 7bd87ad38c6..438f47ce601 100644 --- a/moli-renderer-v8/src/context_bootstrap/performance_runtime.rs +++ b/moli-renderer-v8/src/context_bootstrap/performance_runtime.rs @@ -80,6 +80,22 @@ pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_RESPONSE_STATUS_SLOT "__moliPerformanceResourceResponseStatus"; pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_CONTENT_TYPE_SLOT: &str = "__moliPerformanceResourceContentType"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_DELIVERY_TYPE_SLOT: &str = + "__moliPerformanceResourceDeliveryType"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_FINAL_RESPONSE_HEADERS_START_SLOT: + &str = "__moliPerformanceResourceFinalResponseHeadersStart"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_FIRST_INTERIM_RESPONSE_START_SLOT: + &str = "__moliPerformanceResourceFirstInterimResponseStart"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_WORKER_ROUTER_EVALUATION_START_SLOT: + &str = "__moliPerformanceResourceWorkerRouterEvaluationStart"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_WORKER_CACHE_LOOKUP_START_SLOT: &str = + "__moliPerformanceResourceWorkerCacheLookupStart"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_WORKER_MATCHED_ROUTER_SOURCE_SLOT: + &str = "__moliPerformanceResourceWorkerMatchedRouterSource"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_WORKER_FINAL_ROUTER_SOURCE_SLOT: &str = + "__moliPerformanceResourceWorkerFinalRouterSource"; +pub(in crate::context_bootstrap) const PERFORMANCE_RESOURCE_CONTENT_ENCODING_SLOT: &str = + "__moliPerformanceResourceContentEncoding"; pub(in crate::context_bootstrap) const PERFORMANCE_EVENT_COUNTS_SLOT: &str = "__moliPerformanceEventCounts"; pub(in crate::context_bootstrap) const PERFORMANCE_EVENT_COUNTS_VALUES_SLOT: &str = @@ -307,6 +323,7 @@ pub(crate) struct ResourcePerformanceEntry { start_unix_millis: Option, end_unix_millis: Option, response_start_unix_millis: Option, + request_start_unix_millis: Option, next_hop_protocol: String, transfer_size: f64, encoded_body_size: f64, @@ -314,6 +331,10 @@ pub(crate) struct ResourcePerformanceEntry { render_blocking_status: String, response_status: f64, content_type: String, + delivery_type: String, + content_encoding: String, + final_response_headers_start_unix_millis: Option, + first_interim_response_start_unix_millis: Option, } impl ResourcePerformanceEntry { @@ -427,6 +448,7 @@ impl ResourcePerformanceEntry { start_unix_millis, end_unix_millis: None, response_start_unix_millis: None, + request_start_unix_millis: None, next_hop_protocol: String::new(), transfer_size, encoded_body_size: body_size, @@ -436,6 +458,20 @@ impl ResourcePerformanceEntry { render_blocking_status: "non-blocking".to_owned(), response_status: f64::from(response_status), content_type, + delivery_type: if cache_state == moli_fetch::ResponseCacheState::None { + "" + } else { + "cache" + } + .to_owned(), + content_encoding: if body_size > 0.0 { + moli_fetch::resource_timing_content_encoding(response_headers) + } else { + "" + } + .to_owned(), + final_response_headers_start_unix_millis: None, + first_interim_response_start_unix_millis: None, } } @@ -445,7 +481,7 @@ impl ResourcePerformanceEntry { start_unix_millis: Option, network: &crate::protocol_types::ChildFrameDocumentNetworkSnapshot, ) -> Self { - Self::from_response_parts( + let mut entry = Self::from_response_parts( name, initiator_type, start_unix_millis, @@ -454,7 +490,11 @@ impl ResourcePerformanceEntry { network.encoded_data_length as f64, network.cache_state, network.preload_state.clone(), - ) + ); + if !network.resource_timing_body_visible { + entry.content_encoding.clear(); + } + entry } pub(crate) fn from_network_failure( @@ -477,6 +517,7 @@ impl ResourcePerformanceEntry { start_unix_millis, end_unix_millis: None, response_start_unix_millis: None, + request_start_unix_millis: None, next_hop_protocol: String::new(), transfer_size: 0.0, encoded_body_size: 0.0, @@ -484,6 +525,10 @@ impl ResourcePerformanceEntry { render_blocking_status: "non-blocking".to_owned(), response_status: 0.0, content_type: String::new(), + delivery_type: String::new(), + content_encoding: String::new(), + final_response_headers_start_unix_millis: None, + first_interim_response_start_unix_millis: None, } } } @@ -541,6 +586,12 @@ fn append_resource_performance_entry<'s>( entry.response_status, &entry.content_type, ); + entries::initialize_resource_timing_response_metadata( + scope, + resource, + &entry.delivery_type, + &entry.content_encoding, + ); if let Some(end_time) = end_time { set_performance_entry_slot_number( scope, @@ -564,6 +615,30 @@ fn append_resource_performance_entry<'s>( response_start, ); } + for (slot, value) in [ + ( + PERFORMANCE_RESOURCE_REQUEST_START_SLOT, + entry.request_start_unix_millis, + ), + ( + PERFORMANCE_RESOURCE_FINAL_RESPONSE_HEADERS_START_SLOT, + entry.final_response_headers_start_unix_millis, + ), + ( + PERFORMANCE_RESOURCE_FIRST_INTERIM_RESPONSE_START_SLOT, + entry.first_interim_response_start_unix_millis, + ), + ] { + if let Some(value) = value { + set_performance_entry_slot_number( + scope, + resource, + slot, + moli_time::coarsened_dom_time_millis(value - time_origin) + .clamp(start_time, end_time), + ); + } + } if let Some(protocol) = v8_string(scope, &entry.next_hop_protocol) { set_private_value( scope, diff --git a/moli-renderer-v8/src/context_bootstrap/performance_runtime/entries.rs b/moli-renderer-v8/src/context_bootstrap/performance_runtime/entries.rs index 7e793fcd3c0..390009e7fa6 100644 --- a/moli-renderer-v8/src/context_bootstrap/performance_runtime/entries.rs +++ b/moli-renderer-v8/src/context_bootstrap/performance_runtime/entries.rs @@ -123,6 +123,29 @@ struct PerformanceResourceTimingSlotDeclaration { #[webapi(slot = PERFORMANCE_RESOURCE_CONTENT_TYPE_SLOT)] content_type: String, + #[webapi(slot = PERFORMANCE_RESOURCE_DELIVERY_TYPE_SLOT, constructor_default = "")] + delivery_type: &'static str, + + #[webapi(slot = PERFORMANCE_RESOURCE_FINAL_RESPONSE_HEADERS_START_SLOT, constructor_default)] + final_response_headers_start: f64, + + #[webapi(slot = PERFORMANCE_RESOURCE_FIRST_INTERIM_RESPONSE_START_SLOT, constructor_default)] + first_interim_response_start: f64, + + #[webapi(slot = PERFORMANCE_RESOURCE_WORKER_ROUTER_EVALUATION_START_SLOT, constructor_default)] + worker_router_evaluation_start: f64, + + #[webapi(slot = PERFORMANCE_RESOURCE_WORKER_CACHE_LOOKUP_START_SLOT, constructor_default)] + worker_cache_lookup_start: f64, + + #[webapi(slot = PERFORMANCE_RESOURCE_WORKER_MATCHED_ROUTER_SOURCE_SLOT, constructor_default = "")] + worker_matched_router_source: &'static str, + + #[webapi(slot = PERFORMANCE_RESOURCE_WORKER_FINAL_ROUTER_SOURCE_SLOT, constructor_default = "")] + worker_final_router_source: &'static str, + + #[webapi(slot = PERFORMANCE_RESOURCE_CONTENT_ENCODING_SLOT, constructor_default = "")] + content_encoding: &'static str, } #[derive(WebApiObject)] @@ -135,6 +158,7 @@ struct PerformanceResourceTimingJsonSnapshotDeclaration { duration: f64, navigation_id: f64, initiator_type: String, + delivery_type: String, next_hop_protocol: String, worker_start: f64, redirect_start: f64, @@ -146,14 +170,21 @@ struct PerformanceResourceTimingJsonSnapshotDeclaration { connect_end: f64, secure_connection_start: f64, request_start: f64, + final_response_headers_start: f64, + first_interim_response_start: f64, response_start: f64, response_end: f64, + worker_router_evaluation_start: f64, + worker_cache_lookup_start: f64, + worker_matched_router_source: String, + worker_final_router_source: String, transfer_size: f64, encoded_body_size: f64, decoded_body_size: f64, - render_blocking_status: String, response_status: f64, + render_blocking_status: String, content_type: String, + content_encoding: String, } impl PerformanceResourceTimingJsonSnapshotDeclaration { @@ -169,6 +200,7 @@ impl PerformanceResourceTimingJsonSnapshotDeclaration { performance_entry_slot_number(scope, entry, PERFORMANCE_ENTRY_DURATION_SLOT)?, performance_entry_slot_number(scope, entry, PERFORMANCE_ENTRY_NAVIGATION_ID_SLOT)?, performance_entry_slot_string(scope, entry, PERFORMANCE_RESOURCE_INITIATOR_TYPE_SLOT)?, + performance_entry_slot_string(scope, entry, PERFORMANCE_RESOURCE_DELIVERY_TYPE_SLOT)?, performance_entry_slot_string( scope, entry, @@ -196,8 +228,38 @@ impl PerformanceResourceTimingJsonSnapshotDeclaration { PERFORMANCE_RESOURCE_SECURE_CONNECTION_START_SLOT, )?, performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_REQUEST_START_SLOT)?, + performance_entry_slot_number( + scope, + entry, + PERFORMANCE_RESOURCE_FINAL_RESPONSE_HEADERS_START_SLOT, + )?, + performance_entry_slot_number( + scope, + entry, + PERFORMANCE_RESOURCE_FIRST_INTERIM_RESPONSE_START_SLOT, + )?, performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_RESPONSE_START_SLOT)?, performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_RESPONSE_END_SLOT)?, + performance_entry_slot_number( + scope, + entry, + PERFORMANCE_RESOURCE_WORKER_ROUTER_EVALUATION_START_SLOT, + )?, + performance_entry_slot_number( + scope, + entry, + PERFORMANCE_RESOURCE_WORKER_CACHE_LOOKUP_START_SLOT, + )?, + performance_entry_slot_string( + scope, + entry, + PERFORMANCE_RESOURCE_WORKER_MATCHED_ROUTER_SOURCE_SLOT, + )?, + performance_entry_slot_string( + scope, + entry, + PERFORMANCE_RESOURCE_WORKER_FINAL_ROUTER_SOURCE_SLOT, + )?, performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_TRANSFER_SIZE_SLOT)?, performance_entry_slot_number( scope, @@ -209,13 +271,18 @@ impl PerformanceResourceTimingJsonSnapshotDeclaration { entry, PERFORMANCE_RESOURCE_DECODED_BODY_SIZE_SLOT, )?, + performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_RESPONSE_STATUS_SLOT)?, performance_entry_slot_string( scope, entry, PERFORMANCE_RESOURCE_RENDER_BLOCKING_STATUS_SLOT, )?, - performance_entry_slot_number(scope, entry, PERFORMANCE_RESOURCE_RESPONSE_STATUS_SLOT)?, performance_entry_slot_string(scope, entry, PERFORMANCE_RESOURCE_CONTENT_TYPE_SLOT)?, + performance_entry_slot_string( + scope, + entry, + PERFORMANCE_RESOURCE_CONTENT_ENCODING_SLOT, + )?, )) } } @@ -310,133 +377,189 @@ struct PerformanceResourceTimingPrototypeAccessorsDeclaration { data = callback_data_index_value(scope, 1), enumerable )] - next_hop_protocol: (), + delivery_type: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 2), enumerable )] - worker_start: (), + next_hop_protocol: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 3), enumerable )] - redirect_start: (), + worker_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 4), enumerable )] - redirect_end: (), + redirect_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 5), enumerable )] - fetch_start: (), + redirect_end: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 6), enumerable )] - domain_lookup_start: (), + fetch_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 7), enumerable )] - domain_lookup_end: (), + domain_lookup_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 8), enumerable )] - connect_start: (), + domain_lookup_end: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 9), enumerable )] - connect_end: (), + connect_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 10), enumerable )] - secure_connection_start: (), + connect_end: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 11), enumerable )] - request_start: (), + secure_connection_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 12), enumerable )] - response_start: (), + request_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 13), enumerable )] - response_end: (), + final_response_headers_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 14), enumerable )] - transfer_size: (), + first_interim_response_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 15), enumerable )] - encoded_body_size: (), + response_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 16), enumerable )] - decoded_body_size: (), + response_end: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 17), enumerable )] - render_blocking_status: (), + worker_router_evaluation_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 18), enumerable )] - response_status: (), + worker_cache_lookup_start: (), #[webapi( accessor_property, getter = performance_resource_timing_attribute_getter_callback, data = callback_data_index_value(scope, 19), enumerable )] + worker_matched_router_source: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 20), + enumerable + )] + worker_final_router_source: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 21), + enumerable + )] + transfer_size: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 22), + enumerable + )] + encoded_body_size: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 23), + enumerable + )] + decoded_body_size: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 24), + enumerable + )] + response_status: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 25), + enumerable + )] + render_blocking_status: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 26), + enumerable + )] content_type: (), + #[webapi( + accessor_property, + getter = performance_resource_timing_attribute_getter_callback, + data = callback_data_index_value(scope, 27), + enumerable + )] + content_encoding: (), } #[derive(WebApiFunctionTemplate)] @@ -679,6 +802,7 @@ const PERFORMANCE_ENTRY_DETAIL_ATTRIBUTE_SLOTS: &[&str] = &[PERFORMANCE_ENTRY_DE const PERFORMANCE_RESOURCE_TIMING_ATTRIBUTE_SLOTS: &[&str] = &[ PERFORMANCE_RESOURCE_INITIATOR_TYPE_SLOT, + PERFORMANCE_RESOURCE_DELIVERY_TYPE_SLOT, PERFORMANCE_RESOURCE_NEXT_HOP_PROTOCOL_SLOT, PERFORMANCE_RESOURCE_WORKER_START_SLOT, PERFORMANCE_RESOURCE_REDIRECT_START_SLOT, @@ -690,14 +814,21 @@ const PERFORMANCE_RESOURCE_TIMING_ATTRIBUTE_SLOTS: &[&str] = &[ PERFORMANCE_RESOURCE_CONNECT_END_SLOT, PERFORMANCE_RESOURCE_SECURE_CONNECTION_START_SLOT, PERFORMANCE_RESOURCE_REQUEST_START_SLOT, + PERFORMANCE_RESOURCE_FINAL_RESPONSE_HEADERS_START_SLOT, + PERFORMANCE_RESOURCE_FIRST_INTERIM_RESPONSE_START_SLOT, PERFORMANCE_RESOURCE_RESPONSE_START_SLOT, PERFORMANCE_RESOURCE_RESPONSE_END_SLOT, + PERFORMANCE_RESOURCE_WORKER_ROUTER_EVALUATION_START_SLOT, + PERFORMANCE_RESOURCE_WORKER_CACHE_LOOKUP_START_SLOT, + PERFORMANCE_RESOURCE_WORKER_MATCHED_ROUTER_SOURCE_SLOT, + PERFORMANCE_RESOURCE_WORKER_FINAL_ROUTER_SOURCE_SLOT, PERFORMANCE_RESOURCE_TRANSFER_SIZE_SLOT, PERFORMANCE_RESOURCE_ENCODED_BODY_SIZE_SLOT, PERFORMANCE_RESOURCE_DECODED_BODY_SIZE_SLOT, - PERFORMANCE_RESOURCE_RENDER_BLOCKING_STATUS_SLOT, PERFORMANCE_RESOURCE_RESPONSE_STATUS_SLOT, + PERFORMANCE_RESOURCE_RENDER_BLOCKING_STATUS_SLOT, PERFORMANCE_RESOURCE_CONTENT_TYPE_SLOT, + PERFORMANCE_RESOURCE_CONTENT_ENCODING_SLOT, ]; pub(in crate::context_bootstrap) fn performance_get_entries_callback<'s>( @@ -958,3 +1089,19 @@ pub(in crate::context_bootstrap) fn initialize_resource_timing_slots<'s>( .initialize(scope, entry) .expect("PerformanceResourceTiming slot declaration should initialize"); } + +pub(super) fn initialize_resource_timing_response_metadata<'s>( + scope: &mut v8::PinScope<'s, '_>, + entry: v8::Local<'s, v8::Object>, + delivery_type: &str, + content_encoding: &str, +) { + for (slot, value) in [ + (PERFORMANCE_RESOURCE_DELIVERY_TYPE_SLOT, delivery_type), + (PERFORMANCE_RESOURCE_CONTENT_ENCODING_SLOT, content_encoding), + ] { + if let Some(value) = v8_string(scope, value) { + set_private_value(scope, entry, slot, value.into()); + } + } +} diff --git a/moli-renderer-v8/src/context_bootstrap/performance_runtime/fetch_timing.rs b/moli-renderer-v8/src/context_bootstrap/performance_runtime/fetch_timing.rs index 53948a77f3e..c7aec47f18f 100644 --- a/moli-renderer-v8/src/context_bootstrap/performance_runtime/fetch_timing.rs +++ b/moli-renderer-v8/src/context_bootstrap/performance_runtime/fetch_timing.rs @@ -12,7 +12,16 @@ pub(crate) struct FetchResourceTiming { initiator_type: &'static str, started: Instant, start_unix_millis: f64, - response_start_unix_millis: Option, + response_timing: Option>, +} + +#[derive(Default)] +struct FetchResourceResponseTiming { + request_start: Option, + body_bytes_received: bool, + final_headers_start: Option, + first_interim_start: Option, + end: Option, } impl FetchResourceTiming { @@ -25,7 +34,7 @@ impl FetchResourceTiming { initiator_type, started: Instant::now(), start_unix_millis: moli_time::unix_epoch_millis(), - response_start_unix_millis: None, + response_timing: None, } } @@ -34,12 +43,57 @@ impl FetchResourceTiming { } pub(crate) fn response_started(&mut self) { - self.response_start_unix_millis = Some(self.now()); + self.response_timing = Some(Box::new(FetchResourceResponseTiming { + final_headers_start: Some(self.now()), + ..Default::default() + })); } /// Use the native header observation even when JS delayed its delivery. pub(crate) fn response_started_at(&mut self, start: Option) { - self.response_start_unix_millis = start.map(|instant| self.unix_millis(instant)); + self.response_timing = Some(Box::new(FetchResourceResponseTiming { + final_headers_start: start.map(|instant| self.unix_millis(instant)), + ..Default::default() + })); + } + + pub(crate) fn observe_native_timing(&mut self, native: moli_fetch::FetchResponseTiming) { + let request_start = native + .request_started_at() + .map(|value| self.unix_millis(value)); + let final_start = native + .response_started_at() + .map(|value| self.unix_millis(value)); + let first_interim = native + .first_interim_response_started_at() + .map(|value| self.unix_millis(value)); + let end = native + .response_end_at() + .map(|value| self.unix_millis(value)); + if request_start.is_none() + && final_start.is_none() + && first_interim.is_none() + && end.is_none() + { + return; + } + let timing = self.response_timing.get_or_insert_with(Default::default); + timing.request_start = request_start; + timing.body_bytes_received = native.body_bytes_received(); + if final_start.is_some() { + timing.final_headers_start = final_start; + timing.first_interim_start = first_interim; + } + if end.is_some() { + timing.end = end; + } + } + + fn end(&self) -> f64 { + self.response_timing + .as_ref() + .and_then(|timing| timing.end) + .unwrap_or_else(|| self.now()) } pub(crate) fn completed_at( @@ -65,7 +119,7 @@ impl FetchResourceTiming { self.initiator_type, Some(self.start_unix_millis), ); - entry.end_unix_millis = Some(self.now()); + entry.end_unix_millis = Some(self.end()); entry } @@ -85,9 +139,19 @@ impl FetchResourceTiming { head, if readable { body_size } else { 0 }, ); + if readable + && self + .response_timing + .as_ref() + .is_some_and(|timing| timing.body_bytes_received) + { + entry.content_encoding = + moli_fetch::resource_timing_content_encoding(&head.headers).to_owned(); + } if !readable { entry.response_status = 0.0; entry.content_type.clear(); + entry.content_encoding.clear(); } self.finish_response(&mut entry, origin, head); entry @@ -116,6 +180,7 @@ impl FetchResourceTiming { if head.url_list().has_cross_origin_url(origin) { entry.response_status = 0.0; entry.content_type.clear(); + entry.content_encoding.clear(); } entry } @@ -140,7 +205,7 @@ impl FetchResourceTiming { origin: &WebOrigin, head: &ResponseHead, ) -> bool { - entry.end_unix_millis = Some(self.now()); + entry.end_unix_millis = Some(self.end()); // CORS controls response body information; TAO separately controls // connection timing, transfer size, and the cache state. let allowed = timing_allow_passes(origin, head); @@ -153,8 +218,17 @@ impl FetchResourceTiming { } else { 0.0 }; + if !allowed { + entry.delivery_type.clear(); + } if allowed { - entry.response_start_unix_millis = self.response_start_unix_millis; + if let Some(timing) = self.response_timing.as_ref() { + entry.request_start_unix_millis = timing.request_start; + entry.final_response_headers_start_unix_millis = timing.final_headers_start; + entry.first_interim_response_start_unix_millis = timing.first_interim_start; + entry.response_start_unix_millis = + timing.first_interim_start.or(timing.final_headers_start); + } entry.next_hop_protocol = head .negotiated_http_version .map(|version| version.protocol_name().to_owned()) diff --git a/moli-renderer-v8/src/context_bootstrap/performance_runtime/window_state/pending.rs b/moli-renderer-v8/src/context_bootstrap/performance_runtime/window_state/pending.rs index ef4b4643223..065a8d322b9 100644 --- a/moli-renderer-v8/src/context_bootstrap/performance_runtime/window_state/pending.rs +++ b/moli-renderer-v8/src/context_bootstrap/performance_runtime/window_state/pending.rs @@ -68,7 +68,7 @@ pub(in crate::context_bootstrap::performance_runtime) fn queue_pending_resource_ ) { let global = scope.get_current_context().global(scope); let entries = ensure_private_array(scope, global, WINDOW_PERFORMANCE_PENDING_RESOURCES_SLOT, 0); - let record = v8::Array::new(scope, 12); + let record = v8::Array::new(scope, 17); let Some(name) = v8_string(scope, &entry.name) else { return; }; @@ -84,6 +84,12 @@ pub(in crate::context_bootstrap::performance_runtime) fn queue_pending_resource_ let Some(next_hop_protocol) = v8_string(scope, &entry.next_hop_protocol) else { return; }; + let Some(delivery_type) = v8_string(scope, &entry.delivery_type) else { + return; + }; + let Some(content_encoding) = v8_string(scope, &entry.content_encoding) else { + return; + }; let start_time: v8::Local<'_, v8::Value> = entry .start_unix_millis .map(|value| v8::Number::new(scope, value).into()) @@ -107,6 +113,20 @@ pub(in crate::context_bootstrap::performance_runtime) fn queue_pending_resource_ .map(|value| v8::Number::new(scope, value).into()) .unwrap_or_else(|| v8::undefined(scope).into()), next_hop_protocol.into(), + delivery_type.into(), + content_encoding.into(), + entry + .final_response_headers_start_unix_millis + .map(|value| v8::Number::new(scope, value).into()) + .unwrap_or_else(|| v8::undefined(scope).into()), + entry + .first_interim_response_start_unix_millis + .map(|value| v8::Number::new(scope, value).into()) + .unwrap_or_else(|| v8::undefined(scope).into()), + entry + .request_start_unix_millis + .map(|value| v8::Number::new(scope, value).into()) + .unwrap_or_else(|| v8::undefined(scope).into()), ]; for (index, value) in fields.into_iter().enumerate() { let _ = record.set_index(scope, index as u32, value); @@ -150,7 +170,21 @@ pub(in crate::context_bootstrap::performance_runtime) fn take_pending_resource_e .get_index(scope, 10) .filter(|value| !value.is_undefined()) .and_then(|value| value.number_value(scope)), + request_start_unix_millis: record + .get_index(scope, 16) + .filter(|value| !value.is_undefined()) + .and_then(|value| value.number_value(scope)), next_hop_protocol: array_string(scope, record, 11)?, + delivery_type: array_string(scope, record, 12)?, + content_encoding: array_string(scope, record, 13)?, + final_response_headers_start_unix_millis: record + .get_index(scope, 14) + .filter(|value| !value.is_undefined()) + .and_then(|value| value.number_value(scope)), + first_interim_response_start_unix_millis: record + .get_index(scope, 15) + .filter(|value| !value.is_undefined()) + .and_then(|value| value.number_value(scope)), }) }) .collect() @@ -288,3 +322,44 @@ fn array_number( ) -> Option { values.get_index(scope, index)?.number_value(scope) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn pending_resource_entries_preserve_response_metadata_and_header_boundaries() { + crate::ensure_v8_for_test(); + let mut isolate = v8::Isolate::new(Default::default()); + let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate)); + let scope = &mut scope.init(); + let context = v8::Context::new(scope, Default::default()); + let scope = &mut v8::ContextScope::new(scope, context); + let window = context.global(scope); + let mut entry = ResourcePerformanceEntry::from_network_failure( + "https://pending-resource.test/a", + "fetch", + Some(1000.0), + ); + entry.delivery_type = "cache".to_owned(); + entry.content_encoding = "gzip".to_owned(); + entry.request_start_unix_millis = Some(1000.5); + entry.first_interim_response_start_unix_millis = Some(1001.0); + entry.final_response_headers_start_unix_millis = Some(1002.0); + queue_pending_resource_entry(scope, entry); + let entries = take_pending_resource_entries(scope, window); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].delivery_type, "cache"); + assert_eq!(entries[0].content_encoding, "gzip"); + assert_eq!(entries[0].request_start_unix_millis, Some(1000.5)); + assert_eq!( + entries[0].first_interim_response_start_unix_millis, + Some(1001.0) + ); + assert_eq!( + entries[0].final_response_headers_start_unix_millis, + Some(1002.0) + ); + assert!(take_pending_resource_entries(scope, window).is_empty()); + } +} diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs index e21ec9ebab5..aec9853b8d1 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/loads.rs @@ -206,6 +206,11 @@ impl JsContextHost { } let encoding_context = self.child_document_encoding_context(handle); let task_resource_loader = resource_loader.clone(); + let timing_origin = self + .owner_dispatch_scope_for_node(handle) + .and_then(|owner| self.window_access_origin_for_dispatch_scope(owner)) + .and_then(|origin| url::Url::parse(&origin.serialized_origin()).ok()) + .map(|url| moli_url::WebOrigin::from_url(&url)); resource_loader.spawn_resource_task(async move { let result = async { let request = configure_child_document_navigation_request( @@ -242,6 +247,7 @@ impl JsContextHost { request_headers, head, body, + timing_origin.as_ref(), &encoding_context, ); } @@ -256,6 +262,7 @@ impl JsContextHost { request_headers, head, body, + timing_origin.as_ref(), &encoding_context, ) } @@ -846,6 +853,7 @@ fn child_document_load_outcome_from_response( request_headers: Vec<(String, String)>, head: moli_fetch::ResponseHead, body: moli_fetch::ResponseBody, + timing_origin: Option<&moli_url::WebOrigin>, encoding_context: &super::encoding::ChildDocumentEncodingContext, ) -> Result { if child_document_response_should_ignore_navigation(head.status, &head.headers) { @@ -897,6 +905,8 @@ fn child_document_load_outcome_from_response( from_cache: head.from_cache, cache_state: head.cache_state, preload_state: head.preload_state.clone(), + resource_timing_body_visible: timing_origin + .is_some_and(|origin| !head.url_list().has_cross_origin_url(origin)), }), }, ))) @@ -948,6 +958,81 @@ fn child_document_load_request( mod tests { use super::*; + #[test] + fn child_document_resource_timing_filters_cross_origin_redirects_before_discarding_them() { + let home = url::Url::parse("https://example.test/child").unwrap(); + let remote = url::Url::parse("https://other.test/child").unwrap(); + let origin = moli_url::WebOrigin::from_url(&home); + for (final_url, hops, expected) in [ + (home.clone(), vec![], true), + (remote.clone(), vec![], false), + ( + home.clone(), + vec![home.clone(), remote, home.clone()], + false, + ), + ] { + let head = moli_fetch::ResponseHead { + final_url, + status: 200, + status_text: None, + headers: vec![("Content-Encoding".to_owned(), b"gzip".to_vec())], + request_cookie_report: None, + cookie_set_reports: Vec::new(), + redirected: !hops.is_empty(), + redirect_chain: hops + .windows(2) + .map(|pair| moli_fetch::RedirectInfo { + source: moli_fetch::RedirectSource::Network, + from_url: pair[0].clone(), + to_url: pair[1].clone(), + status: 302, + headers: Vec::new(), + network_extra_info_available: false, + request_extra_info: None, + response_extra_info: None, + redirect_has_extra_info: false, + request_cookie_report: None, + cookie_set_reports: Vec::new(), + from_cache: false, + negotiated_http_version: None, + }) + .collect(), + from_cache: false, + cache_state: Default::default(), + preload_state: Default::default(), + negotiated_http_version: None, + }; + for timing_origin in [Some(&origin), None] { + let outcome = child_document_load_outcome_from_response( + home.as_str().to_owned(), + "GET".to_owned(), + Vec::new(), + head.clone(), + moli_fetch::ResponseBody::materialized_bytes(b"body".to_vec()), + timing_origin, + &super::super::encoding::ChildDocumentEncodingContext::default(), + ) + .unwrap(); + let ChildDocumentLoadOutcome::Loaded(document) = outcome else { + panic!("child should load") + }; + let network = document.document_network.unwrap(); + assert_eq!( + network.resource_timing_body_visible, + expected && timing_origin.is_some() + ); + assert_eq!(network.response_headers, head.headers); + assert!( + serde_json::to_value(&network) + .unwrap() + .get("resource_timing_body_visible") + .is_none() + ); + } + } + } + #[test] fn loaded_child_document_retains_exact_network_response_body() { let body_bytes = b"

child network body \xff

".to_vec(); @@ -970,6 +1055,7 @@ mod tests { negotiated_http_version: None, }, moli_fetch::ResponseBody::materialized_bytes(body_bytes.clone()), + None, &super::super::encoding::ChildDocumentEncodingContext::default(), ) .expect("child document response should load"); diff --git a/moli-renderer-v8/src/network/loads/registry.rs b/moli-renderer-v8/src/network/loads/registry.rs index 3f3e1132ff9..c7d5243705d 100644 --- a/moli-renderer-v8/src/network/loads/registry.rs +++ b/moli-renderer-v8/src/network/loads/registry.rs @@ -586,6 +586,15 @@ impl ResourceLoadLease { should_cancel } + pub(crate) fn response_timing(&self) -> Option { + self.registration + .lifecycle + .lock() + .cancel_handle + .as_ref() + .map(FetchCancelHandle::response_timing) + } + pub(crate) fn is_detached_keepalive(&self) -> bool { self.registration.lifecycle.lock().detached_keepalive } diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/child_document_completion.rs b/moli-renderer-v8/src/runtime/page_vm/tests/child_document_completion.rs index e41e172ef5d..895f5ae9017 100644 --- a/moli-renderer-v8/src/runtime/page_vm/tests/child_document_completion.rs +++ b/moli-renderer-v8/src/runtime/page_vm/tests/child_document_completion.rs @@ -741,6 +741,7 @@ pub(super) fn stale_loaded_completion( from_cache: false, cache_state: Default::default(), preload_state: Default::default(), + resource_timing_body_visible: false, }), }, ))), diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs index e03b8b7210b..aa1a8ff801e 100644 --- a/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs +++ b/moli-renderer-v8/src/script_vm/subresource_fetch/fetch_dispatch.rs @@ -690,7 +690,8 @@ impl ScriptVm { let continuation_started = moli_trace::cdp_runtime_trace_enabled().then(Instant::now); match pending.continuation { - PendingSubresourceContinuation::Fetch(fetch) => { + PendingSubresourceContinuation::Fetch(mut fetch) => { + if let Some(native) = pending.load.response_timing() { fetch.timing.observe_native_timing(native); } let redirect_mode = fetch.redirect_mode(); let body_size = observable_response.body_bytes().len(); let (mut head, body) = observable_response.into_body(); diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs b/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs index 81e9517f191..5946af28aa9 100644 --- a/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs +++ b/moli-renderer-v8/src/script_vm/subresource_fetch/streaming.rs @@ -678,6 +678,9 @@ impl ScriptVm { }; if let PendingSubresourceContinuation::Fetch(fetch) = &mut pending.continuation { fetch.timing.response_started(); + if let Some(native) = pending.load.response_timing() { + fetch.timing.observe_native_timing(native); + } } let trace_fields = async_subresource_trace_fields_for_pending_with_body( "streaming_started", @@ -1794,7 +1797,8 @@ impl ScriptVm { return Ok(AsyncSubresourceFetchBodyActivity::WindowRealmEntered); } let response_body_size = response_body.len(); - if let PendingSubresourceContinuation::Fetch(fetch) = &streaming.pending.continuation { + if let PendingSubresourceContinuation::Fetch(fetch) = &mut streaming.pending.continuation { + if let Some(native) = streaming.pending.load.response_timing() { fetch.timing.observe_native_timing(native); } let response_request = crate::network_host::FetchResponseRequest { method: &streaming.request_method, mode: streaming.pending.request_mode, diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/performance.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/performance.rs index 1b830becbc4..148058c459c 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/performance.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/performance.rs @@ -43,6 +43,41 @@ fn record_failed_script_resource(vm: &mut ScriptVm, document_url: &Url, resource vm.record_script_subresource_network_result(document_url.clone(), request_url, &result); } +#[test] +fn resource_timing_extended_metadata_has_native_getters_and_default_json() { + let mut vm = new_storage_test_vm("https://resource-metadata.test/"); + install_test_resource_recorder(&mut vm); + let result = vm.eval(r#" + __recordResourceTiming('https://resource-metadata.test/failure'); + const entry = performance.getEntriesByType('resource')[0]; + const names = ['deliveryType', 'finalResponseHeadersStart', 'firstInterimResponseStart', + 'workerRouterEvaluationStart', 'workerCacheLookupStart', + 'workerMatchedRouterSource', 'workerFinalRouterSource', 'contentEncoding']; + const expected = ['', 0, 0, 0, 0, '', '', '']; + const proto = PerformanceResourceTiming.prototype; + const json = entry.toJSON(); + let proxyTraps = 0; + const proxy = new Proxy(entry, {get() {proxyTraps++; throw Error('author trap');}}); + const getters = names.every((name, i) => { + const d = Object.getOwnPropertyDescriptor(proto, name); + if (!d || !d.enumerable || !d.configurable || d.set !== undefined || + d.get.name !== 'get ' + name || d.get.length !== 0 || entry[name] !== expected[i]) return false; + for (const invalid of [{}, proxy, Object.create(entry)]) { + try {d.get.call(invalid); return false;} catch (error) {if (!(error instanceof TypeError)) return false;} + } + return Object.hasOwn(json, name) && json[name] === expected[i]; + }); + let authorGetter = 0; + Object.defineProperty(entry, 'contentEncoding', {get() {authorGetter++; throw Error('public getter');}}); + const snapshot = PerformanceResourceTiming.prototype.toJSON.call(entry); + JSON.stringify({getters, proxyTraps, authorGetter, nativeSnapshot: snapshot.contentEncoding === ''}); + "#).expect("extended Resource Timing metadata probe"); + assert_eq!( + result, + r#"{"getters":true,"proxyTraps":0,"authorGetter":0,"nativeSnapshot":true}"# + ); +} + #[test] fn resource_timing_ignores_non_http_resources() { let mut vm = new_storage_test_vm("https://resource-scheme.test/"); diff --git a/moli-renderer-v8/src/worker/global_scope/resource_timing.rs b/moli-renderer-v8/src/worker/global_scope/resource_timing.rs index 74dc2313188..c424fc4365d 100644 --- a/moli-renderer-v8/src/worker/global_scope/resource_timing.rs +++ b/moli-renderer-v8/src/worker/global_scope/resource_timing.rs @@ -247,6 +247,9 @@ impl WorkerResourceTiming { .and_then(|timing| timing.response_started_at()) .or(state.synthetic_response_start); state.clock.response_started_at(start); + if let Some(native) = native { + state.clock.observe_native_timing(native); + } let body_size = if aborted && !completed { 0 } else {