Truly independent web browser
LibWebView: Push session history entries and get length/index as spec
Shannon Booth0d2981512af3
diff --git a/Libraries/LibWebView/ApplyHistoryStep.cpp b/Libraries/LibWebView/ApplyHistoryStep.cppindex 892636c30bd..38c124cb634 100644--- a/Libraries/LibWebView/ApplyHistoryStep.cpp+++ b/Libraries/LibWebView/ApplyHistoryStep.cpp@@ -334,13 +334,8 @@ void ApplyHistoryStep::process_changing_navigable_continuations()return_result(Web::HTML::HistoryStepResult::Applied);return;} else if (m_navigation_type == Web::Bindings::NavigationType::Push) {- target_entry = append_the_claimed_target_entry_again(navigable, *claimed_target_entry);- if (!target_entry) {- // NB: The push left the navigable no entries to append to, so its continuation cannot be applied.- end_changing_navigable_job(changing_navigable_continuation);- continue;- }- updated_target_entry = target_entry;+ append_the_claimed_target_entry_again(navigable, *claimed_target_entry);+ updated_target_entry = move(claimed_target_entry);}}@@ -354,7 +349,7 @@ void ApplyHistoryStep::process_changing_navigable_continuations()// 9. Let entriesForNavigationAPI be the result of getting session history entries for the navigation API// given navigable and targetStep.auto entries_for_navigation_api = m_session_history.get_session_history_entries_for_the_navigation_api(navigable, m_target_step);- if (!history_object_length_and_index.has_value() || !entries_for_navigation_api.has_value()) {+ if (!entries_for_navigation_api.has_value()) {// AD-HOC: The canonical mirror is reconciling the navigable, so its continuation cannot be applied.end_changing_navigable_job(changing_navigable_continuation);continue;@@ -368,7 +363,7 @@ void ApplyHistoryStep::process_changing_navigable_continuations()m_continuation_being_applied = &changing_navigable_continuation;changing_navigable_continuation.continuation = ChangingNavigable::Continuation {- .history_object_length_and_index = *history_object_length_and_index,+ .history_object_length_and_index = history_object_length_and_index,.entries_for_navigation_api = entries_for_navigation_api.release_value(),};m_traversable.apply_changing_navigable_history_step_continuation(*this, changing_navigable_continuation);@@ -381,24 +376,21 @@ void ApplyHistoryStep::process_changing_navigable_continuations()// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation// A pushState that jumped the queue cleared the entry a navigation appended, as forward history, and took its step. So,// we re-run the appending steps of finalizing a cross-doc navigation, against the session history as the push left it.-CanonicalSessionHistoryEntry* ApplyHistoryStep::append_the_claimed_target_entry_again(CanonicalNavigable& navigable, CanonicalSessionHistoryEntry& claimed_target_entry)+void ApplyHistoryStep::append_the_claimed_target_entry_again(CanonicalNavigable& navigable, CanonicalSessionHistoryEntry& claimed_target_entry){// 9.1. Clear the forward session history of traversable.// 9.2. Set targetStep to traversable's current session history step + 1.// 9.3. Set historyEntry's step to targetStep.// 9.4. Append historyEntry to targetEntries.auto target_step = m_session_history.push_session_history_entry(navigable, claimed_target_entry);- if (!target_step.has_value())- return nullptr;// NB: The run now targets a step past the push-committed one. So it's the newest run again and may commit its step.- m_step = *target_step;- m_target_step = *target_step;+ m_step = target_step;+ m_target_step = target_step;m_generation = ++m_traversable_state.generation_counter;// The push also took the navigable's current session history entry over.navigable.set_current_session_history_entry(claimed_target_entry);- return &claimed_target_entry;}void ApplyHistoryStep::update_nonchanging_navigables()@@ -409,15 +401,11 @@ void ApplyHistoryStep::update_nonchanging_navigables()// 17. Let (scriptHistoryLength, scriptHistoryIndex) be the result of getting the history object length and index// given traversable and targetStep.auto history_object_length_and_index = m_session_history.get_the_history_object_length_and_index(m_target_step);- if (!history_object_length_and_index.has_value()) {- set_current_session_history_step();- return;- }// 18. For each navigable of nonchangingNavigablesThatStillNeedUpdates, queue a global task on the navigation and// traversal task source given navigable's active window to run the steps:for (auto navigable_id : m_nonchanging_navigables_that_still_need_updates) {- if (!m_traversable.update_nonchanging_navigable_history_step_state(*this, navigable_id, *history_object_length_and_index))+ if (!m_traversable.update_nonchanging_navigable_history_step_state(*this, navigable_id, history_object_length_and_index))m_completed_nonchanging_jobs++;}diff --git a/Libraries/LibWebView/ApplyHistoryStep.h b/Libraries/LibWebView/ApplyHistoryStep.hindex dae67330c7e..066232d0541 100644--- a/Libraries/LibWebView/ApplyHistoryStep.h+++ b/Libraries/LibWebView/ApplyHistoryStep.h@@ -151,7 +151,7 @@ private:void run_changing_navigable_jobs();void update_nonchanging_navigables();void set_current_session_history_step();- CanonicalSessionHistoryEntry* append_the_claimed_target_entry_again(CanonicalNavigable&, CanonicalSessionHistoryEntry& claimed_target_entry);+ void append_the_claimed_target_entry_again(CanonicalNavigable&, CanonicalSessionHistoryEntry& claimed_target_entry);void set_ongoing_navigation_to_traversal(CanonicalNavigable&, CanonicalSessionHistoryEntry const&);void return_result(Web::HTML::HistoryStepResult);diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cppindex 98562fc80d5..26f5e3589a9 100644--- a/Libraries/LibWebView/CanonicalTraversable.cpp+++ b/Libraries/LibWebView/CanonicalTraversable.cpp@@ -2255,10 +2255,6 @@ void CanonicalTraversable::finalize_a_same_document_navigation(HistoryOperation&// 5.3. Set targetEntry's step to targetStep.// 5.4. Append targetEntry to targetEntries.target_step = m_session_history.push_session_history_entry(*target_navigable, *target_entry);- if (!target_step.has_value()) {- finish_history_operation(operation.operation_id);- return;- }}// Otherwise:else {@@ -2468,10 +2464,6 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation// 3. Set historyEntry's step to targetStep.// 4. Append historyEntry to targetEntries.target_step = m_session_history.push_session_history_entry(*navigable, move(history_entry));- if (!target_step.has_value()) {- finish_history_operation(operation.operation_id);- return;- }}// Otherwise:else {diff --git a/Libraries/LibWebView/SessionHistory.cpp b/Libraries/LibWebView/SessionHistory.cppindex 339f9541b5d..4f25d06f829 100644--- a/Libraries/LibWebView/SessionHistory.cpp+++ b/Libraries/LibWebView/SessionHistory.cpp@@ -321,14 +321,8 @@ void TraversableSessionHistory::clear_the_forward_session_history()// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation, step 9// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-same-document-navigation, step 5-Optional<i32> TraversableSessionHistory::push_session_history_entry(CanonicalNavigable const& navigable, NonnullRefPtr<CanonicalSessionHistoryEntry> entry)+i32 TraversableSessionHistory::push_session_history_entry(CanonicalNavigable const& navigable, NonnullRefPtr<CanonicalSessionHistoryEntry> entry){- // NB: The entries of a navigable whose document is active are among those clearing the forward session history- // keeps, as the entry of the document containing it is at or before the current step.- // FIXME: Assert that instead, once navigable creation and destruction are ordered with the session history.- if (!session_history_entries_at_or_before(navigable, m_current_session_history_step).has_value())- return {};-// 1. Clear the forward session history of traversable.clear_the_forward_session_history();@@ -447,7 +441,7 @@ Optional<TraversableSessionHistory::TraversalTarget> TraversableSessionHistory::}// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries-static Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> get_session_history_entries(Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const& traversable_session_history_entries, CanonicalNavigable const& navigable, Optional<i32> at_or_before_step)+static Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> get_session_history_entries(Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const& traversable_session_history_entries, CanonicalNavigable const& navigable){// 1. Let traversable be navigable's traversable navigable.// NB: The caller has already resolved navigable through its CanonicalTraversable.@@ -461,8 +455,6 @@ static Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> get_// 4. Let docStates be an empty ordered set of document states.Vector<CanonicalDocumentState const*> document_states;auto append_document_state = [&](CanonicalSessionHistoryEntry const& entry) {- if (at_or_before_step.has_value() && entry.step > *at_or_before_step)- return;if (!document_states.contains_slow(entry.document_state.ptr()))document_states.append(entry.document_state.ptr());};@@ -495,12 +487,7 @@ static Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> get_Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> TraversableSessionHistory::get_session_history_entries(CanonicalNavigable const& navigable) const{- return WebView::get_session_history_entries(m_entries, navigable, {});-}--Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> TraversableSessionHistory::session_history_entries_at_or_before(CanonicalNavigable const& navigable, i32 step) const-{- return WebView::get_session_history_entries(m_entries, navigable, step);+ return WebView::get_session_history_entries(m_entries, navigable);}CanonicalSessionHistoryEntry& TraversableSessionHistory::current_entry() const@@ -554,7 +541,7 @@ CanonicalSessionHistoryEntry* TraversableSessionHistory::get_the_target_history_}// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-history-object-length-and-index-Optional<Web::HTML::HistoryObjectLengthAndIndex> TraversableSessionHistory::get_the_history_object_length_and_index(i32 step) const+Web::HTML::HistoryObjectLengthAndIndex TraversableSessionHistory::get_the_history_object_length_and_index(i32 step) const{// 1. Let steps be the result of getting all used history steps within traversable.auto steps = used_steps();@@ -563,11 +550,8 @@ Optional<Web::HTML::HistoryObjectLengthAndIndex> TraversableSessionHistory::get_auto script_history_length = steps.size();// 3. Assert: steps contains step.- // AD-HOC: The canonical mirror can be reconciling a removed child navigable, so answer nothing instead of- // asserting; the caller treats it as a failed job.auto script_history_index = steps.find_first_index(step);- if (!script_history_index.has_value())- return {};+ VERIFY(script_history_index.has_value());// 4. Let scriptHistoryIndex be the index of step in steps.// 5. Return (scriptHistoryLength, scriptHistoryIndex).diff --git a/Libraries/LibWebView/SessionHistory.h b/Libraries/LibWebView/SessionHistory.hindex e10ab8a12a8..89d236896ef 100644--- a/Libraries/LibWebView/SessionHistory.h+++ b/Libraries/LibWebView/SessionHistory.h@@ -51,8 +51,8 @@ public:bool remove_nested_history(CanonicalNavigable const& parent_navigable, Web::HTML::CrossProcessId parent_document_state_id, Web::HTML::CrossProcessId child_navigable_id);void clear_the_forward_session_history();// Clears the forward session history, then appends entry to navigable's session history entries at the step after- // the current one, which it returns. Changes nothing if navigable has no entries at or before the current step.- Optional<i32> push_session_history_entry(CanonicalNavigable const&, NonnullRefPtr<CanonicalSessionHistoryEntry>);+ // the current one, which it returns.+ i32 push_session_history_entry(CanonicalNavigable const&, NonnullRefPtr<CanonicalSessionHistoryEntry>);// Replaces entry_to_replace in navigable's session history entries with entry, at its step. Changes nothing if// entry_to_replace is not among them.bool replace_session_history_entry(CanonicalNavigable const&, CanonicalSessionHistoryEntry const& entry_to_replace, NonnullRefPtr<CanonicalSessionHistoryEntry>);@@ -66,7 +66,7 @@ public:[[nodiscard]] Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> get_session_history_entries(CanonicalNavigable const&) const;[[nodiscard]] Optional<i32> get_the_used_step(i32 step) const;[[nodiscard]] CanonicalSessionHistoryEntry* get_the_target_history_entry(CanonicalNavigable const&, i32 step) const;- [[nodiscard]] Optional<Web::HTML::HistoryObjectLengthAndIndex> get_the_history_object_length_and_index(i32 step) const;+ [[nodiscard]] Web::HTML::HistoryObjectLengthAndIndex get_the_history_object_length_and_index(i32 step) const;[[nodiscard]] Optional<Vector<Web::HTML::SessionHistoryEntryDescriptor>> get_session_history_entries_for_the_navigation_api(CanonicalNavigable const&, i32 target_step) const;[[nodiscard]] Vector<CanonicalNavigable&> get_all_navigables_whose_current_session_history_entry_will_change_or_reload(CanonicalNavigable& traversable, i32 target_step) const;[[nodiscard]] Vector<Web::HTML::CrossProcessId> get_all_navigables_that_might_experience_a_cross_document_traversal(CanonicalNavigable const& traversable, i32 target_step) const;@@ -81,9 +81,6 @@ public:[[nodiscard]] CanonicalSessionHistoryEntry* top_level_entry_for_step(i32 step) const;private:- // The entries of navigable among those at or before step, which clearing the forward session history at step keeps.- [[nodiscard]] Optional<Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> const&> session_history_entries_at_or_before(CanonicalNavigable const&, i32 step) const;-// https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-entriesVector<NonnullRefPtr<CanonicalSessionHistoryEntry>> m_entries;diff --git a/Tests/LibWebView/TestSessionHistory.cpp b/Tests/LibWebView/TestSessionHistory.cppindex 8326aa84f7b..a7e7c291a20 100644--- a/Tests/LibWebView/TestSessionHistory.cpp+++ b/Tests/LibWebView/TestSessionHistory.cpp@@ -225,23 +225,6 @@ static Web::HTML::SessionHistoryEntryDescriptor entry(i32 step, StringView url,return entry;}-static Web::HTML::PendingSessionHistoryEntryDescriptor pending_entry(StringView url, u64 document_state_id)-{- auto descriptor = entry(0, url, document_state_id, ""sv);- return {- .url = move(descriptor.url),- .document_state = move(descriptor.document_state),- .classic_history_api_state = move(descriptor.classic_history_api_state),- .navigation_api_state = move(descriptor.navigation_api_state),- .navigation_api_key = move(descriptor.navigation_api_key),- .navigation_api_id = move(descriptor.navigation_api_id),- .scroll_restoration_mode = descriptor.scroll_restoration_mode,- .scroll_position_data = move(descriptor.scroll_position_data),- .directive_state_id = descriptor.directive_state_id,- .directive_state_value = move(descriptor.directive_state_value),- };-}-static Web::HTML::SameDocumentNavigationEntry same_document_entry(Web::HTML::SessionHistoryEntryDescriptor entry){return {@@ -521,32 +504,6 @@ TEST_CASE(entry_among_its_own_nested_histories_is_rejected)EXPECT(WebView::CanonicalSessionHistoryEntry::create_from_descriptor(parent_entry).is_error());}-TEST_CASE(failed_push_keeps_entries_and_document_states)-{- WebView::CanonicalTraversable traversable { {}, initial_history_entry() };- traversable.set_id({ 9, 1 });- give_active_document(traversable);- auto& child = traversable.append_child(make<WebView::CanonicalNavigable>(navigable_id("frame"sv)));-- // The child's session history entries are in the forward history, which pushing an entry clears.- WebView::TraversableSessionHistory history { initial_history_entry() };- EXPECT(history.initialize_for_testing({ entry(0, "https://parent.example/"sv), entry(1, "https://parent.example/next"sv, 10, "main"sv, { nested_history("frame"sv, { entry(1, "https://child.example/"sv) }) }) }, { 0, 1 }, 0));- auto const* forward_entry = history.entry_at(1);- VERIFY(forward_entry);- auto const* forward_document_state = forward_entry->document_state.ptr();- auto const* child_entry = forward_document_state->nested_histories[0].entries[0].ptr();-- EXPECT(!history.push_session_history_entry(child, canonical_entry(entry(0, "https://child.example/pushed"sv))).has_value());-- EXPECT_EQ(history.size(), 2uz);- EXPECT_EQ(history.entry_at(1), forward_entry);- EXPECT_EQ(history.entry_at(1)->document_state.ptr(), forward_document_state);- VERIFY(forward_document_state->nested_histories.size() == 1);- VERIFY(forward_document_state->nested_histories[0].entries.size() == 1);- EXPECT_EQ(forward_document_state->nested_histories[0].entries[0].ptr(), child_entry);- EXPECT_EQ(history.current_step(), 0);-}-TEST_CASE(child_history_mutations_use_the_reported_parent_document_state){WebView::CanonicalTraversable traversable { {}, initial_history_entry() };@@ -1031,35 +988,6 @@ TEST_CASE(nested_cross_document_replacement_updates_copied_session_histories)}}-TEST_CASE(failed_nested_cross_document_push_preserves_forward_history)-{- WebView::CanonicalTraversable traversable { {}, initial_history_entry() };- traversable.set_id({ 9, 1 });- give_active_document(traversable);- auto& child = traversable.append_child(make<WebView::CanonicalNavigable>(navigable_id("frame"sv)));- WebView::TraversableSessionHistory history { initial_history_entry() };-- auto update_result = history.initialize_for_testing(- {- entry(0, "https://top.example/current"sv),- entry(2, "https://top.example/forward"sv, { nested_history("frame"sv, { entry(1, "https://frame.example/current"sv, 20, ""sv) }) }),- },- { 0, 1, 2 }, 0);- EXPECT_EQ(update_result, true);-- auto target_step = finalize_cross_document_navigation_for_testing(- history, child, pending_entry("https://frame.example/pushed"sv, 21), Web::HTML::HistoryHandlingBehavior::Push);-- EXPECT(!target_step.has_value());- EXPECT_EQ(history.current_step(), 0);- EXPECT_EQ(history.size(), 2uz);- EXPECT_EQ(history.used_step_count(), 3uz);- EXPECT(can_go_forward(history));- expect_entry(history, 0, 0, "https://top.example/current"sv);- expect_entry(history, 1, 2, "https://top.example/forward"sv);- expect_nested_history(*history.entry_at(1), 0, "frame"sv, 1);-}-TEST_CASE(nested_finalization_rejects_a_changed_initial_entry_identity){WebView::CanonicalTraversable traversable { {}, initial_history_entry() };