From 97f70d7da5154aaccf7a594ce5d481c8ff8af3aa Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 27 Mar 2024 14:54:46 +0100 Subject: [PATCH] LibWeb: Clone session history entry in reloading --- .../LibWeb/HTML/SessionHistoryEntry.cpp | 17 +++++++++++++ .../LibWeb/HTML/SessionHistoryEntry.h | 2 ++ .../LibWeb/HTML/TraversableNavigable.cpp | 24 ++++++++++++------- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp b/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp index 363772d9f3677a0..f5c5aaee83867da 100644 --- a/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp +++ b/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.cpp @@ -28,6 +28,23 @@ SessionHistoryEntry::SessionHistoryEntry() { } +JS::NonnullGCPtr SessionHistoryEntry::clone() const +{ + JS::NonnullGCPtr entry = *heap().allocate_without_realm(); + entry->m_step = m_step; + entry->m_url = m_url; + entry->m_document_state = m_document_state; + entry->m_classic_history_api_state = m_classic_history_api_state; + entry->m_navigation_api_state = m_navigation_api_state; + entry->m_navigation_api_key = m_navigation_api_key; + entry->m_navigation_api_id = m_navigation_api_id; + entry->m_scroll_restoration_mode = m_scroll_restoration_mode; + entry->m_policy_container = m_policy_container; + entry->m_browsing_context_name = m_browsing_context_name; + entry->m_original_source_browsing_context = m_original_source_browsing_context; + return entry; +} + // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-document JS::GCPtr SessionHistoryEntry::document() const { diff --git a/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.h b/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.h index 9cdcf6518acb861..e132557a0c0190f 100644 --- a/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.h +++ b/Userland/Libraries/LibWeb/HTML/SessionHistoryEntry.h @@ -37,6 +37,8 @@ class SessionHistoryEntry final : public JS::Cell { void visit_edges(Cell::Visitor&) override; + JS::NonnullGCPtr clone() const; + JS::GCPtr document() const; enum class Pending { diff --git a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp index 73993fac8a90f20..dcc0a4ff2ffd973 100644 --- a/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/TraversableNavigable.cpp @@ -401,6 +401,7 @@ TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_history_ JS::Handle target_entry; JS::Handle navigable; bool update_only = false; + bool reloading = false; }; // 11. Let changingNavigableContinuations be an empty queue of changing navigable continuation states. @@ -471,7 +472,10 @@ TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_history_ // and oldOrigin is the same as navigable's current session history entry's document state's origin, // then fire a traverse navigate event given targetEntry and userInvolvementForNavigateEvents. - auto after_document_populated = [old_origin, target_entry, changing_navigable_continuation, &changing_navigable_continuations, &vm, &navigable]() mutable { + auto after_document_populated = [old_origin, changing_navigable_continuation, &changing_navigable_continuations, &vm, &navigable](bool reloading, JS::NonnullGCPtr target_entry) mutable { + changing_navigable_continuation.target_entry = target_entry; + changing_navigable_continuation.reloading = reloading; + // 1. If targetEntry's document is null, then set changingNavigableContinuation's update-only to true. if (!target_entry->document()) { changing_navigable_continuation.update_only = true; @@ -520,21 +524,21 @@ TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_history_ // 6. Let allowPOST be targetEntry's document state's reload pending. auto allow_POST = target_entry->document_state()->reload_pending(); + auto target_entry_clone = target_entry->clone(); + // 7. In parallel, attempt to populate the history entry's document for targetEntry, given navigable, potentiallyTargetSpecificSourceSnapshotParams, // targetSnapshotParams, with allowPOST set to allowPOST and completionSteps set to queue a global task on the navigation and traversal task source given // navigable's active window to run afterDocumentPopulated. - Platform::EventLoopPlugin::the().deferred_invoke([target_entry, potentially_target_specific_source_snapshot_params, target_snapshot_params, this, allow_POST, navigable, after_document_populated] { - navigable->populate_session_history_entry_document(target_entry, *potentially_target_specific_source_snapshot_params, target_snapshot_params, {}, Empty {}, CSPNavigationType::Other, allow_POST, [this, after_document_populated]() mutable { - queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [after_document_populated]() mutable { - after_document_populated(); - }); + Platform::EventLoopPlugin::the().deferred_invoke([target_entry_clone, potentially_target_specific_source_snapshot_params, target_snapshot_params, allow_POST, navigable, after_document_populated] { + navigable->populate_session_history_entry_document(target_entry_clone, *potentially_target_specific_source_snapshot_params, target_snapshot_params, {}, Empty {}, CSPNavigationType::Other, allow_POST, [after_document_populated, target_entry_clone]() mutable { + after_document_populated(true, *target_entry_clone); }) .release_value_but_fixme_should_propagate_errors(); }); } // Otherwise, run afterDocumentPopulated immediately. else { - after_document_populated(); + after_document_populated(false, *target_entry); } }); } @@ -613,12 +617,16 @@ TraversableNavigable::HistoryStepResult TraversableNavigable::apply_the_history_ auto entries_for_navigation_api = get_session_history_entries_for_the_navigation_api(*navigable, target_step); // 11. Queue a global task on the navigation and traversal task source given navigable's active window to run the steps: - queue_global_task(Task::Source::NavigationAndTraversal, *navigable->active_window(), [&completed_change_jobs, target_entry, navigable, displayed_document, update_only = changing_navigable_continuation.update_only, script_history_length, script_history_index, entries_for_navigation_api = move(entries_for_navigation_api), user_involvement_for_navigate_events]() mutable { + auto reloading = changing_navigable_continuation.reloading; + queue_global_task(Task::Source::NavigationAndTraversal, *navigable->active_window(), [&completed_change_jobs, target_entry, navigable, displayed_document, reloading, update_only = changing_navigable_continuation.update_only, script_history_length, script_history_index, entries_for_navigation_api = move(entries_for_navigation_api), user_involvement_for_navigate_events]() mutable { // NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed. if (navigable->has_been_destroyed()) { return; } + if (reloading) + navigable->set_active_session_history_entry(*target_entry); + // 1. If changingNavigableContinuation's update-only is false, then: if (!update_only) { // 1. If targetEntry's document does not equal displayedDocument, then: