Skip to content

Commit

Permalink
LibURL+LibWeb+LibIPC: Represent blob URL entry's object using structs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shannonbooth authored and tcl3 committed Jan 21, 2025
1 parent a0b0e91 commit ca3d9d9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Libraries/LibIPC/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Libraries/LibIPC/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibURL/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
17 changes: 13 additions & 4 deletions Libraries/LibURL/URL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions Libraries/LibWeb/DOMURL/DOMURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
});
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ WebIDL::ExceptionOr<GC::Ref<PendingResponse>> 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);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/HTML/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ca3d9d9

Please sign in to comment.