From ca3d9d9ee077467b4c9f4bbc4c665734979b2d96 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 19 Jan 2025 18:12:46 +1300 Subject: [PATCH] LibURL+LibWeb+LibIPC: Represent blob URL entry's object using structs Instead of just putting in members directly, wrap them up in structs which represent what a URL blob entry is meant to hold per the spec. This makes more obvious what this is meant to represent, such as the ByteBuffer being used to represent the bytes behind a Blob. This also allows us to use a stronger type for a function that needs to return a Blob URL entry's object. --- Libraries/LibIPC/Decoder.cpp | 8 +++++--- Libraries/LibIPC/Encoder.cpp | 6 +++--- Libraries/LibURL/URL.cpp | 2 +- Libraries/LibURL/URL.h | 17 +++++++++++++---- Libraries/LibWeb/DOMURL/DOMURL.cpp | 8 +++++--- Libraries/LibWeb/Fetch/Fetching/Fetching.cpp | 2 +- Libraries/LibWeb/HTML/Window.cpp | 2 +- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cpp index 3de7417fced..022e52661cd 100644 --- a/Libraries/LibIPC/Decoder.cpp +++ b/Libraries/LibIPC/Decoder.cpp @@ -89,9 +89,11 @@ ErrorOr decode(Decoder& decoder) return url; url.set_blob_url_entry(URL::BlobURLEntry { - .type = TRY(decoder.decode()), - .byte_buffer = TRY(decoder.decode()), - .environment_origin = TRY(decoder.decode()), + .object = URL::BlobURLEntry::Object { + .type = TRY(decoder.decode()), + .data = TRY(decoder.decode()), + }, + .environment { .origin = TRY(decoder.decode()) }, }); return url; diff --git a/Libraries/LibIPC/Encoder.cpp b/Libraries/LibIPC/Encoder.cpp index a83b9904cc3..22c8e3ef90e 100644 --- a/Libraries/LibIPC/Encoder.cpp +++ b/Libraries/LibIPC/Encoder.cpp @@ -109,9 +109,9 @@ ErrorOr encode(Encoder& encoder, URL::URL const& value) auto const& blob = value.blob_url_entry().value(); - TRY(encoder.encode(blob.type)); - TRY(encoder.encode(blob.byte_buffer)); - TRY(encoder.encode(blob.environment_origin)); + TRY(encoder.encode(blob.object.type)); + TRY(encoder.encode(blob.object.data)); + TRY(encoder.encode(blob.environment.origin)); return {}; } diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cpp index ca06c31464b..308cd344487 100644 --- a/Libraries/LibURL/URL.cpp +++ b/Libraries/LibURL/URL.cpp @@ -365,7 +365,7 @@ Origin URL::origin() const if (scheme() == "blob"sv) { // 1. If url’s blob URL entry is non-null, then return url’s blob URL entry’s environment’s origin. if (blob_url_entry().has_value()) - return blob_url_entry()->environment_origin; + return blob_url_entry()->environment.origin; // 2. Let pathURL be the result of parsing the result of URL path serializing url. auto path_url = Parser::basic_parse(serialize_path()); diff --git a/Libraries/LibURL/URL.h b/Libraries/LibURL/URL.h index 51b540c0965..c699162a4f6 100644 --- a/Libraries/LibURL/URL.h +++ b/Libraries/LibURL/URL.h @@ -41,11 +41,20 @@ enum class ExcludeFragment { }; // https://w3c.github.io/FileAPI/#blob-url-entry -// NOTE: This represents the raw bytes behind a 'Blob' (and does not yet support a MediaSourceQuery). struct BlobURLEntry { - String type; - ByteBuffer byte_buffer; - Origin environment_origin; + // This represents the raw bytes behind a 'Blob' (and does not yet support a MediaSourceQuery). + struct Object { + String type; + ByteBuffer data; + }; + + // This represents the parts of HTML::Environment that we need for a BlobURL entry. + struct Environment { + Origin origin; + }; + + Object object; + Environment environment; }; void append_percent_encoded_if_necessary(StringBuilder&, u32 code_point, PercentEncodeSet set = PercentEncodeSet::Userinfo); diff --git a/Libraries/LibWeb/DOMURL/DOMURL.cpp b/Libraries/LibWeb/DOMURL/DOMURL.cpp index 3601f9acec9..cb7f5e17ce6 100644 --- a/Libraries/LibWeb/DOMURL/DOMURL.cpp +++ b/Libraries/LibWeb/DOMURL/DOMURL.cpp @@ -516,9 +516,11 @@ URL::URL parse(StringView input, Optional base_url, Optionalset_blob_url_entry(URL::BlobURLEntry { - .type = blob_url_entry->object->type(), - .byte_buffer = MUST(ByteBuffer::copy(blob_url_entry->object->raw_bytes())), - .environment_origin = blob_url_entry->environment->origin(), + .object = URL::BlobURLEntry::Object { + .type = blob_url_entry->object->type(), + .data = MUST(ByteBuffer::copy(blob_url_entry->object->raw_bytes())), + }, + .environment { .origin = blob_url_entry->environment->origin() }, }); } diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index a7d53956986..934626b731b 100644 --- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -826,7 +826,7 @@ WebIDL::ExceptionOr> scheme_fetch(JS::Realm& realm, Inf } // 3. Let blob be blobURLEntry’s object. - auto const blob = FileAPI::Blob::create(realm, blob_url_entry.value().byte_buffer, blob_url_entry.value().type); + auto const blob = FileAPI::Blob::create(realm, blob_url_entry.value().object.data, blob_url_entry.value().object.type); // 4. Let response be a new response. auto response = Infrastructure::Response::create(vm); diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp index 46852537be4..d925a2a153f 100644 --- a/Libraries/LibWeb/HTML/Window.cpp +++ b/Libraries/LibWeb/HTML/Window.cpp @@ -161,7 +161,7 @@ static TokenizedFeature::NoOpener get_noopener_for_window_open(DOM::Document con // 1. If url is not null and url's blob URL entry is not null: if (url.has_value() && url->blob_url_entry().has_value()) { // 1. Let blobOrigin be url's blob URL entry's environment's origin. - auto blob_origin = url->blob_url_entry()->environment_origin; + auto blob_origin = url->blob_url_entry()->environment.origin; // 2. Let topLevelOrigin be sourceDocument's relevant settings object's top-level origin. auto top_level_origin = source_document.relevant_settings_object().top_level_origin;