Skip to content

Truly independent web browser

LibWebView: Track a reload's load without an ongoing navigation

Shannon Booth47e78dbc0e9d
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 63f84e57712..35c5d5e73e8 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -242,7 +242,6 @@ void CanonicalNavigable::begin_navigation(Web::HTML::PreparedNavigationDescripto
.url = url,
.navigation_id = navigation_id,
.sequence_number = traversable.next_sequence_number(),
- .has_started = true,
});
// 20. If url's scheme is "javascript", then:
@@ -1255,13 +1254,6 @@ void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& ent
clear_ongoing_navigation();
}
-CanonicalNavigation& CanonicalNavigable::ensure_ongoing_navigation()
-{
- if (!m_ongoing_navigation.has_value())
- m_ongoing_navigation = CanonicalNavigation {};
- return *m_ongoing_navigation;
-}
-
void CanonicalNavigable::set_ongoing_navigation(CanonicalNavigation ongoing_navigation)
{
// NB: Taken before the handle covering this navigation's start is dropped below, so that a revoked entry is not
@@ -1316,7 +1308,8 @@ void CanonicalNavigable::retain_blob_url_token(URL::BlobURLEntry::Token token)
void CanonicalNavigable::set_navigation_population_worker(WebContentPage& page)
{
- auto& ongoing_navigation = ensure_ongoing_navigation();
+ VERIFY(m_ongoing_navigation.has_value());
+ auto& ongoing_navigation = *m_ongoing_navigation;
VERIFY(!ongoing_navigation.population_worker);
ongoing_navigation.population_worker = page;
}
@@ -1336,7 +1329,8 @@ bool CanonicalNavigable::navigation_population_worker_matches(WebContentPage con
void CanonicalNavigable::set_navigation_host(WebContentPage& page)
{
- auto& ongoing_navigation = ensure_ongoing_navigation();
+ VERIFY(m_ongoing_navigation.has_value());
+ auto& ongoing_navigation = *m_ongoing_navigation;
ongoing_navigation.host = page;
// The population worker conducts the navigation until the hosting process takes over.
@@ -1395,7 +1389,7 @@ bool CanonicalNavigable::matches_ongoing_navigation(Optional<Utf16String> const&
{
// A live transaction owns the view's loading state, so completion signals must name it.
if (m_ongoing_navigation.has_value())
- return m_ongoing_navigation->has_started && navigation_id == m_ongoing_navigation->navigation_id;
+ return navigation_id == m_ongoing_navigation->navigation_id;
// Otherwise completion signals concern the active document's tracked load.
return navigation_id == m_active_document_load.navigation_id;
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index 5ac97f18f26..3dd656a5b9f 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -218,7 +218,6 @@ public:
Optional<CanonicalNavigation>& ongoing_navigation() { return m_ongoing_navigation; }
Optional<CanonicalNavigation> const& ongoing_navigation() const { return m_ongoing_navigation; }
bool ongoing_navigation_is_traversal() const { return m_ongoing_navigation_traversal_operation_id.has_value(); }
- CanonicalNavigation& ensure_ongoing_navigation();
// Held so that revoking a blob URL cannot take the entry away from a navigation on its way to this navigable, or
// from the document it loaded. Session history holds none, so a revoked blob URL cannot be traversed back to.
diff --git a/Libraries/LibWebView/CanonicalNavigation.h b/Libraries/LibWebView/CanonicalNavigation.h
index c1cd425c364..6a6f8a19fec 100644
--- a/Libraries/LibWebView/CanonicalNavigation.h
+++ b/Libraries/LibWebView/CanonicalNavigation.h
@@ -47,7 +47,6 @@ public:
// request, which population uses up. A javascript: URL has none.
Optional<Web::HTML::PreparedNavigationDescriptor> retry {};
u64 sequence_number { 0 };
- bool has_started { false };
Phase phase { Phase::Started };
OwnPtr<NavigationLoader> loader {};
RefPtr<WebContentPage> population_worker {};
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index b2d1688d2db..86bfa6cb43f 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -424,16 +424,10 @@ void ViewImplementation::reload()
on_before_browser_initiated_navigation();
set_loading_state(true);
- auto const* current_entry = traversable().session_history().current_entry();
- Optional<URL::URL> ongoing_url;
- if (traversable().ongoing_navigation().has_value())
- ongoing_url = move(traversable().ongoing_navigation()->url);
- else if (current_entry)
- ongoing_url = current_entry->url;
- traversable().set_ongoing_navigation(CanonicalNavigation {
- .url = move(ongoing_url),
- .sequence_number = traversable().next_sequence_number(),
- });
+ // A reload from the browser's UI is the newest navigation. The navigation under way is abandoned, and the active
+ // document's load is no longer the one the view tracks.
+ traversable().clear_ongoing_navigation();
+ traversable().clear_active_document_load();
if (m_crash_state.has_value()) {
prepare_for_navigation_after_crash();
recover_current_session_history_entry_with_history_operation();
@@ -441,8 +435,11 @@ void ViewImplementation::reload()
}
traversable().reload([this](Web::HTML::HistoryStepResult result, Optional<i32> committed_step) {
- if (result != Web::HTML::HistoryStepResult::Applied)
- did_cancel_navigation({});
+ if (result != Web::HTML::HistoryStepResult::Applied && !traversable().has_uncommitted_navigation()) {
+ set_loading_state(false);
+ if (m_webdriver_navigation_observation.has_value())
+ complete_webdriver_navigation(m_webdriver_navigation_observation->navigation_id);
+ }
if (committed_step.has_value())
update_navigation_action_state();
dump_session_history("reload-complete"sv);
@@ -2479,14 +2476,11 @@ void ViewImplementation::cancel_all_native_geolocation_requests()
Application::the().stop_watching_geolocation_position(watch.value);
}
-void ViewImplementation::did_start_navigation(Optional<Utf16String> navigation_id, URL::URL const& url)
+void ViewImplementation::did_start_navigation(Optional<Utf16String> const& navigation_id)
{
- auto& ongoing = traversable().ensure_ongoing_navigation();
- if (ongoing.sequence_number == 0)
- ongoing.sequence_number = traversable().next_sequence_number();
- ongoing.navigation_id = move(navigation_id);
- ongoing.url = url;
- ongoing.has_started = true;
+ // A load a history operation starts, rather than a navigation, is tracked as the active document's.
+ if (!navigation_id.has_value())
+ traversable().clear_active_document_load();
set_loading_state(true);
dump_session_history("did-start-navigation"sv);
@@ -2494,10 +2488,9 @@ void ViewImplementation::did_start_navigation(Optional<Utf16String> navigation_i
bool ViewImplementation::did_cancel_navigation(Optional<Utf16String> const& navigation_id)
{
- // A cancel may arrive before a UI-issued load reports its start. A started navigation's cancel must name it.
auto const& ongoing = traversable().ongoing_navigation();
auto stale = ongoing.has_value()
- ? ongoing->has_started && navigation_id != ongoing->navigation_id
+ ? navigation_id != ongoing->navigation_id
: navigation_id != traversable().active_document_load().navigation_id;
if (stale)
return false;
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 0d83f0e38a8..a5a7b0e2372 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -566,7 +566,7 @@ protected:
explicit ViewImplementation(IsPrivate = IsPrivate::No);
void set_url(URL::URL);
- void did_start_navigation(Optional<Utf16String> navigation_id, URL::URL const&);
+ void did_start_navigation(Optional<Utf16String> const& navigation_id);
void did_cancel_loading(Optional<Utf16String> const& navigation_id);
bool did_cancel_navigation(Optional<Utf16String> const& navigation_id);
void did_finish_navigation();
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index a4dceba2a97..532d7d0b7f4 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -456,7 +456,7 @@ void WebContentPage::begin_top_level_load(Optional<Utf16String> navigation_id, U
auto& view = this->view();
view.m_history_visit_transition_for_current_load = view.m_history_visit_transition_for_next_load;
view.m_history_visit_transition_for_next_load = HistoryVisitTransition::Link;
- view.did_start_navigation(move(navigation_id), url);
+ view.did_start_navigation(navigation_id);
view.set_url({}, url);
view.set_title({}, Utf16String::from_utf8(url.serialize()));
diff --git a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
index 0b8996080b5..b2e811ee084 100644
--- a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
+++ b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
@@ -145,7 +145,7 @@ TEST_CASE(response_browsing_context_is_activated_only_at_commit)
});
auto* destination_context = &destination_document->browsing_context();
auto navigation_id = Utf16String::from_utf8("navigation"sv);
- traversable.ensure_ongoing_navigation().navigation_id = navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
traversable.populate_document_for_ongoing_navigation(WebView::CanonicalDocumentState::create({}), destination_document);
EXPECT_EQ(&traversable.active_browsing_context(), initial_context);
@@ -156,7 +156,7 @@ TEST_CASE(response_browsing_context_is_activated_only_at_commit)
EXPECT_EQ(&traversable.active_browsing_context(), initial_context);
EXPECT(initial_group->browsing_context_set().contains(initial_context));
- traversable.ensure_ongoing_navigation().navigation_id = navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
auto destination_document_state = WebView::CanonicalDocumentState::create({});
traversable.populate_document_for_ongoing_navigation(destination_document_state, destination_document);
auto committed_entry = WebView::CanonicalSessionHistoryEntry::create(destination_document_state);
@@ -202,7 +202,7 @@ TEST_CASE(child_navigation_under_a_document_that_switched_groups_uses_its_group)
// The destination document's frames are created once it is activated.
auto navigation_id = Utf16String::from_utf8("navigation"sv);
- traversable.ensure_ongoing_navigation().navigation_id = navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
auto destination_document_state = WebView::CanonicalDocumentState::create({});
traversable.populate_document_for_ongoing_navigation(destination_document_state, destination_document);
Web::HTML::HostedNavigableState committed_state {
@@ -250,13 +250,13 @@ TEST_CASE(clearing_a_navigation_abandons_only_the_document_populated_for_it)
// A document a history job populated outlives a navigation admitted and cleared meanwhile.
auto job_document = make_document();
traversable.populate_document(WebView::CanonicalDocumentState::create({}), job_document);
- traversable.ensure_ongoing_navigation().navigation_id = Utf16String::from_utf8("navigation"sv);
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = Utf16String::from_utf8("navigation"sv) });
traversable.clear_ongoing_navigation();
EXPECT_EQ(traversable.pending_document().ptr(), job_document.ptr());
// A document populated for a navigation goes with it.
auto navigation_id = Utf16String::from_utf8("navigation"sv);
- traversable.ensure_ongoing_navigation().navigation_id = navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
auto navigation_document = make_document();
traversable.populate_document_for_ongoing_navigation(WebView::CanonicalDocumentState::create({}), navigation_document);
EXPECT_EQ(traversable.pending_document().ptr(), navigation_document.ptr());
@@ -274,13 +274,13 @@ TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
auto claimed_navigation_id = Utf16String::from_utf8("claimed"sv);
auto claimed_document_state = WebView::CanonicalDocumentState::create({});
auto claimed_document = make_document();
- traversable.ensure_ongoing_navigation().navigation_id = claimed_navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = claimed_navigation_id });
traversable.populate_document_for_ongoing_navigation(claimed_document_state, claimed_document);
traversable.claim_document_populated_for_ongoing_navigation(*claimed_document);
// A newer navigation replaces the ongoing one before the claimed document is activated, and populates its own.
traversable.clear_ongoing_navigation();
- traversable.ensure_ongoing_navigation().navigation_id = Utf16String::from_utf8("newer"sv);
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = Utf16String::from_utf8("newer"sv) });
auto newer_document = make_document();
traversable.populate_document_for_ongoing_navigation(WebView::CanonicalDocumentState::create({}), newer_document);
EXPECT_EQ(traversable.document_populated_for(*claimed_document_state).ptr(), claimed_document.ptr());
@@ -307,7 +307,7 @@ TEST_CASE(populated_document_replaces_tracked_load_when_document_state_is_reused
entry->navigation_api_id = Utf16String::from_utf8("entry"sv);
traversable.set_active_session_history_entry(entry);
auto navigation_id = Utf16String::from_utf8("reload"sv);
- traversable.ensure_ongoing_navigation().navigation_id = navigation_id;
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
auto destination_url = URL::Parser::basic_parse("https://ladybird.org/redirected"sv).release_value();
Web::HTML::HostedNavigableState committed_state {