Skip to content

Truly independent web browser

LibWebView: Choose the page to populate a document in one place

Shannon Boothee95373c5a49
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 6c1025effb5..3be86c133b4 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -804,6 +804,42 @@ ErrorOr<NonnullRefPtr<WebContentPage>> CanonicalNavigable::obtain_page_to_host_i
return *host->page(page_id);
}
+ErrorOr<CanonicalNavigable::PageToPopulateDocument> CanonicalNavigable::obtain_page_to_populate_document(CanonicalSessionHistoryEntry& entry, NavigationLoader& loader, WebContentPage& page_that_created_navigation_params)
+{
+ auto response_document = loader.response_document();
+ if (!response_document.has_value())
+ return PageToPopulateDocument { page_that_created_navigation_params, nullptr };
+
+ NonnullRefPtr document_state = entry.document_state;
+ 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 (ongoing_navigation() && ongoing_navigation()->history_entry == &entry)
+ populate_document_for_ongoing_navigation(document, loader.result().inline_content_origin);
+ else
+ 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.
+ NonnullRefPtr<WebContentPage> host = page_that_created_navigation_params;
+ 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);
+ return host_or_error.release_error();
+ }
+ host = host_or_error.release_value();
+
+ // Obtaining the page can replace the tab's process, whose change callbacks can abandon the document.
+ if (!document_state->populated_document.has_value() || document_state->populated_document->document != document)
+ return Error::from_string_literal("The populated document was abandoned");
+ }
+
+ // The host takes the navigable over when the document is activated, after the displayed document is unloaded.
+ place_populated_document(*document, *host);
+ return PageToPopulateDocument { move(host), move(document) };
+}
+
RefPtr<CanonicalDocument> CanonicalNavigable::document_with_id(Web::HTML::CrossProcessId id) const
{
if (active_document().id() == id)
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index ddb721b8804..47728144695 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -153,6 +153,14 @@ public:
// for the traversable, see CanonicalTraversable::obtain_page_to_host_traversable.
ErrorOr<NonnullRefPtr<WebContentPage>> obtain_page_to_host(CanonicalDocument const&, Optional<URL::Origin> const& initiator_origin);
+ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
+ // The page to run the task step 5 queues, with the document it populates if the response creates one.
+ struct PageToPopulateDocument {
+ NonnullRefPtr<WebContentPage> page;
+ RefPtr<CanonicalDocument> document;
+ };
+ ErrorOr<PageToPopulateDocument> obtain_page_to_populate_document(CanonicalSessionHistoryEntry&, NavigationLoader&, WebContentPage& page_that_created_navigation_params);
+
// A page hosting a document populated for the navigable when it is not the page hosting the displayed one. The
// displayed document stays with its host until the next is activated, so that it is unloaded there before the
// container is handed over.
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index 787fd4a8c2a..a87026a9a42 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -1433,38 +1433,28 @@ void CanonicalTraversable::continue_history_navigation_population(Web::HTML::Cro
return;
auto loader = move(pending_job.value()->population_loader);
- // The task queued by step 5 of attempting to populate the history entry's document runs in the process hosting
- // the browsing context that the document it creates belongs to. A response that creates no document is finished
- // by the process that fetched it.
- auto response_document = loader->response_document();
- if (response_document.has_value()) {
- response_document->document_id = Application::the().allocate_ui_process_cross_process_id();
+ NonnullRefPtr target_entry = pending_job.value()->job.target_entry;
+ auto host_or_error = navigable->obtain_page_to_populate_document(target_entry, *loader, *endpoint);
+ if (host_or_error.is_error()) {
+ did_fail_history_navigation_population(operation_id, navigable_id, *endpoint);
+ return;
+ }
+ auto host = host_or_error.release_value();
+
+ // Obtaining the page can replace the tab's process, whose change callbacks can finish the operation.
+ operation = find_history_operation(operation_id);
+ if (operation)
+ pending_job = operation->pending_changing_jobs.get(navigable_id);
+ if (!operation || !pending_job.has_value()) {
+ if (host.document)
+ navigable->abandon_populated_document(*host.document);
+ return;
+ }
+ if (host.document) {
pending_job.value()->did_populate_document = CanonicalNavigable::DidPopulateDocument::Yes;
- auto document = navigable->create_and_initialize_a_document(*response_document);
- pending_job.value()->document = document;
- loader->set_document(*document, *navigable);
- navigable->populate_document(pending_job.value()->job.target_entry->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.
- if (!response_document->is_inline_content || navigable->is_top_level_traversable()) {
- auto host = navigable->obtain_page_to_host(*document, pending_job.value()->job.target_entry->document_state->initiator_origin);
- if (host.is_error()) {
- did_fail_history_navigation_population(operation_id, navigable_id, *endpoint);
- return;
- }
- // Obtaining the page can replace the tab's process, whose change callbacks can finish the operation.
- endpoint = host.release_value();
- operation = find_history_operation(operation_id);
- if (!operation)
- return;
- pending_job = operation->pending_changing_jobs.get(navigable_id);
- if (!pending_job.has_value())
- return;
- }
- // The host takes the navigable over when the document is activated, after the displayed document is unloaded.
- navigable->place_populated_document(*document, *endpoint);
+ pending_job.value()->document = host.document;
}
+ endpoint = host.page;
add_history_operation_completion_endpoint(*operation, *endpoint);
auto& job = *pending_job.value();
job.population_loader = move(loader);
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 2fee896fd80..c14b659911b 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -308,41 +308,15 @@ bool WebContentPage::continue_navigation_population_in_selected_process(Web::HTM
if (!acquiring)
return false;
ongoing_navigation->state = CanonicalNavigation::ChoosingHost { acquiring->worker, move(acquiring->loader) };
- auto is_still_choosing_host = [&] {
- auto const* ongoing_navigation = navigable->ongoing_navigation();
- return ongoing_navigation
- && ongoing_navigation->navigation_id == navigation_id
- && ongoing_navigation->state.has<CanonicalNavigation::ChoosingHost>();
- };
- // The task queued by step 5 of attempting to populate the history entry's document runs in the process hosting
- // the browsing context that the document it creates belongs to. A response that creates no document is finished
- // by the process that fetched it.
- RefPtr<WebContentPage> host = this;
- auto response_document = navigable->ongoing_navigation()->loader()->response_document();
- if (response_document.has_value()) {
- response_document->document_id = Application::the().allocate_ui_process_cross_process_id();
- NonnullRefPtr history_entry = *navigable->ongoing_navigation()->history_entry;
- auto document = navigable->create_and_initialize_a_document(*response_document);
- navigable->ongoing_navigation()->loader()->set_document(*document, *navigable);
- navigable->populate_document_for_ongoing_navigation(*document, navigable->ongoing_navigation()->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.
- if (!response_document->is_inline_content || navigable->is_top_level_traversable()) {
- auto host_or_error = navigable->obtain_page_to_host(*document, history_entry->document_state->initiator_origin);
- if (host_or_error.is_error()) {
- warnln("Unable to obtain a page to host the navigation's document: {}", host_or_error.error());
- return false;
- }
- host = host_or_error.release_value();
- // Obtaining the page can replace the tab's process, whose change callbacks can reenter navigation.
- if (!is_still_choosing_host())
- return false;
- }
- // The host takes the navigable over when the document is activated, after the displayed document is unloaded.
- navigable->place_populated_document(*document, *host);
+ NonnullRefPtr history_entry = *ongoing_navigation->history_entry;
+ auto host_or_error = navigable->obtain_page_to_populate_document(history_entry, *ongoing_navigation->loader(), *this);
+ if (host_or_error.is_error()) {
+ warnln("Unable to obtain a page to populate the navigation's document: {}", host_or_error.error());
+ return false;
}
+ auto host = host_or_error.release_value().page;
+
auto& loader = *navigable->ongoing_navigation()->loader();
navigable->set_navigation_host(*host);
auto finish_handoff = [page = NonnullRefPtr<WebContentPage>(*this), host, navigable_id, navigation_id](bool succeeded) {