Skip to content

Truly independent web browser

LibWebView: Hold a history step run's state per changing navigable

Shannon Booth750041801809
diff --git a/Libraries/LibWebView/ApplyHistoryStep.cpp b/Libraries/LibWebView/ApplyHistoryStep.cpp
index a2f12befbf2..677c857f163 100644
--- a/Libraries/LibWebView/ApplyHistoryStep.cpp
+++ b/Libraries/LibWebView/ApplyHistoryStep.cpp
@@ -138,15 +138,16 @@ void ApplyHistoryStep::get_changing_and_nonchanging_navigables()
{
// 6. Let changingNavigables be the result of get all navigables whose current session history entry will change
// or reload given traversable and targetStep.
- m_changing_navigables = m_session_history.get_all_navigables_whose_current_session_history_entry_will_change_or_reload(m_traversable_navigable, m_target_step);
+ for (auto navigable_id : m_session_history.get_all_navigables_whose_current_session_history_entry_will_change_or_reload(m_traversable_navigable, m_target_step))
+ m_changing_navigables.append({ .id = navigable_id });
// 7. Let nonchangingNavigablesThatStillNeedUpdates be the result of getting all navigables that only need
// history object length/index update given traversable and targetStep.
m_nonchanging_navigables_that_still_need_updates = m_session_history.get_all_navigables_that_only_need_history_object_length_index_update(m_traversable_navigable, m_target_step);
// 8. For each navigable of changingNavigables:
- for (auto navigable_id : m_changing_navigables) {
- auto* navigable = find_navigable(navigable_id);
+ for (auto& changing_navigable : m_changing_navigables) {
+ auto* navigable = find_navigable(changing_navigable.id);
// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.
auto* target_entry = navigable ? m_session_history.get_the_target_history_entry(*navigable, m_target_step) : nullptr;
@@ -159,10 +160,10 @@ void ApplyHistoryStep::get_changing_and_nonchanging_navigables()
// 3. If targetEntry's document is not navigable's active document, then queue a global task on the navigation
// and traversal task source of navigable's active window to run these steps:
if (!navigable->active_document_is(*target_entry))
- m_jobs.queue_navigation_api_state_clear_task(navigable_id);
+ m_jobs.queue_navigation_api_state_clear_task(changing_navigable.id);
// 4. Set navigable's ongoing navigation to "traversal".
- set_ongoing_navigation_to_traversal(*navigable, *target_entry);
+ set_ongoing_navigation_to_traversal(changing_navigable, *navigable, *target_entry);
}
run_changing_navigable_jobs();
@@ -171,7 +172,8 @@ void ApplyHistoryStep::get_changing_and_nonchanging_navigables()
void ApplyHistoryStep::run_changing_navigable_jobs()
{
// 12. For each navigable of changingNavigables, queue a global task on the navigation and traversal task source.
- for (auto navigable_id : m_changing_navigables) {
+ for (auto& changing_navigable : m_changing_navigables) {
+ auto navigable_id = changing_navigable.id;
auto const* navigable = find_navigable(navigable_id);
auto* target_entry = navigable ? m_session_history.get_the_target_history_entry(*navigable, m_target_step) : nullptr;
if (!target_entry) {
@@ -201,16 +203,14 @@ void ApplyHistoryStep::run_changing_navigable_jobs()
.target_entry_reload_pending = target_entry->document_state->reload_pending,
.user_involvement = m_user_involvement,
.navigation_type = m_navigation_type,
+ .traversal_yields_to = changing_navigable.traversal_yields_to,
+ .canceled_navigation_id = changing_navigable.canceled_navigation_id,
};
- if (auto yield = m_same_document_traversal_yields.get(navigable_id); yield.has_value()) {
- job.traversal_yields_to = yield->yields_to;
- job.canceled_navigation_id = yield->canceled_navigation_id;
- }
if (!m_jobs.select_changing_navigable_history_step_job_endpoint(job)) {
changing_navigable_job_completed(navigable_id, Web::HTML::ChangingNavigableHistoryStepJobDisposition::Skipped);
continue;
}
- m_claimed_target_entries.set(navigable_id, *target_entry);
+ changing_navigable.claimed_target_entry = target_entry;
m_jobs.run_changing_navigable_history_step_job(
move(job),
@@ -355,17 +355,18 @@ void ApplyHistoryStep::process_changing_navigable_continuations()
// NB: navigable's own sync navigations wait from step 8 on. So, none of them can change the entry after this.
auto* target_entry = navigable ? m_session_history.get_the_target_history_entry(*navigable, m_target_step) : nullptr;
RefPtr<CanonicalSessionHistoryEntry> updated_target_entry;
- if (auto claimed_target_entry = m_claimed_target_entries.get(navigable_id); target_entry && claimed_target_entry.has_value()) {
- auto is_in_claimed_slot = target_entry->document_state == claimed_target_entry.value()->document_state
- && target_entry->navigation_api_key == claimed_target_entry.value()->navigation_api_key;
+ auto* changing_navigable = find_changing_navigable(navigable_id);
+ if (auto claimed_target_entry = changing_navigable ? changing_navigable->claimed_target_entry : nullptr; target_entry && claimed_target_entry) {
+ auto is_in_claimed_slot = target_entry->document_state == claimed_target_entry->document_state
+ && target_entry->navigation_api_key == claimed_target_entry->navigation_api_key;
if (is_in_claimed_slot) {
- if (target_entry->navigation_api_id != claimed_target_entry.value()->navigation_api_id)
+ if (target_entry->navigation_api_id != claimed_target_entry->navigation_api_id)
updated_target_entry = target_entry;
} else if (m_navigation_type == Web::Bindings::NavigationType::Traverse) {
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.value());
+ target_entry = append_the_claimed_target_entry_again(*navigable, *claimed_target_entry);
if (!target_entry) {
return_result(Web::HTML::HistoryStepResult::NoMatchingEntry);
return;
@@ -512,7 +513,7 @@ Web::HTML::SessionHistoryEntryDescriptor ApplyHistoryStepJobs::ChangingNavigable
return descriptor;
}
-void ApplyHistoryStep::set_ongoing_navigation_to_traversal(CanonicalNavigable& navigable, CanonicalSessionHistoryEntry const& target_entry)
+void ApplyHistoryStep::set_ongoing_navigation_to_traversal(ChangingNavigable& changing_navigable, CanonicalNavigable& navigable, CanonicalSessionHistoryEntry const& target_entry)
{
// AD-HOC: As in browsers, a push or replace leaves the ongoing navigation alone: a newer navigation goes on, and
// commits after it. See https://github.com/whatwg/html/issues/12581.
@@ -543,7 +544,7 @@ void ApplyHistoryStep::set_ongoing_navigation_to_traversal(CanonicalNavigable& n
&& ongoing_navigation
&& ongoing_navigation->sequence_number > m_operation_sequence_number) {
if (is_same_document_traversal)
- m_same_document_traversal_yields.set(navigable.id(), { Web::HTML::TraversalYieldsTo::AdmittedNavigation, {} });
+ changing_navigable.traversal_yields_to = Web::HTML::TraversalYieldsTo::AdmittedNavigation;
return;
}
@@ -562,16 +563,25 @@ void ApplyHistoryStep::set_ongoing_navigation_to_traversal(CanonicalNavigable& n
if (traversal_crosses_documents) {
navigable.clear_ongoing_navigation();
navigable.set_ongoing_navigation_to_traversal(m_operation_id);
- m_navigables_with_ongoing_history_traversal.set(navigable.id());
+ changing_navigable.has_ongoing_history_traversal = true;
}
- if (is_same_document_traversal)
- m_same_document_traversal_yields.set(navigable.id(), { Web::HTML::TraversalYieldsTo::UnadmittedNavigation, move(canceled_navigation_id) });
+ if (is_same_document_traversal) {
+ changing_navigable.traversal_yields_to = Web::HTML::TraversalYieldsTo::UnadmittedNavigation;
+ changing_navigable.canceled_navigation_id = move(canceled_navigation_id);
+ }
+}
+
+ApplyHistoryStep::ChangingNavigable* ApplyHistoryStep::find_changing_navigable(Web::HTML::CrossProcessId navigable_id)
+{
+ auto it = m_changing_navigables.find_if([&](auto const& changing_navigable) { return changing_navigable.id == navigable_id; });
+ return it == m_changing_navigables.end() ? nullptr : &*it;
}
void ApplyHistoryStep::clear_ongoing_navigation_traversal(Web::HTML::CrossProcessId navigable_id)
{
- if (!m_navigables_with_ongoing_history_traversal.remove(navigable_id))
+ auto* changing_navigable = find_changing_navigable(navigable_id);
+ if (!changing_navigable || !exchange(changing_navigable->has_ongoing_history_traversal, false))
return;
if (auto* navigable = find_navigable(navigable_id))
navigable->clear_ongoing_navigation_traversal(m_operation_id);
@@ -579,11 +589,8 @@ void ApplyHistoryStep::clear_ongoing_navigation_traversal(Web::HTML::CrossProces
void ApplyHistoryStep::clear_all_ongoing_navigation_traversals()
{
- for (auto navigable_id : m_navigables_with_ongoing_history_traversal) {
- if (auto* navigable = find_navigable(navigable_id))
- navigable->clear_ongoing_navigation_traversal(m_operation_id);
- }
- m_navigables_with_ongoing_history_traversal.clear();
+ for (auto& changing_navigable : m_changing_navigables)
+ clear_ongoing_navigation_traversal(changing_navigable.id);
}
CanonicalNavigable* ApplyHistoryStep::find_navigable(Web::HTML::CrossProcessId navigable_id)
diff --git a/Libraries/LibWebView/ApplyHistoryStep.h b/Libraries/LibWebView/ApplyHistoryStep.h
index 81732e4a358..24679c1021e 100644
--- a/Libraries/LibWebView/ApplyHistoryStep.h
+++ b/Libraries/LibWebView/ApplyHistoryStep.h
@@ -140,7 +140,9 @@ private:
CanonicalSessionHistoryEntry* append_the_claimed_target_entry_again(CanonicalNavigable&, CanonicalSessionHistoryEntry& claimed_target_entry);
void changing_navigable_job_completed(Web::HTML::CrossProcessId, Web::HTML::ChangingNavigableHistoryStepJobDisposition);
- void set_ongoing_navigation_to_traversal(CanonicalNavigable&, CanonicalSessionHistoryEntry const&);
+ struct ChangingNavigable;
+ ChangingNavigable* find_changing_navigable(Web::HTML::CrossProcessId);
+ void set_ongoing_navigation_to_traversal(ChangingNavigable&, CanonicalNavigable&, CanonicalSessionHistoryEntry const&);
void clear_ongoing_navigation_traversal(Web::HTML::CrossProcessId);
void clear_all_ongoing_navigation_traversals();
void return_result(Web::HTML::HistoryStepResult);
@@ -168,9 +170,17 @@ private:
// The algorithm's variables.
i32 m_target_step { 0 };
- Vector<Web::HTML::CrossProcessId> m_changing_navigables;
+ struct ChangingNavigable {
+ Web::HTML::CrossProcessId id;
+ // The target entry the navigable's job claimed, looked up again before it is activated (whatwg/html#12961).
+ RefPtr<CanonicalSessionHistoryEntry> claimed_target_entry {};
+ // AD-HOC: What a same-document traversal yields to, with the admitted navigation it canceled.
+ Web::HTML::TraversalYieldsTo traversal_yields_to { Web::HTML::TraversalYieldsTo::Nothing };
+ Optional<Utf16String> canceled_navigation_id {};
+ bool has_ongoing_history_traversal { false };
+ };
+ Vector<ChangingNavigable> m_changing_navigables;
Vector<Web::HTML::CrossProcessId> m_nonchanging_navigables_that_still_need_updates;
- HashTable<Web::HTML::CrossProcessId> m_navigables_with_ongoing_history_traversal;
size_t m_completed_change_jobs { 0 };
// NB: The continuation states live in the processes that ran the jobs; this queue holds the navigables whose
// continuations are ready to be applied.
@@ -178,22 +188,10 @@ private:
HashTable<Web::HTML::CrossProcessId> m_navigables_that_must_wait_before_handling_sync_navigation;
size_t m_completed_nonchanging_jobs { 0 };
- // AD-HOC: Synchronous navigations jump the traversal queue while a run is paused, and navigations are admitted
- // independently of the queue. What keeps a paused run consistent with them:
- // - The generation that orders the runs' commits of the current step (whatwg/html#12576).
- // - The target entry each changing navigable's job claimed, which the run looks up again before
- // activating it (whatwg/html#12961). Its document state and navigation API key name its slot in the
- // session history: a same-document replacement of the entry keeps both.
- // - How far the run lets synchronous navigation steps jump.
- // - What a same-document traversal yields to, with the admitted navigation it canceled.
+ // AD-HOC: The generation that orders the runs' commits of the current step (whatwg/html#12576).
u64 m_generation;
- HashMap<Web::HTML::CrossProcessId, NonnullRefPtr<CanonicalSessionHistoryEntry>> m_claimed_target_entries;
+ // How far the run lets synchronous navigation steps jump.
Optional<u64> m_synchronous_navigation_steps_to_jump_through;
- struct SameDocumentTraversalYield {
- Web::HTML::TraversalYieldsTo yields_to;
- Optional<Utf16String> canceled_navigation_id;
- };
- HashMap<Web::HTML::CrossProcessId, SameDocumentTraversalYield> m_same_document_traversal_yields;
// The synchronous navigation steps this run is paused on ("running nested apply history step" is true).
RefPtr<Core::Promise<Empty>> m_running_synchronous_navigation_steps;