LibWeb: Implement "attempt to populate the history entry's document"

Implements:
https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document

This is going to be a replacement for `FrameLoader::load()` after
switching to navigables.

Brief description of `populate_session_history_entry_document`:
- If navigation params have url with fetch scheme then DOM document
  will be populated by fetching url and parsing response. This
  is going to be a replacement for `FrameLoader::load(AK::URL&)`.
- If url in navigation params is abort:srcdoc then DOM document
  will be populated by parsing HTML text passed in document resource.
  This is going to be a replacement for `FrameLoader::load_html()`
This commit is contained in:
Aliaksandr Kalenik 2023-04-06 18:10:12 +03:00 committed by Andreas Kling
parent 804af38a96
commit de2c016556
Notes: sideshowbarker 2024-07-16 23:34:44 +09:00
5 changed files with 238 additions and 0 deletions

View File

@ -470,6 +470,8 @@ public:
void make_active(); void make_active();
void set_salvageable(bool value) { m_salvageable = value; };
protected: protected:
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;

View File

@ -13,6 +13,7 @@
#include <LibTextCodec/Decoder.h> #include <LibTextCodec/Decoder.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentLoading.h> #include <LibWeb/DOM/DocumentLoading.h>
#include <LibWeb/HTML/Navigable.h>
#include <LibWeb/HTML/NavigationParams.h> #include <LibWeb/HTML/NavigationParams.h>
#include <LibWeb/HTML/Parser/HTMLEncodingDetection.h> #include <LibWeb/HTML/Parser/HTMLEncodingDetection.h>
#include <LibWeb/HTML/Parser/HTMLParser.h> #include <LibWeb/HTML/Parser/HTMLParser.h>
@ -201,4 +202,100 @@ bool parse_document(DOM::Document& document, ByteBuffer const& data)
return false; return false;
} }
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#loading-a-document
JS::GCPtr<DOM::Document> load_document(Optional<HTML::NavigationParams> navigation_params)
{
VERIFY(navigation_params.has_value());
auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", *navigation_params).release_value_but_fixme_should_propagate_errors();
auto& realm = document->realm();
if (navigation_params->response->body().has_value()) {
auto process_body = [navigation_params, document](ByteBuffer bytes) {
if (!parse_document(*document, bytes)) {
// FIXME: Load html page with an error if parsing failed.
TODO();
}
};
auto process_body_error = [](auto&) {
// FIXME: Load html page with an error if read of body failed.
TODO();
};
navigation_params->response->body()->fully_read(
realm,
move(process_body),
move(process_body_error),
JS::NonnullGCPtr { realm.global_object() })
.release_value_but_fixme_should_propagate_errors();
}
return document;
}
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-ua-inline
JS::GCPtr<DOM::Document> create_document_for_inline_content(JS::GCPtr<HTML::Navigable> navigable, Optional<String> navigation_id, StringView content_html)
{
auto& vm = navigable->vm();
// 1. Let origin be a new opaque origin.
HTML::Origin origin {};
// 2. Let coop be a new cross-origin opener policy.
auto coop = HTML::CrossOriginOpenerPolicy {};
// 3. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with
// url: response's URL
// origin: origin
// cross-origin opener policy: coop
HTML::CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result {
.url = AK::URL("about:error"), // AD-HOC
.origin = origin,
.cross_origin_opener_policy = coop
};
// 4. Let navigationParams be a new navigation params with
// id: navigationId
// request: null
// response: a new response
// origin: origin
// policy container: a new policy container
// final sandboxing flag set: an empty set
// cross-origin opener policy: coop
// COOP enforcement result: coopEnforcementResult
// reserved environment: null
// navigable: navigable
// FIXME: navigation timing type: navTimingType
// FIXME: fetch controller: fetch controller
// FIXME: commit early hints: null
auto response = Fetch::Infrastructure::Response::create(vm);
response->url_list().append(AK::URL("about:error")); // AD-HOC: https://github.com/whatwg/html/issues/9122
HTML::NavigationParams navigation_params {
.id = navigation_id,
.request = {},
.response = *response,
.origin = move(origin),
.policy_container = HTML::PolicyContainer {},
.final_sandboxing_flag_set = HTML::SandboxingFlagSet {},
.cross_origin_opener_policy = move(coop),
.coop_enforcement_result = move(coop_enforcement_result),
.reserved_environment = {},
.browsing_context = navigable->active_browsing_context(),
.navigable = navigable,
};
// 5. Let document be the result of creating and initializing a Document object given "html", "text/html", and navigationParams.
auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", navigation_params).release_value_but_fixme_should_propagate_errors();
// 6. Either associate document with a custom rendering that is not rendered using the normal Document rendering rules, or mutate document until it represents the content the
// user agent wants to render.
auto parser = HTML::HTMLParser::create(document, content_html, "utf-8");
parser->run(AK::URL("about:error"));
// 7. Return document.
return document;
}
} }

