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.
This commit is contained in:
Shannon Booth 2025-01-19 18:12:46 +13:00 committed by Tim Ledbetter
parent a0b0e91d4f
commit ca3d9d9ee0
Notes: github-actions[bot] 2025-01-21 19:23:20 +00:00
7 changed files with 29 additions and 16 deletions

View file

@ -89,9 +89,11 @@ ErrorOr<URL::URL> decode(Decoder& decoder)
return url;
url.set_blob_url_entry(URL::BlobURLEntry {
.type = TRY(decoder.decode<String>()),
.byte_buffer = TRY(decoder.decode<ByteBuffer>()),
.environment_origin = TRY(decoder.decode<URL::Origin>()),
.object = URL::BlobURLEntry::Object {
.type = TRY(decoder.decode<String>()),
.data = TRY(decoder.decode<ByteBuffer>()),
},
.environment { .origin = TRY(decoder.decode<URL::Origin>()) },
});
return url;

View file

@ -109,9 +109,9 @@ ErrorOr<void> 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 {};
}

View file

@ -365,7 +365,7 @@ Origin URL::origin() const
if (scheme() == "blob"sv) {
// 1. If urls blob URL entry is non-null, then return urls blob URL entrys environments 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());

View file

@ -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);

View file

@ -516,9 +516,11 @@ URL::URL parse(StringView input, Optional<URL::URL const&> base_url, Optional<St
auto blob_url_entry = FileAPI::resolve_a_blob_url(*url);
if (blob_url_entry.has_value()) {
url->set_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() },
});
}

View file

@ -826,7 +826,7 @@ WebIDL::ExceptionOr<GC::Ref<PendingResponse>> scheme_fetch(JS::Realm& realm, Inf
}
// 3. Let blob be blobURLEntrys 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);

View file

@ -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;