Skip to content

Commit c61fa1d

Browse files
trflynn89awesomekling
authored andcommitted
LibWeb: Pass the HTMLLinkElement fetched resource around as a ByteBuffer
Instead of passing around a large variant, which can only contain a ByteBuffer, let's extract the ByteBuffer ahead of time and pass that around.
1 parent 523433d commit c61fa1d

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Libraries/LibWeb/HTML/HTMLLinkElement.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,19 +439,25 @@ void HTMLLinkElement::default_fetch_and_process_linked_resource()
439439

440440
// 1. Let success be true.
441441
bool success = true;
442+
ByteBuffer successful_body_bytes;
442443

443444
// 2. If either of the following conditions are met:
444445
// - bodyBytes is null or failure; or
445446
// - response's status is not an ok status,
446-
if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
447-
// then set success to false.
448-
success = false;
449-
}
447+
// then set success to false.
448+
body_bytes.visit(
449+
[&](ByteBuffer& body_bytes) {
450+
if (Fetch::Infrastructure::is_ok_status(response->status()))
451+
successful_body_bytes = move(body_bytes);
452+
else
453+
success = false;
454+
},
455+
[&](auto) { success = false; });
450456

451457
// FIXME: 3. Otherwise, wait for the link resource's critical subresources to finish loading.
452458

453459
// 4. Process the linked resource given el, success, response, and bodyBytes.
454-
process_linked_resource(success, response, body_bytes);
460+
process_linked_resource(success, response, move(successful_body_bytes));
455461
};
456462

457463
if (m_fetch_controller)
@@ -499,10 +505,10 @@ bool HTMLLinkElement::stylesheet_linked_resource_fetch_setup_steps(Fetch::Infras
499505
}
500506

501507
// https://html.spec.whatwg.org/multipage/semantics.html#process-the-linked-resource
502-
void HTMLLinkElement::process_linked_resource(bool success, Fetch::Infrastructure::Response const& response, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer> body_bytes)
508+
void HTMLLinkElement::process_linked_resource(bool success, Fetch::Infrastructure::Response const& response, ByteBuffer body_bytes)
503509
{
504510
if (m_relationship & Relationship::Stylesheet)
505-
process_stylesheet_resource(success, response, body_bytes);
511+
process_stylesheet_resource(success, response, move(body_bytes));
506512
}
507513

508514
void HTMLLinkElement::process_icon_resource()
@@ -519,7 +525,7 @@ void HTMLLinkElement::process_icon_resource()
519525
}
520526

521527
// https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:process-the-linked-resource
522-
void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const& response, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer> body_bytes)
528+
void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const& response, ByteBuffer body_bytes)
523529
{
524530
// 1. If the resource's Content-Type metadata is not text/css, then set success to false.
525531
auto mime_type_string = m_mime_type;
@@ -532,9 +538,8 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
532538
mime_type_charset = charset.value();
533539
}
534540

535-
if (mime_type_string.has_value() && mime_type_string != "text/css"sv) {
541+
if (mime_type_string.has_value() && mime_type_string != "text/css"sv)
536542
success = false;
537-
}
538543

539544
// FIXME: 2. If el no longer creates an external resource link that contributes to the styling processing model,
540545
// or if, since the resource in question was fetched, it has become appropriate to fetch it again, then return.
@@ -581,7 +586,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
581586
if (!environment_encoding.has_value() && document().encoding().has_value())
582587
environment_encoding = document().encoding().value();
583588

584-
auto maybe_decoded_string = css_decode_bytes(environment_encoding, mime_type_charset, body_bytes.get<ByteBuffer>());
589+
auto maybe_decoded_string = css_decode_bytes(environment_encoding, mime_type_charset, body_bytes);
585590
if (maybe_decoded_string.is_error()) {
586591
dbgln("Failed to decode CSS file: {}", response.url().value_or(URL::URL()));
587592
dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));

Libraries/LibWeb/HTML/HTMLLinkElement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ class WEB_API HTMLLinkElement final
139139
bool linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request&);
140140
bool stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request&);
141141

142-
void process_linked_resource(bool success, Fetch::Infrastructure::Response const&, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer>);
142+
void process_linked_resource(bool success, Fetch::Infrastructure::Response const&, ByteBuffer);
143143
void process_icon_resource();
144-
void process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const&, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer>);
144+
void process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const&, ByteBuffer);
145145

146146
struct Relationship {
147147
enum {

0 commit comments

Comments
 (0)