Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(navigation): own pending requests in target

ldm08bbe0cde64fc
diff --git a/moli-protocol/src/conn.rs b/moli-protocol/src/conn.rs
index 0ea066f52..338aa358a 100644
--- a/moli-protocol/src/conn.rs
+++ b/moli-protocol/src/conn.rs
@@ -512,10 +512,7 @@ pub(crate) use runtime_load::{
};
use scheduler_hooks::CdpSchedulerHooks;
use scheduler_state::CdpConnectionSchedulerState;
-pub use scheduler_state::{
- BackgroundNavigationCancellation, BackgroundNavigationGateKey, CdpSchedulerEvent,
- CdpTurnOutcome,
-};
+pub use scheduler_state::{CdpSchedulerEvent, CdpTurnOutcome};
#[cfg(test)]
pub(crate) use site_data_manager_surface::{
BrowserContextReservedSiteDataOwnerState, BrowserContextSiteDataManagerOwnerState,
@@ -1421,18 +1418,54 @@ impl CdpConnection {
.runtime_inspector_response_ready_sender()
}
- pub(crate) fn record_background_navigation_started_scheduler_event(
+ pub(crate) fn document_navigation_cancellation_handle(
+ &self,
+ token: &DocumentNavigationToken,
+ ) -> Option<moli_fetch::FetchCancelHandle> {
+ let browser_context_id = self.browser_context_id_for_target(&token.target_id)?;
+ self.browser_context_by_id(browser_context_id)?
+ .document_navigation_cancellation_handle(token)
+ }
+
+ pub(crate) fn arm_background_navigation_completion(
&mut self,
token: &DocumentNavigationToken,
- state: &NavigationDispatchState,
- cancellation: BackgroundNavigationCancellation,
- ) {
- let key = BackgroundNavigationGateKey::for_navigation(token, state);
- self.scheduler_state
- .push_scheduler_event(CdpSchedulerEvent::BackgroundNavigationStarted {
- key,
- cancellation,
- });
+ additional_cancellation: Option<moli_fetch::FetchCancelHandle>,
+ ) -> bool {
+ let browser_context_id = self
+ .browser_context_id_for_target(&token.target_id)
+ .map(str::to_owned);
+ let Some(browser_context_id) = browser_context_id else {
+ if let Some(cancellation) = additional_cancellation {
+ cancellation.cancel();
+ }
+ return false;
+ };
+ self.browser_context_by_id_mut(&browser_context_id)
+ .is_some_and(|browser_context| {
+ browser_context.arm_background_navigation_completion(token, additional_cancellation)
+ })
+ }
+
+ pub(crate) fn settle_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ ) -> bool {
+ let browser_context_id = self
+ .browser_context_id_for_target(&token.target_id)
+ .map(str::to_owned);
+ let Some(browser_context_id) = browser_context_id else {
+ return false;
+ };
+ self.browser_context_by_id_mut(&browser_context_id)
+ .is_some_and(|browser_context| {
+ browser_context.settle_background_navigation_completion(token)
+ })
+ }
+
+ pub fn has_inflight_background_navigation(&self) -> bool {
+ self.browser_contexts()
+ .any(BrowserContext::has_inflight_background_navigation)
}
pub fn has_pending_document_navigation_for_session_owner(
@@ -1613,13 +1646,12 @@ impl CdpConnection {
pub(crate) fn start_document_navigation_for_session_owner(
&mut self,
session_id: Option<&str>,
- event_session_id: Option<String>,
loader_id: String,
) -> Option<DocumentNavigationToken> {
let (browser_context_id, target_id) = self.target_owner_identity_for_session(session_id)?;
let target_id = target_id?;
self.browser_context_by_id_mut(&browser_context_id)?
- .start_document_navigation_for_target(&target_id, event_session_id, loader_id)
+ .start_document_navigation_for_target(&target_id, loader_id)
}
pub(crate) fn commit_document_navigation_for_session_owner_if_matches(
@@ -2245,6 +2277,12 @@ impl CdpConnection {
) -> Vec<BackgroundProtocolEvent> {
let completion = match completion {
crate::domains::page::BackgroundNavigationCompletion::Lifecycle(completion) => {
+ if !self.settle_background_navigation_completion(completion.navigation_token()) {
+ tracing::debug!(
+ token = ?completion.navigation_token(),
+ "background navigation completion did not match the target-owned request"
+ );
+ }
completion
}
crate::domains::page::BackgroundNavigationCompletion::MainDocumentBody(completion) => {
diff --git a/moli-protocol/src/conn/runtime_load.rs b/moli-protocol/src/conn/runtime_load.rs
index 0af9f22b7..0f7bda984 100644
--- a/moli-protocol/src/conn/runtime_load.rs
+++ b/moli-protocol/src/conn/runtime_load.rs
@@ -694,7 +694,7 @@ fn spawn_captured_body_replay(
pub(crate) struct BackgroundNavigationLoadJob {
engine: NavigationEngine,
page_reservation: RendererPageReservationToken,
- cancellation: BackgroundNavigationCancellation,
+ cancellation: FetchCancelHandle,
early_result: Option<BackgroundNavigationEarlyResult>,
load_inputs: TargetNavigationLoadInputs,
method: String,
@@ -808,10 +808,6 @@ impl BackgroundNavigationEarlyResult {
}
impl BackgroundNavigationLoadJob {
- pub(crate) fn cancellation(&self) -> BackgroundNavigationCancellation {
- self.cancellation.clone()
- }
-
fn emit_early_result_for_successful_document(
early_result: &mut Option<BackgroundNavigationEarlyResult>,
navigation: &Result<NavigationLoadOutcome, String>,
@@ -904,7 +900,7 @@ impl BackgroundNavigationLoadJob {
self.body,
self.request_headers.clone(),
None,
- self.cancellation.fetch_cancel_handle(),
+ self.cancellation.clone(),
)
.await;
let navigation_response = match navigation_response {
@@ -2370,12 +2366,14 @@ impl CdpConnection {
.await
}
- pub(crate) fn background_navigation_load_job_for_navigation(
+ pub(crate) fn navigation_load_job_for_navigation(
&mut self,
+ token: &DocumentNavigationToken,
navigation: &NavigationDispatchState,
body_progress_source: MainDocumentBodyProgressSource,
early_result: Option<BackgroundNavigationEarlyResult>,
- ) -> BackgroundNavigationLoadJob {
+ ) -> Option<BackgroundNavigationLoadJob> {
+ let cancellation = self.document_navigation_cancellation_handle(token)?;
let load_inputs = self.navigation_load_inputs_for_navigation(navigation);
// Ensure the connection's resident browser resource runtime exists,
// then share only that transport/cache owner with the background job.
@@ -2388,10 +2386,10 @@ impl CdpConnection {
&load_inputs,
&engine,
);
- BackgroundNavigationLoadJob {
+ Some(BackgroundNavigationLoadJob {
engine,
page_reservation,
- cancellation: BackgroundNavigationCancellation::new(),
+ cancellation,
early_result,
load_inputs,
method: navigation.request_method.clone(),
@@ -2400,7 +2398,24 @@ impl CdpConnection {
request_headers: navigation.request_headers.clone(),
body_progress_source,
shared_resource_runtime,
- }
+ })
+ }
+
+ pub(crate) fn background_navigation_load_job_for_navigation(
+ &mut self,
+ token: &DocumentNavigationToken,
+ navigation: &NavigationDispatchState,
+ body_progress_source: MainDocumentBodyProgressSource,
+ early_result: Option<BackgroundNavigationEarlyResult>,
+ ) -> Option<BackgroundNavigationLoadJob> {
+ let job = self.navigation_load_job_for_navigation(
+ token,
+ navigation,
+ body_progress_source,
+ early_result,
+ )?;
+ self.arm_background_navigation_completion(token, None)
+ .then_some(job)
}
pub(crate) fn background_streaming_response_navigation_load_job_for_navigation(
diff --git a/moli-protocol/src/conn/scheduler_state.rs b/moli-protocol/src/conn/scheduler_state.rs
index d98245bde..e13d0ef93 100644
--- a/moli-protocol/src/conn/scheduler_state.rs
+++ b/moli-protocol/src/conn/scheduler_state.rs
@@ -7,113 +7,8 @@ use serde_json::{Value, json};
const RECENT_ACTIVITY_TRACE_LIMIT: usize = 128;
-/// Transport cancellation authority for one exact background main-resource
-/// navigation.
-///
-/// The protocol scheduler keeps this beside the navigation gate so starting a
-/// replacement in the same frame can abort the superseded fetch immediately,
-/// matching the lifetime of Chromium's frame-owned `NavigationRequest`.
-#[derive(Clone, Debug, Default)]
-pub struct BackgroundNavigationCancellation {
- handle: moli_fetch::FetchCancelHandle,
-}
-
-impl BackgroundNavigationCancellation {
- pub fn new() -> Self {
- Self::default()
- }
-
- pub fn cancel(&self) {
- self.handle.cancel();
- }
-
- pub fn is_cancelled(&self) -> bool {
- self.handle.is_cancelled()
- }
-
- pub(crate) fn fetch_cancel_handle(&self) -> moli_fetch::FetchCancelHandle {
- self.handle.clone()
- }
-
- pub(crate) fn from_fetch_cancel_handle(handle: moli_fetch::FetchCancelHandle) -> Self {
- Self { handle }
- }
-}
-
-#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-pub struct BackgroundNavigationGateKey {
- target_id: Option<String>,
- session_id: Option<String>,
- frame_id: String,
- loader_id: String,
- navigation_request_id: Option<u64>,
-}
-
-impl BackgroundNavigationGateKey {
- pub(crate) fn for_navigation(
- token: &super::state::DocumentNavigationToken,
- state: &super::state::NavigationDispatchState,
- ) -> Self {
- // Keep this key limited to fields that are stable from background task
- // spawn through lifecycle completion. `navigate_id` is intentionally
- // excluded because early Page.navigate replies clear it before the
- // completion is sent.
- Self {
- target_id: Some(token.target_id.clone()),
- session_id: token
- .initiating_session_id
- .clone()
- .or_else(|| state.navigate_session_id.clone())
- .or_else(|| state.session_id.clone()),
- frame_id: state.frame_id.clone(),
- loader_id: token.loader_id.clone(),
- navigation_request_id: Some(token.request_id.get()),
- }
- }
-
- /// Whether a newly-started navigation owns the same browsing-context
- /// lane as an older in-flight navigation.
- ///
- /// Session identity is intentionally not part of a target-backed lane:
- /// two DevTools sessions can address the same frame, but a cross-document
- /// navigation from either session still supersedes the frame's previous
- /// navigation. The exact loader/request fields remain part of equality so
- /// a late completion cannot retire the replacement gate.
- pub fn supersedes(&self, pending: &Self) -> bool {
- if self.frame_id != pending.frame_id {
- return false;
- }
- match (&self.target_id, &pending.target_id) {
- (Some(target_id), Some(pending_target_id)) => target_id == pending_target_id,
- (None, None) => self.session_id == pending.session_id,
- (Some(_), None) | (None, Some(_)) => false,
- }
- }
-
- #[cfg(feature = "test-support")]
- pub(crate) fn from_test_parts(
- target_id: Option<String>,
- session_id: Option<String>,
- frame_id: String,
- loader_id: String,
- navigation_request_id: Option<u64>,
- ) -> Self {
- Self {
- target_id,
- session_id,
- frame_id,
- loader_id,
- navigation_request_id,
- }
- }
-}
-
#[derive(Debug)]
pub enum CdpSchedulerEvent {
- BackgroundNavigationStarted {
- key: BackgroundNavigationGateKey,
- cancellation: BackgroundNavigationCancellation,
- },
ProtocolWorkPublished {
work: crate::domains::activity::ProtocolSchedulerWork,
},
diff --git a/moli-protocol/src/conn/state/attachment_identity.rs b/moli-protocol/src/conn/state/attachment_identity.rs
index 0d2368f03..464b54511 100644
--- a/moli-protocol/src/conn/state/attachment_identity.rs
+++ b/moli-protocol/src/conn/state/attachment_identity.rs
@@ -37,10 +37,6 @@ impl NavigationRequestId {
"navigation request id",
))
}
-
- pub(crate) fn get(self) -> u64 {
- self.0.get()
- }
}
fn allocate_nonzero_u64(counter: &AtomicU64, name: &str) -> NonZeroU64 {
@@ -67,7 +63,7 @@ mod tests {
assert_ne!(first_attachment, second_attachment);
assert_ne!(first_navigation, second_navigation);
assert_ne!(first_attachment.get(), 0);
- assert_ne!(first_navigation.get(), 0);
+ assert_ne!(first_navigation.0.get(), 0);
}
#[test]
diff --git a/moli-protocol/src/conn/state/browser_context.rs b/moli-protocol/src/conn/state/browser_context.rs
index 2785a3965..d904db4b0 100644
--- a/moli-protocol/src/conn/state/browser_context.rs
+++ b/moli-protocol/src/conn/state/browser_context.rs
@@ -1093,12 +1093,10 @@ impl BrowserContext {
loader_id: String,
) -> Option<DocumentNavigationToken> {
let target_id = self.active_target_id()?.to_owned();
- let session_id = self.active_session_id_owned();
- let token = self.active_target.runtime_slot.start_document_navigation(
- target_id.clone(),
- session_id,
- loader_id,
- );
+ let token = self
+ .active_target
+ .runtime_slot
+ .start_document_navigation(target_id.clone(), loader_id);
self.mark_target_initial_empty_document_pending_cross_document_navigation(&target_id);
Some(token)
}
@@ -1106,26 +1104,21 @@ impl BrowserContext {
pub(crate) fn start_document_navigation_for_target(
&mut self,
target_id: &str,
- session_id: Option<String>,
loader_id: String,
) -> Option<DocumentNavigationToken> {
if self.active_target_id() == Some(target_id) {
let target_id = target_id.to_owned();
- let token = self.active_target.runtime_slot.start_document_navigation(
- target_id.clone(),
- session_id.or_else(|| self.active_session_id_owned()),
- loader_id,
- );
+ let token = self
+ .active_target
+ .runtime_slot
+ .start_document_navigation(target_id.clone(), loader_id);
self.mark_target_initial_empty_document_pending_cross_document_navigation(&target_id);
return Some(token);
}
let target = self.background_target_mut(target_id)?;
- let target_session_id = session_id.or_else(|| target.session_id().map(str::to_owned));
- let token = target.runtime_slot.start_document_navigation(
- target_id.to_owned(),
- target_session_id,
- loader_id,
- );
+ let token = target
+ .runtime_slot
+ .start_document_navigation(target_id.to_owned(), loader_id);
self.mark_target_initial_empty_document_pending_cross_document_navigation(target_id);
Some(token)
}
@@ -1148,6 +1141,73 @@ impl BrowserContext {
})
}
+ pub(crate) fn document_navigation_cancellation_handle(
+ &self,
+ token: &DocumentNavigationToken,
+ ) -> Option<moli_fetch::FetchCancelHandle> {
+ if self.active_target_id() == Some(token.target_id.as_str()) {
+ return self
+ .active_target
+ .runtime_slot
+ .document_navigation_cancellation_handle(token);
+ }
+ self.background_target(&token.target_id).and_then(|target| {
+ target
+ .runtime_slot()
+ .document_navigation_cancellation_handle(token)
+ })
+ }
+
+ pub(crate) fn arm_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ additional_cancellation: Option<moli_fetch::FetchCancelHandle>,
+ ) -> bool {
+ if self.active_target_id() == Some(token.target_id.as_str()) {
+ return self
+ .active_target
+ .runtime_slot
+ .arm_background_navigation_completion(token, additional_cancellation);
+ }
+ let Some(target) = self.background_target_mut(&token.target_id) else {
+ if let Some(cancellation) = additional_cancellation {
+ cancellation.cancel();
+ }
+ return false;
+ };
+ target
+ .runtime_slot
+ .arm_background_navigation_completion(token, additional_cancellation)
+ }
+
+ pub(crate) fn settle_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ ) -> bool {
+ if self.active_target_id() == Some(token.target_id.as_str()) {
+ return self
+ .active_target
+ .runtime_slot
+ .settle_background_navigation_completion(token);
+ }
+ self.background_target_mut(&token.target_id)
+ .is_some_and(|target| {
+ target
+ .runtime_slot
+ .settle_background_navigation_completion(token)
+ })
+ }
+
+ pub(crate) fn has_inflight_background_navigation(&self) -> bool {
+ self.active_target
+ .runtime_slot
+ .has_inflight_background_navigation()
+ || self
+ .background_targets
+ .iter()
+ .any(|target| target.runtime_slot().has_inflight_background_navigation())
+ }
+
pub(crate) fn accepts_document_body_completion_event(
&self,
token: &DocumentNavigationToken,
diff --git a/moli-protocol/src/conn/state/devtools_renderer_channel.rs b/moli-protocol/src/conn/state/devtools_renderer_channel.rs
index dbf7b7131..857c6f321 100644
--- a/moli-protocol/src/conn/state/devtools_renderer_channel.rs
+++ b/moli-protocol/src/conn/state/devtools_renderer_channel.rs
@@ -442,7 +442,6 @@ mod tests {
fn navigation(label: u64) -> DocumentNavigationToken {
DocumentNavigationToken {
target_id: "TID-channel".to_owned(),
- initiating_session_id: None,
loader_id: format!("LID-{label}"),
request_id: crate::conn::state::NavigationRequestId::allocate(),
}
diff --git a/moli-protocol/src/conn/state/page_slot.rs b/moli-protocol/src/conn/state/page_slot.rs
index add69b896..bd9c8cd7e 100644
--- a/moli-protocol/src/conn/state/page_slot.rs
+++ b/moli-protocol/src/conn/state/page_slot.rs
@@ -47,7 +47,6 @@ impl TargetPageAbsenceReason {
#[derive(Debug, Clone)]
pub(crate) struct DocumentNavigationToken {
pub(crate) target_id: String,
- pub(crate) initiating_session_id: Option<String>,
pub(crate) loader_id: String,
pub(crate) request_id: NavigationRequestId,
}
@@ -70,6 +69,74 @@ impl Hash for DocumentNavigationToken {
}
}
+/// The target-owned lifetime of one cross-Document navigation request.
+///
+/// The exact token remains here from navigation admission until the request
+/// either commits or fails. Background navigation additionally keeps this
+/// owner alive until its lifecycle completion is drained; transport
+/// cancellation is therefore retired by the same exact-token transition as
+/// the protocol gate instead of by a scheduler-side mirror.
+#[derive(Debug)]
+pub(crate) struct PendingNavigationRequest {
+ token: DocumentNavigationToken,
+ cancellation_handles: Vec<moli_fetch::FetchCancelHandle>,
+ background_completion_pending: bool,
+ committed: bool,
+}
+
+impl PendingNavigationRequest {
+ fn new(token: DocumentNavigationToken) -> Self {
+ Self {
+ token,
+ cancellation_handles: vec![moli_fetch::FetchCancelHandle::new()],
+ background_completion_pending: false,
+ committed: false,
+ }
+ }
+
+ fn matches(&self, token: &DocumentNavigationToken) -> bool {
+ self.token == *token
+ }
+
+ fn cancellation_handle(&self) -> moli_fetch::FetchCancelHandle {
+ self.cancellation_handles
+ .first()
+ .expect("a pending navigation request must own cancellation authority")
+ .clone()
+ }
+
+ fn arm_background_completion(
+ &mut self,
+ additional_cancellation: Option<moli_fetch::FetchCancelHandle>,
+ ) {
+ if let Some(cancellation) = additional_cancellation {
+ self.cancellation_handles.push(cancellation);
+ }
+ self.background_completion_pending = true;
+ }
+
+ fn settle_background_completion(&mut self) {
+ self.background_completion_pending = false;
+ self.cancellation_handles.clear();
+ }
+
+ fn retire_without_cancellation(&mut self) {
+ self.cancellation_handles.clear();
+ }
+
+ fn cancel(&self) {
+ for cancellation in &self.cancellation_handles {
+ cancellation.cancel();
+ }
+ }
+}
+
+impl Drop for PendingNavigationRequest {
+ fn drop(&mut self) {
+ self.cancel();
+ }
+}
+
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CommittedRendererDocumentBinding {
pub(crate) renderer_frame: RendererFrameToken,
@@ -265,7 +332,7 @@ pub(crate) struct TargetPageSlot {
loaded_page_absence_reason: TargetPageAbsenceReason,
page_attachment_id: Option<TargetPageAttachmentId>,
loaded_page_generation: u64,
- pending_document_navigation: Option<DocumentNavigationToken>,
+ pending_navigation_request: Option<PendingNavigationRequest>,
committed_document_navigation: Option<DocumentNavigationToken>,
renderer_document_lifecycle: RendererDocumentLifecycleProtocolState,
next_renderer_document_lifecycle_waiter_id: u64,
@@ -467,7 +534,6 @@ impl TargetPageSlot {
pub(crate) fn start_document_navigation(
&mut self,
target_id: String,
- session_id: Option<String>,
loader_id: String,
) -> DocumentNavigationToken {
self.finish_renderer_document_lifecycle_observers(
@@ -475,21 +541,74 @@ impl TargetPageSlot {
);
let token = DocumentNavigationToken {
target_id,
- initiating_session_id: session_id,
loader_id,
request_id: NavigationRequestId::allocate(),
};
self.pending_renderer_page = None;
- self.pending_document_navigation = Some(token.clone());
+ self.pending_navigation_request = Some(PendingNavigationRequest::new(token.clone()));
token
}
+ pub(crate) fn document_navigation_cancellation_handle(
+ &self,
+ token: &DocumentNavigationToken,
+ ) -> Option<moli_fetch::FetchCancelHandle> {
+ self.pending_navigation_request
+ .as_ref()
+ .filter(|request| request.matches(token) && !request.committed)
+ .map(PendingNavigationRequest::cancellation_handle)
+ }
+
+ pub(crate) fn arm_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ additional_cancellation: Option<moli_fetch::FetchCancelHandle>,
+ ) -> bool {
+ let Some(request) = self.pending_navigation_request.as_mut().filter(|request| {
+ request.matches(token) && !request.committed && !request.background_completion_pending
+ }) else {
+ if let Some(cancellation) = additional_cancellation {
+ cancellation.cancel();
+ }
+ return false;
+ };
+ request.arm_background_completion(additional_cancellation);
+ true
+ }
+
+ pub(crate) fn settle_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ ) -> bool {
+ let Some(request) = self
+ .pending_navigation_request
+ .as_mut()
+ .filter(|request| request.matches(token) && request.background_completion_pending)
+ else {
+ return false;
+ };
+ request.settle_background_completion();
+ if request.committed {
+ self.pending_navigation_request = None;
+ }
+ true
+ }
+
+ pub(crate) fn has_inflight_background_navigation(&self) -> bool {
+ self.pending_navigation_request
+ .as_ref()
+ .is_some_and(|request| request.background_completion_pending)
+ }
+
pub(crate) fn bind_pending_document_navigation_renderer_page(
&mut self,
token: &DocumentNavigationToken,
renderer_page: RendererPageResidenceIdentity,
) -> bool {
- if self.pending_document_navigation.as_ref() != Some(token)
+ if !self
+ .pending_navigation_request
+ .as_ref()
+ .is_some_and(|request| request.matches(token) && !request.committed)
|| self.pending_renderer_page.is_some()
{
return false;
@@ -518,26 +637,32 @@ impl TargetPageSlot {
&self,
token: &DocumentNavigationToken,
) -> bool {
- self.pending_document_navigation.as_ref() == Some(token)
+ self.pending_navigation_request
+ .as_ref()
+ .is_some_and(|request| request.matches(token) && !request.committed)
}
pub(crate) fn accepts_document_body_completion_event(
&self,
token: &DocumentNavigationToken,
) -> bool {
- match self.pending_document_navigation.as_ref() {
- Some(pending) => pending == token,
+ match self.pending_navigation_request.as_ref() {
+ Some(pending) => pending.matches(token),
None => self.committed_document_navigation.as_ref() == Some(token),
}
}
pub(crate) fn has_pending_document_navigation(&self) -> bool {
- self.pending_document_navigation.is_some()
+ self.pending_navigation_request
+ .as_ref()
+ .is_some_and(|request| !request.committed)
}
pub(crate) fn current_document_loader_id(&self) -> Option<&str> {
- self.pending_document_navigation
+ self.pending_navigation_request
.as_ref()
+ .filter(|request| !request.committed)
+ .map(|request| &request.token)
.or(self.committed_document_navigation.as_ref())
.map(|navigation| navigation.loader_id.as_str())
}
@@ -552,11 +677,19 @@ impl TargetPageSlot {
&mut self,
token: &DocumentNavigationToken,
) -> bool {
- if self.pending_document_navigation.as_ref() != Some(token) {
+ let Some(request) = self
+ .pending_navigation_request
+ .as_mut()
+ .filter(|request| request.matches(token) && !request.committed)
+ else {
return false;
- }
+ };
self.committed_document_navigation = Some(token.clone());
- self.pending_document_navigation = None;
+ request.committed = true;
+ if !request.background_completion_pending {
+ request.retire_without_cancellation();
+ self.pending_navigation_request = None;
+ }
true
}
@@ -565,9 +698,9 @@ impl TargetPageSlot {
loader_id: &str,
) -> bool {
if self
- .pending_document_navigation
+ .pending_navigation_request
.as_ref()
- .is_some_and(|pending| pending.loader_id == loader_id)
+ .is_some_and(|pending| !pending.committed && pending.token.loader_id == loader_id)
{
if matches!(
self.pending_renderer_page.as_ref(),
@@ -578,7 +711,7 @@ impl TargetPageSlot {
) {
self.pending_renderer_page = None;
}
- self.pending_document_navigation = None;
+ self.pending_navigation_request = None;
return true;
}
false
@@ -588,7 +721,7 @@ impl TargetPageSlot {
self.finish_renderer_document_lifecycle_observers(
RendererDocumentLifecycleObservation::Unavailable,
);
- self.pending_document_navigation = None;
+ self.pending_navigation_request = None;
self.committed_document_navigation = None;
self.pending_renderer_page = None;
self.renderer_document_lifecycle = RendererDocumentLifecycleProtocolState::default();
@@ -1157,7 +1290,7 @@ impl TargetPageSlot {
pub(crate) fn take_root_network_idle_binding(
&mut self,
) -> Option<CommittedRendererDocumentBinding> {
- if self.pending_document_navigation.is_some() {
+ if self.has_pending_document_navigation() {
return None;
}
if !self.root_post_load_binding_is_current() {
@@ -1234,11 +1367,8 @@ mod pending_renderer_page_tests {
#[test]
fn navigation_binding_cannot_follow_a_superseding_navigation() {
let mut slot = TargetPageSlot::default();
- let first = slot.start_document_navigation(
- "TID-pending-page".to_owned(),
- Some("SID-first".to_owned()),
- "LOADER-first".to_owned(),
- );
+ let first = slot
+ .start_document_navigation("TID-pending-page".to_owned(), "LOADER-first".to_owned());
let first_page = renderer_page(8, 21);
assert!(slot.bind_pending_document_navigation_renderer_page(&first, first_page));
assert!(slot.routes_renderer_page(first_page));
@@ -1247,11 +1377,8 @@ mod pending_renderer_page_tests {
"one navigation generation cannot replace its bound renderer Page"
);
- let second = slot.start_document_navigation(
- "TID-pending-page".to_owned(),
- Some("SID-second".to_owned()),
- "LOADER-second".to_owned(),
- );
+ let second = slot
+ .start_document_navigation("TID-pending-page".to_owned(), "LOADER-second".to_owned());
assert!(
!slot.routes_renderer_page(first_page),
"a new navigation must retire the prior pending renderer Page route"
@@ -1392,11 +1519,8 @@ mod renderer_document_lifecycle_tests {
);
let mut slot = page_slot_with_attachment();
slot.set_page_attachment_id_for_test(4);
- let navigation = slot.start_document_navigation(
- "FRAME-9".to_owned(),
- Some("SID-9".to_owned()),
- "LOADER-9".to_owned(),
- );
+ let navigation =
+ slot.start_document_navigation("FRAME-9".to_owned(), "LOADER-9".to_owned());
assert!(slot.commit_pending_document_navigation_if_matches(&navigation));
let accepted = slot.bind_renderer_document_lifecycle(
RendererPageCreationArtifacts {
@@ -1506,11 +1630,8 @@ mod renderer_document_lifecycle_tests {
);
let mut slot = page_slot_with_attachment();
slot.set_page_attachment_id_for_test(5);
- let navigation = slot.start_document_navigation(
- "FRAME-10".to_owned(),
- Some("SID-10".to_owned()),
- "LOADER-10".to_owned(),
- );
+ let navigation =
+ slot.start_document_navigation("FRAME-10".to_owned(), "LOADER-10".to_owned());
assert!(slot.commit_pending_document_navigation_if_matches(&navigation));
assert_eq!(
slot.bind_renderer_document_lifecycle(
@@ -1738,11 +1859,8 @@ mod renderer_document_lifecycle_tests {
);
let mut slot = page_slot_with_attachment();
slot.set_page_attachment_id_for_test(6);
- let navigation = slot.start_document_navigation(
- "FRAME-11".to_owned(),
- Some("SID-11".to_owned()),
- "LOADER-11".to_owned(),
- );
+ let navigation =
+ slot.start_document_navigation("FRAME-11".to_owned(), "LOADER-11".to_owned());
assert!(slot.commit_pending_document_navigation_if_matches(&navigation));
assert_eq!(
slot.bind_renderer_document_lifecycle(
@@ -2195,11 +2313,7 @@ mod renderer_document_lifecycle_tests {
assert!(slot.arm_root_post_load_observation("LOADER-12"));
assert!(!slot.arm_root_post_load_observation("LOADER-12"));
- slot.start_document_navigation(
- "FRAME-12".to_owned(),
- Some("SID-12".to_owned()),
- "LOADER-13".to_owned(),
- );
+ slot.start_document_navigation("FRAME-12".to_owned(), "LOADER-13".to_owned());
assert!(slot.take_root_network_idle_binding().is_none());
assert_eq!(
slot.take_root_frame_stopped_loading_binding()
diff --git a/moli-protocol/src/conn/state/runtime_slot.rs b/moli-protocol/src/conn/state/runtime_slot.rs
index f4e8091e7..c5acda4be 100644
--- a/moli-protocol/src/conn/state/runtime_slot.rs
+++ b/moli-protocol/src/conn/state/runtime_slot.rs
@@ -332,13 +332,12 @@ impl TargetRuntimeSlot {
pub(crate) fn start_document_navigation(
&mut self,
target_id: String,
- session_id: Option<String>,
loader_id: String,
) -> DocumentNavigationToken {
self.devtools_renderer_channel.reopen_after_target_crash();
let token = self
.page_slot
- .start_document_navigation(target_id, session_id, loader_id);
+ .start_document_navigation(target_id, loader_id);
self.devtools_renderer_channel
.navigation_started(token.clone())
.expect("an open target runtime slot must accept a new document navigation");
@@ -538,6 +537,35 @@ impl TargetRuntimeSlot {
.accepts_pending_document_navigation_event(token)
}
+ pub(crate) fn document_navigation_cancellation_handle(
+ &self,
+ token: &DocumentNavigationToken,
+ ) -> Option<moli_fetch::FetchCancelHandle> {
+ self.page_slot
+ .document_navigation_cancellation_handle(token)
+ }
+
+ pub(crate) fn arm_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ additional_cancellation: Option<moli_fetch::FetchCancelHandle>,
+ ) -> bool {
+ self.page_slot
+ .arm_background_navigation_completion(token, additional_cancellation)
+ }
+
+ pub(crate) fn settle_background_navigation_completion(
+ &mut self,
+ token: &DocumentNavigationToken,
+ ) -> bool {
+ self.page_slot
+ .settle_background_navigation_completion(token)
+ }
+
+ pub(crate) fn has_inflight_background_navigation(&self) -> bool {
+ self.page_slot.has_inflight_background_navigation()
+ }
+
pub(crate) fn accepts_document_body_completion_event(
&self,
token: &DocumentNavigationToken,
diff --git a/moli-protocol/src/conn/state/tests.rs b/moli-protocol/src/conn/state/tests.rs
index 1ad454427..0ae9ab7ba 100644
--- a/moli-protocol/src/conn/state/tests.rs
+++ b/moli-protocol/src/conn/state/tests.rs
@@ -1471,12 +1471,6 @@ fn navigation_request_identity_rejects_stale_tokens_without_ordering() {
.start_document_navigation_for_active_target("LOADER-1".to_owned())
.expect("active target should create first navigation token");
assert!(context.accepts_pending_document_navigation_event(&first));
- let mut same_request_from_different_session = first.clone();
- same_request_from_different_session.initiating_session_id = Some("SID-other".to_owned());
- assert_eq!(
- same_request_from_different_session, first,
- "initiating session is dispatch metadata, not navigation identity"
- );
let second = context
.start_document_navigation_for_active_target("LOADER-2".to_owned())
@@ -1488,7 +1482,6 @@ fn navigation_request_identity_rejects_stale_tokens_without_ordering() {
);
assert_ne!(second.request_id, first.request_id);
assert_eq!(second.target_id, "TID-nav");
- assert_eq!(second.initiating_session_id.as_deref(), Some("SID-nav"));
assert_eq!(second.loader_id, "LOADER-2");
}
@@ -1658,11 +1651,7 @@ fn background_target_initial_empty_document_record_tracks_navigation_lifecycle()
);
let token = context
- .start_document_navigation_for_target(
- "TID-initial-bg",
- Some("SID-initial-bg".to_owned()),
- "LOADER-initial-bg".to_owned(),
- )
+ .start_document_navigation_for_target("TID-initial-bg", "LOADER-initial-bg".to_owned())
.expect("background target should start document navigation");
assert!(
context
diff --git a/moli-protocol/src/domains/activity/main_document.rs b/moli-protocol/src/domains/activity/main_document.rs
index 6e330efc3..b84f6650d 100644
--- a/moli-protocol/src/domains/activity/main_document.rs
+++ b/moli-protocol/src/domains/activity/main_document.rs
@@ -1637,12 +1637,8 @@ mod tests {
Some(&binding),
RendererDocumentLifecycleMilestone::Load,
);
- conn.start_document_navigation_for_session_owner(
- Some("SID-nav"),
- Some("SID-nav".to_owned()),
- "LID-2".to_owned(),
- )
- .expect("replacement navigation token");
+ conn.start_document_navigation_for_session_owner(Some("SID-nav"), "LID-2".to_owned())
+ .expect("replacement navigation token");
assert_eq!(
observer.wait().await,
@@ -1673,12 +1669,8 @@ mod tests {
conn.enqueue_deferred_main_document_load_completion(old_completion);
let work = take_deferred_load_work_for_test(&mut conn);
- conn.start_document_navigation_for_session_owner(
- Some("SID-nav"),
- Some("SID-nav".to_owned()),
- "LID-2".to_owned(),
- )
- .expect("replacement navigation token");
+ conn.start_document_navigation_for_session_owner(Some("SID-nav"), "LID-2".to_owned())
+ .expect("replacement navigation token");
assert!(
conn.take_scheduler_events().is_empty(),
diff --git a/moli-protocol/src/domains/debugger.rs b/moli-protocol/src/domains/debugger.rs
index 84b6f6829..a70ce09b1 100644
--- a/moli-protocol/src/domains/debugger.rs
+++ b/moli-protocol/src/domains/debugger.rs
@@ -155,7 +155,6 @@ mod tests {
let navigation = ctx
.conn
.start_document_navigation_for_session_owner(
- None,
None,
"LOADER-debugger-interrupt".to_owned(),
)
diff --git a/moli-protocol/src/domains/emulation/tests.rs b/moli-protocol/src/domains/emulation/tests.rs
index f5361b4d8..b14f79646 100644
--- a/moli-protocol/src/domains/emulation/tests.rs
+++ b/moli-protocol/src/domains/emulation/tests.rs
@@ -421,11 +421,7 @@ async fn idle_override_updates_idle_detector_and_clear_restores_actual_state() {
}));
ctx.conn
- .start_document_navigation_for_session_owner(
- Some("SID-1"),
- Some("SID-1".to_owned()),
- "LID-idle-same-site".to_owned(),
- )
+ .start_document_navigation_for_session_owner(Some("SID-1"), "LID-idle-same-site".to_owned())
.expect("same-site navigation should enter the pending state");
let configuration = ctx
.conn
diff --git a/moli-protocol/src/domains/fetch/commands.rs b/moli-protocol/src/domains/fetch/commands.rs
index 2731edae2..45f3ed371 100644
--- a/moli-protocol/src/domains/fetch/commands.rs
+++ b/moli-protocol/src/domains/fetch/commands.rs
@@ -1,7 +1,6 @@
use crate::conn::{
- BackgroundNavigationBodyCompletionSink, BackgroundNavigationCancellation, CapturedBody,
- CdpConnection, Cmd, DEFAULT_LOADER_ID, PendingStreamingDocumentResponseNavigation,
- monotonic_timestamp_seconds,
+ BackgroundNavigationBodyCompletionSink, CapturedBody, CdpConnection, Cmd, DEFAULT_LOADER_ID,
+ PendingStreamingDocumentResponseNavigation, monotonic_timestamp_seconds,
};
use crate::devtools_runtime::{
DevToolsAuthChallengeAction, DevToolsCommand, DevToolsContinueInterceptedRequestCommand,
@@ -1896,17 +1895,12 @@ fn continue_streaming_document_response_in_background(
.is_none()
.then(|| conn.none_session_owner_route_override())
.flatten();
- let cancellation =
- BackgroundNavigationCancellation::from_fetch_cancel_handle(response.cancellation_handle());
+ let cancellation = response.cancellation_handle();
if response_code.is_none()
&& response_headers.is_empty()
&& let Some(prepared_document) = prepared_document
{
- conn.record_background_navigation_started_scheduler_event(
- &document_navigation_token,
- &navigation,
- cancellation,
- );
+ conn.arm_background_navigation_completion(&document_navigation_token, Some(cancellation));
tokio::task::spawn_local(async move {
let body_completion_sink = BackgroundNavigationBodyCompletionSink::new(
sender.clone(),
@@ -1934,11 +1928,7 @@ fn continue_streaming_document_response_in_background(
response_headers,
body_progress_source,
);
- conn.record_background_navigation_started_scheduler_event(
- &document_navigation_token,
- &navigation,
- cancellation,
- );
+ conn.arm_background_navigation_completion(&document_navigation_token, Some(cancellation));
tokio::task::spawn_local(async move {
let body_completion_sink = BackgroundNavigationBodyCompletionSink::new(
sender.clone(),
diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rs
index a16841ef1..a2feeabfa 100644
--- a/moli-protocol/src/domains/page/navigation.rs
+++ b/moli-protocol/src/domains/page/navigation.rs
@@ -535,20 +535,6 @@ impl BackgroundNavigationCompletion {
Self::MainDocumentBody(_) => "main_document_body",
}
}
-
- pub fn background_navigation_gate_key(
- &self,
- ) -> Option<crate::conn::BackgroundNavigationGateKey> {
- match self {
- Self::Lifecycle(completion) => {
- Some(crate::conn::BackgroundNavigationGateKey::for_navigation(
- &completion.token,
- &completion.state,
- ))
- }
- Self::MainDocumentBody(_) => None,
- }
- }
}
pub struct BackgroundNavigationLifecycleCompletion {
@@ -582,6 +568,10 @@ impl BackgroundNavigationLifecycleCompletion {
self.state.navigate_session_id.as_deref()
}
+ pub(crate) fn navigation_token(&self) -> &DocumentNavigationToken {
+ &self.token
+ }
+
pub(crate) fn none_session_owner_route(&self) -> Option<CdpSessionRoute> {
self.none_session_owner_route.clone()
}
@@ -2892,7 +2882,6 @@ fn start_navigate_to_url_command_with_background_policy_and_request(
let navigation_loader_id = navigation_state.loader_id.as_str();
let document_navigation_token = conn.start_document_navigation_for_session_owner(
command_session_id,
- session_id.clone(),
navigation_loader_id.to_owned(),
);
let Some(document_navigation_token) = document_navigation_token else {
@@ -3026,22 +3015,22 @@ fn start_navigate_to_url_command_with_background_policy_and_request(
)
})
});
- let job = conn.background_navigation_load_job_for_navigation(
+ let Some(job) = conn.background_navigation_load_job_for_navigation(
+ &document_navigation_token,
&completion_state,
body_progress_source,
early_result,
- );
- let cancellation = job.cancellation();
+ ) else {
+ return NavigateCommandStart::CompletePlan(CommandOutputPlan::error(
+ -31998,
+ "NavigationRequestNotCurrent",
+ ));
+ };
let none_session_owner_route = completion_state
.navigate_session_id
.is_none()
.then(|| conn.none_session_owner_route_override())
.flatten();
- conn.record_background_navigation_started_scheduler_event(
- &document_navigation_token,
- &completion_state,
- cancellation,
- );
tokio::task::spawn_local(async move {
let body_completion_sink = crate::conn::BackgroundNavigationBodyCompletionSink::new(
sender.clone(),
@@ -3092,11 +3081,17 @@ fn start_navigate_to_url_command_with_background_policy_and_request(
network::MainDocumentBodyProgressSource::default()
};
navigation_state.request_announced = navigation_state.request_id.is_some();
- let job = conn.background_navigation_load_job_for_navigation(
+ let Some(job) = conn.navigation_load_job_for_navigation(
+ &document_navigation_token,
&navigation_state,
body_progress_source,
None,
- );
+ ) else {
+ return NavigateCommandStart::CompletePlan(CommandOutputPlan::error(
+ -31998,
+ "NavigationRequestNotCurrent",
+ ));
+ };
NavigateCommandStart::PendingLoad(Box::new(PendingNavigateLoadCommand {
prefix_events: out,
token: document_navigation_token,
diff --git a/moli-protocol/src/domains/page/tests/lifecycle.rs b/moli-protocol/src/domains/page/tests/lifecycle.rs
index 0589773b5..cad6331f1 100644
--- a/moli-protocol/src/domains/page/tests/lifecycle.rs
+++ b/moli-protocol/src/domains/page/tests/lifecycle.rs
@@ -2654,7 +2654,6 @@ async fn crash_targets_background_owner_without_promotion() {
let background_document_token = bc
.start_document_navigation_for_target(
"TID-background",
- Some("SID-background".to_owned()),
"LOADER-background-crash".to_owned(),
)
.expect("background target should start document navigation");
diff --git a/moli-protocol/src/domains/runtime/tests/evaluate.rs b/moli-protocol/src/domains/runtime/tests/evaluate.rs
index 2ef76ab02..5646e3b4e 100644
--- a/moli-protocol/src/domains/runtime/tests/evaluate.rs
+++ b/moli-protocol/src/domains/runtime/tests/evaluate.rs
@@ -1932,7 +1932,6 @@ async fn document_navigation_gate_is_scoped_to_background_target_owner() {
browser_context
.start_document_navigation_for_target(
"TID-background",
- Some("SID-background".to_owned()),
"PENDING-BACKGROUND-LOADER".to_owned(),
)
.expect("background document navigation should start");
diff --git a/moli-protocol/src/lib.rs b/moli-protocol/src/lib.rs
index cb1b4e9e6..6162a5c2c 100644
--- a/moli-protocol/src/lib.rs
+++ b/moli-protocol/src/lib.rs
@@ -18,8 +18,7 @@ pub mod version;
pub use devtools_runtime::*;
pub use conn::{
- BackgroundCommandResponsePayload, BackgroundNavigationCancellation,
- BackgroundNavigationGateKey, BackgroundProtocolEvent, CdpCommandTaskStep, CdpConnection,
+ BackgroundCommandResponsePayload, BackgroundProtocolEvent, CdpCommandTaskStep, CdpConnection,
CdpInitialStoragePartition, CdpRendererCommandAccess, CdpRendererCommandReplacement,
CdpRendererCommandReplayDispatch, CdpSchedulerEvent, CdpTargetHostLifecycleDelta,
CdpTargetHostLifecycleObserver, CdpTurnOutcome, CommandDispatchContext,
diff --git a/moli-protocol/src/test_support.rs b/moli-protocol/src/test_support.rs
index 1024227e5..de9d74be2 100644
--- a/moli-protocol/src/test_support.rs
+++ b/moli-protocol/src/test_support.rs
@@ -9,27 +9,57 @@
use moli_core::{RendererDocumentLifecycleIdentity, RendererOutputResidenceIdentity};
use crate::{
- BackgroundNavigationGateKey, DeferredMainDocumentLoadCompletionOutputInterest,
- DeferredMainDocumentLoadObservationId, ProtocolSchedulerWork,
- conn::RendererPageResidenceIdentity,
+ DeferredMainDocumentLoadCompletionOutputInterest, DeferredMainDocumentLoadObservationId,
+ ProtocolSchedulerWork,
+ conn::{CdpConnection, DocumentNavigationToken, RendererPageResidenceIdentity},
};
-/// Constructs the exact key carried by a synthetic background-navigation
-/// completion in scheduler-only tests.
-pub fn background_navigation_gate_key(
- target_id: Option<String>,
- session_id: Option<String>,
- frame_id: String,
- loader_id: String,
- navigation_request_id: Option<u64>,
-) -> BackgroundNavigationGateKey {
- BackgroundNavigationGateKey::from_test_parts(
- target_id,
- session_id,
- frame_id,
- loader_id,
- navigation_request_id,
- )
+/// Opaque exact-token fixture for scheduler tests that need a real
+/// target-owned background navigation request.
+pub struct BackgroundNavigationRequestFixture {
+ token: DocumentNavigationToken,
+ cancellation: moli_fetch::FetchCancelHandle,
+}
+
+impl BackgroundNavigationRequestFixture {
+ pub fn is_cancelled(&self) -> bool {
+ self.cancellation.is_cancelled()
+ }
+}
+
+pub fn arm_background_navigation_request(
+ conn: &mut CdpConnection,
+ loader_id: &str,
+) -> BackgroundNavigationRequestFixture {
+ conn.install_default_browser_target();
+ let target_id = conn
+ .browser_context
+ .as_ref()
+ .and_then(|context| context.active_target_id())
+ .expect("the default browser target must have an active target")
+ .to_owned();
+ let token = conn
+ .browser_context
+ .as_mut()
+ .and_then(|context| {
+ context.start_document_navigation_for_target(&target_id, loader_id.to_owned())
+ })
+ .expect("the active target must accept a navigation request fixture");
+ let cancellation = conn
+ .document_navigation_cancellation_handle(&token)
+ .expect("the target-owned request must expose its cancellation handle");
+ assert!(conn.arm_background_navigation_completion(&token, None));
+ BackgroundNavigationRequestFixture {
+ token,
+ cancellation,
+ }
+}
+
+pub fn settle_background_navigation_request(
+ conn: &mut CdpConnection,
+ fixture: &BackgroundNavigationRequestFixture,
+) -> bool {
+ conn.settle_background_navigation_completion(&fixture.token)
}
/// Constructs one nonzero deferred-load observation identity.
diff --git a/moli-protocol/src/testing.rs b/moli-protocol/src/testing.rs
index 4f14ff311..89b4d279f 100644
--- a/moli-protocol/src/testing.rs
+++ b/moli-protocol/src/testing.rs
@@ -1875,7 +1875,6 @@ fn enqueue_scheduler_events_like_scheduler(
CdpSchedulerEvent::ProtocolWorkPublished { work } => {
queue.push_back(TestDeferredSchedulerWork(work));
}
- CdpSchedulerEvent::BackgroundNavigationStarted { .. } => {}
CdpSchedulerEvent::PageScreencastStarted { .. } => {}
}
}
diff --git a/moli/src/cdp_scheduler.rs b/moli/src/cdp_scheduler.rs
index 7461a8409..3de4ccac7 100644
--- a/moli/src/cdp_scheduler.rs
+++ b/moli/src/cdp_scheduler.rs
@@ -10,10 +10,9 @@ use moli_core::{
runtime::NavigationRuntimeConfig,
};
use moli_protocol::{
- BackgroundNavigationCancellation, BackgroundNavigationCompletion, BackgroundNavigationGateKey,
- BackgroundProtocolEvent, CdpCommandTaskStep, CdpConnection, CdpInitialStoragePartition,
- CdpRendererCommandAccess, CdpSchedulerEvent, CdpTargetHostLifecycleObserver,
- CommandDispatchContext, CompletedCdpCommandDispatch,
+ BackgroundNavigationCompletion, BackgroundProtocolEvent, CdpCommandTaskStep, CdpConnection,
+ CdpInitialStoragePartition, CdpRendererCommandAccess, CdpSchedulerEvent,
+ CdpTargetHostLifecycleObserver, CommandDispatchContext, CompletedCdpCommandDispatch,
CompletedDeferredMainDocumentLoadCompletion, DeferredMainDocumentLoadCompletionOutputAction,
DeferredMainDocumentLoadCompletionOutputInterest, DeferredMainDocumentLoadObservationId,
DeferredMainDocumentLoadPredecessorCandidate, PageScreencastCaptureCompletion,
@@ -102,7 +101,6 @@ pub(crate) enum CommandStartAction {
pub(crate) struct CdpScheduler {
conn: CdpConnection,
- background_navigation_gate: BackgroundNavigationGate,
pending_navigation_background_events: VecDeque<BackgroundProtocolEvent>,
runtime_command_output_barriers: RuntimeCommandOutputBarriers,
queues: SchedulerQueues,
@@ -131,57 +129,6 @@ fn next_page_screencast_deadline(now: TokioInstant, interval: Duration) -> Tokio
now + interval
}
-#[derive(Debug, Default)]
-struct BackgroundNavigationGate {
- /// `Page.navigate` background tasks whose lifecycle
- /// `BackgroundNavigationCompletion` has not yet been drained. The command
- /// response may be produced independently; this gate does not claim where
- /// that response falls relative to response-head or body EOF.
- pending: HashMap<BackgroundNavigationGateKey, BackgroundNavigationCancellation>,
-}
-
-impl BackgroundNavigationGate {
- fn has_inflight_navigation(&self) -> bool {
- !self.pending.is_empty()
- }
-
- fn note_navigation_started(
- &mut self,
- key: BackgroundNavigationGateKey,
- cancellation: BackgroundNavigationCancellation,
- ) {
- if self.pending.contains_key(&key) {
- return;
- }
- let pending_before = self.pending.len();
- self.pending.retain(|pending, pending_cancellation| {
- if !key.supersedes(pending) {
- return true;
- }
- pending_cancellation.cancel();
- false
- });
- let superseded = pending_before - self.pending.len();
- if superseded > 0 {
- tracing::debug!(
- superseded,
- ?key,
- "retired superseded background navigation gate"
- );
- }
- self.pending.insert(key, cancellation);
- }
-
- fn note_navigation_completion_drained(&mut self, key: &BackgroundNavigationGateKey) {
- if self.pending.remove(key).is_none() {
- tracing::debug!(
- ?key,
- "background navigation completion did not match any pending gate"
- );
- }
- }
-}
-
#[derive(Debug, Default)]
struct ForegroundNavigationNetworkBarrier {
wait_for_document_response_started: bool,
@@ -667,7 +614,6 @@ impl CdpScheduler {
fn new(conn: CdpConnection) -> Self {
Self {
conn,
- background_navigation_gate: BackgroundNavigationGate::default(),
pending_navigation_background_events: VecDeque::new(),
runtime_command_output_barriers: RuntimeCommandOutputBarriers::default(),
queues: SchedulerQueues::default(),
@@ -1809,7 +1755,7 @@ impl CdpScheduler {
}
pub(crate) fn has_inflight_background_navigation(&self) -> bool {
- self.background_navigation_gate.has_inflight_navigation()
+ self.conn.has_inflight_background_navigation()
}
pub(crate) fn command_waits_for_navigation_flush(&self, command: &ParsedCdpCommand) -> bool {
@@ -1921,10 +1867,6 @@ impl CdpScheduler {
);
}
match event {
- CdpSchedulerEvent::BackgroundNavigationStarted { key, cancellation } => {
- self.background_navigation_gate
- .note_navigation_started(key, cancellation);
- }
CdpSchedulerEvent::ProtocolWorkPublished { work } => {
if moli_trace::cdp_nav_timing_enabled() {
tracing::info!(
@@ -2466,10 +2408,6 @@ impl CdpScheduler {
Option<moli_core::RendererOutputFence>,
) {
let trace_started = moli_trace::cdp_runtime_trace_enabled().then(Instant::now);
- if let Some(key) = completion.background_navigation_gate_key() {
- self.background_navigation_gate
- .note_navigation_completion_drained(&key);
- }
let outcome = self
.conn
.drain_background_navigation_completion_turn_async(completion)
diff --git a/moli/src/cdp_scheduler/tests.rs b/moli/src/cdp_scheduler/tests.rs
index 139171daf..aca366204 100644
--- a/moli/src/cdp_scheduler/tests.rs
+++ b/moli/src/cdp_scheduler/tests.rs
@@ -11,8 +11,8 @@ use moli_core::{
},
};
use moli_protocol::{
- BackgroundNavigationCancellation, BackgroundNavigationGateKey, BackgroundProtocolEvent,
- CdpConnection, CdpSchedulerEvent, DeferredMainDocumentLoadCompletionOutputAction,
+ BackgroundProtocolEvent, CdpConnection, CdpSchedulerEvent,
+ DeferredMainDocumentLoadCompletionOutputAction,
DeferredMainDocumentLoadCompletionOutputInterest, DeferredMainDocumentLoadPredecessorCandidate,
ProtocolSchedulerWork,
conn::RuntimeInspectorResponseReady,
@@ -22,16 +22,17 @@ use moli_protocol::{
NavigationFrameEvent, NavigationFrameEventKind, NetworkRequestEvent,
},
test_support::{
- background_navigation_gate_key as make_background_navigation_gate_key,
- deferred_main_document_load_observation_id, deferred_main_document_load_output_interest,
+ arm_background_navigation_request, deferred_main_document_load_observation_id,
+ deferred_main_document_load_output_interest,
root_frame_stopped_loading_work as make_root_frame_stopped_loading_work,
+ settle_background_navigation_request,
},
};
use serde_json::json;
use super::{
- BackgroundNavigationGate, CdpScheduler, ForegroundNavigationNetworkBarrier,
- ProtocolOutputSequence, ProtocolSchedulerResidence, ProtocolSchedulerStep, SchedulerQueues,
+ CdpScheduler, ForegroundNavigationNetworkBarrier, ProtocolOutputSequence,
+ ProtocolSchedulerResidence, ProtocolSchedulerStep, SchedulerQueues,
devtools_navigation_lifecycle_milestone, drain_pending_background_events,
next_page_screencast_deadline, page_screencast_interval,
};
@@ -452,26 +453,6 @@ fn root_document_lifecycle_identity(
}
}
-fn background_navigation_gate_key() -> BackgroundNavigationGateKey {
- background_navigation_gate_key_for("TID-nav", "SID-nav", "FRAME-nav", "LOADER-nav", 1)
-}
-
-fn background_navigation_gate_key_for(
- target_id: &str,
- session_id: &str,
- frame_id: &str,
- loader_id: &str,
- navigation_request_id: u64,
-) -> BackgroundNavigationGateKey {
- make_background_navigation_gate_key(
- Some(target_id.to_owned()),
- Some(session_id.to_owned()),
- frame_id.to_owned(),
- loader_id.to_owned(),
- Some(navigation_request_id),
- )
-}
-
fn root_frame_stopped_loading_work(publish_sequence: u64, frame_id: &str) -> ProtocolSchedulerWork {
make_root_frame_stopped_loading_work(
publish_sequence,
@@ -1090,13 +1071,9 @@ fn concrete_protocol_output_rejects_missing_earlier_publication() {
#[test]
fn background_navigation_blocks_concrete_protocol_residence_not_renderer_ingress() {
- let mut scheduler = CdpScheduler::new(CdpConnection::new());
- scheduler
- .background_navigation_gate
- .note_navigation_started(
- background_navigation_gate_key(),
- BackgroundNavigationCancellation::new(),
- );
+ let mut conn = CdpConnection::new();
+ let _navigation = arm_background_navigation_request(&mut conn, "LOADER-nav");
+ let mut scheduler = CdpScheduler::new(conn);
scheduler.apply_scheduler_events(vec![CdpSchedulerEvent::ProtocolWorkPublished {
work: root_frame_stopped_loading_work(1, "FRAME-1"),
}]);
@@ -1108,61 +1085,30 @@ fn background_navigation_blocks_concrete_protocol_residence_not_renderer_ingress
}
#[test]
-fn replacement_navigation_retires_only_the_superseded_frame_gate() {
- let mut gate = BackgroundNavigationGate::default();
- let source = background_navigation_gate_key_for(
- "TID-nav",
- "SID-source",
- "FRAME-nav",
- "LOADER-source",
- 1,
- );
- let replacement = background_navigation_gate_key_for(
- "TID-nav",
- "SID-replacement",
- "FRAME-nav",
- "LOADER-replacement",
- 2,
- );
- let unrelated = background_navigation_gate_key_for(
- "TID-other",
- "SID-other",
- "FRAME-other",
- "LOADER-other",
- 3,
- );
- let source_cancellation = BackgroundNavigationCancellation::new();
- let replacement_cancellation = BackgroundNavigationCancellation::new();
- let unrelated_cancellation = BackgroundNavigationCancellation::new();
-
- gate.note_navigation_started(source.clone(), source_cancellation.clone());
- gate.note_navigation_started(unrelated.clone(), unrelated_cancellation.clone());
- gate.note_navigation_started(replacement.clone(), replacement_cancellation.clone());
- assert!(source_cancellation.is_cancelled());
- assert!(!replacement_cancellation.is_cancelled());
- assert!(!unrelated_cancellation.is_cancelled());
-
- gate.note_navigation_completion_drained(&source);
+fn replacement_navigation_cancels_and_exactly_settles_the_target_owned_request() {
+ let mut conn = CdpConnection::new();
+ let source = arm_background_navigation_request(&mut conn, "LOADER-source");
+ let replacement = arm_background_navigation_request(&mut conn, "LOADER-replacement");
+ assert!(source.is_cancelled());
+ assert!(!replacement.is_cancelled());
+
+ assert!(!settle_background_navigation_request(&mut conn, &source));
assert!(
- gate.has_inflight_navigation(),
- "a stale completion must not clear either live navigation gate"
+ conn.has_inflight_background_navigation(),
+ "a stale completion must not clear the replacement request"
);
- gate.note_navigation_completion_drained(&replacement);
- assert!(
- gate.has_inflight_navigation(),
- "replacing one frame must not clear an unrelated target's gate"
- );
- gate.note_navigation_completion_drained(&unrelated);
- assert!(!gate.has_inflight_navigation());
+ assert!(settle_background_navigation_request(
+ &mut conn,
+ &replacement
+ ));
+ assert!(!conn.has_inflight_background_navigation());
}
#[test]
fn scheduler_defers_subresource_network_events_until_background_navigation_gate_clears() {
- let mut scheduler = CdpScheduler::new(CdpConnection::new());
- let key = background_navigation_gate_key();
- scheduler
- .background_navigation_gate
- .note_navigation_started(key.clone(), BackgroundNavigationCancellation::new());
+ let mut conn = CdpConnection::new();
+ let navigation = arm_background_navigation_request(&mut conn, "LOADER-nav");
+ let mut scheduler = CdpScheduler::new(conn);
let document_output = scheduler.route_background_event_around_inflight_navigation(
network_response_event(DevToolsNetworkResourceType::Document, "REQ-document"),
@@ -1179,9 +1125,10 @@ fn scheduler_defers_subresource_network_events_until_background_navigation_gate_
assert!(script_terminal.is_empty());
assert_eq!(scheduler.pending_navigation_background_events.len(), 2);
- scheduler
- .background_navigation_gate
- .note_navigation_completion_drained(&key);
+ assert!(settle_background_navigation_request(
+ &mut scheduler.conn,
+ &navigation
+ ));
let released = scheduler.drain_pending_navigation_background_events();
let released = released
.into_background_events()
@@ -1211,11 +1158,9 @@ fn scheduler_defers_subresource_network_events_until_background_navigation_gate_
#[test]
fn navigation_gate_release_precedes_later_renderer_boundary_network_output() {
- let mut scheduler = CdpScheduler::new(CdpConnection::new());
- let key = background_navigation_gate_key();
- scheduler
- .background_navigation_gate
- .note_navigation_started(key.clone(), BackgroundNavigationCancellation::new());
+ let mut conn = CdpConnection::new();
+ let navigation = arm_background_navigation_request(&mut conn, "LOADER-nav");
+ let mut scheduler = CdpScheduler::new(conn);
let request_id = "REQ-boundary-race";
assert!(
@@ -1227,9 +1172,10 @@ fn navigation_gate_release_precedes_later_renderer_boundary_network_output() {
.is_empty()
);
- scheduler
- .background_navigation_gate
- .note_navigation_completion_drained(&key);
+ assert!(settle_background_navigation_request(
+ &mut scheduler.conn,
+ &navigation
+ ));
let mut output = ProtocolOutputSequence::empty();
scheduler.append_navigation_gate_release_before_renderer_boundary(&mut output);
output.append(ProtocolOutputSequence::from_background_event(