diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs index bbc13ab552d..de74e6f02eb 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs @@ -198,5 +198,10 @@ pub(crate) fn install_navigation_entry_view_for_holder<'s>( current_entry, entry_seed.activation.as_ref(), ); + if let Some(entry) = &entry_seed.session_history.admitted_entry { + // An accepted child load may reuse a Window whose Document committed + // before this projection was refreshed. Prune only the installed view. + super::session_history_traversal::finish_entry(scope, owner, Some(&entry.key)); + } super::navigation_serialize::publish_top_level_navigation_history(scope, owner); } diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs index f343ae9f185..6bd4711f755 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_coordinator.rs @@ -16,7 +16,7 @@ use super::navigation_traversal_execution::TraversalTarget; use super::navigation_traversal_plan::JointTraversalPlan; use super::navigation_window::{ child_browsing_context_handle_for_runtime_owner, navigation_document_has_opaque_origin, - navigation_document_is_active, window_location_for_holder, window_navigation_for_holder, + navigation_document_is_active, window_navigation_for_holder, window_task_target_for_runtime_owner, }; use crate::document_runtime::DomHandle; @@ -104,6 +104,10 @@ fn capture_participants<'s>( history: v8::Global::new(scope, target.history), navigation: navigation.map(|value| v8::Global::new(scope, value)), source: entry_reference(scope, target.history, target.current_index)?, + source_url: history_entries(scope, target.history) + .and_then(|entries| entries.get(target.current_index as usize).cloned()) + .map(|entry| entry.borrow().url.clone()) + .unwrap_or_default(), destination: entry_reference(scope, target.history, target.target_index)?, outcome: Default::default(), }) @@ -157,6 +161,16 @@ pub(super) fn execute<'s>( } else { dispatch_beforeunload_for_runtime_owner(scope, target.owner); } + if validate(scope, &admission).is_none() { + abort(scope, &admission, None); + return; + } + if let Some(navigation) = &admission.participants[index].navigation { + let navigation = v8::Local::new(scope, navigation); + super::navigation_result::cancel_active_cross_document_navigation( + scope, navigation, None, + ); + } } if validate(scope, &admission).is_none() { abort(scope, &admission, None); @@ -337,13 +351,25 @@ fn settle_aborted_admission<'s>( admission: &PendingHistoryTraversalAdmission, error: Option>, ) { - let canceled = error.is_none(); let _execution = crate::script_cleanup::ScriptExecutionScope::enter(scope); let error = error.unwrap_or_else(|| { navigation_dom_exception(scope, "History traversal was canceled", "AbortError") }); let mut results_rejected = false; for participant in &admission.participants { + let signal = participant + .outcome + .signal + .as_ref() + .map(|signal| v8::Local::new(scope, signal)); + // A participant without a signal has no event to abort. An earlier abort + // has already notified its listeners. + let Some(signal) = signal.filter(|signal| { + !context_host_ptr_from_global_bridge(scope) + .is_some_and(|host| unsafe { &mut *host }.abort_signal_aborted(scope, *signal)) + }) else { + continue; + }; let navigation = participant .navigation .as_ref() @@ -359,41 +385,18 @@ fn settle_aborted_admission<'s>( }) .and_then(|_| take_navigation_transition_committed_resolver(scope, navigation)) }); - let signal = participant - .outcome - .signal - .as_ref() - .map(|signal| v8::Local::new(scope, signal)); - // An event aborted during dispatch already notified its listeners. - let report_abort = if let Some(signal) = signal - && !context_host_ptr_from_global_bridge(scope) - .is_some_and(|host| unsafe { &mut *host }.abort_signal_aborted(scope, signal)) - { - crate::native_bridge::abort::abort_signal(scope, signal, error); - true - } else { - false - }; + crate::native_bridge::abort::abort_signal(scope, signal, error); if !results_rejected { results::reject_pending_navigation_results(scope, &admission.results, error); results_rejected = true; } if let Some(navigation) = navigation { - if report_abort { - let filename = if canceled { - let owner = super::navigation_window::runtime_window_owner(scope, navigation); - window_location_for_holder(scope, owner) - .and_then(|location| { - super::location_runtime::location_href_slot(scope, location) - }) - .unwrap_or_default() - } else { - String::new() - }; - super::navigation_lifecycle::finish_navigation_error_events( - scope, navigation, error, &filename, - ); - } + super::navigation_lifecycle::finish_navigation_error_events( + scope, + navigation, + error, + &participant.source_url, + ); if let Some(resolver) = committed_resolver { let _ = resolver.reject(scope, error); } diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_execution.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_execution.rs index 289492ba1c9..f0023909ef3 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_execution.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_execution.rs @@ -150,7 +150,7 @@ fn apply_history_traversal_by_delta( host: &mut PageScriptHost, exact_target: crate::native_bridge::WindowTaskTarget, delta: i64, - from: moli_page_types::SessionHistoryStepId, + from: moli_session_history::SessionHistoryStepId, ) { let Some(owner) = history_traversal_target_window(scope, host, exact_target) else { return; diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs index 2ccd9366cce..3e365d74821 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_traversal_plan.rs @@ -215,7 +215,7 @@ pub(super) fn history_delta_traversal_target<'s>( scope: &mut v8::PinScope<'s, '_>, history: v8::Local<'s, v8::Object>, delta: i64, - from: Option, + from: Option, ) -> Option> { let owner = runtime_window_owner(scope, history); let host = unsafe { &mut *crate::util::context_host_ptr_from_global_bridge(scope)? }; diff --git a/moli-renderer-v8/src/context_bootstrap/session_history.rs b/moli-renderer-v8/src/context_bootstrap/session_history.rs index 3c6b51019b5..4feabbd3f01 100644 --- a/moli-renderer-v8/src/context_bootstrap/session_history.rs +++ b/moli-renderer-v8/src/context_bootstrap/session_history.rs @@ -133,7 +133,15 @@ pub(super) fn restore<'s>( moli_page_types::SessionHistoryUpdateKind::Traverse { delta }, ); } - super::session_history_traversal::finish_entry(scope, owner, Some(&entry.key)); + // Document ownership can commit before a preserved Window installs its + // new history view. Release the barrier only once that view is current. + if super::navigation_entry::navigation_current_entry(scope, owner) + .and_then(|entry| entry_reference(scope, entry)) + .as_ref() + == Some(&entry) + { + super::session_history_traversal::finish_entry(scope, owner, Some(&entry.key)); + } let root = binding.popup.map_or( OwnerDispatchScope::Top, OwnerDispatchScope::LightweightPopup, diff --git a/moli-renderer-v8/src/context_bootstrap/session_history_traversal.rs b/moli-renderer-v8/src/context_bootstrap/session_history_traversal.rs index ae0e99e712b..7f7b48ce704 100644 --- a/moli-renderer-v8/src/context_bootstrap/session_history_traversal.rs +++ b/moli-renderer-v8/src/context_bootstrap/session_history_traversal.rs @@ -219,16 +219,23 @@ pub(crate) fn finish_without_document_commit( return; } let context = host.session_histories.context(owner); - if let Some(step) = host + if let Some((step, delta)) = host .session_histories - .finish_traversal_entry(popup, context, None) + .finish_traversal_without_document_commit(popup, context) && let Some(root) = session_history::owner_for_context( scope, host, - moli_page_types::SessionHistoryContextId::ROOT, + moli_session_history::SessionHistoryContextId::ROOT, popup, ) { + if let Some(delta) = delta.filter(|delta| *delta != 0) { + session_history::publish( + scope, + root, + moli_page_types::SessionHistoryUpdateKind::Traverse { delta }, + ); + } // A retiring child must never materialize its Window again. finish(scope, host_ptr, root, popup, step); } diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs index a4055df31aa..ca8c2927c01 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/nested_history.rs @@ -1,7 +1,7 @@ use super::*; use crate::context_bootstrap::navigation_entry_public_token; use crate::native_bridge::OwnerDispatchScope; -use moli_page_types::{ +use moli_session_history::{ JointSessionHistory, SessionHistoryContextId, SessionHistoryEntry, SessionHistoryStepId, }; @@ -212,6 +212,9 @@ impl PageScriptHost { .find(|entry| navigation_entry_public_token(entry.key.as_str()) == key.as_str())? .clone(); record.seed.current_index = target.history_index; + // The parent already selected the joint position. Reattach the child's + // historical view without replaying an older load's traversal admission. + record.seed.session_history = Default::default(); record.seed.activation = Some(NavigationActivationSeed { entry: target, from: None, diff --git a/moli-renderer-v8/src/native_bridge/context_host/session_history.rs b/moli-renderer-v8/src/native_bridge/context_host/session_history.rs index a0f3974b690..1d87493d617 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/session_history.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/session_history.rs @@ -18,6 +18,7 @@ struct PendingSessionHistoryTraversal { step: SessionHistoryStepId, committed: bool, remaining: HashMap, + rollback: Option, } impl RendererSessionHistories { @@ -65,6 +66,7 @@ impl RendererSessionHistories { step, committed: false, remaining: targets.into_iter().collect(), + rollback: None, }, ); } @@ -76,10 +78,12 @@ impl RendererSessionHistories { ) -> Option { let step = plan.target_step(); let delta = self.get_mut(popup).commit_traversal(plan)?; + let rollback = self.get_mut(popup).plan_traversal(plan.source_step()); if let Some(pending) = self.pending.get_mut(&popup) && pending.step == step { pending.committed = true; + pending.rollback = rollback; } Some(delta) } @@ -92,6 +96,7 @@ impl RendererSessionHistories { ) -> Option { let pending = self.pending.get_mut(&popup)?; if key.is_none_or(|key| pending.remaining.get(&context) == Some(key)) { + pending.rollback = None; pending.remaining.remove(&context); } if !pending.remaining.is_empty() { @@ -105,6 +110,32 @@ impl RendererSessionHistories { }) } + pub(crate) fn finish_traversal_without_document_commit( + &mut self, + popup: Option, + context: SessionHistoryContextId, + ) -> Option<(SessionHistoryStepId, Option)> { + let pending = self.pending.get_mut(&popup)?; + pending.remaining.remove(&context)?; + if !pending.remaining.is_empty() { + return None; + } + let pending = self.pending.remove(&popup)?; + // No participant materialized its destination. An ignored response + // must not consume a history.back(), but a newer mutation wins over + // this reservation: never repair a stale rollback plan. + let delta = pending + .rollback + .as_ref() + .and_then(|plan| self.get_mut(popup).commit_traversal(plan)); + let step = if delta.is_some() || !pending.committed { + self.get_mut(popup).current_step() + } else { + pending.step + }; + Some((step, delta)) + } + pub(crate) fn is_pending_traversal_entry( &self, popup: Option, @@ -194,3 +225,71 @@ impl RendererSessionHistories { .find_map(|(owner, id)| (*id == context).then_some(*owner)) } } + +#[cfg(test)] +mod tests { + use super::*; + use moli_session_history::{NavigationHistoryDocumentId, SessionHistoryEntry}; + + fn entry() -> SessionHistoryEntry { + SessionHistoryEntry { + key: NavigationHistoryEntryKey::allocate(), + document: NavigationHistoryDocumentId::allocate(), + } + } + + #[test] + fn ignored_joint_loads_restore_only_an_unmaterialized_unchanged_reservation() { + for mode in ["ignored", "partial", "successor"] { + let mut histories = RendererSessionHistories::default(); + let a = SessionHistoryContextId::allocate(); + let b = SessionHistoryContextId::allocate(); + let first_a = entry(); + let first_b = entry(); + let model = histories.get_mut(None); + model.attach(SessionHistoryContextId::ROOT, None, entry()); + model.attach(a, Some(SessionHistoryContextId::ROOT), first_a.clone()); + model.attach(b, Some(SessionHistoryContextId::ROOT), first_b.clone()); + let destination = model.current_step(); + model.push(a, entry()); + model.push(b, entry()); + let original = model.current_step(); + let plan = model.plan_traversal(destination).unwrap(); + histories.begin_traversal( + None, + destination, + vec![(a, first_a.key.clone()), (b, first_b.key.clone())], + ); + assert_eq!(histories.commit_traversal(None, &plan), Some(-2)); + if mode == "partial" { + assert_eq!( + histories.finish_traversal_entry(None, a, Some(&first_a.key)), + None + ); + } else { + assert_eq!( + histories.finish_traversal_without_document_commit(None, a), + None + ); + } + if mode == "successor" { + histories.get_mut(None).push(a, entry()); + } + let accepted = histories.get_mut(None).current_step(); + let (_, delta) = histories + .finish_traversal_without_document_commit(None, b) + .unwrap(); + assert_eq!(delta, (mode == "ignored").then_some(2), "{mode}"); + assert_eq!( + histories.get_mut(None).current_step(), + if mode == "ignored" { + original + } else { + accepted + }, + "{mode}" + ); + assert!(!histories.has_pending_traversal(None)); + } + } +} diff --git a/moli-renderer-v8/src/native_bridge/history_queue.rs b/moli-renderer-v8/src/native_bridge/history_queue.rs index b4effa4f841..12d55b62aa6 100644 --- a/moli-renderer-v8/src/native_bridge/history_queue.rs +++ b/moli-renderer-v8/src/native_bridge/history_queue.rs @@ -7,7 +7,7 @@ use crate::page_task_queue::{ RendererPageHistoryTraversalTaskKind, RendererPageNavigationApiTaskId, RendererPageNavigationApiTaskKind, RendererPageNavigationApiTaskProducer, }; -use moli_page_types::SessionHistoryStepId; +use moli_session_history::SessionHistoryStepId; use moli_webapi::navigation_values::navigation_result_object; use std::{ cell::RefCell, diff --git a/moli-renderer-v8/src/native_bridge/history_traversal.rs b/moli-renderer-v8/src/native_bridge/history_traversal.rs index 33c19d0d99e..021650435e3 100644 --- a/moli-renderer-v8/src/native_bridge/history_traversal.rs +++ b/moli-renderer-v8/src/native_bridge/history_traversal.rs @@ -44,6 +44,7 @@ pub(crate) struct HistoryTraversalParticipant { pub(crate) history: v8::Global, pub(crate) navigation: Option>, pub(crate) source: SessionHistoryEntry, + pub(crate) source_url: String, pub(crate) destination: SessionHistoryEntry, pub(crate) outcome: TraversalParticipantOutcome, } diff --git a/moli-renderer-v8/src/runtime/navigation_history.rs b/moli-renderer-v8/src/runtime/navigation_history.rs index 65f131cf2a3..69f5abceb47 100644 --- a/moli-renderer-v8/src/runtime/navigation_history.rs +++ b/moli-renderer-v8/src/runtime/navigation_history.rs @@ -1,13 +1,13 @@ use std::sync::Arc; use anyhow::{Result, anyhow}; -use moli_page_types::{ - JointSessionHistory, SessionHistoryContextId, SessionHistoryEntry, SessionHistoryStepId, -}; use moli_page_types::{ NavigationHistoryMutation, cross_document_navigation_seed, reload_navigation_seed, traversal_navigation_seed_candidate, }; +use moli_session_history::{ + JointSessionHistory, SessionHistoryContextId, SessionHistoryEntry, SessionHistoryStepId, +}; use parking_lot::Mutex; use url::Url; @@ -82,7 +82,7 @@ impl RendererNavigationHistory { let joint = self.joint.lock().clone().and_then(|mut joint| { let current = current_entry(seed)?; let entry = SessionHistoryEntry { - key: moli_page_types::NavigationHistoryEntryKey::from_serialized( + key: moli_session_history::NavigationHistoryEntryKey::from_serialized( crate::context_bootstrap::navigation_entry_public_token(current.key.as_str()), ), document: current.document_id.clone(), @@ -97,7 +97,8 @@ impl RendererNavigationHistory { .or_else(|| { joint.step_for_entry(SessionHistoryContextId::ROOT, &entry.key) })?; - joint.traverse(step)?; + let plan = joint.plan_traversal(step)?; + joint.commit_traversal(&plan)?; } _ => joint.replace(SessionHistoryContextId::ROOT, entry), } diff --git a/moli-session-history/src/tests.rs b/moli-session-history/src/tests.rs index a382ebd641f..bc3ab5650c1 100644 --- a/moli-session-history/src/tests.rs +++ b/moli-session-history/src/tests.rs @@ -1,5 +1,45 @@ use super::*; +#[test] +fn restored_contexts_require_readmission_only_when_the_transition_changes() { + let mut history = JointSessionHistory::default(); + let root = SessionHistoryContextId::ROOT; + let child = SessionHistoryContextId::allocate(); + history.attach(root, None, entry("top", "top")); + history.attach(child, Some(root), entry("child-0", "child")); + let first = history.current_step(); + history.push(child, entry("child-1", "child")); + let second = history.current_step(); + let saved = history.context_entries(child); + history.detach(child); + let stale = history.plan_traversal(first).unwrap(); + + history.restore_context(child, root, &saved); + let restored = history.clone(); + assert_eq!(history.commit_traversal(&stale), None); + assert_eq!( + history, restored, + "failed admission must preserve restored entries" + ); + let plan = history.plan_traversal(first).unwrap(); + assert_eq!(history.commit_traversal(&plan), Some(-1)); + assert_eq!(history.entry(child), Some(&entry("child-0", "child"))); + + let forward = history.plan_traversal(second).unwrap(); + let stable = SessionHistoryContextId::allocate(); + history.restore_context( + stable, + root, + &[ + (first, entry("stable", "stable")), + (second, entry("stable", "stable")), + ], + ); + assert_eq!(history.commit_traversal(&forward), Some(1)); + assert_eq!(history.entry(child), Some(&entry("child-1", "child"))); + assert_eq!(history.entry(stable), Some(&entry("stable", "stable"))); +} + #[test] fn traversal_plan_contains_the_entire_mixed_transition_in_either_context_order() { for cross_created_first in [true, false] {