Skip to content

Truly independent web browser

LibWebView: Populate and abandon a document by its document state

Shannon Booth9c39331340ac
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 1dcf601294f..fe684bbec39 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -6,6 +6,7 @@
#include <LibWebView/CanonicalNavigable.h>
+#include <AK/AnyOf.h>
#include <AK/Random.h>
#include <LibWebCommon/HTML/BrowsingContext.h>
#include <LibWebCommon/HTML/HistoryOperation.h>
@@ -796,10 +797,7 @@ ErrorOr<CanonicalNavigable::PageToPopulateDocument> CanonicalNavigable::obtain_p
response_document->document_id = Application::the().allocate_ui_process_cross_process_id();
auto document = create_and_initialize_a_document(*response_document);
loader.set_document(*document, *this);
- if (auto* navigation = uncommitted_navigation(); navigation && navigation->history_entry == &entry)
- populate_document_for_navigation(*navigation, document, loader.result().inline_content_origin);
- else
- populate_document(document_state, document, loader.result().inline_content_origin);
+ populate_document(document_state, document, loader.result().inline_content_origin);
// A document created for inline content stands in for the resource the process that fetched it could not load; that
// process hosts it.
@@ -807,7 +805,7 @@ ErrorOr<CanonicalNavigable::PageToPopulateDocument> CanonicalNavigable::obtain_p
if (!response_document->is_inline_content || is_top_level_traversable()) {
auto host_or_error = obtain_page_to_host(*document, document_state->initiator_origin);
if (host_or_error.is_error()) {
- abandon_populated_document(*document);
+ abandon_populated_document(document_state, *document);
return host_or_error.release_error();
}
host = host_or_error.release_value();
@@ -834,25 +832,21 @@ RefPtr<CanonicalDocument> CanonicalNavigable::document_with_id(Web::HTML::CrossP
return document;
}
+// A document state that is not a navigation's is a history job's, which the navigable holds while the job runs.
void CanonicalNavigable::populate_document(NonnullRefPtr<CanonicalDocumentState> document_state, NonnullRefPtr<CanonicalDocument> document, Optional<URL::Origin> inline_content_origin)
{
- document->determine_whether_it_is_local_file_content(blob_url_store());
- if (document->is_local_file_content())
- document_state->held_local_file_content = true;
- if (m_document_state_populated_by_history_job)
- abandon_populated_document(m_document_state_populated_by_history_job.release_nonnull());
- document_state->populated_document = PopulatedDocument { move(document), move(inline_content_origin) };
- m_document_state_populated_by_history_job = move(document_state);
-}
-
-void CanonicalNavigable::populate_document_for_navigation(CanonicalNavigation& navigation, NonnullRefPtr<CanonicalDocument> document, Optional<URL::Origin> inline_content_origin)
-{
- VERIFY(navigation.history_entry);
- NonnullRefPtr document_state = navigation.history_entry->document_state;
document->determine_whether_it_is_local_file_content(blob_url_store());
if (document->is_local_file_content())
document_state->held_local_file_content = true;
abandon_populated_document(document_state);
+ auto is_for_navigation = any_of(m_navigations, [&](auto const& navigation) {
+ return navigation->history_entry && navigation->history_entry->document_state == document_state;
+ });
+ if (!is_for_navigation) {
+ if (m_document_state_populated_by_history_job)
+ abandon_populated_document(m_document_state_populated_by_history_job.release_nonnull());
+ m_document_state_populated_by_history_job = document_state;
+ }
document_state->populated_document = PopulatedDocument { move(document), move(inline_content_origin) };
}
@@ -868,7 +862,7 @@ void CanonicalNavigable::populate_document_for_javascript_url(CanonicalNavigatio
if (document->web_ui_host().has_value() && document->web_ui_host() != active_document().web_ui_host())
return;
navigation.history_entry = move(history_entry);
- populate_document_for_navigation(navigation, move(document));
+ populate_document(navigation.history_entry->document_state, move(document));
}
// The process hosting a document populated for the navigable created it with an origin other than its navigation
@@ -906,15 +900,10 @@ void CanonicalNavigable::did_create_populated_document_with_an_origin_of_its_own
populated_document->document = move(document);
}
-void CanonicalNavigable::abandon_populated_document(CanonicalDocument const& document)
+void CanonicalNavigable::abandon_populated_document(NonnullRefPtr<CanonicalDocumentState> document_state, CanonicalDocument const& document)
{
- RefPtr<CanonicalDocumentState> document_state_holding_document;
- for_each_populated_document_state([&](CanonicalDocumentState& document_state) {
- if (document_state.populated_document->document == &document)
- document_state_holding_document = document_state;
- });
- if (document_state_holding_document)
- abandon_populated_document(document_state_holding_document.release_nonnull());
+ if (document_state->populated_document.has_value() && document_state->populated_document->document == &document)
+ abandon_populated_document(move(document_state));
}
// A page created to host the abandoned document is discarded, unless it hosts another document of the navigable.
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index 2aa7d2474d2..f8919ad55e2 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -91,11 +91,10 @@ public:
RefPtr<CanonicalDocument> document_with_id(Web::HTML::CrossProcessId) const;
void populate_document(NonnullRefPtr<CanonicalDocumentState>, NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});
- void populate_document_for_navigation(CanonicalNavigation&, NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});
// Populates nothing for a document that would hold access the active document lacks.
void populate_document_for_javascript_url(CanonicalNavigation&, NonnullRefPtr<CanonicalSessionHistoryEntry>, NonnullRefPtr<CanonicalDocument>);
void did_create_populated_document_with_an_origin_of_its_own(WebContentPage const& host, Web::HTML::CrossProcessId document_id, Web::HTML::PopulatedDocumentOrigin, Web::HTML::EnvironmentId const& environment_id);
- void abandon_populated_document(CanonicalDocument const&);
+ void abandon_populated_document(NonnullRefPtr<CanonicalDocumentState>, CanonicalDocument const&);
void place_populated_document(CanonicalDocument&, WebContentPage&);
// The document states holding a populated document: the history entries' of the navigations under way, and the one a
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index 01ea36d3730..c9361622d09 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -1022,7 +1022,7 @@ struct CanonicalTraversable::HistoryOperation {
if (population_loader)
population_loader->reclaim_response_body_after_failed_handoff();
if (navigable.has_value() && document)
- navigable->abandon_populated_document(*document);
+ navigable->abandon_populated_document(job.target_entry->document_state, *document);
}
ApplyHistoryStepJobs::ChangingNavigableHistoryStepJob job;
@@ -1467,7 +1467,7 @@ void CanonicalTraversable::continue_history_navigation_population(Web::HTML::Cro
pending_job = operation->pending_changing_jobs.get(navigable_id);
if (!operation || !pending_job.has_value()) {
if (host.document)
- navigable->abandon_populated_document(*host.document);
+ navigable->abandon_populated_document(target_entry->document_state, *host.document);
return;
}
if (host.document)
@@ -2981,7 +2981,7 @@ void CanonicalTraversable::did_receive_changing_navigable_continuation_applied(W
if (activated_navigable_state.has_value())
did_activate_history_entry(*operation, navigable_id, source_page, *pending_job.value()->job.target_entry, activated_navigable_state.release_value());
else if (auto navigable = find(navigable_id); navigable.has_value() && pending_job.value()->document)
- navigable->abandon_populated_document(*pending_job.value()->document);
+ navigable->abandon_populated_document(pending_job.value()->job.target_entry->document_state, *pending_job.value()->document);
if (previous_entry_persisted_state.has_value()) {
auto navigable = find(navigable_id);
if (navigable.has_value())
diff --git a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
index 892aa05ce60..5e6fcbd3316 100644
--- a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
+++ b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
@@ -178,7 +178,7 @@ TEST_CASE(response_browsing_context_is_activated_only_at_commit)
auto navigation_id = Utf16String::from_utf8("navigation"sv);
auto abandoned_document_state = WebView::CanonicalDocumentState::create({});
traversable.set_ongoing_navigation(navigation_for_testing(navigation_id, 0, WebView::CanonicalSessionHistoryEntry::create(abandoned_document_state)));
- traversable.populate_document_for_navigation(*traversable.uncommitted_navigation(), destination_document);
+ traversable.populate_document(traversable.uncommitted_navigation()->history_entry->document_state, destination_document);
EXPECT_EQ(&traversable.active_browsing_context(), initial_context);
EXPECT(initial_group->browsing_context_set().contains(initial_context));
@@ -191,7 +191,7 @@ TEST_CASE(response_browsing_context_is_activated_only_at_commit)
auto destination_document_state = WebView::CanonicalDocumentState::create({});
auto committed_entry = WebView::CanonicalSessionHistoryEntry::create(destination_document_state);
traversable.set_ongoing_navigation(navigation_for_testing(navigation_id, 0, committed_entry));
- traversable.populate_document_for_navigation(*traversable.uncommitted_navigation(), destination_document);
+ traversable.populate_document(traversable.uncommitted_navigation()->history_entry->document_state, destination_document);
Web::HTML::HostedNavigableState committed_state {
.active_document_url = destination_url,
.active_document_is_fully_active = true,
@@ -235,7 +235,7 @@ TEST_CASE(child_navigation_under_a_document_that_switched_groups_uses_its_group)
auto navigation_id = Utf16String::from_utf8("navigation"sv);
auto destination_document_state = WebView::CanonicalDocumentState::create({});
traversable.set_ongoing_navigation(navigation_for_testing(navigation_id, 0, WebView::CanonicalSessionHistoryEntry::create(destination_document_state)));
- traversable.populate_document_for_navigation(*traversable.uncommitted_navigation(), destination_document);
+ traversable.populate_document(traversable.uncommitted_navigation()->history_entry->document_state, destination_document);
Web::HTML::HostedNavigableState committed_state {
.active_document_url = destination_url,
.active_document_is_fully_active = true,
@@ -290,7 +290,7 @@ TEST_CASE(clearing_a_navigation_abandons_only_the_document_populated_for_it)
auto navigation_document_state = WebView::CanonicalDocumentState::create({});
traversable.set_ongoing_navigation(navigation_for_testing(navigation_id, 0, WebView::CanonicalSessionHistoryEntry::create(navigation_document_state)));
auto navigation_document = make_document();
- traversable.populate_document_for_navigation(*traversable.uncommitted_navigation(), navigation_document);
+ traversable.populate_document(traversable.uncommitted_navigation()->history_entry->document_state, navigation_document);
EXPECT_EQ(navigation_document_state->populated_document->document.ptr(), navigation_document.ptr());
traversable.clear_ongoing_navigation();
EXPECT(!navigation_document_state->populated_document.has_value());
@@ -313,7 +313,7 @@ TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
auto newer_document_state = WebView::CanonicalDocumentState::create({});
traversable.set_ongoing_navigation(navigation_for_testing(Utf16String::from_utf8("newer"sv), 0, WebView::CanonicalSessionHistoryEntry::create(newer_document_state)));
auto newer_document = make_document();
- traversable.populate_document_for_navigation(*traversable.uncommitted_navigation(), newer_document);
+ traversable.populate_document(traversable.uncommitted_navigation()->history_entry->document_state, newer_document);
EXPECT_EQ(claimed_document_state->populated_document->document.ptr(), claimed_document.ptr());
Web::HTML::HostedNavigableState committed_state {