Skip to content

Truly independent web browser

LibWebView: Hold a populated document on its document state

Shannon Boothb7d9f4bc555b
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 18c39bb3d9f..99fab7e1f27 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -804,47 +804,14 @@ ErrorOr<NonnullRefPtr<WebContentPage>> CanonicalNavigable::obtain_page_to_host_i
return *host->page(page_id);
}
-Optional<PopulatedDocument> const& CanonicalNavigable::populated_document() const
-{
- if (ongoing_navigation() && ongoing_navigation()->populated_document.has_value())
- return ongoing_navigation()->populated_document;
- return m_document_populated_by_history_job;
-}
-
-RefPtr<CanonicalDocument> CanonicalNavigable::pending_document() const
-{
- auto const& populated_document = this->populated_document();
- return populated_document.has_value() ? populated_document->document.ptr() : nullptr;
-}
-
-RefPtr<CanonicalDocument> CanonicalNavigable::document_populated_for(CanonicalDocumentState const& document_state) const
-{
- RefPtr<CanonicalDocument> document;
- for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (populated_document.document_state == &document_state)
- document = populated_document.document;
- });
- return document;
-}
-
-PopulatedDocument const* CanonicalNavigable::populated_document_with_state_id(Web::HTML::CrossProcessId document_state_id) const
-{
- PopulatedDocument const* result = nullptr;
- for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (populated_document.document_state->id == document_state_id)
- result = &populated_document;
- });
- return result;
-}
-
RefPtr<CanonicalDocument> CanonicalNavigable::document_with_id(Web::HTML::CrossProcessId id) const
{
if (active_document().id() == id)
return active_document();
RefPtr<CanonicalDocument> document;
- for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (populated_document.document->id() == id)
- document = populated_document.document;
+ for_each_populated_document_state([&](CanonicalDocumentState const& document_state) {
+ if (document_state.populated_document->document->id() == id)
+ document = document_state.populated_document->document;
});
return document;
}
@@ -854,18 +821,21 @@ void CanonicalNavigable::populate_document(NonnullRefPtr<CanonicalDocumentState>
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(m_document_populated_by_history_job);
- m_document_populated_by_history_job = PopulatedDocument { move(document_state), move(document), move(inline_content_origin) };
+ 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_ongoing_navigation(NonnullRefPtr<CanonicalDocumentState> document_state, NonnullRefPtr<CanonicalDocument> document, Optional<URL::Origin> inline_content_origin)
+void CanonicalNavigable::populate_document_for_ongoing_navigation(NonnullRefPtr<CanonicalDocument> document, Optional<URL::Origin> inline_content_origin)
{
- VERIFY(ongoing_navigation());
+ VERIFY(ongoing_navigation() && ongoing_navigation()->history_entry);
+ NonnullRefPtr document_state = ongoing_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(ongoing_navigation()->populated_document);
- ongoing_navigation()->populated_document = PopulatedDocument { move(document_state), move(document), move(inline_content_origin) };
+ abandon_populated_document(document_state);
+ document_state->populated_document = PopulatedDocument { move(document), move(inline_content_origin) };
}
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-to-a-javascript:-url
@@ -888,10 +858,10 @@ bool CanonicalNavigable::populate_document_for_javascript_url(NonnullRefPtr<Cano
void CanonicalNavigable::did_create_populated_document_with_an_origin_of_its_own(WebContentPage const& host, Web::HTML::CrossProcessId document_id, Web::HTML::PopulatedDocumentOrigin populated_document_origin, Web::HTML::EnvironmentId const& environment_id)
{
Optional<PopulatedDocument&> populated_document;
- if (ongoing_navigation() && ongoing_navigation()->populated_document.has_value() && ongoing_navigation()->populated_document->document->id() == document_id)
- populated_document = *ongoing_navigation()->populated_document;
- else if (m_document_populated_by_history_job.has_value() && m_document_populated_by_history_job->document->id() == document_id)
- populated_document = *m_document_populated_by_history_job;
+ for_each_populated_document_state([&](CanonicalDocumentState& document_state) {
+ if (document_state.populated_document->document->id() == document_id)
+ populated_document = *document_state.populated_document;
+ });
if (!populated_document.has_value() || populated_document->document->host() != &host)
return;
@@ -924,9 +894,10 @@ bool CanonicalNavigable::take_navigation_to_finalize(Utf16String const& navigati
auto* navigation = ongoing_navigation();
if (!navigation || navigation->navigation_id != navigation_id || navigation->host() != &page)
return false;
- if (navigation->populated_document.has_value()) {
- abandon_populated_document(m_document_populated_by_history_job);
- m_document_populated_by_history_job = navigation->populated_document.release_value();
+ if (navigation->history_entry && navigation->history_entry->document_state->populated_document.has_value()) {
+ 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 = navigation->history_entry->document_state;
}
m_navigation_being_finalized = move(*navigation);
m_ongoing_navigation = Empty {};
@@ -935,20 +906,25 @@ bool CanonicalNavigable::take_navigation_to_finalize(Utf16String const& navigati
void CanonicalNavigable::abandon_populated_document(CanonicalDocument const& document)
{
- if (ongoing_navigation() && ongoing_navigation()->populated_document.has_value() && ongoing_navigation()->populated_document->document == &document)
- abandon_populated_document(ongoing_navigation()->populated_document);
- if (m_document_populated_by_history_job.has_value() && m_document_populated_by_history_job->document == &document)
- abandon_populated_document(m_document_populated_by_history_job);
+ 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());
}
// A page created to host the abandoned document is discarded, unless it hosts another document of the navigable.
-void CanonicalNavigable::abandon_populated_document(Optional<PopulatedDocument>& populated_document)
+void CanonicalNavigable::abandon_populated_document(NonnullRefPtr<CanonicalDocumentState> document_state)
{
- if (!populated_document.has_value())
+ if (m_document_state_populated_by_history_job == document_state)
+ m_document_state_populated_by_history_job = nullptr;
+ if (!document_state->populated_document.has_value())
return;
- NonnullRefPtr document = populated_document->document;
+ NonnullRefPtr document = document_state->populated_document->document;
RefPtr<WebContentPage> host = document->host();
- populated_document.clear();
+ document_state->populated_document.clear();
if (!host || top_level_traversable().hosts(*this, *host) || pending_host_matches(*host))
return;
host->async_discard_provisional_navigable(id());
@@ -956,11 +932,9 @@ void CanonicalNavigable::abandon_populated_document(Optional<PopulatedDocument>&
top_level_traversable().release_page_if_unused(host.release_nonnull());
}
-void CanonicalNavigable::place_pending_document(WebContentPage& page)
+void CanonicalNavigable::place_populated_document(CanonicalDocument& document, WebContentPage& page)
{
- auto document = pending_document();
- VERIFY(document);
- document->set_host(page);
+ document.set_host(page);
send_viewport_to_host();
}
@@ -976,20 +950,25 @@ bool CanonicalNavigable::pending_host_matches(WebContentPage const& page) const
void CanonicalNavigable::discard_pending_host()
{
- if (ongoing_navigation())
- abandon_populated_document(ongoing_navigation()->populated_document);
- abandon_populated_document(m_document_populated_by_history_job);
+ Vector<NonnullRefPtr<CanonicalDocumentState>> document_states;
+ for_each_populated_document_state([&](CanonicalDocumentState& document_state) {
+ document_states.append(document_state);
+ });
+ for (auto& document_state : document_states)
+ abandon_populated_document(move(document_state));
}
void CanonicalNavigable::discard_pending_host(WebContentPage const& page)
{
- auto is_pending_in_page = [&](Optional<PopulatedDocument> const& populated_document) {
- return populated_document.has_value() && populated_document->document->host() == &page && active_document().host() != &page;
- };
- if (ongoing_navigation() && is_pending_in_page(ongoing_navigation()->populated_document))
- abandon_populated_document(ongoing_navigation()->populated_document);
- if (is_pending_in_page(m_document_populated_by_history_job))
- abandon_populated_document(m_document_populated_by_history_job);
+ if (active_document().host() == &page)
+ return;
+ Vector<NonnullRefPtr<CanonicalDocumentState>> document_states;
+ for_each_populated_document_state([&](CanonicalDocumentState& document_state) {
+ if (document_state.populated_document->document->host() == &page)
+ document_states.append(document_state);
+ });
+ for (auto& document_state : document_states)
+ abandon_populated_document(move(document_state));
}
void CanonicalNavigable::set_viewport(Web::DevicePixelRect viewport_rect, Web::DevicePixelRect viewport_intersection, double device_pixel_ratio)
@@ -1204,16 +1183,15 @@ 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.
- RefPtr<CanonicalDocument> document = document_populated_for(*entry.document_state);
+ RefPtr<CanonicalDocument> document;
auto save_extra_document_state = true;
- for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (populated_document.document_state == entry.document_state && populated_document.inline_content_origin.has_value() && populated_document.document->origin().is_same_origin(*populated_document.inline_content_origin))
+ 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 (ongoing_navigation() && ongoing_navigation()->populated_document.has_value() && ongoing_navigation()->populated_document->document_state == entry.document_state)
- ongoing_navigation()->populated_document.clear();
- if (m_document_populated_by_history_job.has_value() && m_document_populated_by_history_job->document_state == entry.document_state)
- m_document_populated_by_history_job.clear();
+ }
+ if (m_document_state_populated_by_history_job == entry.document_state)
+ m_document_state_populated_by_history_job = nullptr;
VERIFY(document || !active_document_changed);
if (!document)
document = previous_document;
@@ -1326,8 +1304,8 @@ void CanonicalNavigable::clear_ongoing_navigation_state()
void CanonicalNavigable::clear_ongoing_navigation()
{
// The document populated for the navigation is not going to be activated.
- if (ongoing_navigation())
- abandon_populated_document(ongoing_navigation()->populated_document);
+ if (ongoing_navigation() && ongoing_navigation()->history_entry)
+ abandon_populated_document(ongoing_navigation()->history_entry->document_state);
clear_ongoing_navigation_state();
}
@@ -1367,16 +1345,14 @@ bool CanonicalNavigable::cancel_navigation_for_client(WebContentClient& client)
// The history operation finalizing a navigation is over. The document populated for it, if it was not activated, is not
// going to be.
-void CanonicalNavigable::did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::CrossProcessId document_state_id, Web::HTML::HistoryStepResult result)
+void CanonicalNavigable::did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::HistoryStepResult result)
{
if (!m_navigation_being_finalized.has_value() || m_navigation_being_finalized->navigation_id != navigation_id)
return;
- m_navigation_being_finalized.clear();
- if (auto const* populated_document = populated_document_with_state_id(document_state_id)) {
- NonnullRefPtr document = populated_document->document;
- abandon_populated_document(*document);
- }
- if (result != Web::HTML::HistoryStepResult::Applied && m_active_document_load.navigation_id == navigation_id)
+ auto navigation = m_navigation_being_finalized.release_value();
+ if (navigation.history_entry)
+ abandon_populated_document(navigation.history_entry->document_state);
+ if (result != Web::HTML::HistoryStepResult::Applied && m_active_document_load.navigation_id == navigation.navigation_id)
clear_active_document_load();
}
diff --git a/Libraries/LibWebView/CanonicalNavigable.h b/Libraries/LibWebView/CanonicalNavigable.h
index cf1f3b15830..dfb7a3056ba 100644
--- a/Libraries/LibWebView/CanonicalNavigable.h
+++ b/Libraries/LibWebView/CanonicalNavigable.h
@@ -89,31 +89,25 @@ public:
// https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target-name
Utf16String const& target_name() const;
- // The document state of the session history entry the navigable is navigating or traversing to, and the document
- // populated for it, which becomes the document state's document when the entry is activated. A document populated
- // for a navigation goes with the navigation until a history job claims it to activate it; one a history job
- // populated or claimed goes when the job's operation finishes. A newer navigation can populate its own document
- // while a claimed one waits to be activated.
- RefPtr<CanonicalDocument> pending_document() const;
- RefPtr<CanonicalDocument> document_populated_for(CanonicalDocumentState const&) const;
- PopulatedDocument const* populated_document_with_state_id(Web::HTML::CrossProcessId) const;
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_ongoing_navigation(NonnullRefPtr<CanonicalDocumentState>, NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});
+ void populate_document_for_ongoing_navigation(NonnullRefPtr<CanonicalDocument>, Optional<URL::Origin> inline_content_origin = {});
// Returns false, populating nothing, for a document that would hold access the active document lacks.
bool populate_document_for_javascript_url(NonnullRefPtr<CanonicalDocumentState>, 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);
bool take_navigation_to_finalize(Utf16String const& navigation_id, WebContentPage const&);
void abandon_populated_document(CanonicalDocument const&);
- void place_pending_document(WebContentPage&);
+ void place_populated_document(CanonicalDocument&, WebContentPage&);
+ // The document states holding a populated document: the ongoing navigation's history entry's, and the one a history
+ // job is to activate.
template<typename Callback>
- void for_each_populated_document(Callback callback) const
+ void for_each_populated_document_state(Callback callback) const
{
- if (auto const* navigation = ongoing_navigation(); navigation && navigation->populated_document.has_value())
- callback(*navigation->populated_document);
- if (m_document_populated_by_history_job.has_value())
- callback(*m_document_populated_by_history_job);
+ if (auto const* navigation = ongoing_navigation(); navigation && navigation->history_entry && navigation->history_entry->document_state->populated_document.has_value())
+ callback(*navigation->history_entry->document_state);
+ if (m_document_state_populated_by_history_job)
+ callback(*m_document_state_populated_by_history_job);
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
@@ -165,8 +159,8 @@ public:
template<typename Callback>
void for_each_pending_host(Callback callback) const
{
- for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (auto const& host = populated_document.document->host(); host && host != active_document().host())
+ for_each_populated_document_state([&](CanonicalDocumentState const& document_state) {
+ if (auto const& host = document_state.populated_document->document->host(); host && host != active_document().host())
callback(*host);
});
}
@@ -232,8 +226,9 @@ public:
void clear_ongoing_navigation_state();
void set_navigation_host(WebContentPage&);
bool cancel_navigation_for_client(WebContentClient&);
+ CanonicalNavigation* navigation_being_finalized() { return m_navigation_being_finalized.has_value() ? &*m_navigation_being_finalized : nullptr; }
CanonicalNavigation const* navigation_being_finalized() const { return m_navigation_being_finalized.has_value() ? &*m_navigation_being_finalized : nullptr; }
- void did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::CrossProcessId document_state_id, Web::HTML::HistoryStepResult);
+ void did_finish_finalizing_navigation(Utf16String const& navigation_id, Web::HTML::HistoryStepResult);
bool has_uncommitted_navigation() const { return ongoing_navigation(); }
Optional<Utf16String> tracked_load_navigation_id() const;
@@ -249,11 +244,8 @@ private:
Vector<NonnullOwnPtr<CanonicalNavigable>> m_children;
Optional<Web::HTML::HostedNavigableState> m_hosted_state;
- Optional<PopulatedDocument> const& populated_document() const;
- void abandon_populated_document(Optional<PopulatedDocument>&);
- // AD-HOC: A reload populates the document state of the active session history entry, whose document stays the
- // navigable's active document until the populated one is activated.
- Optional<PopulatedDocument> m_document_populated_by_history_job;
+ void abandon_populated_document(NonnullRefPtr<CanonicalDocumentState>);
+ RefPtr<CanonicalDocumentState> m_document_state_populated_by_history_job;
RefPtr<CanonicalSessionHistoryEntry> m_current_session_history_entry;
RefPtr<CanonicalSessionHistoryEntry> m_active_session_history_entry;
struct Traversal {
diff --git a/Libraries/LibWebView/CanonicalNavigation.h b/Libraries/LibWebView/CanonicalNavigation.h
index 99472f5ed89..57a7d277ace 100644
--- a/Libraries/LibWebView/CanonicalNavigation.h
+++ b/Libraries/LibWebView/CanonicalNavigation.h
@@ -23,13 +23,6 @@
namespace WebView {
-struct PopulatedDocument {
- NonnullRefPtr<CanonicalDocumentState> document_state;
- NonnullRefPtr<CanonicalDocument> document;
- // The origin of a document the hosting process creates for inline content in place of the response.
- Optional<URL::Origin> inline_content_origin {};
-};
-
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
// A navigable's navigation, from its admission until its document is activated or it is canceled or superseded, with
// what it acquires on the way.
@@ -75,7 +68,8 @@ public:
Optional<Web::HTML::PreparedNavigationDescriptor> retry {};
u64 sequence_number { 0 };
State state { Admitted {} };
- Optional<PopulatedDocument> populated_document {};
+ // Navigate's historyEntry, from step 23.6 on, whose document state holds the document populated for it.
+ RefPtr<CanonicalSessionHistoryEntry> history_entry {};
WebContentPage* worker() const;
WebContentPage* host() const;
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
index 7af646b41e9..d309c063626 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.cpp
@@ -90,17 +90,22 @@ static NonnullRefPtr<CanonicalDocumentState> document_state_from_descriptor(Web:
return document_state;
}
+static void set_entry_fields_from_descriptor(CanonicalSessionHistoryEntry& entry, Web::HTML::SessionHistoryEntryDescriptor const& descriptor)
+{
+ entry.step = descriptor.step;
+ entry.url = descriptor.url;
+ entry.classic_history_api_state = descriptor.classic_history_api_state;
+ entry.navigation_api_state = descriptor.navigation_api_state;
+ entry.navigation_api_key = descriptor.navigation_api_key;
+ entry.navigation_api_id = descriptor.navigation_api_id;
+ entry.scroll_restoration_mode = descriptor.scroll_restoration_mode;
+ entry.scroll_position_data = descriptor.scroll_position_data;
+}
+
static NonnullRefPtr<CanonicalSessionHistoryEntry> create_entry_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const& descriptor, CanonicalSessionHistoryEntry::DocumentStates& document_states, CanonicalSessionHistoryEntry::UpdateDocumentState update_document_state)
{
auto entry = CanonicalSessionHistoryEntry::create(document_state_from_descriptor(descriptor.document_state, document_states, update_document_state));
- entry->step = descriptor.step;
- entry->url = descriptor.url;
- entry->classic_history_api_state = descriptor.classic_history_api_state;
- entry->navigation_api_state = descriptor.navigation_api_state;
- entry->navigation_api_key = descriptor.navigation_api_key;
- entry->navigation_api_id = descriptor.navigation_api_id;
- entry->scroll_restoration_mode = descriptor.scroll_restoration_mode;
- entry->scroll_position_data = descriptor.scroll_position_data;
+ set_entry_fields_from_descriptor(*entry, descriptor);
return entry;
}
@@ -121,6 +126,17 @@ static bool has_document_state_cycle(CanonicalDocumentState const& document_stat
return false;
}
+static ErrorOr<void> reject_document_state_cycle(CanonicalDocumentState const& document_state, CanonicalSessionHistoryEntry::DocumentStates& document_states)
+{
+ Vector<CanonicalDocumentState const*> ancestors;
+ if (!has_document_state_cycle(document_state, ancestors))
+ return {};
+ // NB: Breaks the cycle, so that the entries are freed.
+ for (auto& [id, state] : document_states)
+ state->nested_histories.clear();
+ return Error::from_string_literal("A session history entry's document state is among its nested histories' entries");
+}
+
ErrorOr<NonnullRefPtr<CanonicalSessionHistoryEntry>> CanonicalSessionHistoryEntry::create_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const& descriptor)
{
DocumentStates document_states;
@@ -130,16 +146,22 @@ ErrorOr<NonnullRefPtr<CanonicalSessionHistoryEntry>> CanonicalSessionHistoryEntr
ErrorOr<NonnullRefPtr<CanonicalSessionHistoryEntry>> CanonicalSessionHistoryEntry::create_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const& descriptor, DocumentStates& document_states, UpdateDocumentState update_document_state)
{
auto entry = create_entry_from_descriptor(descriptor, document_states, update_document_state);
- Vector<CanonicalDocumentState const*> ancestors;
- if (has_document_state_cycle(*entry->document_state, ancestors)) {
- // NB: Breaks the cycle, so that the entries are freed.
- for (auto& [id, document_state] : document_states)
- document_state->nested_histories.clear();
- return Error::from_string_literal("A session history entry's document state is among its nested histories' entries");
- }
+ TRY(reject_document_state_cycle(*entry->document_state, document_states));
return entry;
}
+// The entry's document state is updated when the descriptor names it, and replaced otherwise.
+ErrorOr<void> CanonicalSessionHistoryEntry::update_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const& descriptor)
+{
+ DocumentStates document_states;
+ document_states.set(document_state->id, document_state);
+ auto updated_document_state = document_state_from_descriptor(descriptor.document_state, document_states, UpdateDocumentState::Yes);
+ TRY(reject_document_state_cycle(*updated_document_state, document_states));
+ document_state = move(updated_document_state);
+ set_entry_fields_from_descriptor(*this, descriptor);
+ return {};
+}
+
NonnullRefPtr<CanonicalSessionHistoryEntry> CanonicalSessionHistoryEntry::create(NonnullRefPtr<CanonicalDocumentState> document_state)
{
return adopt_ref(*new CanonicalSessionHistoryEntry(move(document_state)));
diff --git a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
index 5d099c7ff6d..ac08b3865a5 100644
--- a/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
+++ b/Libraries/LibWebView/CanonicalSessionHistoryEntry.h
@@ -31,6 +31,12 @@ struct CanonicalNestedHistory {
Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> entries;
};
+struct PopulatedDocument {
+ NonnullRefPtr<CanonicalDocument> document;
+ // The origin of a document the hosting process creates for inline content in place of the response.
+ Optional<URL::Origin> inline_content_origin {};
+};
+
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-2
class WEBVIEW_API CanonicalDocumentState final : public RefCounted<CanonicalDocumentState> {
public:
@@ -47,6 +53,11 @@ public:
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-document
RefPtr<CanonicalDocument> document;
+ // AD-HOC: The document populated for the document state becomes its document when its entry is activated. A reload
+ // populates the document state of the active session history entry, whose document stays the navigable's
+ // active document until then.
+ Optional<PopulatedDocument> populated_document;
+
Variant<Web::HTML::SerializedPolicyContainer, Web::HTML::DocumentStateClient> history_policy_container { Web::HTML::DocumentStateClient::Tag };
Web::Fetch::Infrastructure::RequestReferrerType request_referrer { Web::Fetch::Infrastructure::RequestReferrer::Client };
Web::ReferrerPolicy::ReferrerPolicy request_referrer_policy { Web::ReferrerPolicy::DEFAULT_REFERRER_POLICY };
@@ -87,6 +98,8 @@ public:
~CanonicalSessionHistoryEntry();
+ ErrorOr<void> update_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const&);
+
Web::HTML::SessionHistoryEntryDescriptor descriptor() const;
Web::HTML::SessionHistoryEntryIdentity identity() const { return { document_state->id, navigation_api_id }; }
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index fe6915c1303..262d41590e6 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -160,9 +160,9 @@ void CanonicalTraversable::adopt_nested_history_for_created_child(CanonicalNavig
if (container_document.is_completely_loaded())
return;
RefPtr<CanonicalDocumentState> document_state;
- parent.for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (populated_document.document == &container_document)
- document_state = populated_document.document_state;
+ parent.for_each_populated_document_state([&](CanonicalDocumentState& populated_document_state) {
+ if (populated_document_state.populated_document->document == &container_document)
+ document_state = populated_document_state;
});
if (!document_state && &parent.active_document() == &container_document)
document_state = parent.active_session_history_entry()->document_state;
@@ -688,8 +688,8 @@ bool CanonicalTraversable::is_origin_held_by_a_document(URL::Origin const& origi
for (auto const& traversable : user_agent_top_level_traversable_set()) {
auto decision = traversable->for_each_in_inclusive_subtree([&](CanonicalNavigable const& navigable) {
auto held = holds(navigable.active_document());
- navigable.for_each_populated_document([&](PopulatedDocument const& populated_document) {
- held = held || holds(populated_document.document);
+ navigable.for_each_populated_document_state([&](CanonicalDocumentState const& document_state) {
+ held = held || holds(document_state.populated_document->document);
});
return held ? IterationDecision::Break : IterationDecision::Continue;
});
@@ -1463,7 +1463,7 @@ void CanonicalTraversable::continue_history_navigation_population(Web::HTML::Cro
return;
}
// The host takes the navigable over when the document is activated, after the displayed document is unloaded.
- navigable->place_pending_document(*endpoint);
+ navigable->place_populated_document(*document, *endpoint);
}
add_history_operation_completion_endpoint(*operation, *endpoint);
auto& job = *pending_job.value();
@@ -1648,7 +1648,7 @@ void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operatio
return;
RefPtr<WebContentPage> host = page_hosting(*navigable);
- if (auto document = navigable->document_populated_for(*target_entry.document_state); document && document->host() == source_page)
+ if (auto const& populated_document = target_entry.document_state->populated_document; populated_document.has_value() && populated_document->document->host() == source_page)
host = source_page;
Optional<Utf16String const&> navigation_id;
@@ -1876,8 +1876,8 @@ bool CanonicalTraversable::page_populates_a_history_entry_for_local_file(WebCont
RefPtr<WebContentPage> CanonicalTraversable::changing_job_endpoint(CanonicalNavigable const& navigable, CanonicalDocumentState const& target_document_state) const
{
- if (auto document = navigable.document_populated_for(target_document_state); document && document->host())
- return document->host();
+ if (auto const& populated_document = target_document_state.populated_document; populated_document.has_value() && populated_document->document->host())
+ return populated_document->document->host();
return page_hosting(navigable);
}
@@ -1991,8 +1991,8 @@ ApplyHistoryStepJobs CanonicalTraversable::create_apply_history_step_jobs(Histor
auto navigable_id = job.navigable_id;
auto pending_job = make<HistoryOperation::PendingChangingJob>(move(job), move(on_complete));
if (auto const* parameters = operation.parameters.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>(); parameters && parameters->navigable_id == navigable_id) {
- if (auto navigable = find(navigable_id); navigable.has_value())
- pending_job->document = navigable->document_populated_for(*pending_job->job.target_entry->document_state);
+ if (auto const& populated_document = pending_job->job.target_entry->document_state->populated_document; populated_document.has_value())
+ pending_job->document = populated_document->document;
}
operation.pending_changing_jobs.set(navigable_id, move(pending_job));
dispatch_changing_navigable_history_step_job(operation, navigable_id); },
@@ -2528,10 +2528,12 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation
return;
}
- // NB: historyEntry's document state is the navigation's documentState, which holds the document it populated.
+ // NB: historyEntry is the navigation's, as the process that populated its document reports it: populating saves
+ // state in the entry.
+ auto& navigation = *navigable->navigation_being_finalized();
CanonicalSessionHistoryEntry::DocumentStates document_states;
- if (auto const* populated_document = navigable->populated_document_with_state_id(parameters.history_entry.document_state.id))
- document_states.set(populated_document->document_state->id, populated_document->document_state);
+ if (navigation.history_entry)
+ document_states.set(navigation.history_entry->document_state->id, navigation.history_entry->document_state);
auto history_entry_or_error = CanonicalSessionHistoryEntry::create_from_descriptor(Web::HTML::create_session_history_entry_descriptor(parameters.history_entry, 0), document_states, CanonicalSessionHistoryEntry::UpdateDocumentState::Yes);
if (history_entry_or_error.is_error()) {
finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});
@@ -2543,7 +2545,7 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation
// NB: The process running a javascript: URL navigation evaluates it, creates newDocument in navigable's active
// browsing context, and reports historyEntry with these steps. The UI process runs the steps that decide
// newDocument's origin, and populates the document it holds for historyEntry's document state.
- if (navigable->navigation_being_finalized()->state.has<CanonicalNavigation::EvaluatingJavaScriptURL>()) {
+ if (navigation.state.has<CanonicalNavigation::EvaluatingJavaScriptURL>()) {
auto const& initiator_origin = history_entry->document_state->initiator_origin;
// 4. If initiatorOrigin is not same origin-domain with targetNavigable's active document's origin, then return.
@@ -2578,6 +2580,11 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation
// initiator origin: initiatorOrigin
// origin: initiatorOrigin
history_entry->document_state->origin = initiator_origin;
+
+ // 12. Let historyEntry be a new session history entry, with
+ // URL: entryToReplace's URL
+ // document state: documentState
+ navigation.history_entry = history_entry;
if (!navigable->populate_document_for_javascript_url(history_entry->document_state, navigable->create_and_initialize_a_document(response_document))) {
finish_history_operation(operation.operation_id, Web::HTML::HistoryStepResult::NoMatchingEntry, {});
return;
@@ -2594,8 +2601,10 @@ void CanonicalTraversable::finalize_a_cross_document_navigation(HistoryOperation
// answering that it started.
// 3. If historyEntry's document is null, then return.
- // NB: The document populated for historyEntry's document state waits on navigable until historyEntry is activated.
- RefPtr<CanonicalDocument> document = navigable->document_populated_for(*history_entry->document_state);
+ // NB: The document populated for historyEntry waits in its document state until historyEntry is activated.
+ RefPtr<CanonicalDocument> document;
+ if (auto const& populated_document = history_entry->document_state->populated_document; populated_document.has_value())
+ document = populated_document->document;
if (!document) {
if (navigable->is_top_level_traversable()) {
if (auto view = this->view(); view.has_value())
@@ -2749,7 +2758,7 @@ void CanonicalTraversable::release_history_operation(HistoryOperation& operation
pending_job->abandon(find(navigable_id));
if (auto const* parameters = operation.parameters.get_pointer<Web::FinalizeCrossDocumentNavigationHistoryOperationParameters>()) {
if (auto navigable = find(parameters->navigable_id); navigable.has_value())
- navigable->did_finish_finalizing_navigation(parameters->navigation_id, parameters->history_entry.document_state.id, result);
+ navigable->did_finish_finalizing_navigation(parameters->navigation_id, result);
}
}
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index bd0993c600f..2fee896fd80 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -228,9 +228,10 @@ void WebContentPage::for_each_hosted_document(Function<IterationDecision(Canonic
auto decision = IterationDecision::Continue;
if (traversable().hosts(navigable, *this))
decision = callback(navigable.active_document());
- navigable.for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (decision == IterationDecision::Continue && populated_document.document->host() == this)
- decision = callback(*populated_document.document);
+ navigable.for_each_populated_document_state([&](CanonicalDocumentState const& document_state) {
+ auto& document = *document_state.populated_document->document;
+ if (decision == IterationDecision::Continue && document.host() == this)
+ decision = callback(document);
});
return decision;
});
@@ -321,18 +322,15 @@ bool WebContentPage::continue_navigation_population_in_selected_process(Web::HTM
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();
- auto const& request = navigable->ongoing_navigation()->loader()->request();
+ 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);
- auto document_state = CanonicalDocumentState::create(request.history_entry.document_state.id);
- document_state->initiator_origin = request.history_entry.document_state.initiator_origin;
- document_state->origin = request.history_entry.document_state.origin;
- navigable->populate_document_for_ongoing_navigation(move(document_state), *document, navigable->ongoing_navigation()->loader()->result().inline_content_origin);
+ 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, request.history_entry.document_state.initiator_origin);
+ 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;
@@ -343,7 +341,7 @@ bool WebContentPage::continue_navigation_population_in_selected_process(Web::HTM
return false;
}
// The host takes the navigable over when the document is activated, after the displayed document is unloaded.
- navigable->place_pending_document(*host);
+ navigable->place_populated_document(*document, *host);
}
auto& loader = *navigable->ongoing_navigation()->loader();
navigable->set_navigation_host(*host);
@@ -2044,6 +2042,7 @@ void WebContentPage::did_complete_navigation_unload_check(Web::HTML::CrossProces
move(checking->start_request),
Application::the().allocate_ui_process_cross_process_id());
population_request.target_snapshot_params.sandboxing_flags |= navigable->snapshot_target_snapshot_params().sandboxing_flags;
+ ongoing_navigation->history_entry = MUST(CanonicalSessionHistoryEntry::create_from_descriptor(Web::HTML::create_session_history_entry_descriptor(population_request.history_entry, 0)));
ongoing_navigation->state = CanonicalNavigation::CreatingNavigationParams { *this, NavigationLoader::create(client().is_private(), move(population_request)) };
async_create_navigation_params(ongoing_navigation->loader()->request());
@@ -2093,7 +2092,15 @@ void WebContentPage::did_finish_navigation_params_creation(Web::HTML::CrossProce
ongoing_navigation->state = CanonicalNavigation::AcquiringResponseBody { *this, move(loader) };
auto& acquiring_loader = *ongoing_navigation->loader();
acquiring_loader.did_finish_navigation_params_creation(result.release_value());
- ongoing_navigation->url = acquiring_loader.request().history_entry.url;
+ if (ongoing_navigation->history_entry->update_from_descriptor(Web::HTML::create_session_history_entry_descriptor(acquiring_loader.request().history_entry, 0)).is_error()) {
+ auto rejected_result = acquiring_loader.take_result();
+ NavigationLoader::discard(client().is_private(), rejected_result);
+ end_recorded_load_for_canceled_navigation();
+ navigable->clear_ongoing_navigation();
+ client().did_misbehave("did_finish_navigation_params_creation"sv, "replacement document state forms a cycle"sv);
+ return;
+ }
+ ongoing_navigation->url = ongoing_navigation->history_entry->url;
acquiring_loader.acquire_response_body([page = NonnullRefPtr<WebContentPage>(*this), navigable_id, navigation_id = move(navigation_id)](bool succeeded) {
// The page may have closed while the body was fetched; its navigation then has nothing left to finish.
@@ -2811,7 +2818,7 @@ Messages::WebContentTestClient::DidRequestHasPopulatedDocumentForTestingResponse
if (!navigable.has_value())
return false;
bool has_populated_document = false;
- navigable->for_each_populated_document([&](auto const&) {
+ navigable->for_each_populated_document_state([&](auto const&) {
has_populated_document = true;
});
return has_populated_document;
@@ -2864,11 +2871,12 @@ Optional<bool> WebContentPage::hosted_environment_has_cross_site_ancestor(Web::H
// NB: A document being populated has a cross-site ancestor if its navigable's parent's active document has one,
// or is of another site.
- navigable.for_each_populated_document([&](PopulatedDocument const& populated_document) {
- if (result.has_value() || populated_document.document->host() != this || !is_of_environment(*populated_document.document))
+ navigable.for_each_populated_document_state([&](CanonicalDocumentState const& document_state) {
+ auto const& document = *document_state.populated_document->document;
+ if (result.has_value() || document.host() != this || !is_of_environment(document))
return;
auto const* parent = navigable.parent();
- result = parent && (parent->active_document_has_cross_site_ancestor() || !parent->active_document().origin().is_same_site(populated_document.document->origin()));
+ result = parent && (parent->active_document_has_cross_site_ancestor() || !parent->active_document().origin().is_same_site(document.origin()));
});
return result.has_value() ? IterationDecision::Break : IterationDecision::Continue;
});
diff --git a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
index 65bddc6fd3d..dc5b5ecb7d5 100644
--- a/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
+++ b/Tests/LibWebView/TestCanonicalBrowsingContextGroup.cpp
@@ -145,21 +145,22 @@ 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.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
- traversable.populate_document_for_ongoing_navigation(WebView::CanonicalDocumentState::create({}), destination_document);
+ auto abandoned_document_state = WebView::CanonicalDocumentState::create({});
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id, .history_entry = WebView::CanonicalSessionHistoryEntry::create(abandoned_document_state) });
+ traversable.populate_document_for_ongoing_navigation(destination_document);
EXPECT_EQ(&traversable.active_browsing_context(), initial_context);
EXPECT(initial_group->browsing_context_set().contains(initial_context));
traversable.clear_ongoing_navigation();
- EXPECT(!traversable.pending_document());
+ EXPECT(!abandoned_document_state->populated_document.has_value());
EXPECT_EQ(&traversable.active_browsing_context(), initial_context);
EXPECT(initial_group->browsing_context_set().contains(initial_context));
- 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);
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id, .history_entry = committed_entry });
+ traversable.populate_document_for_ongoing_navigation(destination_document);
Web::HTML::HostedNavigableState committed_state {
.active_document_url = destination_url,
.active_document_is_fully_active = true,
@@ -202,9 +203,9 @@ 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.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);
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id, .history_entry = WebView::CanonicalSessionHistoryEntry::create(destination_document_state) });
+ traversable.populate_document_for_ongoing_navigation(destination_document);
Web::HTML::HostedNavigableState committed_state {
.active_document_url = destination_url,
.active_document_is_fully_active = true,
@@ -249,19 +250,22 @@ 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);
+ auto job_document_state = WebView::CanonicalDocumentState::create({});
+ traversable.populate_document(job_document_state, job_document);
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());
+ EXPECT_EQ(job_document_state->populated_document->document.ptr(), job_document.ptr());
// A document populated for a navigation goes with it.
auto navigation_id = Utf16String::from_utf8("navigation"sv);
- traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id });
+ auto navigation_document_state = WebView::CanonicalDocumentState::create({});
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = navigation_id, .history_entry = WebView::CanonicalSessionHistoryEntry::create(navigation_document_state) });
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());
+ traversable.populate_document_for_ongoing_navigation(navigation_document);
+ EXPECT_EQ(navigation_document_state->populated_document->document.ptr(), navigation_document.ptr());
traversable.clear_ongoing_navigation();
- EXPECT_EQ(traversable.pending_document().ptr(), job_document.ptr());
+ EXPECT(!navigation_document_state->populated_document.has_value());
+ EXPECT_EQ(job_document_state->populated_document->document.ptr(), job_document.ptr());
}
TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
@@ -277,10 +281,11 @@ TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
traversable.populate_document(claimed_document_state, claimed_document);
// A newer navigation starts before the claimed document is activated, and populates its own.
- traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = Utf16String::from_utf8("newer"sv) });
+ auto newer_document_state = WebView::CanonicalDocumentState::create({});
+ traversable.set_ongoing_navigation(WebView::CanonicalNavigation { .navigation_id = Utf16String::from_utf8("newer"sv), .history_entry = WebView::CanonicalSessionHistoryEntry::create(newer_document_state) });
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());
+ traversable.populate_document_for_ongoing_navigation(newer_document);
+ EXPECT_EQ(claimed_document_state->populated_document->document.ptr(), claimed_document.ptr());
Web::HTML::HostedNavigableState committed_state {
.active_document_url = URL::about_blank(),
@@ -293,7 +298,8 @@ TEST_CASE(document_claimed_by_a_history_job_outlives_a_newer_navigation)
};
traversable.did_commit_navigation(*WebView::CanonicalSessionHistoryEntry::create(claimed_document_state), move(committed_state), 1, claimed_navigation_id, WebView::CanonicalNavigable::DidPopulateDocument::Yes, {});
EXPECT_EQ(&traversable.active_document(), claimed_document.ptr());
- EXPECT_EQ(traversable.pending_document().ptr(), newer_document.ptr());
+ EXPECT(!claimed_document_state->populated_document.has_value());
+ EXPECT_EQ(newer_document_state->populated_document->document.ptr(), newer_document.ptr());
EXPECT(traversable.ongoing_navigation());
}