Skip to content

Truly independent web browser

LibWebView: Save a document state's origin in the UI process

Shannon Booth88882c666aae
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 84551176734..a9d9733da32 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -1207,7 +1207,7 @@ bool CanonicalNavigable::active_document_is(CanonicalSessionHistoryEntry const&
return m_active_session_history_entry && entry.document_state->document == &active_document();
}
-void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& entry, Web::HTML::HostedNavigableState hosted_state, u64 operation_sequence_number, Optional<Utf16String const&> navigation_id, DidPopulateDocument did_populate_document, RefPtr<WebContentPage> host)
+void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& entry, Web::HTML::HostedNavigableState hosted_state, u64 operation_sequence_number, Optional<Utf16String const&> navigation_id, RefPtr<WebContentPage> host)
{
// A navigation admitted after the operation, such as one a same-document traversal yields to, is newer than it.
auto commits_ongoing_navigation = !ongoing_navigation()
@@ -1219,13 +1219,11 @@ void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& ent
NonnullRefPtr previous_document = active_document();
// The document populated for the entry becomes its document state's document below.
+ // NB: The process hosting the document ran the steps populating it for the entry it holds.
+ entry.document_state->save_the_origin_of_the_populated_document();
RefPtr<CanonicalDocument> document;
- auto save_extra_document_state = true;
- if (auto populated_document = exchange(entry.document_state->populated_document, {}); populated_document.has_value()) {
+ if (auto populated_document = exchange(entry.document_state->populated_document, {}); populated_document.has_value())
document = populated_document->document;
- if (populated_document->inline_content_origin.has_value() && document->origin().is_same_origin(*populated_document->inline_content_origin))
- save_extra_document_state = false;
- }
if (m_document_state_populated_by_history_job == entry.document_state)
m_document_state_populated_by_history_job = nullptr;
VERIFY(document || !active_document_changed);
@@ -1247,13 +1245,6 @@ void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& ent
entry.document_state->document = document;
m_active_session_history_entry = entry;
- // https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
- // 7. If entry's document state's document is not null, then:
- // 2. If saveExtraDocumentState is true:
- // 1. Set entry's document state's origin to document's origin.
- // NB: The process hosting the document ran these steps for the entry it holds.
- if (did_populate_document == DidPopulateDocument::Yes && document != previous_document && save_extra_document_state)
- entry.document_state->origin = document->origin();
if (document != previous_document) {
document->make_active();
if (!document->host())
@@ -1291,9 +1282,9 @@ void CanonicalNavigable::did_commit_navigation(CanonicalSessionHistoryEntry& ent
if (!commits_ongoing_navigation)
return;
- // The activated document's load becomes the navigable's tracked load. Reloads can reuse the document state,
- // while a same-document activation leaves the active document's load in place.
- if (active_document_changed || did_populate_document == DidPopulateDocument::Yes) {
+ // The activated document's load becomes the navigable's tracked load. A same-document activation leaves the active
+ // document's load in place.
+ if (document != previous_document) {
Optional<Utf16String> load_navigation_id;
if (navigation_id.has_value())
load_navigation_id = *navigation_id;
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index 5144fa34d2e..bf0a5ed6456 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -209,13 +209,9 @@ public:
bool current_session_history_entry_is(CanonicalSessionHistoryEntry const&) const;
bool active_document_is(CanonicalSessionHistoryEntry const&) const;
- enum class DidPopulateDocument {
- No,
- Yes,
- };
// A history operation activated the entry. It commits the navigation it names, or with none named, the ongoing
// navigation admitted before the operation's sequence number.
- void did_commit_navigation(CanonicalSessionHistoryEntry&, Web::HTML::HostedNavigableState, u64 operation_sequence_number, Optional<Utf16String const&> navigation_id, DidPopulateDocument, RefPtr<WebContentPage> host);
+ void did_commit_navigation(CanonicalSessionHistoryEntry&, Web::HTML::HostedNavigableState, u64 operation_sequence_number, Optional<Utf16String const&> navigation_id, RefPtr<WebContentPage> host);
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
// The navigation, if the navigable's ongoing navigation is a navigation ID rather than "traversal" or null.
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
index d309c063626..be6a32f31dd 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
@@ -27,6 +27,23 @@ CanonicalDocumentState::CanonicalDocumentState(Web::HTML::CrossProcessId id)
CanonicalDocumentState::~CanonicalDocumentState() = default;
+// https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
+void CanonicalDocumentState::save_the_origin_of_the_populated_document()
+{
+ // 7. If entry's document state's document is not null, then:
+ if (!populated_document.has_value())
+ return;
+ auto const& document = *populated_document->document;
+
+ // 2. If saveExtraDocumentState is true:
+ // NB: It is false for a document created for inline content in place of the response.
+ if (populated_document->inline_content_origin.has_value() && document.origin().is_same_origin(*populated_document->inline_content_origin))
+ return;
+
+ // 1. Set entry's document state's origin to document's origin.
+ origin = document.origin();
+}
+
Web::HTML::SessionHistoryDocumentStateDescriptor CanonicalDocumentState::descriptor() const
{
Vector<Web::HTML::SessionHistoryNestedHistoryDescriptor> nested_history_descriptors;
@@ -64,7 +81,7 @@ static NonnullRefPtr<CanonicalDocumentState> document_state_from_descriptor(Web:
if (existing && update_document_state == CanonicalSessionHistoryEntry::UpdateDocumentState::No)
return existing.release_nonnull();
- auto document_state = existing ? existing.release_nonnull() : CanonicalDocumentState::create(descriptor.id);
+ auto document_state = existing ? NonnullRefPtr { *existing } : CanonicalDocumentState::create(descriptor.id);
document_states.set(descriptor.id, document_state);
document_state->history_policy_container = descriptor.history_policy_container;
document_state->request_referrer = descriptor.request_referrer;
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
index ac08b3865a5..7053d06ccd7 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
@@ -46,6 +46,7 @@ public:
~CanonicalDocumentState();
Web::HTML::SessionHistoryDocumentStateDescriptor descriptor() const;
+ void save_the_origin_of_the_populated_document();
// NB: The identity the processes hosting the document state's documents give it.
Web::HTML::CrossProcessId id;
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index a6d6353cdff..3d03c1a3e3a 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -1034,7 +1034,6 @@ struct CanonicalTraversable::HistoryOperation {
Web::HTML::UnloadDisplayedDocument unload_displayed_document { Web::HTML::UnloadDisplayedDocument::No };
OwnPtr<NavigationLoader> population_loader;
- CanonicalNavigable::DidPopulateDocument did_populate_document { CanonicalNavigable::DidPopulateDocument::No };
RefPtr<CanonicalDocument> document;
};
HashMap<Web::HTML::CrossProcessId, NonnullOwnPtr<PendingChangingJob>> pending_changing_jobs;
@@ -1482,10 +1481,8 @@ void CanonicalTraversable::continue_history_navigation_population(Web::HTML::Cro
navigable->abandon_populated_document(*host.document);
return;
}
- if (host.document) {
- pending_job.value()->did_populate_document = CanonicalNavigable::DidPopulateDocument::Yes;
+ if (host.document)
pending_job.value()->document = host.document;
- }
endpoint = host.page;
add_history_operation_completion_endpoint(*operation, *endpoint);
auto& job = *pending_job.value();
@@ -1663,7 +1660,7 @@ void CanonicalTraversable::unload_displayed_document_for_cross_document_navigati
// The process running a changing navigable's job activated targetEntry's document and applied the continuation's
// remaining steps.
-void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operation, Web::HTML::CrossProcessId navigable_id, NonnullRefPtr<WebContentPage> source_page, CanonicalSessionHistoryEntry& target_entry, CanonicalNavigable::DidPopulateDocument did_populate_document, Web::HTML::HostedNavigableState activated_navigable_state)
+void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operation, Web::HTML::CrossProcessId navigable_id, NonnullRefPtr<WebContentPage> source_page, CanonicalSessionHistoryEntry& target_entry, Web::HTML::HostedNavigableState activated_navigable_state)
{
auto navigable = find(navigable_id);
if (!navigable.has_value())
@@ -1676,7 +1673,7 @@ void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operatio
Optional<Utf16String const&> navigation_id;
if (auto const* navigation = finalized_navigation(operation))
navigation_id = navigation->navigation_id;
- navigable->did_commit_navigation(target_entry, move(activated_navigable_state), operation.sequence_number, navigation_id, did_populate_document, move(host));
+ navigable->did_commit_navigation(target_entry, move(activated_navigable_state), operation.sequence_number, navigation_id, move(host));
if (navigable_id == id()) {
if (auto view = this->view(); view.has_value()) {
@@ -2134,8 +2131,13 @@ void CanonicalTraversable::did_attempt_to_populate_the_history_entry_document(We
// NB: Populating historyEntry's document saves state in the entry, which the page that populated it reports.
auto history_entry_descriptor = Web::HTML::create_session_history_entry_descriptor(parameters.history_entry, 0);
if (!navigation->state.has<CanonicalNavigation::EvaluatingJavaScriptURL>()) {
- if (navigation->history_entry && history_entry_descriptor.document_state.id == navigation->history_entry->document_state->id)
- (void)navigation->history_entry->update_from_descriptor(history_entry_descriptor);
+ if (!navigation->history_entry)
+ return;
+ if (history_entry_descriptor.document_state.id == navigation->history_entry->document_state->id
+ && navigation->history_entry->update_from_descriptor(history_entry_descriptor).is_error()) {
+ return;
+ }
+ navigation->history_entry->document_state->save_the_origin_of_the_populated_document();
return;
}
@@ -2973,7 +2975,7 @@ void CanonicalTraversable::did_receive_changing_navigable_continuation_applied(W
if (!pending_job.has_value())
return;
if (activated_navigable_state.has_value())
- did_activate_history_entry(*operation, navigable_id, source_page, *pending_job.value()->job.target_entry, pending_job.value()->did_populate_document, activated_navigable_state.release_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);
if (previous_entry_persisted_state.has_value()) {
diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.h
index a7e21f9f2c9..50c9b9d506c 100644
--- a/Libraries/LibWebView/CanonicalTraversable.h
+++ b/Libraries/LibWebView/CanonicalTraversable.h
@@ -199,7 +199,7 @@ private:
void send_changing_navigable_continuation_task(HistoryOperation&, Web::HTML::CrossProcessId navigable_id, Web::HTML::UnloadDisplayedDocument);
void deactivate_a_document_for_cross_document_navigation(HistoryOperation&, Web::HTML::CrossProcessId navigable_id);
void unload_displayed_document_for_cross_document_navigation(HistoryOperation&, Web::HTML::CrossProcessId navigable_id);
- void did_activate_history_entry(HistoryOperation&, Web::HTML::CrossProcessId navigable_id, NonnullRefPtr<WebContentPage> source_page, CanonicalSessionHistoryEntry& target_entry, CanonicalNavigable::DidPopulateDocument, Web::HTML::HostedNavigableState activated_navigable_state);
+ void did_activate_history_entry(HistoryOperation&, Web::HTML::CrossProcessId navigable_id, NonnullRefPtr<WebContentPage> source_page, CanonicalSessionHistoryEntry& target_entry, Web::HTML::HostedNavigableState activated_navigable_state);
enum class UnloadedInItsHost : bool {
No,
Yes,
diff --git a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
index dc5b5ecb7d5..6da54b86243 100644
--- a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
+++ b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
@@ -170,7 +170,7 @@ TEST_CASE(response_browsing_context_is_activated_only_at_commit)
.delays_the_load_event_of_its_container = false,
.compositor_context_id = {},
};
- traversable.did_commit_navigation(*committed_entry, move(committed_state), 1, navigation_id, WebView::CanonicalNavigable::DidPopulateDocument::Yes, {});
+ traversable.did_commit_navigation(*committed_entry, move(committed_state), 1, navigation_id, {});
EXPECT_EQ(&traversable.active_browsing_context(), destination_context);
EXPECT(initial_group->browsing_context_set().is_empty());
EXPECT(!traversable.ongoing_navigation());
@@ -215,7 +215,7 @@ TEST_CASE(child_navigation_under_a_document_that_switched_groups_uses_its_group)
.delays_the_load_event_of_its_container = false,
.compositor_context_id = {},
};
- traversable.did_commit_navigation(*WebView::CanonicalSessionHistoryEntry::create(destination_document_state), move(committed_state), 1, navigation_id, WebView::CanonicalNavigable::DidPopulateDocument::Yes, {});
+ traversable.did_commit_navigation(*WebView::CanonicalSessionHistoryEntry::create(destination_document_state), move(committed_state), 1, navigation_id, {});
Web::HTML::ReplicatedContainerState embedder {};
auto frame_document = WebView::CanonicalBrowsingContext::create_a_new_browsing_context_and_document(destination_document.ptr(), embedder, *destination_group, {}, {}).document;
@@ -296,7 +296,7 @@ TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
.delays_the_load_event_of_its_container = false,
.compositor_context_id = {},
};
- traversable.did_commit_navigation(*WebView::CanonicalSessionHistoryEntry::create(claimed_document_state), move(committed_state), 1, claimed_navigation_id, WebView::CanonicalNavigable::DidPopulateDocument::Yes, {});
+ traversable.did_commit_navigation(*WebView::CanonicalSessionHistoryEntry::create(claimed_document_state), move(committed_state), 1, claimed_navigation_id, {});
EXPECT_EQ(&traversable.active_document(), claimed_document.ptr());
EXPECT(!claimed_document_state->populated_document.has_value());
EXPECT_EQ(newer_document_state->populated_document->document.ptr(), newer_document.ptr());
@@ -311,6 +311,7 @@ TEST_CASE(populated_document_replaces_tracked_load_when_document_state_is_reused
traversable.set_active_session_history_entry(entry);
auto navigation_id = Utf16String::from_utf8("reload"sv);
traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
+ traversable.populate_document(entry->document_state, WebView::CanonicalBrowsingContext::create_a_new_top_level_browsing_context_and_document({}).document);
auto destination_url = URL::Parser::basic_parse("https://ladybird.org/redirected"sv).release_value();
Web::HTML::HostedNavigableState committed_state {
@@ -322,7 +323,7 @@ TEST_CASE(populated_document_replaces_tracked_load_when_document_state_is_reused
.delays_the_load_event_of_its_container = false,
.compositor_context_id = {},
};
- traversable.did_commit_navigation(*entry, move(committed_state), 1, navigation_id, WebView::CanonicalNavigable::DidPopulateDocument::Yes, {});
+ traversable.did_commit_navigation(*entry, move(committed_state), 1, navigation_id, {});
EXPECT_EQ(traversable.active_document_load().navigation_id, navigation_id);
}
@@ -346,11 +347,11 @@ TEST_CASE(same_document_traversal_commits_only_navigations_admitted_before_it)
// A navigation admitted after the traversal was requested goes on past the traversal's activation.
traversable.set_ongoing_navigation({ .navigation_id = Utf16String::from_utf8("newer"sv), .sequence_number = 2 });
- traversable.did_commit_navigation(*entry, committed_state(), 1, {}, WebView::CanonicalNavigable::DidPopulateDocument::No, {});
+ traversable.did_commit_navigation(*entry, committed_state(), 1, {}, {});
EXPECT(traversable.ongoing_navigation());
// One admitted before it does not.
- traversable.did_commit_navigation(*entry, committed_state(), 3, {}, WebView::CanonicalNavigable::DidPopulateDocument::No, {});
+ traversable.did_commit_navigation(*entry, committed_state(), 3, {}, {});
EXPECT(!traversable.ongoing_navigation());
}
diff --git a/Tests/LibWebView/TestSessionHistory.cpp b/Tests/LibWebView/TestSessionHistory.cpp
index 3dd1ad7a320..fcf8105b79b 100644
--- a/Tests/LibWebView/TestSessionHistory.cpp
+++ b/Tests/LibWebView/TestSessionHistory.cpp
@@ -469,6 +469,27 @@ TEST_CASE(queued_same_document_entries_are_addressed_by_identity)
EXPECT_EQ(updated_current_entry->document_state->navigable_target_name, Utf16String::from_utf8("updated-name"sv));
}
+TEST_CASE(update_leaves_the_origins_of_a_document_state_alone)
+{
+ auto descriptor = entry(0, "https://a.example/"sv);
+ auto origin = URL::Parser::basic_parse("https://a.example/"sv).release_value().origin();
+ descriptor.document_state.origin = origin;
+ descriptor.document_state.initiator_origin = origin;
+ WebView::CanonicalSessionHistoryEntry::DocumentStates document_states;
+ auto held_entry = MUST(WebView::CanonicalSessionHistoryEntry::create_from_descriptor(descriptor, document_states));
+
+ auto other_origin = URL::Parser::basic_parse("https://b.example/"sv).release_value().origin();
+ descriptor.document_state.origin = other_origin;
+ descriptor.document_state.initiator_origin = other_origin;
+ descriptor.document_state.ever_populated = true;
+ auto updated_entry = MUST(WebView::CanonicalSessionHistoryEntry::create_from_descriptor(descriptor, document_states, WebView::CanonicalSessionHistoryEntry::UpdateDocumentState::Yes));
+
+ EXPECT_EQ(updated_entry->document_state.ptr(), held_entry->document_state.ptr());
+ EXPECT(held_entry->document_state->ever_populated);
+ EXPECT(held_entry->document_state->origin->is_same_origin(origin));
+ EXPECT(held_entry->document_state->initiator_origin->is_same_origin(origin));
+}
+
TEST_CASE(entry_among_its_own_nested_histories_is_rejected)
{
auto parent_entry = entry(0, "https://parent.example/"sv, 10, "main"sv, { nested_history("frame"sv, { entry(0, "https://child.example/"sv, 10, ""sv) }) });