feat(modules): preload modules from Document response Link headers

This commit is contained in:
ldm0
2026-11-21 20:03:34 +08:00
parent ea9bd55680
commit 68bc3ebd0c
31 changed files with 1643 additions and 195 deletions
+1 -1
View File
@@ -5278,7 +5278,7 @@ async fn module_source_phase_wasm_import_reuses_modulepreload_resource() -> Resu
);
assert_eq!(
diagnostic_global(&page, "moduleSourcePhasePreloadInitiator"),
Some(&JsValueSnapshot::String("link".to_owned()))
Some(&JsValueSnapshot::String("script".to_owned()))
);
assert_eq!(
diagnostic_global(&page, "moduleSourcePhasePreloadImportIsModule"),
@@ -10,8 +10,8 @@ type Check = dyn Fn(
&Url,
ContentSecurityPolicyRedirectStatus,
) -> (
Option<ContentSecurityPolicyUrlViolation>,
Option<ContentSecurityPolicyUrlViolation>,
Vec<ContentSecurityPolicyUrlViolation>,
Vec<ContentSecurityPolicyUrlViolation>,
) + Send
+ Sync;
@@ -49,22 +49,26 @@ impl PartialEq for ModuleFetchCspContext {
impl Eq for ModuleFetchCspContext {}
impl ModuleFetchCspContext {
pub(crate) fn new(
pub(crate) fn new<R, E>(
original_url: Url,
owner_scope: Option<crate::native_bridge::OwnerDispatchScope>,
check: impl Fn(
&Url,
&Url,
ContentSecurityPolicyRedirectStatus,
) -> (
Option<ContentSecurityPolicyUrlViolation>,
Option<ContentSecurityPolicyUrlViolation>,
) + Send
check: impl Fn(&Url, &Url, ContentSecurityPolicyRedirectStatus) -> (R, E)
+ Send
+ Sync
+ 'static,
) -> Self {
) -> Self
where
R: IntoIterator<Item = ContentSecurityPolicyUrlViolation>,
E: IntoIterator<Item = ContentSecurityPolicyUrlViolation>,
{
Self(Arc::new(Inner {
check: Box::new(check),
check: Box::new(move |url, original, status| {
let (reports, enforced) = check(url, original, status);
(
reports.into_iter().collect(),
enforced.into_iter().collect(),
)
}),
original_url,
owner_scope,
state: Mutex::new(State::default()),
@@ -102,38 +106,38 @@ impl ModuleFetchCspContext {
ContentSecurityPolicyRedirectStatus::FollowedRedirect
},
);
let message = enforce.first().map(|violation| {
format!(
"Module request blocked by Content Security Policy directive `{}`",
violation.effective_directive
)
});
let mut state = self.0.state.lock();
if state
if !state
.checked_hops
.iter()
.any(|(checked_ordinal, checked_url)| *checked_ordinal == ordinal && checked_url == url)
.any(|(n, checked)| *n == ordinal && checked == url)
{
if let Some(enforce) = enforce {
anyhow::bail!(
"Module request blocked by Content Security Policy directive `{}`",
enforce.effective_directive
);
}
return Ok(());
state.checked_hops.push((ordinal, url.clone()));
state.violations.extend(report);
state.violations.extend(enforce);
}
state.checked_hops.push((ordinal, url.clone()));
if let Some(report) = report {
state.violations.push(report);
match message {
Some(message) => anyhow::bail!(message),
None => Ok(()),
}
if let Some(enforce) = enforce {
let message = format!(
"Module request blocked by Content Security Policy directive `{}`",
enforce.effective_directive
);
state.violations.push(enforce);
anyhow::bail!(message);
}
Ok(())
}
// Coalesced/cache consumers still validate every recorded redirect against
// their own policy. The transport owner's checks are not another owner's authority.
pub(crate) fn check_response(&self, response: &moli_fetch::Response) -> anyhow::Result<()> {
self.check_response_head(&response.head())
}
pub(crate) fn check_response_head(
&self,
response: &moli_fetch::ResponseHead,
) -> anyhow::Result<()> {
for (index, redirect) in response.redirect_chain.iter().enumerate() {
self.check_hop(&redirect.to_url, index + 1)?;
}
@@ -31,6 +31,7 @@ impl ModuleFetchMetadata {
pub(crate) fn from_modulepreload_script_fetch_metadata(metadata: &ScriptFetchMetadata) -> Self {
let mut mapped = Self::from_script_fetch_metadata_inner(metadata);
mapped.request_metadata.integrity = metadata.integrity.clone();
mapped.request_metadata.nonce = metadata.nonce.clone();
mapped.request_metadata.fetch_priority = None;
mapped
}
@@ -81,10 +81,16 @@ pub(crate) enum DocumentNavigationEmbeddingContext<'a> {
Nested(&'a [Option<Url>]),
}
#[derive(Debug, Clone)]
enum DocumentPreloadPolicyMetadata {
Script(crate::planning::ScriptFetchMetadata),
Style(Option<String>),
}
#[derive(Debug, Clone)]
pub(crate) struct DocumentRequestPolicySnapshot {
resource_kind: ContentSecurityPolicyResourceKind,
script_metadata: Option<Box<crate::planning::ScriptFetchMetadata>>,
preload_metadata: Option<Box<DocumentPreloadPolicyMetadata>>,
policy_self_url: Option<Box<Url>>,
enforce_policies: Vec<DocumentContentSecurityPolicy>,
report_only_policies: Vec<ContentSecurityPolicy>,
@@ -138,7 +144,13 @@ impl DocumentRequestPolicySnapshot {
metadata: crate::planning::ScriptFetchMetadata,
) -> Self {
self.resource_kind = ContentSecurityPolicyResourceKind::DocumentScriptElement;
self.script_metadata = Some(Box::new(metadata));
self.preload_metadata = Some(Box::new(DocumentPreloadPolicyMetadata::Script(metadata)));
self
}
pub(crate) fn for_style_preload(mut self, nonce: Option<String>) -> Self {
self.resource_kind = ContentSecurityPolicyResourceKind::DocumentStyleElement;
self.preload_metadata = Some(Box::new(DocumentPreloadPolicyMetadata::Style(nonce)));
self
}
@@ -154,7 +166,7 @@ impl DocumentRequestPolicySnapshot {
) -> Self {
Self {
resource_kind: ContentSecurityPolicyResourceKind::DocumentConnect,
script_metadata: None,
preload_metadata: None,
policy_self_url: None,
enforce_policies: document_response_content_security_policies(
&enforce_policies,
@@ -209,7 +221,45 @@ impl DocumentRequestPolicySnapshot {
redirect_status: ContentSecurityPolicyRedirectStatus,
) -> DocumentContentSecurityPolicyCheck {
let policy_url = self.policy_self_url.as_deref().unwrap_or(document_url);
if let Some(metadata) = self.script_metadata.as_ref() {
if let Some(DocumentPreloadPolicyMetadata::Style(nonce)) = self.preload_metadata.as_deref()
{
let check = |policies: Vec<DocumentContentSecurityPolicyString>, disposition| {
let mut violations =
document_style_element_url_policy_violations_from_document_policies(
policies,
policy_url,
request_url,
redirect_status,
disposition,
ContentSecurityPolicyStyleElementRequest {
nonce: nonce.as_deref(),
},
);
for violation in &mut violations {
violation.document_uri =
crate::content_security_policy::csp_url_for_report(document_url);
violation.source_file =
crate::content_security_policy::csp_url_for_report(document_url);
}
violations
};
return DocumentContentSecurityPolicyCheck {
enforced_violations: check(
self.enforce_policies.clone(),
ContentSecurityPolicyDisposition::Enforce,
),
report_only_violations: check(
document_response_content_security_policy_strings(
&self.report_only_policies,
&self.reporting_endpoints,
),
ContentSecurityPolicyDisposition::Report,
),
};
}
if let Some(DocumentPreloadPolicyMetadata::Script(metadata)) =
self.preload_metadata.as_deref()
{
let request = ContentSecurityPolicyScriptElementRequest {
nonce: metadata.nonce.as_deref(),
integrity: metadata.integrity.as_deref(),
@@ -302,7 +352,7 @@ impl DocumentRequestPolicySnapshot {
request_url: &Url,
redirect_status: ContentSecurityPolicyRedirectStatus,
) -> Option<DocumentContentSecurityPolicyViolation> {
if self.script_metadata.is_some() {
if self.preload_metadata.is_some() {
return self
.check_url(document_url, request_url, redirect_status)
.enforced_violations
@@ -333,7 +383,7 @@ impl DocumentRequestPolicySnapshot {
request_url: &Url,
redirect_status: ContentSecurityPolicyRedirectStatus,
) -> Option<DocumentContentSecurityPolicyViolation> {
if self.script_metadata.is_some() {
if self.preload_metadata.is_some() {
return self
.check_url(document_url, request_url, redirect_status)
.report_only_violations
@@ -1433,7 +1483,7 @@ impl DocumentRuntime {
}
DocumentConnectPolicySnapshot {
resource_kind: ContentSecurityPolicyResourceKind::DocumentConnect,
script_metadata: None,
preload_metadata: None,
policy_self_url: policy
.content_security_policy_self_url
.clone()
@@ -109,6 +109,7 @@ pub(crate) use module_clients::FrameDocumentModulepreloadEventActionRunner;
pub(crate) use module_clients::FrameDocumentModulepreloadFetchTask;
pub(crate) use module_clients::FrameDocumentModulepreloadLinkClient;
pub(crate) use module_clients::FrameDocumentModulepreloadMaterializedWork;
pub(crate) use module_clients::FrameDocumentModulepreloadSource;
pub(crate) use module_clients::FrameDocumentModulepreloadTerminalOutcome;
pub(crate) use module_clients::FrameDocumentModulepreloadTerminalWork;
pub(crate) use module_clients::FrameDocumentModulepreloadWorkAwaitingRealm;
@@ -576,10 +576,48 @@ impl FrameDocumentModuleDependencyTerminalWork {
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct FrameDocumentModulepreloadFetchPayload {
client: FrameDocumentModulepreloadLinkClient,
source: FrameDocumentModulepreloadSource,
request: NativeModuleSingleFetchRequest,
}
/// The request belongs to the Document even when no element receives events.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FrameDocumentModulepreloadSource {
Link(FrameDocumentModulepreloadLinkClient),
ResponseHeader {
child_handle: DomHandle,
owner: FrameDocumentTaskOwner,
},
}
impl FrameDocumentModulepreloadSource {
pub(crate) fn owner(self) -> FrameDocumentTaskOwner {
match self {
Self::Link(client) => client.owner(),
Self::ResponseHeader { owner, .. } => owner,
}
}
pub(crate) fn child_handle(self) -> DomHandle {
match self {
Self::Link(client) => client.child_handle(),
Self::ResponseHeader { child_handle, .. } => child_handle,
}
}
pub(crate) fn link_client(self) -> Option<FrameDocumentModulepreloadLinkClient> {
match self {
Self::Link(client) => Some(client),
Self::ResponseHeader { .. } => None,
}
}
pub(crate) fn link_handle(self) -> Option<DomHandle> {
self.link_client()
.map(FrameDocumentModulepreloadLinkClient::link_handle)
}
}
/// Modulepreload work discovered before its child default realm is ready.
///
/// The parser can establish the exact Document owner before any V8 realm has
@@ -589,14 +627,16 @@ pub(crate) struct FrameDocumentModulepreloadFetchPayload {
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct FrameDocumentModulepreloadWorkAwaitingRealm {
expected_realm_id: Option<FrameRealmId>,
client: FrameDocumentModulepreloadLinkClient,
kind: FrameDocumentModulepreloadWorkAwaitingRealmKind,
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum FrameDocumentModulepreloadWorkAwaitingRealmKind {
FetchStart(Box<NativeModuleSingleFetchRequest>),
LinkError,
FetchStart {
source: FrameDocumentModulepreloadSource,
request: Box<NativeModuleSingleFetchRequest>,
},
LinkError(FrameDocumentModulepreloadLinkClient),
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -608,13 +648,15 @@ pub(crate) enum FrameDocumentModulepreloadMaterializedWork {
impl FrameDocumentModulepreloadWorkAwaitingRealm {
pub(crate) fn fetch_start(
expected_realm_id: Option<FrameRealmId>,
client: FrameDocumentModulepreloadLinkClient,
source: FrameDocumentModulepreloadSource,
request: NativeModuleSingleFetchRequest,
) -> Self {
Self {
expected_realm_id,
client,
kind: FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart(Box::new(request)),
kind: FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart {
source,
request: Box::new(request),
},
}
}
@@ -624,13 +666,21 @@ impl FrameDocumentModulepreloadWorkAwaitingRealm {
) -> Self {
Self {
expected_realm_id,
client,
kind: FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError,
kind: FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError(client),
}
}
fn source(&self) -> FrameDocumentModulepreloadSource {
match &self.kind {
FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart { source, .. } => *source,
FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError(client) => {
FrameDocumentModulepreloadSource::Link(*client)
}
}
}
pub(crate) fn owner(&self) -> FrameDocumentTaskOwner {
self.client.owner()
self.source().owner()
}
pub(crate) fn expected_realm_id(&self) -> Option<FrameRealmId> {
@@ -644,19 +694,19 @@ impl FrameDocumentModulepreloadWorkAwaitingRealm {
}
pub(crate) fn child_handle(&self) -> DomHandle {
self.client.child_handle()
self.source().child_handle()
}
pub(crate) fn link_handle(&self) -> DomHandle {
self.client.link_handle()
pub(crate) fn link_handle(&self) -> Option<DomHandle> {
self.source().link_handle()
}
pub(crate) fn request(&self) -> Option<&NativeModuleSingleFetchRequest> {
match &self.kind {
FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart(request) => {
FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart { request, .. } => {
Some(request.as_ref())
}
FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError => None,
FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError(_) => None,
}
}
@@ -671,21 +721,16 @@ impl FrameDocumentModulepreloadWorkAwaitingRealm {
return None;
}
Some(match self.kind {
FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart(request) => {
FrameDocumentModulepreloadWorkAwaitingRealmKind::FetchStart { source, request } => {
FrameDocumentModulepreloadMaterializedWork::FetchStart(Box::new(
FrameDocumentModulepreloadFetchTask::from_modulepreload_fetch_parts(
realm_id,
self.client,
*request,
FrameDocumentModulepreloadFetchTask::from_source_parts(
realm_id, source, *request,
),
))
}
FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError => {
FrameDocumentModulepreloadWorkAwaitingRealmKind::LinkError(client) => {
FrameDocumentModulepreloadMaterializedWork::LinkError(
FrameDocumentModulepreloadTerminalWork::from_link_error_parts(
realm_id,
self.client,
),
FrameDocumentModulepreloadTerminalWork::from_link_error_parts(realm_id, client),
)
}
})
@@ -1200,27 +1245,39 @@ impl FrameDocumentModulepreloadFetchTask {
client: FrameDocumentModulepreloadLinkClient,
request: NativeModuleSingleFetchRequest,
) -> Self {
Self::new(
client.owner(),
Self::from_source_parts(
realm_id,
FrameDocumentModulepreloadFetchPayload { client, request },
FrameDocumentModulepreloadSource::Link(client),
request,
)
}
pub(crate) fn link_handle(&self) -> DomHandle {
self.payload().client.link_handle()
pub(crate) fn from_source_parts(
realm_id: FrameRealmId,
source: FrameDocumentModulepreloadSource,
request: NativeModuleSingleFetchRequest,
) -> Self {
Self::new(
source.owner(),
realm_id,
FrameDocumentModulepreloadFetchPayload { source, request },
)
}
pub(crate) fn link_handle(&self) -> Option<DomHandle> {
self.payload().source.link_handle()
}
pub(crate) fn target(&self) -> ChildDocumentModuleFetchTarget {
ChildDocumentModuleFetchTarget::new(
self.payload().client.child_handle(),
self.payload().source.child_handle(),
self.owner(),
self.realm_id(),
)
}
pub(crate) fn client(&self) -> FrameDocumentModulepreloadLinkClient {
self.payload().client
pub(crate) fn client(&self) -> Option<FrameDocumentModulepreloadLinkClient> {
self.payload().source.link_client()
}
pub(crate) fn request(&self) -> &NativeModuleSingleFetchRequest {
@@ -17,7 +17,7 @@ use super::ChildDocumentModulatorStore;
pub(crate) enum FrameDocumentModulepreloadStartAction {
ScheduleFetch {
target: ChildDocumentModuleFetchTarget,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
load_id: u64,
request: Box<NativeModuleGraphFetchRequest>,
@@ -25,19 +25,19 @@ pub(crate) enum FrameDocumentModulepreloadStartAction {
JoinedFetching {
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
},
JoinedTerminalSuccess {
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
},
JoinedTerminalFailure {
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
},
}
@@ -52,7 +52,7 @@ pub(crate) trait FrameDocumentModulepreloadStartActionHooks {
fn schedule_modulepreload_fetch(
&mut self,
target: ChildDocumentModuleFetchTarget,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
load_id: u64,
request: Box<NativeModuleGraphFetchRequest>,
@@ -62,7 +62,7 @@ pub(crate) trait FrameDocumentModulepreloadStartActionHooks {
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
);
@@ -70,7 +70,7 @@ pub(crate) trait FrameDocumentModulepreloadStartActionHooks {
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
);
@@ -78,7 +78,7 @@ pub(crate) trait FrameDocumentModulepreloadStartActionHooks {
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
);
}
@@ -436,11 +436,13 @@ impl ChildDocumentModulatorStore {
let target = task.target();
let link_handle = task.link_handle();
let key = task.request().module_key().clone();
let link_client = NativeModulepreloadLinkClient::new_for_frame_document(
link_handle,
key.clone(),
task.client(),
);
let link_client = task.client().map(|client| {
NativeModulepreloadLinkClient::new_for_frame_document(
client.link_handle(),
key.clone(),
client,
)
});
let document_modulator_entry = self.document_modulator_entry_mut(owner, realm_id);
let disposition = document_modulator_entry
.document_modulator
@@ -450,9 +452,11 @@ impl ChildDocumentModulatorStore {
let (load_id, fetch_request) = document_modulator_entry
.document_modulator
.reserve_modulepreload_fetch(task.into_request());
document_modulator_entry
.document_modulator
.add_modulepreload_link_client(key.clone(), link_client);
if let Some(link_client) = link_client {
document_modulator_entry
.document_modulator
.add_modulepreload_link_client(key.clone(), link_client);
}
FrameDocumentModulepreloadStartAction::ScheduleFetch {
target,
link_handle,
@@ -462,9 +466,11 @@ impl ChildDocumentModulatorStore {
}
}
ModuleMapFetchDisposition::JoinedFetching(_entry_id) => {
document_modulator_entry
.document_modulator
.add_modulepreload_link_client(key.clone(), link_client);
if let Some(link_client) = link_client {
document_modulator_entry
.document_modulator
.add_modulepreload_link_client(key.clone(), link_client);
}
FrameDocumentModulepreloadStartAction::JoinedFetching {
owner,
realm_id,
@@ -474,9 +480,11 @@ impl ChildDocumentModulatorStore {
}
ModuleMapFetchDisposition::AlreadyFetched(_)
| ModuleMapFetchDisposition::AlreadyCompiled(_) => {
document_modulator_entry
.document_modulator
.add_terminal_modulepreload_link_client(key.clone(), link_client);
if let Some(link_client) = link_client {
document_modulator_entry
.document_modulator
.add_terminal_modulepreload_link_client(key.clone(), link_client);
}
FrameDocumentModulepreloadStartAction::JoinedTerminalSuccess {
owner,
realm_id,
@@ -485,9 +493,11 @@ impl ChildDocumentModulatorStore {
}
}
ModuleMapFetchDisposition::AlreadyFailed(_) => {
document_modulator_entry
.document_modulator
.add_terminal_modulepreload_link_client(key.clone(), link_client);
if let Some(link_client) = link_client {
document_modulator_entry
.document_modulator
.add_terminal_modulepreload_link_client(key.clone(), link_client);
}
FrameDocumentModulepreloadStartAction::JoinedTerminalFailure {
owner,
realm_id,
@@ -1,6 +1,122 @@
use super::*;
use crate::frame_owner_model::FrameDocumentModuleScriptTerminalTask;
#[test]
fn response_modulepreload_joins_the_module_map_without_manufacturing_link_events() {
use crate::frame_owner_model::FrameDocumentModulepreloadSource;
let mut store = ChildDocumentModulatorStore::default();
let owner =
FrameDocumentTaskOwner::new(FrameSchedulerLaneId(1), LocalWindowId(2), DocumentId(3));
let realm_id = FrameRealmId(4);
let request = modulepreload_request("header-entry.mjs");
let source = FrameDocumentModulepreloadSource::ResponseHeader {
child_handle: DomHandle::new(1),
owner,
};
let task =
FrameDocumentModulepreloadFetchTask::from_source_parts(realm_id, source, request.clone());
let duplicate = task.clone();
let FrameDocumentModulepreloadStartAction::ScheduleFetch {
target,
link_handle,
load_id,
..
} = store.start_modulepreload_fetch_task(task)
else {
panic!("header modulepreload must reserve its own Document module entry");
};
assert_eq!(target.task_owner(), owner);
assert_eq!(target.realm_id(), realm_id);
assert_eq!(target.child_handle(), DomHandle::new(1));
assert!(link_handle.is_none());
assert!(matches!(
store.start_modulepreload_fetch_task(duplicate.clone()),
FrameDocumentModulepreloadStartAction::JoinedFetching {
link_handle: None,
..
}
));
let link_handle = DomHandle::new(77);
let link = FrameDocumentModulepreloadFetchTask::from_modulepreload_fetch_parts(
realm_id,
modulepreload_link_client(owner, link_handle),
request.clone(),
);
assert!(
matches!(store.start_modulepreload_fetch_task(link), FrameDocumentModulepreloadStartAction::JoinedFetching { link_handle: Some(actual), .. } if actual == link_handle)
);
let inflight = store
.take_modulepreload_graph_fetch(owner.document_owner(), realm_id, load_id)
.unwrap();
let batch = store
.finish_modulepreload_fetch(
owner,
realm_id,
inflight,
Ok(ModuleGraphFetchedSource::new(
request.source_url().clone(),
ModuleSource::text("export const value = 1;"),
)),
)
.unwrap();
let events = batch.into_modulepreload_terminal_works();
assert_eq!(events.len(), 1);
assert_eq!(events[0].link_handle(), link_handle);
assert!(events[0].successful());
assert!(matches!(
store.start_modulepreload_fetch_task(duplicate),
FrameDocumentModulepreloadStartAction::JoinedTerminalSuccess {
link_handle: None,
..
}
));
assert!(
store
.take_ready_document_modulator_terminal_batches(owner, realm_id)
.is_empty()
);
}
#[test]
fn response_modulepreload_awaiting_a_realm_retains_exact_document_and_realm_guards() {
use crate::frame_owner_model::{
FrameDocumentModulepreloadMaterializedWork, FrameDocumentModulepreloadSource,
FrameDocumentModulepreloadWorkAwaitingRealm,
};
let owner =
FrameDocumentTaskOwner::new(FrameSchedulerLaneId(1), LocalWindowId(2), DocumentId(3));
let realm_id = FrameRealmId(4);
let source = FrameDocumentModulepreloadSource::ResponseHeader {
child_handle: DomHandle::new(1),
owner,
};
let mut work = FrameDocumentModulepreloadWorkAwaitingRealm::fetch_start(
None,
source,
modulepreload_request("header-pending.mjs"),
);
assert_eq!(work.owner(), owner);
assert_eq!(work.child_handle(), DomHandle::new(1));
assert!(work.link_handle().is_none());
work.bind_first_established_realm(realm_id);
work.bind_first_established_realm(FrameRealmId(5));
assert!(
work.clone()
.into_materialized_work(FrameRealmId(5))
.is_none()
);
let FrameDocumentModulepreloadMaterializedWork::FetchStart(task) =
work.into_materialized_work(realm_id).unwrap()
else {
panic!("header hint cannot manufacture a link error event");
};
assert_eq!(
task.target(),
ChildDocumentModuleFetchTarget::new(DomHandle::new(1), owner, realm_id)
);
assert!(task.client().is_none());
}
#[test]
fn modulepreload_start_action_reserves_fetch_in_modulator_store() {
let mut store = ChildDocumentModulatorStore::default();
@@ -31,7 +147,7 @@ fn modulepreload_start_action_reserves_fetch_in_modulator_store() {
assert_eq!(target.child_handle(), DomHandle::new(1));
assert_eq!(target.task_owner(), owner);
assert_eq!(target.realm_id(), realm_id);
assert_eq!(action_link_handle, link_handle);
assert_eq!(action_link_handle, Some(link_handle));
assert_eq!(action_key, key);
assert_eq!(fetch_request.source_url(), request.source_url());
let inflight = store
@@ -63,7 +179,7 @@ impl FrameDocumentModulepreloadStartActionHooks for FakeModulepreloadStartHooks
fn schedule_modulepreload_fetch(
&mut self,
_target: ChildDocumentModuleFetchTarget,
_link_handle: DomHandle,
_link_handle: Option<DomHandle>,
_key: ModuleMapKey,
_load_id: u64,
_request: Box<NativeModuleGraphFetchRequest>,
@@ -75,7 +191,7 @@ impl FrameDocumentModulepreloadStartActionHooks for FakeModulepreloadStartHooks
&mut self,
_owner: FrameDocumentOwner,
_realm_id: FrameRealmId,
_link_handle: DomHandle,
_link_handle: Option<DomHandle>,
_key: &ModuleMapKey,
) {
self.joined_fetching_records += 1;
@@ -85,7 +201,7 @@ impl FrameDocumentModulepreloadStartActionHooks for FakeModulepreloadStartHooks
&mut self,
_owner: FrameDocumentOwner,
_realm_id: FrameRealmId,
_link_handle: DomHandle,
_link_handle: Option<DomHandle>,
_key: &ModuleMapKey,
) {
self.joined_terminal_success_records += 1;
@@ -95,7 +211,7 @@ impl FrameDocumentModulepreloadStartActionHooks for FakeModulepreloadStartHooks
&mut self,
_owner: FrameDocumentOwner,
_realm_id: FrameRealmId,
_link_handle: DomHandle,
_link_handle: Option<DomHandle>,
_key: &ModuleMapKey,
) {
self.joined_terminal_failure_records += 1;
@@ -141,7 +257,7 @@ fn modulepreload_start_action_runner_posts_terminal_followup() {
let action = FrameDocumentModulepreloadStartAction::JoinedTerminalSuccess {
owner: document_owner,
realm_id,
link_handle,
link_handle: Some(link_handle),
key,
};
let hooks = FakeModulepreloadStartHooks {
@@ -336,7 +452,7 @@ fn modulepreload_start_action_joins_fetching_entry_in_modulator_store() {
assert_eq!(action_owner, document_owner);
assert_eq!(action_realm_id, realm_id);
assert_eq!(link_handle, joined_link);
assert_eq!(link_handle, Some(joined_link));
assert_eq!(action_key, key);
}
@@ -381,7 +497,7 @@ fn modulepreload_start_action_joins_terminal_entry_as_terminal_work() {
};
assert_eq!(action_owner, document_owner);
assert_eq!(action_realm_id, realm_id);
assert_eq!(action_link_handle, link_handle);
assert_eq!(action_link_handle, Some(link_handle));
assert_eq!(action_key, key);
let batch = store.take_ready_document_modulator_terminal_batches(owner, realm_id);
+2
View File
@@ -1,6 +1,7 @@
mod driver;
mod dynamic_resolver;
mod evaluation;
mod fetch_context;
mod frame_document_parser_tree;
mod graph;
mod graph_fetch_store;
@@ -47,6 +48,7 @@ pub(crate) use self::dynamic_resolver::{
PendingDynamicModuleImport,
};
pub(crate) use self::evaluation::ModuleEvaluationRecord;
pub(crate) use self::fetch_context::NativeModuleFetchContext;
pub(crate) use self::import_attributes::{import_attributes_key, module_request_attributes};
pub(crate) use self::import_map::ImportMapRegistryState;
pub(crate) use self::json::{json_module_exception_source_url, parse_json_module};
@@ -0,0 +1,256 @@
use crate::types::AsyncSubresourceFetchResponseFilter;
use std::sync::Arc;
use moli_fetch::{FetchCancelHandle, Request, RequestRedirectCheck};
use parking_lot::Mutex;
use url::Url;
use crate::{
context_bootstrap::{FetchResourceTiming, ResourcePerformanceEntry},
document_runtime::{
DocumentContentSecurityPolicyViolation, DocumentRequestPolicySnapshot,
document_content_security_policy_error_message,
},
dynamic_script_owner::DynamicScriptServiceWorkerContext,
native_bridge::{OwnerDispatchScope, PageScriptHost},
network::loads::ResourceLoadLease,
network_host::{FetchCspRedirectState, WindowCspReportRequestContext},
protocol_types::NavigationResponse,
service_worker_runtime::ServiceWorkerRequestDestination,
types::SubresourceResourceType,
};
use super::{ModuleFetchMetadata, ModuleKind};
/// Native settings of the Document that starts one module fetch. Neither
/// caller realms nor element wrappers are retained by asynchronous transport.
pub(crate) struct NativeModuleFetchContext {
policy: DocumentRequestPolicySnapshot,
report_context: WindowCspReportRequestContext,
redirects: FetchCspRedirectState,
violations: Arc<Mutex<Vec<DocumentContentSecurityPolicyViolation>>>,
service_worker: DynamicScriptServiceWorkerContext,
request_origin: moli_url::WebOrigin,
destination: ServiceWorkerRequestDestination,
resource_type: SubresourceResourceType,
timing: Mutex<FetchResourceTiming>,
entry: Mutex<Option<ResourcePerformanceEntry>>,
}
impl std::fmt::Debug for NativeModuleFetchContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NativeModuleFetchContext")
.field("identity", &self.report_context.identity())
.finish_non_exhaustive()
}
}
impl NativeModuleFetchContext {
pub(crate) fn capture(
scope: &mut v8::PinScope<'_, '_>,
host: &mut PageScriptHost,
owner: OwnerDispatchScope,
kind: ModuleKind,
metadata: &ModuleFetchMetadata,
) -> Option<Arc<Self>> {
let loader = host.document_resource_loader_for_dispatch_scope(owner)?;
let mut policy = host.document_connect_policy_snapshot_for_owner(owner)?;
policy = if kind == ModuleKind::Css {
policy.for_style_preload(metadata.nonce().map(str::to_owned))
} else {
policy.for_script_preload(crate::planning::ScriptFetchMetadata {
nonce: metadata.nonce().map(str::to_owned),
integrity: metadata.integrity().map(str::to_owned),
..Default::default()
})
};
let report_context =
crate::network_host::capture_window_csp_report_request_context(scope, host, owner)?;
let redirects = FetchCspRedirectState::new(&policy);
Some(Arc::new(Self {
policy,
report_context,
redirects,
violations: Arc::default(),
service_worker: DynamicScriptServiceWorkerContext {
browser_context_runtime: host.browser_context_runtime(),
client_id: host.service_worker_client_id_for_subresource_owner(owner),
document_url: loader.fetch_context().document_url().clone(),
},
request_origin: loader.fetch_context().request_origin(),
destination: if kind == ModuleKind::Css {
ServiceWorkerRequestDestination::Style
} else {
ServiceWorkerRequestDestination::Script
},
resource_type: if kind == ModuleKind::Css {
SubresourceResourceType::Stylesheet
} else {
SubresourceResourceType::Script
},
// Modulepreload uses the single-module script fetch algorithm,
// including its script initiator, regardless of its DOM source.
timing: Mutex::new(FetchResourceTiming::from_captured_initiator(
"script",
loader.fetch_context().document_url().as_str().to_owned(),
)),
entry: Mutex::default(),
}))
}
pub(crate) fn check_initial(&self, url: &Url) -> Result<(), String> {
self.check_url(
url,
crate::content_security_policy::ContentSecurityPolicyRedirectStatus::NoRedirect,
)
}
fn check_url(
&self,
url: &Url,
redirect: crate::content_security_policy::ContentSecurityPolicyRedirectStatus,
) -> Result<(), String> {
let (reports, enforced) = self
.policy
.check_url(&self.service_worker.document_url, url, redirect)
.into_violations();
let error = enforced
.first()
.map(|v| document_content_security_policy_error_message(v, "modulepreload"));
self.violations
.lock()
.extend(reports.into_iter().chain(enforced));
error.map_or(Ok(()), Err)
}
pub(crate) fn redirect_check(
self: &Arc<Self>,
request_url: &Url,
) -> Option<RequestRedirectCheck> {
// A Service Worker can retain a redirect guard after its client
// cancels. Keep only report data, not the Document/browser runtime.
let violations = self.violations.clone();
self.redirects.redirect_check(
self.policy.clone(),
self.service_worker.document_url.clone(),
request_url.clone(),
move |violation| violations.lock().push(violation),
)
}
pub(crate) fn check_response_url(&self, response: &NavigationResponse) -> Result<(), String> {
if response.redirected && !self.redirects.was_checked(&response.final_url) {
self.check_url(&response.final_url, crate::content_security_policy::ContentSecurityPolicyRedirectStatus::FollowedRedirect)
} else {
Ok(())
}
}
pub(crate) fn record_response(
&self,
request: &Request,
response: &NavigationResponse,
filter: Option<&AsyncSubresourceFetchResponseFilter>,
native: Option<moli_fetch::FetchResponseTiming>,
) {
let timing = self.timing.lock();
if let Some(native) = native {
timing.observe_native_timing(native);
}
let filter = filter.cloned().unwrap_or_else(|| {
crate::network_host::FetchResponseRequest {
method: "GET",
mode: request.request_mode,
redirect_mode: request.redirect_mode,
}
.network_response_filter(
&self.request_origin,
&response.head(),
request.credentials_mode,
)
});
*self.entry.lock() = Some(timing.completed(
&request.url,
&self.request_origin,
request.request_mode,
&response.head(),
response.body_bytes().len(),
Some(&filter),
));
}
pub(crate) fn record_failure(&self, url: &Url) {
let entry = self.timing.lock().failed(url);
*self.entry.lock() = Some(entry);
}
pub(crate) fn take_performance_entry(&self) -> Option<ResourcePerformanceEntry> {
self.entry.lock().take()
}
pub(crate) fn report_violations(
&self,
scope: &mut v8::PinScope<'_, '_>,
host: &mut PageScriptHost,
dispatch_dom_events: bool,
) {
let violations = std::mem::take(&mut *self.violations.lock());
let host_ptr = host as *mut PageScriptHost;
for violation in violations {
crate::network_host::send_content_security_policy_violation_report_from_window_context(
host,
&self.report_context,
&violation,
);
if dispatch_dom_events {
host.dispatch_document_connect_csp_violation_event_for_exact_owner_without_report_best_effort(scope, host_ptr, self.report_context.identity(), &violation);
}
}
}
pub(crate) fn fetch(
self: Arc<Self>,
load: ResourceLoadLease,
request: Request,
callback: impl FnOnce(
anyhow::Result<(
NavigationResponse,
Option<AsyncSubresourceFetchResponseFilter>,
bool,
)>,
Option<moli_fetch::FetchResponseTiming>,
) + Send
+ 'static,
) {
let runner = load.task_runner();
let request_client = load.request_client();
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel();
let cancel = FetchCancelHandle::new();
load.attach_cancel_handle(cancel.clone());
load.attach_consumer_cancel(move || {
let _ = cancel_tx.send(());
});
runner.clone().spawn(Box::pin(async move {
let result = tokio::select! {
result = async {
match self.service_worker.browser_context_runtime.fetch_service_worker_subresource_for_client_with_metadata_and_cancellation(self.service_worker.client_id, self.service_worker.document_url.clone(), &request, &request_client, runner, self.destination, self.resource_type, cancel.clone()).await? {
Some(response) => Ok((*response.response, response.response_filter, !response.from_network_fallback)),
None => {
// The SDK owns its cache/preload consumer hook.
// Hand off the same lease after the SW stage ends.
load.release_consumer_cancel();
let (response_tx, response_rx) = tokio::sync::oneshot::channel();
request_client.fetch_cacheable_script_text_callback_with_load(request, load.clone(), move |response| { let _ = response_tx.send(response); })?;
let response = response_rx.await.map_err(|_| anyhow::anyhow!("module fetch response channel closed"))??;
Ok((NavigationResponse::from(response), None, false))
}
}
} => result,
Ok(()) = cancel_rx => { cancel.cancel(); Err(anyhow::anyhow!("modulepreload fetch cancelled when its Document retired")) }
};
let native = load.response_timing();
load.finish();
callback(result, native);
}));
}
}
+104 -30
View File
@@ -100,6 +100,7 @@ pub(crate) struct NativeModuleGraphFetchRequest {
module_key: Option<ModuleMapKey>,
dependency: Option<NativeModuleGraphDependencyRequest>,
csp_context: Option<crate::content_security_policy::ModuleFetchCspContext>,
fetch_context: Option<std::sync::Arc<super::NativeModuleFetchContext>>,
}
pub(crate) struct ModuleScriptGraphFetchContinuation {
@@ -231,6 +232,7 @@ impl NativeModuleGraphFetchRequest {
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
}
@@ -377,6 +379,22 @@ impl NativeModuleGraphFetchRequest {
self.fetch_metadata.request_metadata.integrity.as_deref()
}
pub(crate) fn with_fetch_context(
mut self,
context: std::sync::Arc<super::NativeModuleFetchContext>,
) -> Self {
self.fetch_context = Some(context);
self
}
pub(crate) fn fetch_context(&self) -> Option<&std::sync::Arc<super::NativeModuleFetchContext>> {
self.fetch_context.as_ref()
}
pub(crate) fn module_kind(&self) -> ModuleKind {
self.kind
}
pub(crate) fn request(&self, request_origin: &moli_url::WebOrigin) -> anyhow::Result<Request> {
// The client's default is captured for this fetch, not for the compiled
// module. Empty script policies must observe later document policy changes.
@@ -481,15 +499,32 @@ impl NativeModuleGraphFetchRequest {
let source_url = self.source_url.clone();
let kind = self.kind;
let integrity = self.fetch_metadata.request_metadata.integrity.clone();
let request = self.request(&request_origin)?;
let context = self.fetch_context.clone();
let mut request = self.request(&request_origin)?;
if let Some(check) = context
.as_ref()
.filter(|_| self.csp_context.is_none())
.and_then(|context| context.redirect_check(&self.source_url))
{
request = request.with_redirect_check(check);
}
let observed_request = request.clone();
let response_context = context.clone();
let request_mode = request.request_mode;
let credentials_mode = request.credentials_mode;
let csp_context = self.csp_context.clone();
let web_security_disabled = loader.web_security_disabled();
let completion = move |response: anyhow::Result<moli_fetch::Response>| {
let mut network_result: Option<SharedNavigationResponseResult> = None;
let result = response
let completion =
move |response: anyhow::Result<(
NavigationResponse,
Option<crate::types::AsyncSubresourceFetchResponseFilter>,
bool,
)>,
native_timing: Option<moli_fetch::FetchResponseTiming>| {
let mut network_result: Option<SharedNavigationResponseResult> = None;
let result = response
.map_err(|error| {
if let Some(context) = &response_context { context.record_failure(&source_url); }
ModuleLoadError::new(
ModuleLoadStage::Fetch,
format!("failed to fetch module `{source_url}`: {error} (FailedToLoad)"),
@@ -497,16 +532,19 @@ impl NativeModuleGraphFetchRequest {
.message()
.to_owned()
})
.and_then(|response| {
// Main/child parser, runtime, dynamic import and modulepreload
// all convert responses here, including cached/coalesced results.
// Decide CSP from the admitted request snapshot before source
// construction; owner completions only dispatch its reports.
.and_then(|(response, service_worker_filter, from_service_worker)| {
if let Some(context) = &csp_context {
context.check_response(&response).map_err(|error| error.to_string())?;
context.check_response_head(&response.head()).map_err(|error| error.to_string())?;
}
let response = NavigationResponse::from(response);
network_result = Some(std::sync::Arc::new(Ok(response.clone())));
if let Some(context) = &response_context {
if csp_context.is_none()
&& let Err(error) = context.check_response_url(&response) {
context.record_failure(&source_url);
return Err(error);
}
context.record_response(&observed_request, &response, service_worker_filter.as_ref(), native_timing);
}
if !(200..=299).contains(&response.status) {
return Err(ModuleLoadError::new(
ModuleLoadStage::Fetch,
@@ -544,19 +582,28 @@ impl NativeModuleGraphFetchRequest {
&response.headers,
);
let (head, body_bytes) = response.into_byte_parts();
crate::network_host::validate_cors_response_chain(
&request_origin,
&head,
credentials_mode,
web_security_disabled,
).map_err(|error| ModuleLoadError::new(ModuleLoadStage::Fetch, error).message().to_owned())?;
let response_is_eligible = crate::network_host::network_response_filter(
&request_origin,
&head,
request_mode,
moli_fetch::RequestRedirectMode::Follow,
web_security_disabled,
).is_none();
if !from_service_worker {
crate::network_host::validate_cors_response_chain(
&request_origin,
&head,
credentials_mode,
web_security_disabled,
).map_err(|error| ModuleLoadError::new(ModuleLoadStage::Fetch, error).message().to_owned())?;
}
let response_is_eligible = if from_service_worker {
service_worker_filter.as_ref().is_none_or(crate::types::AsyncSubresourceFetchResponseFilter::is_readable)
} else {
crate::network_host::network_response_filter(
&request_origin,
&head,
request_mode,
moli_fetch::RequestRedirectMode::Follow,
web_security_disabled,
).is_none()
};
if from_service_worker && !response_is_eligible {
return Err("Service Worker returned an unreadable module response".to_owned());
}
if !crate::subresource_integrity::response_matches_subresource_integrity_metadata(
&body_bytes,
integrity.as_deref(),
@@ -587,14 +634,31 @@ impl NativeModuleGraphFetchRequest {
.with_response_referrer_policy(response_referrer_policy),
})
});
if network_result.is_none()
&& let Err(error) = &result
if network_result.is_none()
&& let Err(error) = &result
{
network_result = Some(std::sync::Arc::new(Err(error.clone())));
}
callback(result, network_result);
};
if let Some(context) = context {
if self.csp_context.is_none()
&& let Err(error) = context.check_initial(&self.source_url)
{
network_result = Some(std::sync::Arc::new(Err(error.clone())));
load.finish();
completion(Err(anyhow::anyhow!(error)), None);
return Ok(());
}
callback(result, network_result);
};
loader.fetch_cacheable_script_text_callback_with_load(request, load, completion)
context.fetch(load, request, completion);
Ok(())
} else {
loader.fetch_cacheable_script_text_callback_with_load(request, load, move |response| {
completion(
response.map(|response| (NavigationResponse::from(response), None, false)),
None,
)
})
}
}
}
@@ -3376,6 +3440,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3453,6 +3518,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3505,6 +3571,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3544,6 +3611,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3602,6 +3670,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3652,6 +3721,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3691,6 +3761,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -3723,6 +3794,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
@@ -4508,6 +4580,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
};
let (tx, rx) = oneshot::channel();
let registry = ResourceLoadRegistry::new(
@@ -4581,6 +4654,7 @@ import "./c.mjs";
module_key: None,
dependency: None,
csp_context: None,
fetch_context: None,
}
.request(&moli_url::WebOrigin::from_url(&url(
"https://app.example.test/page",
+17 -1
View File
@@ -173,7 +173,6 @@ pub(crate) fn modulepreload_fetch_candidate(
return None;
}
let state = modulepreload_as_state(element);
let attributes = modulepreload_attributes_for_state(state)?;
let fetch_metadata = crate::planning::ScriptFetchMetadata::from_script_attributes(
element.attribute("crossorigin"),
element.attribute("referrerpolicy"),
@@ -189,6 +188,23 @@ pub(crate) fn modulepreload_fetch_candidate(
element.attribute("integrity"),
import_map_integrity,
);
modulepreload_fetch_candidate_for_options(
state,
fetch_metadata,
request_url,
document_url,
document_referrer_policy,
)
}
pub(crate) fn modulepreload_fetch_candidate_for_options(
state: ModulepreloadAsState,
fetch_metadata: crate::planning::ScriptFetchMetadata,
request_url: Url,
document_url: &Url,
document_referrer_policy: Option<String>,
) -> Option<ModulepreloadFetchCandidate> {
let attributes = modulepreload_attributes_for_state(state)?;
let key = ModuleMapKey::from_url_and_attributes(&request_url, &attributes).ok()?;
Some(ModulepreloadFetchCandidate {
key: key.clone(),
@@ -7,10 +7,10 @@ use crate::{
ChildDocumentModuleFetchTarget, FrameDocumentModuleClientReservation,
FrameDocumentModuleDependencyFetchTask, FrameDocumentModuleFetchClientStart,
FrameDocumentModuleFetchDisposition, FrameDocumentModulepreloadFetchTask,
FrameDocumentModulepreloadMaterializedWork, FrameDocumentModulepreloadTerminalWork,
FrameDocumentModulepreloadWorkAwaitingRealm, FrameDocumentParserModuleRootFetchStart,
FrameDocumentParserModuleRootStartTask, FrameDocumentTaskOwner, FrameRealmId,
FrameRequestKind,
FrameDocumentModulepreloadMaterializedWork, FrameDocumentModulepreloadSource,
FrameDocumentModulepreloadTerminalWork, FrameDocumentModulepreloadWorkAwaitingRealm,
FrameDocumentParserModuleRootFetchStart, FrameDocumentParserModuleRootStartTask,
FrameDocumentTaskOwner, FrameRealmId, FrameRequestKind,
},
module_runtime::{
ModuleFetchMetadata, ModuleGraphFetchedSource, ModuleMapKey, NativeModuleGraphFetchRequest,
@@ -495,6 +495,42 @@ impl PageScriptHost {
return false;
};
self.queue_child_modulepreload_fetch_for_source(
FrameDocumentModulepreloadSource::Link(client),
request,
)
}
pub(crate) fn queue_child_response_modulepreload(
&mut self,
handle: DomHandle,
owner: FrameDocumentTaskOwner,
request: NativeModuleSingleFetchRequest,
) -> bool {
self.queue_child_modulepreload_fetch_for_source(
FrameDocumentModulepreloadSource::ResponseHeader {
child_handle: handle,
owner,
},
request,
)
}
fn queue_child_modulepreload_fetch_for_source(
&mut self,
source: FrameDocumentModulepreloadSource,
request: NativeModuleSingleFetchRequest,
) -> bool {
let handle = source.child_handle();
let owner = source.owner();
if !self.child_browsing_contexts.contains_key(&handle)
|| !self
.frame_owner_store
.child_document_task_owner_is_current(handle, owner)
{
return false;
}
let expected_realm_id = self
.frame_owner_store
.current_reserved_realm_id_for_document_task_owner(owner);
@@ -506,7 +542,7 @@ impl PageScriptHost {
return self.queue_child_modulepreload_work_awaiting_realm(
FrameDocumentModulepreloadWorkAwaitingRealm::fetch_start(
expected_realm_id,
client,
source,
request,
),
);
@@ -516,16 +552,22 @@ impl PageScriptHost {
tracing::warn!(
?handle,
?owner,
?link_handle,
?source,
"child modulepreload found a materialized context without an exact realm"
);
return false;
};
self.route_child_modulepreload_start(
FrameDocumentModulepreloadFetchTask::from_modulepreload_fetch_parts(
realm_id, client, request,
),
)
let task = match source {
FrameDocumentModulepreloadSource::Link(client) => {
FrameDocumentModulepreloadFetchTask::from_modulepreload_fetch_parts(
realm_id, client, request,
)
}
FrameDocumentModulepreloadSource::ResponseHeader { .. } => {
FrameDocumentModulepreloadFetchTask::from_source_parts(realm_id, source, request)
}
};
self.route_child_modulepreload_start(task)
}
fn queue_child_modulepreload_work_awaiting_realm(
@@ -554,6 +554,14 @@ impl ResourceLoadLease {
}
}
/// Ends one asynchronous consumer before the next stage takes this lease.
/// Retirement still marks the lease cancelled, so a subsequent attachment
/// observes cancellation even when retirement wins during the handoff.
pub(crate) fn release_consumer_cancel(&self) {
let previous = self.registration.lifecycle.lock().consumer_cancel.take();
drop(previous);
}
pub(crate) fn cancel(&self) {
self.registration.cancel();
}
@@ -681,6 +689,49 @@ mod tests {
assert!(other_request.try_restart_with_auth());
}
#[tokio::test(flavor = "current_thread")]
async fn modulepreload_consumer_handoff_preserves_retirement_cancellation() {
for retirement_stage in 0..3 {
let registry = registry();
let lease = lease(
&registry,
ResourceLoadDisposition::Ordinary,
FetchCancelHandle::new(),
);
let first_cancelled = Arc::new(AtomicU64::new(0));
lease.attach_consumer_cancel({
let first_cancelled = first_cancelled.clone();
move || {
first_cancelled.fetch_add(1, Ordering::AcqRel);
}
});
if retirement_stage == 0 {
registry.begin_detach();
}
lease.release_consumer_cancel();
if retirement_stage == 1 {
registry.begin_detach();
}
let next_cancelled = Arc::new(AtomicU64::new(0));
lease.attach_consumer_cancel({
let next_cancelled = next_cancelled.clone();
move || {
next_cancelled.fetch_add(1, Ordering::AcqRel);
}
});
if retirement_stage == 2 {
registry.begin_detach();
}
assert!(lease.is_cancelled());
assert_eq!(
first_cancelled.load(Ordering::Acquire),
u64::from(retirement_stage == 0)
);
assert_eq!(next_cancelled.load(Ordering::Acquire), 1);
assert_eq!(registry.diagnostics().active_ordinary_load_count, 0);
}
}
#[tokio::test(flavor = "current_thread")]
async fn document_retirement_cancels_ordinary_loads() {
let registry = registry();
@@ -37,6 +37,35 @@ pub(crate) fn start_document_response_links(
tracing::debug!(url = %preload.url, error, "response script preload was not loaded");
}
}
if let (
crate::native_bridge::OwnerDispatchScope::Child(handle),
crate::native_bridge::WindowDocumentOwner::Frame(owner),
) = (target.dispatch_scope(), target.owner())
{
for preload in &links.modules {
if !host.window_document_owner_is_current_for_dispatch_scope(
target.owner(),
target.dispatch_scope(),
) {
break;
}
let Some(loader) = host.document_resource_loader_for_window_owner(target.owner())
else {
break;
};
let Some(environment) =
host.subresource_request_environment(&loader, target.dispatch_scope())
else {
break;
};
if let Some(candidate) = preload.fetch_candidate(
&environment.document_url,
host.document_response_preload_referrer_policy(target.dispatch_scope()),
) {
host.queue_child_response_modulepreload(handle, owner, candidate.request);
}
}
}
}
pub(crate) fn apply_response_preload_request_metadata(
@@ -38,6 +38,7 @@ pub(crate) struct MainModulepreloadFetchCompletion {
result: std::result::Result<ModuleGraphFetchedSource, String>,
network_result: Option<SharedNavigationResponseResult>,
network_attribution: MainModuleFetchNetworkAttribution,
fetch_context: Option<std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>>,
}
impl MainModulepreloadFetchCompletion {
@@ -52,9 +53,24 @@ impl MainModulepreloadFetchCompletion {
result,
network_result,
network_attribution,
fetch_context: None,
}
}
pub(crate) fn with_fetch_context(
mut self,
context: Option<std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>>,
) -> Self {
self.fetch_context = context;
self
}
pub(crate) fn fetch_context(
&self,
) -> Option<&std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>> {
self.fetch_context.as_ref()
}
pub(crate) fn target(&self) -> MainModulepreloadFetchTarget {
self.target
}
@@ -68,6 +68,23 @@ impl MainModuleFetchSchedule {
result: std::result::Result<ModuleGraphFetchedSource, String>,
network_result: Option<SharedNavigationResponseResult>,
network_attribution: MainModuleFetchNetworkAttribution,
) {
self.send_completion_with_context(
completion_tx,
result,
network_result,
network_attribution,
None,
);
}
fn send_completion_with_context(
self,
completion_tx: &RendererResourceCompletionSender,
result: std::result::Result<ModuleGraphFetchedSource, String>,
network_result: Option<SharedNavigationResponseResult>,
network_attribution: MainModuleFetchNetworkAttribution,
fetch_context: Option<Arc<crate::module_runtime::NativeModuleFetchContext>>,
) {
match self {
Self::Parser(target) => {
@@ -107,7 +124,8 @@ impl MainModuleFetchSchedule {
result,
network_result,
network_attribution,
),
)
.with_fetch_context(fetch_context),
);
}
}
@@ -117,11 +135,23 @@ impl MainModuleFetchSchedule {
#[derive(Clone)]
pub(crate) struct RendererResourceScheduler {
completion_tx: RendererResourceCompletionSender,
module_fetch_context: Option<Arc<crate::module_runtime::NativeModuleFetchContext>>,
}
impl RendererResourceScheduler {
pub(crate) fn new(completion_tx: RendererResourceCompletionSender) -> Self {
Self { completion_tx }
Self {
completion_tx,
module_fetch_context: None,
}
}
pub(crate) fn with_module_fetch_context(
mut self,
context: Arc<crate::module_runtime::NativeModuleFetchContext>,
) -> Self {
self.module_fetch_context = Some(context);
self
}
pub(crate) fn schedule_main_parser_module_graph_fetch(
@@ -297,6 +327,13 @@ impl RendererResourceScheduler {
let requester = schedule.requester();
let ordering = schedule.ordering();
let request = prioritized_module_graph_fetch_request(requester, ordering, request);
let request = if let Some(context) = &self.module_fetch_context {
request.with_fetch_context(context.clone())
} else {
request
};
let fetch_context = request.fetch_context().cloned();
let callback_context = fetch_context.clone();
let request_url = request.source_url().clone();
let network_attribution =
MainModuleFetchNetworkAttribution::new(document_url, request_url.clone());
@@ -315,11 +352,12 @@ impl RendererResourceScheduler {
&callback_request_url,
result.is_ok(),
);
schedule.send_completion(
schedule.send_completion_with_context(
&completion_tx,
result,
network_result,
callback_attribution,
callback_context,
);
};
if let Err(error) = request.fetch_source_for_document(&loader, send_completion) {
@@ -331,11 +369,12 @@ impl RendererResourceScheduler {
&request_url,
&error,
);
schedule.send_completion(
schedule.send_completion_with_context(
&self.completion_tx,
Err(error.clone()),
Some(Arc::new(Err(error))),
network_attribution,
fetch_context,
);
}
}
@@ -409,6 +448,13 @@ impl RendererResourceScheduler {
let requester = ModuleGraphFetchRequester::ModulePreload;
let ordering = ModuleGraphFetchOrdering::BackgroundPreload;
let request = prioritized_module_graph_fetch_request(requester, ordering, request);
let request = if let Some(context) = &self.module_fetch_context {
request.with_fetch_context(context.clone())
} else {
request
};
let fetch_context = request.fetch_context().cloned();
let callback_context = fetch_context.clone();
let request_url = request.source_url().clone();
trace_module_graph_fetch_scheduled(load_id, requester, ordering, &request_url);
@@ -432,7 +478,8 @@ impl RendererResourceScheduler {
result,
network_result,
callback_attribution,
),
)
.with_fetch_context(callback_context),
);
};
if let Err(error) = request.fetch_source_for_document(&loader, send_completion) {
@@ -451,7 +498,8 @@ impl RendererResourceScheduler {
Err(error.clone()),
Some(Arc::new(Err(error))),
network_attribution,
),
)
.with_fetch_context(fetch_context),
);
}
}
+141 -9
View File
@@ -3,6 +3,7 @@ use std::collections::HashSet;
use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer};
use url::Url;
use crate::modulepreload::{ModulepreloadAsState, ModulepreloadFetchCandidate};
use crate::planning::ScriptFetchMetadata;
/// Response hints retain the response URL as their base, independently of
@@ -11,6 +12,7 @@ use crate::planning::ScriptFetchMetadata;
pub(crate) struct DocumentResponseLinks {
pub(crate) dictionaries: Vec<Url>,
pub(crate) scripts: Vec<ResponseScriptPreload>,
pub(crate) modules: Vec<ResponseModulepreload>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -19,6 +21,29 @@ pub(crate) struct ResponseScriptPreload {
pub(crate) metadata: ScriptFetchMetadata,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResponseModulepreload {
pub(crate) url: Url,
pub(crate) destination: ModulepreloadAsState,
pub(crate) metadata: ScriptFetchMetadata,
}
impl ResponseModulepreload {
pub(crate) fn fetch_candidate(
&self,
document_url: &Url,
document_referrer_policy: Option<String>,
) -> Option<ModulepreloadFetchCandidate> {
crate::modulepreload::modulepreload_fetch_candidate_for_options(
self.destination,
self.metadata.clone(),
self.url.clone(),
document_url,
document_referrer_policy,
)
}
}
impl DocumentResponseLinks {
pub(crate) fn parse(base: &Url, headers: &[(String, Vec<u8>)]) -> Self {
let mut result = Self::default();
@@ -55,20 +80,31 @@ impl DocumentResponseLinks {
.preload_type_matches(link.parameter("type"))
{
let preload = ResponseScriptPreload {
url,
metadata: ScriptFetchMetadata::from_script_attributes(
link.parameter("crossorigin"),
link.parameter("referrerpolicy"),
None,
link.parameter("integrity"),
link.parameter("nonce"),
link.parameter("fetchpriority"),
),
url: url.clone(),
metadata: link.script_fetch_metadata(),
};
if !result.scripts.contains(&preload) {
result.scripts.push(preload);
}
}
let destination = crate::modulepreload::modulepreload_as_state_from_attribute(
link.parameter("as"),
);
if link.has_relation("modulepreload")
&& destination != ModulepreloadAsState::Invalid
&& crate::modulepreload::modulepreload_media_attribute_matches(
link.parameter("media"),
)
{
let preload = ResponseModulepreload {
url,
destination,
metadata: link.script_fetch_metadata(),
};
if !result.modules.contains(&preload) {
result.modules.push(preload);
}
}
}
if !parser.consume(',') {
break;
@@ -85,6 +121,17 @@ struct ResponseLink<'a> {
}
impl ResponseLink<'_> {
fn script_fetch_metadata(&self) -> ScriptFetchMetadata {
ScriptFetchMetadata::from_script_attributes(
self.parameter("crossorigin"),
self.parameter("referrerpolicy"),
None,
self.parameter("integrity"),
self.parameter("nonce"),
self.parameter("fetchpriority"),
)
}
fn parameter(&self, name: &str) -> Option<&str> {
self.parameters
.iter()
@@ -151,6 +198,91 @@ fn parse_link<'a>(
mod tests {
use super::*;
#[test]
fn response_modulepreload_keeps_module_options_and_the_response_url_base() {
let base = Url::parse("https://example.test/pages/document").unwrap();
let headers = vec![("Link".into(), br#"<../entry.mjs#module>; REL="alternate MODULEPRELOAD"; nonce="a\"z"; nonce=wrong; crossorigin=use-credentials; referrerpolicy=no-referrer; integrity="sha256-value", </ignored>; rel=alternate; rel=modulepreload, </image>;rel=modulepreload;as=image, </media>;rel=modulepreload;media="not all", </anchor>;rel=modulepreload;anchor="/other", <../entry.mjs#module>;REL="alternate MODULEPRELOAD";nonce="a\"z";crossorigin=use-credentials;referrerpolicy=no-referrer;integrity="sha256-value", </style>;rel=modulepreload;as=style, </json>;rel=modulepreload;as=json"#.to_vec())];
let parsed = DocumentResponseLinks::parse(&base, &headers);
assert!(parsed.scripts.is_empty());
assert_eq!(parsed.modules.len(), 3);
let hint = &parsed.modules[0];
assert_eq!(hint.url, base.join("../entry.mjs#module").unwrap());
assert_eq!(hint.destination, ModulepreloadAsState::ScriptLike);
let candidate = hint
.fetch_candidate(&base, Some("same-origin".into()))
.unwrap();
assert_eq!(candidate.key.url(), &hint.url);
let fetch = candidate.request.fetch_request();
let metadata = candidate.request.fetch_metadata();
assert_eq!(metadata.nonce(), Some("a\"z"));
assert_eq!(metadata.integrity(), Some("sha256-value"));
assert_eq!(
metadata.credentials_mode,
moli_fetch::RequestCredentialsMode::Include
);
assert_eq!(
metadata.request_metadata.referrer_policy.as_deref(),
Some("no-referrer")
);
assert_eq!(fetch.initiator_url(), &base);
assert_eq!(parsed.modules[1].destination, ModulepreloadAsState::Style);
assert_eq!(
parsed.modules[1]
.fetch_candidate(&base, None)
.unwrap()
.key
.kind(),
crate::module_runtime::ModuleKind::Css
);
assert_eq!(parsed.modules[2].destination, ModulepreloadAsState::Json);
assert_eq!(
parsed.modules[2]
.fetch_candidate(&base, None)
.unwrap()
.key
.kind(),
crate::module_runtime::ModuleKind::Json
);
}
#[test]
fn modulepreload_style_policy_uses_style_nonce_and_the_captured_document() {
use crate::content_security_policy::ContentSecurityPolicyRedirectStatus;
use crate::document_runtime::DocumentRequestPolicySnapshot;
let document = Url::parse("https://example.test/page").unwrap();
let target = Url::parse("https://cdn.test/entry.css").unwrap();
let policy = DocumentRequestPolicySnapshot::from_policies(
vec!["script-src 'none'; style-src 'nonce-az'".into()],
Vec::new(),
Default::default(),
);
let allowed = policy.clone().for_style_preload(Some("az".into()));
assert!(
allowed
.check_url(
&document,
&target,
ContentSecurityPolicyRedirectStatus::NoRedirect
)
.has_no_violations()
);
assert!(
allowed
.check_redirect(&document, &target)
.has_no_violations()
);
assert!(
!policy
.for_style_preload(Some("wrong".into()))
.check_url(
&document,
&target,
ContentSecurityPolicyRedirectStatus::NoRedirect
)
.has_no_violations()
);
}
#[test]
fn response_preloads_preserve_first_parameters_and_quoted_delimiters() {
let base = Url::parse("https://example.test/pages/document").unwrap();
@@ -258,6 +258,30 @@ impl RendererBrowserContextRuntime {
resource_task_runner: RendererResourceTaskRunner,
destination: ServiceWorkerRequestDestination,
resource_type: SubresourceResourceType,
) -> Result<Option<ServiceWorkerDirectFetchResponse>> {
self.fetch_service_worker_subresource_for_client_with_metadata_and_cancellation(
client_id,
document_url,
request,
request_client,
resource_task_runner,
destination,
resource_type,
FetchCancelHandle::new(),
)
.await
}
pub(crate) async fn fetch_service_worker_subresource_for_client_with_metadata_and_cancellation(
&self,
client_id: ServiceWorkerClientId,
document_url: Url,
request: &Request,
request_client: &ResourceRequestClient,
resource_task_runner: RendererResourceTaskRunner,
destination: ServiceWorkerRequestDestination,
resource_type: SubresourceResourceType,
cancel_handle: FetchCancelHandle,
) -> Result<Option<ServiceWorkerDirectFetchResponse>> {
if let Some(preload) = request_client.consume_document_preload(request) {
let preload = preload
@@ -288,8 +312,11 @@ impl RendererBrowserContextRuntime {
let completion_tx =
crate::page_task_queue::RendererResourceCompletionSender::direct_completion_only();
let (direct_completion_tx, direct_completion_rx) = tokio::sync::oneshot::channel();
let redirect_request = request.clone();
let dispatch = ServiceWorkerFetchDispatch {
redirect_check: None,
redirect_check: Some(moli_fetch::RequestRedirectCheck::new(move |next_url| {
redirect_request.check_redirect_target(next_url)
})),
internal_id: 0,
request: ServiceWorkerFetchRequest {
client_id,
@@ -318,7 +345,7 @@ impl RendererBrowserContextRuntime {
completion_tx,
request_client: request_client.clone(),
resource_task_runner,
cancel_handle: FetchCancelHandle::new(),
cancel_handle,
direct_completion_tx: Some(direct_completion_tx),
};
@@ -48,8 +48,10 @@ impl PageVm {
self.vm_mut()
.record_current_child_module_fetch_network_result(
completion.target(),
completion.network_attribution(),
completion.network_result(),
None,
);
let _followup = self
.vm_mut()
@@ -82,8 +84,10 @@ impl PageVm {
self.vm_mut()
.record_current_child_module_fetch_network_result(
completion.target(),
completion.network_attribution(),
completion.network_result(),
None,
);
let _followup = self
.vm_mut()
@@ -117,8 +121,10 @@ impl PageVm {
let output_effect = PageResourceCompletionOutputEffect::capture_if(
self.vm_mut()
.record_current_child_module_fetch_network_result(
completion.target(),
completion.network_attribution(),
completion.network_result(),
None,
),
);
let _followup = self
@@ -140,6 +146,10 @@ impl PageVm {
completion: ChildModulepreloadFetchCompletion,
) -> Result<PageResourceCompletionTurnAction> {
let current_owner = self.current_page_resource_completion_owner(owner);
if let Some(context) = completion.fetch_context() {
self.vm_mut()
.report_module_fetch_context(context, current_owner == Some(owner));
}
if current_owner != Some(owner) {
return Ok(self.discard_stale_child_module_terminal(
source,
@@ -150,11 +160,26 @@ impl PageVm {
));
}
let entry = completion
.fetch_context()
.and_then(|context| context.take_performance_entry())
.or_else(|| {
completion.network_result().map(|result| {
crate::context_bootstrap::ResourcePerformanceEntry::from_network_result(
completion.network_attribution().request_url().as_str(),
"script",
None,
result.as_ref(),
)
})
});
let output_effect = PageResourceCompletionOutputEffect::capture_if(
self.vm_mut()
.record_current_child_module_fetch_network_result(
completion.target(),
completion.network_attribution(),
completion.network_result(),
entry,
),
);
let _followup = self
@@ -296,6 +296,10 @@ impl PageVm {
completion: MainModulepreloadFetchCompletion,
) -> Result<PageResourceCompletionTurnAction> {
let current_owner = self.current_page_resource_completion_owner(owner);
if let Some(context) = completion.fetch_context() {
self.vm_mut()
.report_module_fetch_context(context, current_owner == Some(owner));
}
if current_owner != Some(owner) {
let output_effect = if let Some(network_result) = completion.network_result() {
self.vm_mut()
@@ -338,6 +342,9 @@ impl PageVm {
completion.network_attribution().document_url().clone(),
completion.network_attribution().request_url().clone(),
network_result.as_ref(),
completion
.fetch_context()
.and_then(|context| context.take_performance_entry()),
);
PageResourceCompletionOutputEffect::CaptureRequired
} else {
@@ -270,7 +270,7 @@ async fn production_main_modulepreload_uses_stable_typed_route_and_applies_modul
assert_eq!(records[0].resource_type(), SubresourceResourceType::Script);
assert_eq!(
modulepreload_performance_summary(&mut page_vm, &request_url)?,
"link",
"script",
"modulepreload should retain its producer-attributed Resource Timing initiator"
);
assert!(!queue.has_ready_completion());
@@ -380,7 +380,7 @@ async fn failed_main_modulepreload_queues_joined_client_fanout_for_a_later_turn(
assert!(matches!(body.result(), SubresourceBodyFinishedResult::Failed(error) if error == "net::ERR_ABORTED"));
assert_eq!(
modulepreload_performance_summary(&mut page_vm, &request_url)?,
"link",
"script",
"failed fetches still produce a modulepreload Resource Timing entry"
);
assert!(page_vm.vm_mut().has_ready_native_module_owner_actions());
@@ -713,7 +713,7 @@ fn real_page_vm_replacement_rejects_naturally_colliding_main_modulepreload_targe
&mut page_vm,
&replacement_request_url,
)?,
"link"
"script"
);
assert_eq!(
modulepreload_performance_summary(&mut page_vm, &old_request_url)?,
@@ -277,3 +277,319 @@ document.body.appendChild(preloadFrame);
}).await.expect("header preload Document retirement regression");
}).await;
}
#[tokio::test]
async fn response_modulepreload_is_imported_from_its_child_document_module_map() {
run_page_vm_async_test(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let server = tokio::spawn(async move {
for path in ["/pages/module-child", "/header-module.mjs"] {
let (mut stream, _) = tokio::time::timeout(Duration::from_secs(5), listener.accept()).await??;
let head = request_head(&mut stream).await?;
assert!(head.starts_with(&format!("GET {path} ")), "{head}");
let (kind, fields, body) = if path == "/pages/module-child" {
("text/html", "Link: <../header-module.mjs#preloaded>;rel=modulepreload;nonce=az\r\nContent-Security-Policy: script-src 'nonce-az'\r\n", r#"<!doctype html><head><base href='/wrong/'></head><body><script nonce=az>
const url = new URL('/header-module.mjs#preloaded', location).href;
let started = false;
new PerformanceObserver(list => {
if (started || !list.getEntries().some(entry => entry.name === url)) return;
started = true;
const before = globalThis.headerModuleExecutions || 0;
import(url).then(module => parent.postMessage({
before, executions: globalThis.headerModuleExecutions,
value: module.value, links: document.querySelectorAll('link').length,
timings: performance.getEntriesByName(url).map(entry => entry.initiatorType)
}, '*'));
}).observe({type: 'resource', buffered: true});
</script>"#)
} else {
("text/javascript", "", "globalThis.headerModuleExecutions = (globalThis.headerModuleExecutions || 0) + 1; export const value = 42;")
};
stream.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: {kind}\r\nCache-Control: no-store\r\n{fields}Content-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()).await?;
stream.shutdown().await?;
}
anyhow::Ok(())
});
let mut page_vm = test_page_vm_with_document_url(Url::parse(&format!("{base}/parent")).unwrap());
let executor = page_vm.local_executor.clone();
executor.run(async move {
page_vm.vm_mut().eval(r#"
addEventListener('message', event => { globalThis.headerModuleResult = event.data; });
const frame = document.createElement('iframe'); frame.src = '/pages/module-child';
document.body.appendChild(frame);
"#)?;
drive_websocket_until_done(&mut page_vm, "String(globalThis.headerModuleResult !== undefined)", "header modulepreload module-map consumption").await?;
let result = page_vm.vm_mut().eval("JSON.stringify(headerModuleResult)")?;
assert_eq!(serde_json::from_str::<serde_json::Value>(&result)?, json!({"before": 0, "executions": 1, "value": 42, "links": 0, "timings": ["script"]}));
assert_eq!(page_vm.vm_mut().eval("String(performance.getEntriesByType('resource').some(entry => entry.name.includes('/header-module.mjs')))")?, "false");
server.await??;
anyhow::Ok(())
}).await.expect("header modulepreload child regression");
}).await;
}
async fn assert_controlled_response_module_preload(response_kind: &'static str) {
run_page_vm_async_test(async move {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let script = "globalThis.controlledPreloadExecutions = (globalThis.controlledPreloadExecutions || 0) + 1; export const answer = 42;";
let quoted_script = serde_json::to_string(script).unwrap();
let response = match response_kind {
"fallback" => String::new(),
"forward" => "event.respondWith(fetch(event.request));".to_owned(),
"response" => format!("event.respondWith(new Response({quoted_script}, {{headers: {{'Content-Type': 'text/javascript'}}}}));"),
"stream" => format!("event.respondWith(new Response(new ReadableStream({{start(controller) {{controller.enqueue(new TextEncoder().encode({quoted_script})); controller.close();}}}}), {{headers: {{'Content-Type': 'text/javascript'}}}}));"),
_ => unreachable!(),
};
let worker = format!(r#"
addEventListener('install', event => event.waitUntil(skipWaiting()));
addEventListener('activate', event => event.waitUntil(clients.claim()));
addEventListener('fetch', event => {{
if (new URL(event.request.url).pathname !== '/asset.js') return;
event.waitUntil(fetch('/seen?destination=' + event.request.destination + '&referrer=' + encodeURIComponent(event.request.referrer) + '&policy=' + encodeURIComponent(event.request.referrerPolicy)));
{response}
}});
"#);
let child = r#"<!doctype html><script>
const url = new URL('/asset.js', location).href;
let started = false;
new PerformanceObserver(list => {
if (started || !list.getEntries().some(entry => entry.name === url)) return;
started = true;
const before = globalThis.controlledPreloadExecutions || 0;
import(url).then(module => parent.postMessage({
before, executions: globalThis.controlledPreloadExecutions, answer: module.answer,
controlled: !!navigator.serviceWorker.controller,
links: document.querySelectorAll('link').length,
timings: performance.getEntriesByName(url).map(entry => entry.initiatorType)
}, '*')).catch(error => parent.postMessage({error: String(error)}, '*'));
}).observe({type: 'resource', buffered: true});
</script>"#;
let network_script = matches!(response_kind, "fallback" | "forward");
let server = tokio::spawn(async move {
let mut paths = Vec::new();
for _ in 0..if network_script { 4 } else { 3 } {
let (mut stream, _) = tokio::time::timeout(Duration::from_secs(5), listener.accept()).await??;
let request = request_head(&mut stream).await?;
let path = request.split_ascii_whitespace().nth(1).unwrap().to_owned();
let (content_type, fields, body) = match path.as_str() {
"/sw.js" => ("text/javascript", "", worker.as_str()),
"/controlled/child" => ("text/html", "Link: </asset.js>;rel=modulepreload;referrerpolicy=no-referrer\r\n", child),
"/seen?destination=script&referrer=&policy=no-referrer" => ("text/plain", "", "observed"),
"/asset.js" if network_script => {
let expected_destination = if response_kind == "fallback" { "script" } else { "empty" };
assert!(request.to_ascii_lowercase().contains(&format!("sec-fetch-dest: {expected_destination}\r\n")), "{request}");
assert!(!request.to_ascii_lowercase().contains("\r\nreferer:"), "{request}");
("text/javascript", "", script)
}
_ => panic!("unexpected controlled preload request: {request}"),
};
let response = format!("HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nCache-Control: no-store\r\n{fields}Content-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len());
stream.write_all(response.as_bytes()).await?;
stream.shutdown().await?;
paths.push(path);
}
assert_eq!(paths.iter().filter(|path| path.as_str() == "/seen?destination=script&referrer=&policy=no-referrer").count(), 1);
assert_eq!(paths.iter().filter(|path| path.as_str() == "/asset.js").count(), usize::from(network_script));
anyhow::Ok(())
});
let mut page_vm = test_page_vm_with_document_url(Url::parse(&format!("{base}/parent")).unwrap());
let executor = page_vm.local_executor.clone();
executor.run(async move {
page_vm.vm_mut().eval(r#"
addEventListener('message', event => { globalThis.controlledPreloadResult = event.data; });
(async () => {
const registration = await navigator.serviceWorker.register('/sw.js', {scope: '/controlled/'});
const worker = registration.installing || registration.waiting || registration.active;
if (worker.state !== 'activated') await new Promise(resolve => worker.addEventListener('statechange', () => {
if (worker.state === 'activated') resolve();
}));
const frame = document.createElement('iframe'); frame.src = '/controlled/child';
document.body.appendChild(frame);
})().catch(error => { globalThis.controlledPreloadResult = {error: String(error)}; });
"#)?;
drive_page_work_until_done_with_explicit_producer_admission(
&mut page_vm,
"String(globalThis.controlledPreloadResult !== undefined)",
"controlled header preload should complete before its consumer",
|page_vm| {
page_vm.runtime_hooks.browser_context_runtime.drain_service_worker_service_lane();
},
).await?;
let result = page_vm.vm_mut().eval("JSON.stringify(controlledPreloadResult)")?;
assert_eq!(serde_json::from_str::<serde_json::Value>(&result)?, json!({"before": 0, "executions": 1, "controlled": true, "links": 0, "timings": ["script"], "answer": 42}));
assert_eq!(page_vm.vm_mut().eval("String(performance.getEntriesByType('resource').some(entry => entry.name.endsWith('/asset.js')))")?, "false");
server.await??;
anyhow::Ok(())
}).await.expect("controlled response modulepreload regression");
}).await;
}
#[tokio::test]
async fn response_modulepreload_service_worker_network_fallback_uses_its_document_module_map() {
assert_controlled_response_module_preload("fallback").await;
}
#[tokio::test]
async fn response_modulepreload_service_worker_forwarding_uses_its_document_module_map() {
assert_controlled_response_module_preload("forward").await;
}
#[tokio::test]
async fn response_modulepreload_service_worker_generated_response_uses_its_document_module_map() {
assert_controlled_response_module_preload("response").await;
}
#[tokio::test]
async fn response_modulepreload_service_worker_stream_uses_its_document_module_map() {
assert_controlled_response_module_preload("stream").await;
}
#[tokio::test(flavor = "current_thread")]
async fn response_modulepreload_reports_csp_without_dispatching_into_replacement_realm() {
run_page_vm_async_test(async move {
for replace_realm in [false, true] {
let (base, report_rx, report_server) = spawn_request_capture_http_server().await;
let document_url = Url::parse(&format!("{base}/source.html"))?;
let blocked_url = Url::parse(&format!("{base}/blocked.js"))?;
let mut page_vm = test_page_vm_with_document_url(document_url);
page_vm
.vm_mut()
.set_response_content_security_policies(&[format!(
"script-src 'nonce-az' 'unsafe-eval'; report-uri {base}/csp-report"
)]);
page_vm.vm_mut().eval(
"globalThis.modulepreloadCspEvents = 0; \
const frame = document.createElement('iframe'); \
frame.id = 'modulepreload-csp-realm'; document.body.appendChild(frame);",
)?;
let handle = page_vm
.vm()
.element_handle_by_id_for_test("modulepreload-csp-realm")
.expect("child handle");
materialize_child_realm_through_page_turn_for_test(
&mut page_vm,
"modulepreload-csp-realm",
)?;
let target = page_vm
.vm()
.current_child_document_module_fetch_target(handle)
.expect("source child realm");
let context = page_vm
.vm_mut()
.capture_module_fetch_context(
crate::native_bridge::OwnerDispatchScope::Child(handle),
crate::module_runtime::ModuleKind::JavaScript,
&crate::module_runtime::ModuleFetchMetadata::default(),
)
.map_err(anyhow::Error::msg)?;
let error = context
.check_initial(&blocked_url)
.expect_err("missing preload nonce must be blocked");
if replace_realm {
page_vm.vm_mut().retire_child_frame_realm_for_test(handle);
materialize_child_realm_through_page_turn_for_test(
&mut page_vm,
"modulepreload-csp-realm",
)?;
let replacement = page_vm
.vm()
.current_child_document_module_fetch_target(handle)
.expect("replacement child realm");
assert_eq!(target.task_owner(), replacement.task_owner());
assert_ne!(target.realm_id(), replacement.realm_id());
}
page_vm.vm_mut().eval(
"document.getElementById('modulepreload-csp-realm').contentDocument \
.addEventListener('securitypolicyviolation', () => modulepreloadCspEvents++);",
)?;
let root = page_vm.document_lifecycle.identity().document;
let completion = test_child_modulepreload_completion_for_target(
target,
701,
"modulepreload-csp-realm",
Some(&error),
)
.with_fetch_context(Some(context));
let mut source = RendererPageNetworkingSource::new_for_test();
source.enqueue_local_for_test(
RendererPageResourceCompletion::child_modulepreload_fetch(root, completion),
);
let outcome = page_vm
.apply_one_page_resource_terminal_owner_admission_for_test(&mut source)?
.expect("one modulepreload completion");
assert_eq!(
matches!(
outcome.action.document_effect,
PageResourceCompletionDocumentEffect::DiscardedStaleOwner { .. }
),
replace_realm
);
let events = page_vm
.vm_mut()
.drain_pre_domcontentloaded_content_security_policy_violation_tasks_for_test();
assert_eq!(events, usize::from(!replace_realm));
assert_eq!(
page_vm.vm_mut().eval("String(modulepreloadCspEvents)")?,
if replace_realm { "0" } else { "1" }
);
let report = tokio::time::timeout(Duration::from_secs(5), report_rx).await??;
assert!(report.starts_with("POST /csp-report HTTP/1.1"), "{report}");
let body = report.split_once("\r\n\r\n").expect("report body").1;
let body: serde_json::Value = serde_json::from_str(body)?;
assert_eq!(body["csp-report"]["blocked-uri"], blocked_url.as_str());
assert_eq!(body["csp-report"]["effective-directive"], "script-src-elem");
report_server.await?;
}
anyhow::Ok(())
})
.await
.expect("modulepreload CSP source realm regression");
}
#[tokio::test(flavor = "current_thread")]
async fn response_modulepreload_redirect_guard_does_not_retain_its_document_fetch_context() {
run_page_vm_async_test(async move {
let mut page_vm = test_page_vm_with_document_url(Url::parse(
"https://modulepreload-redirect-context.test/page.html",
)?);
page_vm
.vm_mut()
.set_response_content_security_policies(&["script-src 'self'".to_owned()]);
let context = page_vm
.vm_mut()
.capture_module_fetch_context(
crate::native_bridge::OwnerDispatchScope::Top,
crate::module_runtime::ModuleKind::JavaScript,
&crate::module_runtime::ModuleFetchMetadata::default(),
)
.map_err(anyhow::Error::msg)?;
let source = Url::parse("https://modulepreload-redirect-context.test/module.js")?;
let guard = context
.redirect_check(&source)
.expect("captured CSP requires a redirect guard");
let weak = Arc::downgrade(&context);
drop(context);
assert!(
weak.upgrade().is_none(),
"a retained SW redirect guard must not keep the Document/browser fetch context alive"
);
let request = moli_fetch::Request::get(source.as_str())?.with_redirect_check(guard);
assert!(
request
.check_redirect_target(&Url::parse(
"https://modulepreload-redirect-context.test/allowed.js"
)?)
.is_ok()
);
assert!(
request
.check_redirect_target(&Url::parse("https://other.test/blocked.js")?)
.is_err()
);
anyhow::Ok(())
})
.await
.expect("modulepreload redirect guard ownership regression");
}
@@ -360,12 +360,14 @@ impl<'vm> ChildModuleFetchOwner<'vm> {
&mut self,
completion: ChildModulepreloadFetchCompletion,
) -> FrameDocumentModuleTerminalQueueFollowup {
let policy_checked = completion.fetch_context().is_some();
let (target, load_id, result) = completion.into_module_terminal_parts();
self.finish_modulepreload_fetch_completion(
target.task_owner(),
target.realm_id(),
load_id,
result,
policy_checked,
)
}
@@ -375,6 +377,7 @@ impl<'vm> ChildModuleFetchOwner<'vm> {
realm_id: FrameRealmId,
load_id: u64,
result: std::result::Result<crate::module_runtime::ModuleGraphFetchedSource, String>,
_policy_checked: bool,
) -> FrameDocumentModuleTerminalQueueFollowup {
let owner = task_owner.document_owner();
let Some(request) = self
@@ -457,7 +460,7 @@ impl FrameDocumentModulepreloadStartActionHooks for ScriptVmChildModulepreloadSt
fn schedule_modulepreload_fetch(
&mut self,
target: ChildDocumentModuleFetchTarget,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: ModuleMapKey,
load_id: u64,
request: Box<NativeModuleGraphFetchRequest>,
@@ -505,8 +508,31 @@ impl FrameDocumentModulepreloadStartActionHooks for ScriptVmChildModulepreloadSt
.send_child_modulepreload_fetch(completion);
return;
};
let context = match self.vm.capture_module_fetch_context(
crate::native_bridge::OwnerDispatchScope::Child(target.child_handle()),
request.module_kind(),
request.fetch_metadata(),
) {
Ok(context) => context,
Err(error) => {
let _ = self
.vm
._context_host
.borrow()
.resource_completion_sender()
.send_child_modulepreload_fetch(ChildModulepreloadFetchCompletion::new(
target,
load_id,
Err(error.clone()),
Some(std::sync::Arc::new(Err(error))),
network_attribution,
));
return;
}
};
self.vm
.resource_scheduler()
.with_module_fetch_context(context)
.schedule_child_modulepreload_graph_fetch(
loader,
target,
@@ -527,7 +553,7 @@ impl FrameDocumentModulepreloadStartActionHooks for ScriptVmChildModulepreloadSt
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
) {
tracing::debug!(
@@ -543,7 +569,7 @@ impl FrameDocumentModulepreloadStartActionHooks for ScriptVmChildModulepreloadSt
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
) {
tracing::debug!(
@@ -559,7 +585,7 @@ impl FrameDocumentModulepreloadStartActionHooks for ScriptVmChildModulepreloadSt
&mut self,
owner: FrameDocumentOwner,
realm_id: FrameRealmId,
link_handle: DomHandle,
link_handle: Option<DomHandle>,
key: &ModuleMapKey,
) {
tracing::debug!(
@@ -83,6 +83,7 @@ use crate::wasm_module_support::{
mod child_dynamic_import;
mod document_scripts;
mod fetch_context;
mod module_fetch;
mod module_graph;
mod module_reactions;
@@ -614,10 +614,20 @@ impl ScriptVm {
));
Ok(())
}
#[cfg(test)]
pub(super) fn apply_main_modulepreload_fetch_result(
&mut self,
load_id: u64,
result: std::result::Result<ModuleGraphFetchedSource, String>,
) -> Result<Option<ModuleEntryId>> {
self.apply_main_modulepreload_fetch_result_with_policy(load_id, result, false)
}
fn apply_main_modulepreload_fetch_result_with_policy(
&mut self,
load_id: u64,
result: std::result::Result<ModuleGraphFetchedSource, String>,
_policy_checked: bool,
) -> Result<Option<ModuleEntryId>> {
let Some(preload) = self
.document_runtime
@@ -660,12 +670,17 @@ impl ScriptVm {
) -> Result<()> {
let completion = authorization.into_completion();
let target = completion.target();
self.apply_main_modulepreload_fetch_result(target.load_id(), completion.into_result())?
.ok_or_else(|| {
anyhow::anyhow!(
"authorized main modulepreload terminal lost its exact in-flight request"
)
})?;
let policy_checked = completion.fetch_context().is_some();
self.apply_main_modulepreload_fetch_result_with_policy(
target.load_id(),
completion.into_result(),
policy_checked,
)?
.ok_or_else(|| {
anyhow::anyhow!(
"authorized main modulepreload terminal lost its exact in-flight request"
)
})?;
Ok(())
}
pub(crate) fn accept_main_parser_async_module_script(
@@ -0,0 +1,41 @@
use crate::{
module_runtime::{ModuleFetchMetadata, ModuleKind, NativeModuleFetchContext},
native_bridge::OwnerDispatchScope,
};
use std::sync::Arc;
impl crate::script_vm::ScriptVm {
pub(crate) fn capture_module_fetch_context(
&mut self,
owner: OwnerDispatchScope,
kind: ModuleKind,
metadata: &ModuleFetchMetadata,
) -> Result<Arc<NativeModuleFetchContext>, String> {
self.with_default_context_scope(|scope, host_ptr| {
NativeModuleFetchContext::capture(
scope,
unsafe { &mut *host_ptr },
owner,
kind,
metadata,
)
.ok_or_else(|| anyhow::anyhow!("modulepreload lost its exact Document fetch context"))
})
.map_err(|error| error.to_string())
}
pub(crate) fn report_module_fetch_context(
&mut self,
context: &NativeModuleFetchContext,
dispatch_dom_events: bool,
) {
if let Err(error) = self.with_default_context_scope(|scope, host_ptr| {
context.report_violations(scope, unsafe { &mut *host_ptr }, dispatch_dom_events);
Ok(())
}) {
self.record_runtime_warning(format_args!(
"modulepreload CSP reporting failed: {error}"
));
}
}
}
@@ -237,7 +237,12 @@ impl ScriptVm {
let document_owner = self.current_main_document_task_owner().ok_or_else(|| {
"main modulepreload fetch started without a current Document owner".to_owned()
})?;
let resource_scheduler = self.resource_scheduler();
let context = self.capture_module_fetch_context(
crate::native_bridge::OwnerDispatchScope::Top,
request.module_key().kind(),
request.fetch_metadata(),
)?;
let resource_scheduler = self.resource_scheduler().with_module_fetch_context(context);
let outcome = self
.document_runtime
.start_main_document_modulepreload_fetch(document_owner, &resource_scheduler, request)
@@ -252,7 +257,12 @@ impl ScriptVm {
let document_owner = self.current_main_document_task_owner().ok_or_else(|| {
"main modulepreload fetch started without a current Document owner".to_owned()
})?;
let resource_scheduler = self.resource_scheduler();
let context = self.capture_module_fetch_context(
crate::native_bridge::OwnerDispatchScope::Top,
request.module_key().kind(),
request.fetch_metadata(),
)?;
let resource_scheduler = self.resource_scheduler().with_module_fetch_context(context);
let outcome = self
.document_runtime
.start_main_document_modulepreload_link_fetch(
@@ -753,6 +763,7 @@ impl ScriptVm {
document_url: Url,
request_url: Url,
network_result: &std::result::Result<crate::types::NavigationResponse, String>,
native_entry: Option<crate::context_bootstrap::ResourcePerformanceEntry>,
) {
self._context_host
.borrow_mut()
@@ -764,7 +775,11 @@ impl ScriptVm {
SubresourceRequestInitiatorType::Parser,
network_result,
);
self.record_modulepreload_resource_performance_entry(&request_url, network_result);
if let Some(entry) = native_entry {
self.record_modulepreload_performance_entry(entry);
} else {
self.record_modulepreload_resource_performance_entry(&request_url, network_result);
}
}
pub(crate) fn record_historical_main_modulepreload_network_result(
&mut self,
@@ -791,17 +806,24 @@ impl ScriptVm {
let performance_entry =
crate::context_bootstrap::ResourcePerformanceEntry::from_network_result(
request_url.as_str(),
"link",
"script",
None,
network_result,
);
self.record_modulepreload_performance_entry(performance_entry);
}
fn record_modulepreload_performance_entry(
&mut self,
performance_entry: crate::context_bootstrap::ResourcePerformanceEntry,
) {
if let Err(error) = self.with_default_context_scope(|scope, _host_ptr| {
crate::context_bootstrap::record_resource_performance_entry(scope, performance_entry);
Ok(())
}) {
self.record_runtime_warning(format_args!(
"failed to record native modulepreload performance entry for `{}`: {}",
request_url, error
"failed to record native modulepreload performance entry: {}",
error
));
}
}
@@ -5,18 +5,40 @@ impl ScriptVm {
&mut self,
links: &crate::response_links::DocumentResponseLinks,
) -> Result<()> {
if links.dictionaries.is_empty() && links.scripts.is_empty() {
if links.dictionaries.is_empty() && links.scripts.is_empty() && links.modules.is_empty() {
return Ok(());
}
self.with_default_context_scope(|scope, host_ptr| {
let host = unsafe { &mut *host_ptr };
if let Some(target) = host.current_window_document_task_target_for_dispatch_scope(
crate::native_bridge::OwnerDispatchScope::Top,
) {
crate::network_host::start_document_response_links(scope, host, target, links);
let owner = self.current_main_document_task_owner();
let (scripting_enabled, referrer_policy) =
self.with_default_context_scope(|scope, host_ptr| {
let host = unsafe { &mut *host_ptr };
let dispatch_scope = crate::native_bridge::OwnerDispatchScope::Top;
let scripting_enabled = host.owner_scripting_enabled(dispatch_scope);
let referrer_policy =
host.document_response_preload_referrer_policy(dispatch_scope);
if let Some(target) =
host.current_window_document_task_target_for_dispatch_scope(dispatch_scope)
{
crate::network_host::start_document_response_links(scope, host, target, links);
}
Ok((scripting_enabled, referrer_policy))
})?;
if !scripting_enabled || owner.is_none() {
return Ok(());
}
for preload in &links.modules {
if self.current_main_document_task_owner() != owner {
break;
}
Ok(())
})
if let Some(candidate) = preload.fetch_candidate(
self.document_runtime.document_url(),
referrer_policy.clone(),
) && let Err(error) = self.register_native_modulepreload_for_owner(candidate.request)
{
tracing::debug!(url = %preload.url, error, "response modulepreload was not loaded");
}
}
Ok(())
}
#[cfg(all(test, feature = "devtools"))]
@@ -245,8 +245,10 @@ impl ScriptVm {
}
pub(crate) fn record_current_child_module_fetch_network_result(
&mut self,
target: crate::frame_owner_model::ChildDocumentModuleFetchTarget,
attribution: &crate::types::ChildModuleFetchNetworkAttribution,
network_result: Option<&crate::types::SharedNavigationResponseResult>,
native_entry: Option<crate::context_bootstrap::ResourcePerformanceEntry>,
) -> bool {
let Some(network_result) = network_result else {
return false;
@@ -254,6 +256,29 @@ impl ScriptVm {
self._context_host
.borrow_mut()
.record_current_child_module_fetch_network_result(attribution, network_result.as_ref());
let entry = native_entry.unwrap_or_else(|| {
crate::context_bootstrap::ResourcePerformanceEntry::from_network_result(
attribution.request_url().as_str(),
"script",
None,
network_result.as_ref(),
)
});
if let Err(error) = self.with_default_context_scope(|scope, host_ptr| {
unsafe { &mut *host_ptr }.record_document_resource_timing(
scope,
crate::native_bridge::WindowDocumentTaskTarget::new(
crate::native_bridge::WindowDocumentOwner::Frame(target.task_owner()),
crate::native_bridge::OwnerDispatchScope::Child(target.child_handle()),
),
entry,
);
Ok(())
}) {
self.record_runtime_warning(format_args!(
"failed to record child module resource timing: {error}"
));
}
true
}
pub(crate) fn record_historical_child_module_fetch_network_result(
+16
View File
@@ -1851,6 +1851,7 @@ pub(super) struct ChildModulepreloadFetchCompletion {
result: std::result::Result<crate::module_runtime::ModuleGraphFetchedSource, String>,
network_result: Option<SharedNavigationResponseResult>,
network_attribution: ChildModuleFetchNetworkAttribution,
fetch_context: Option<std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>>,
}
impl ChildModulepreloadFetchCompletion {
@@ -1867,9 +1868,24 @@ impl ChildModulepreloadFetchCompletion {
result,
network_result,
network_attribution,
fetch_context: None,
}
}
pub(crate) fn with_fetch_context(
mut self,
context: Option<std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>>,
) -> Self {
self.fetch_context = context;
self
}
pub(crate) fn fetch_context(
&self,
) -> Option<&std::sync::Arc<crate::module_runtime::NativeModuleFetchContext>> {
self.fetch_context.as_ref()
}
pub(super) fn target(&self) -> crate::frame_owner_model::ChildDocumentModuleFetchTarget {
self.target
}