From f76f8dcce10307f478eb552b820afb4e8869a200 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 30 Apr 2025 12:33:29 +0100 Subject: [PATCH] LibWeb/CSS: Return the FetchController from fetch_a_style_resource() --- Libraries/LibWeb/CSS/CSSImportRule.cpp | 2 +- Libraries/LibWeb/CSS/Fetch.cpp | 67 +++++++++++++------------- Libraries/LibWeb/CSS/Fetch.h | 2 +- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSImportRule.cpp b/Libraries/LibWeb/CSS/CSSImportRule.cpp index b5d179c6bfa..1f3e699c336 100644 --- a/Libraries/LibWeb/CSS/CSSImportRule.cpp +++ b/Libraries/LibWeb/CSS/CSSImportRule.cpp @@ -116,7 +116,7 @@ void CSSImportRule::fetch() m_document_load_event_delayer.emplace(*m_document); // 4. Fetch a style resource from parsedUrl, with stylesheet parentStylesheet, destination "style", CORS mode "no-cors", and processResponse being the following steps given response response and byte stream, null or failure byteStream: - fetch_a_style_resource(parsed_url.value(), { parent_style_sheet }, Fetch::Infrastructure::Request::Destination::Style, CorsMode::NoCors, + (void)fetch_a_style_resource(parsed_url.value(), { parent_style_sheet }, Fetch::Infrastructure::Request::Destination::Style, CorsMode::NoCors, [strong_this = GC::Ref { *this }, parent_style_sheet = GC::Ref { parent_style_sheet }, parsed_url = parsed_url.value()](auto response, auto maybe_byte_stream) { // AD-HOC: Stop delaying the load event. ScopeGuard guard = [strong_this] { diff --git a/Libraries/LibWeb/CSS/Fetch.cpp b/Libraries/LibWeb/CSS/Fetch.cpp index ebceef14a20..e124310a3fb 100644 --- a/Libraries/LibWeb/CSS/Fetch.cpp +++ b/Libraries/LibWeb/CSS/Fetch.cpp @@ -14,7 +14,7 @@ namespace Web::CSS { // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource -static GC::Ptr fetch_a_style_resource_impl(StyleResourceURL const& url_value, StyleSheetOrDocument sheet_or_document, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode) +static WebIDL::ExceptionOr> fetch_a_style_resource_impl(StyleResourceURL const& url_value, StyleSheetOrDocument sheet_or_document, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode) { // AD-HOC: Not every caller has a CSSStyleSheet, so allow passing a Document in instead for URL completion. // Spec issue: https://github.com/w3c/csswg-drafts/issues/12065 @@ -40,7 +40,7 @@ static GC::Ptr fetch_a_style_resource_impl(Style [](CSS::URL const& url) { return url.url(); }); auto parsed_url = ::URL::Parser::basic_parse(url_string, base); if (!parsed_url.has_value()) - return {}; + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::URIError, "Failed to parse URL"sv }; // 4. Let req be a new request whose url is parsedUrl, whose destination is destination, mode is corsMode, // origin is environmentSettings’s origin, credentials mode is "same-origin", use-url-credentials flag is set, @@ -76,17 +76,16 @@ static GC::Ptr fetch_a_style_resource_impl(Style } // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource -void fetch_a_style_resource(StyleResourceURL const& url_value, StyleSheetOrDocument sheet_or_document, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response) +WebIDL::ExceptionOr> fetch_a_style_resource(StyleResourceURL const& url_value, StyleSheetOrDocument sheet_or_document, Fetch::Infrastructure::Request::Destination destination, CorsMode cors_mode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response) { - if (auto request = fetch_a_style_resource_impl(url_value, sheet_or_document, destination, cors_mode)) { - auto& environment_settings = HTML::relevant_settings_object(sheet_or_document.visit([](auto& it) -> JS::Object& { return it; })); - auto& vm = environment_settings.vm(); + auto request = TRY(fetch_a_style_resource_impl(url_value, sheet_or_document, destination, cors_mode)); + auto& environment_settings = HTML::relevant_settings_object(sheet_or_document.visit([](auto& it) -> JS::Object& { return it; })); + auto& vm = environment_settings.vm(); - Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {}; - fetch_algorithms_input.process_response_consume_body = move(process_response); + Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {}; + fetch_algorithms_input.process_response_consume_body = move(process_response); - (void)Fetch::Fetching::fetch(environment_settings.realm(), *request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))); - } + return Fetch::Fetching::fetch(environment_settings.realm(), *request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))); } // https://drafts.csswg.org/css-images-4/#fetch-an-external-image-for-a-stylesheet @@ -100,36 +99,36 @@ GC::Ptr fetch_an_external_image_for_a_stylesheet(St // NB: We can't directly call fetch_a_style_resource() because we want to make use of SharedResourceRequest to // deduplicate image requests. - if (auto request = fetch_a_style_resource_impl(url_value, sheet_or_document, Fetch::Infrastructure::Request::Destination::Image, CorsMode::NoCors)) { + auto maybe_request = fetch_a_style_resource_impl(url_value, sheet_or_document, Fetch::Infrastructure::Request::Destination::Image, CorsMode::NoCors); + if (maybe_request.is_error()) + return nullptr; + auto& request = maybe_request.value(); - auto document = sheet_or_document.visit( - [&](GC::Ref const& sheet) -> GC::Ref { return *sheet->owning_document(); }, - [](GC::Ref const& document) -> GC::Ref { return document; }); - auto& realm = document->realm(); + auto document = sheet_or_document.visit( + [&](GC::Ref const& sheet) -> GC::Ref { return *sheet->owning_document(); }, + [](GC::Ref const& document) -> GC::Ref { return document; }); + auto& realm = document->realm(); - auto shared_resource_request = HTML::SharedResourceRequest::get_or_create(realm, document->page(), request->url()); - shared_resource_request->add_callbacks( - [document, weak_document = document->make_weak_ptr()] { - if (!weak_document) - return; + auto shared_resource_request = HTML::SharedResourceRequest::get_or_create(realm, document->page(), request->url()); + shared_resource_request->add_callbacks( + [document, weak_document = document->make_weak_ptr()] { + if (!weak_document) + return; - if (auto navigable = document->navigable()) { - // Once the image has loaded, we need to re-resolve CSS properties that depend on the image's dimensions. - document->set_needs_to_resolve_paint_only_properties(); + if (auto navigable = document->navigable()) { + // Once the image has loaded, we need to re-resolve CSS properties that depend on the image's dimensions. + document->set_needs_to_resolve_paint_only_properties(); - // FIXME: Do less than a full repaint if possible? - document->set_needs_display(); - } - }, - nullptr); + // FIXME: Do less than a full repaint if possible? + document->set_needs_display(); + } + }, + nullptr); - if (shared_resource_request->needs_fetching()) - shared_resource_request->fetch_resource(realm, *request); + if (shared_resource_request->needs_fetching()) + shared_resource_request->fetch_resource(realm, *request); - return shared_resource_request; - } - - return nullptr; + return shared_resource_request; } } diff --git a/Libraries/LibWeb/CSS/Fetch.h b/Libraries/LibWeb/CSS/Fetch.h index 3e6b3035510..b3d17871652 100644 --- a/Libraries/LibWeb/CSS/Fetch.h +++ b/Libraries/LibWeb/CSS/Fetch.h @@ -25,7 +25,7 @@ using StyleResourceURL = Variant<::URL::URL, CSS::URL>; using StyleSheetOrDocument = Variant, GC::Ref>; // https://drafts.csswg.org/css-values-4/#fetch-a-style-resource -void fetch_a_style_resource(StyleResourceURL const& url, StyleSheetOrDocument, Fetch::Infrastructure::Request::Destination, CorsMode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response); +WebIDL::ExceptionOr> fetch_a_style_resource(StyleResourceURL const& url, StyleSheetOrDocument, Fetch::Infrastructure::Request::Destination, CorsMode, Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_response); // https://drafts.csswg.org/css-images-4/#fetch-an-external-image-for-a-stylesheet GC::Ptr fetch_an_external_image_for_a_stylesheet(StyleResourceURL const&, StyleSheetOrDocument);