Files
ladybird/Libraries/LibWeb/DOM/DocumentLoading.h
Aliaksandr Kalenik 76d9cc4baf LibWeb: Replace spin_until in execute_script with deferred parser start
HTMLScriptElement::execute_script() and SVGScriptElement had spin_until
calls waiting for ready_to_run_scripts to become true. The race exists
because load_html_document() resolves the session history signal and
starts the parser in the same deferred_invoke — so the parser can hit a
<script> before update_for_history_step_application() sets the flag.

Instead of spinning, defer parser->run() until the document is ready.
Document gains a m_deferred_parser_start callback that is invoked when
set_ready_to_run_scripts() is called. The callback is cleared before
invocation to avoid reentrancy issues (parser->run() can synchronously
execute scripts). All three document loading paths (HTML, XML, text)
now check ready_to_run_scripts before starting the parser and defer if
needed.

create_document_for_inline_content() (used for error pages) now calls
set_ready_to_run_scripts() before mutating the document, ensuring the
invariant holds for all parser paths.

The spin_until calls are replaced with VERIFY assertions.
2026-03-29 01:05:35 +01:00

93 lines
3.6 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/Navigable.h>
#include <LibWeb/HTML/UserNavigationInvolvement.h>
namespace Web {
bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<String> content_encoding);
GC::Ptr<DOM::Document> load_document(HTML::NavigationParams const& navigation_params, NonnullRefPtr<Core::Promise<Empty>> signal_to_continue_session_history_processing, ReadonlyBytes sniff_bytes);
bool can_load_document_with_type(MimeSniff::MimeType const&);
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#read-ua-inline
template<typename MutateDocument>
GC::Ref<DOM::Document> create_document_for_inline_content(GC::Ptr<HTML::Navigable> navigable, Optional<String> navigation_id, HTML::UserNavigationInvolvement user_involvement, MutateDocument mutate_document)
{
auto& vm = navigable->vm();
VERIFY(navigable->active_document());
// 1. Let origin be a new opaque origin.
auto origin = URL::Origin::create_opaque();
// 2. Let coop be a new opener policy.
auto coop = HTML::OpenerPolicy {};
// 3. Let coopEnforcementResult be a new opener policy enforcement result with
// url: response's URL
// origin: origin
// opener policy: coop
HTML::OpenerPolicyEnforcementResult coop_enforcement_result {
.url = URL::about_error(), // AD-HOC: https://github.com/whatwg/html/issues/9122
.origin = origin,
.opener_policy = coop
};
// 4. Let navigationParams be a new navigation params with
// id: navigationId
// navigable: navigable
// request: null
// response: a new response
// origin: origin
// fetch controller: null
// commit early hints: null
// COOP enforcement result: coopEnforcementResult
// reserved environment: null
// policy container: a new policy container
// final sandboxing flag set: an empty set
// opener policy: coop
// FIXME: navigation timing type: navTimingType
// about base URL: null
// user involvement: userInvolvement
auto response = Fetch::Infrastructure::Response::create(vm);
response->url_list().append(URL::about_error()); // AD-HOC: https://github.com/whatwg/html/issues/9122
auto navigation_params = vm.heap().allocate<HTML::NavigationParams>(
move(navigation_id),
navigable,
nullptr,
response,
nullptr,
nullptr,
move(coop_enforcement_result),
nullptr,
move(origin),
vm.heap().allocate<HTML::PolicyContainer>(vm.heap()),
HTML::SandboxingFlagSet {},
move(coop),
OptionalNone {},
user_involvement);
// 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"_string, 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.
document->set_ready_to_run_scripts();
mutate_document(*document);
// FIXME: 7. Act as if the user agent had stopped parsing document.
// We currently do this in the caller instead, to avoid deadlocks.
// 8. Return document.
return document;
}
}