View File

@ -12,5 +12,7 @@
namespace Web { namespace Web {
bool parse_document(DOM::Document& document, ByteBuffer const& data); bool parse_document(DOM::Document& document, ByteBuffer const& data);
JS::GCPtr<DOM::Document> load_document(Optional<HTML::NavigationParams> navigation_params);
JS::GCPtr<DOM::Document> create_document_for_inline_content(JS::GCPtr<HTML::Navigable> navigable, Optional<String> navigation_id, StringView content_html);
} }

View File

@ -7,9 +7,15 @@
#include <LibWeb/Crypto/Crypto.h> #include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentLoading.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/DocumentState.h> #include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/Navigable.h> #include <LibWeb/HTML/Navigable.h>
#include <LibWeb/HTML/NavigationParams.h>
#include <LibWeb/HTML/SessionHistoryEntry.h> #include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/HTML/TraversableNavigable.h> #include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/Platform/EventLoopPlugin.h> #include <LibWeb/Platform/EventLoopPlugin.h>
@ -218,6 +224,134 @@ Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& Navigable::get_session_history_en
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
WebIDL::ExceptionOr<void> Navigable::populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry> entry, Optional<NavigationParams> navigation_params, Optional<String> navigation_id, SourceSnapshotParams const& source_snapshot_params, Function<void()> completion_steps)
{
// FIXME: 1. Assert: this is running in parallel.
// 2. Assert: if navigationParams is non-null, then navigationParams's response is non-null.
if (navigation_params.has_value())
VERIFY(navigation_params->response);
// 3. Let currentBrowsingContext be navigable's active browsing context.
[[maybe_unused]] auto current_browsing_context = active_browsing_context();
// 4. Let documentResource be entry's document state's resource.
auto document_resource = entry->document_state->resource();
// 5. If navigationParams is null, then:
if (!navigation_params.has_value()) {
// 1. If documentResource is a string, then set navigationParams to the result
// of creating navigation params from a srcdoc resource given entry, navigable,
// targetSnapshotParams, navigationId, and navTimingType.
if (document_resource.has<String>()) {
TODO();
}
// 2. Otherwise, if both of the following are true:
// - entry's URL's scheme is a fetch scheme; and
// - documentResource is null, FIXME: or allowPOST is true and documentResource's request body is not failure
else if (Fetch::Infrastructure::is_fetch_scheme(entry->url.scheme()) && document_resource.has<Empty>()) {
TODO();
}
// FIXME: 3. Otherwise, if entry's URL's scheme is not a fetch scheme, then set navigationParams to a new non-fetch scheme navigation params, with
// initiator origin: entry's document state's initiator origin
else {
TODO();
}
}
// 6. Queue a global task on the navigation and traversal task source, given navigable's active window, to run these steps:
queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, entry, navigation_params, navigation_id, completion_steps = move(completion_steps)] {
// 1. If navigable's ongoing navigation no longer equals navigationId, then run completionSteps and return.
if (navigation_id.has_value() && (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != *navigation_id)) {
completion_steps();
return;
}
// 2. Let failure be false.
auto failure = false;
// FIXME: 3. If navigationParams is a non-fetch scheme navigation params, then set entry's document state's document to the result of running attempt to create a non-fetch
// scheme document given entry's URL, navigable, targetSnapshotParams's sandboxing flags, navigationId, navTimingType, sourceSnapshotParams's has transient
// activation, and navigationParams's initiator origin.
// 4. Otherwise, if navigationParams is null, then set failure to true.
if (!navigation_params.has_value()) {
failure = true;
}
// FIXME: 5. Otherwise, if the result of should navigation response to navigation request of type in target be blocked by Content Security Policy? given navigationParams's request,
// navigationParams's response, navigationParams's policy container's CSP list, cspNavigationType, and navigable is "Blocked", then set failure to true.
// FIXME: 6. Otherwise, if navigationParams's reserved environment is non-null and the result of checking a navigation response's adherence to its embedder policy given
// navigationParams's response, navigable, and navigationParams's policy container's embedder policy is false, then set failure to true.
// 8. If failure is true, then:
if (failure) {
// 1. Set entry's document state's document to the result of creating a document for inline content that doesn't have a DOM, given navigable, null, and navTimingType.
// The inline content should indicate to the user the sort of error that occurred.
// FIXME: Use SourceGenerator to produce error page from file:///res/html/error.html
// and display actual error from fetch response.
auto error_html = String::formatted("<h1>Failed to load {}</h1>"sv, entry->url).release_value_but_fixme_should_propagate_errors();
entry->document_state->set_document(create_document_for_inline_content(this, navigation_id, error_html));
// 2. Set entry's document state's document's salvageable to false.
entry->document_state->document()->set_salvageable(false);
// FIXME: 3. If navigationParams is not null, then:
if (navigation_params.has_value()) {
TODO();
}
}
// FIXME: 9. Otherwise, if navigationParams's response's status is 204 or 205, then:
else if (navigation_params->response->status() == 204 || navigation_params->response->status() == 205) {
// 1. Run completionSteps.
completion_steps();
// 2. Return.
return;
}
// FIXME: 10. Otherwise, if navigationParams's response has a `Content-Disposition`
// header specifying the attachment disposition type, then:
// 11. Otherwise:
else {
// 1. Let document be the result of loading a document given navigationParams, sourceSnapshotParams,
// and entry's document state's initiator origin.
auto document = load_document(navigation_params);
// 2. If document is null, then run completionSteps and return.
if (!document) {
VERIFY_NOT_REACHED();
completion_steps();
return;
}
// 3. Set entry's document state's document to document.
entry->document_state->set_document(document.ptr());
// 4. Set entry's document state's origin to document's origin.
entry->document_state->set_origin(document->origin());
}
// FIXME: 12. If entry's document state's request referrer is "client", then set it to request's referrer.
// 13. If entry's document state's document is not null, then set entry's document state's ever populated to true.
if (entry->document_state->document()) {
entry->document_state->set_ever_populated(true);
}
// 14. Run completionSteps.
completion_steps();
(void)this;
});
(void)source_snapshot_params;
return {};
}
// To navigate a navigable navigable to a URL url using a Document sourceDocument, // To navigate a navigable navigable to a URL url using a Document sourceDocument,
// with an optional POST resource, string, or null documentResource (default null), // with an optional POST resource, string, or null documentResource (default null),
// an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false), // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),

View File

@ -11,6 +11,7 @@
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
#include <LibWeb/HTML/HistoryHandlingBehavior.h> #include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWeb/HTML/POSTResource.h> #include <LibWeb/HTML/POSTResource.h>
#include <LibWeb/HTML/SourceSnapshotParams.h>
namespace Web::HTML { namespace Web::HTML {
@ -60,6 +61,8 @@ public:
Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; } Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; }
WebIDL::ExceptionOr<void> populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry>, Optional<NavigationParams>, Optional<String> navigation_id, SourceSnapshotParams const&, Function<void()>);
WebIDL::ExceptionOr<void> navigate( WebIDL::ExceptionOr<void> navigate(
AK::URL const&, AK::URL const&,
JS::NonnullGCPtr<DOM::Document> source_document, JS::NonnullGCPtr<DOM::Document> source_document,