1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:47:42 +00:00

LibWeb: Add public get accessor function for Blob's internal ByteBuffer

Blob::bytes() returns the ReadonlyBytes representation of our internal
ByteBuffer.

This change requires us to ByteBuffer::copy() Blob's ReadonlyBytes to a
ByteBuffer in XHR::send() and corresponding error handling of the copy
operation.

This removes the need for Blob to declare XMLHttpRequest as a friend
class.
This commit is contained in:
Kenneth Myhra 2022-07-24 15:43:33 +02:00 committed by Linus Groh
parent 417a385db1
commit 622a4f29a7
3 changed files with 16 additions and 7 deletions

View file

@ -76,7 +76,7 @@ ErrorOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& blob_parts)
}, },
// 3. If element is a Blob, append the bytes it represents to bytes. // 3. If element is a Blob, append the bytes it represents to bytes.
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> { [&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
return bytes.try_append(blob->m_byte_buffer.bytes()); return bytes.try_append(blob->bytes());
})); }));
} }
return bytes; return bytes;

View file

@ -48,14 +48,14 @@ public:
virtual JS::Object* create_wrapper(JS::GlobalObject&); virtual JS::Object* create_wrapper(JS::GlobalObject&);
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
private: private:
Blob() = default; Blob() = default;
static ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts); static ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
ByteBuffer m_byte_buffer {}; ByteBuffer m_byte_buffer {};
String m_type {}; String m_type {};
friend class XHR::XMLHttpRequest;
}; };
} }

View file

@ -475,10 +475,19 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page()); auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page());
request.set_method(m_method); request.set_method(m_method);
if (body_with_type.has_value()) { if (body_with_type.has_value()) {
body_with_type->body.source().visit( TRY_OR_RETURN_OOM(body_with_type->body.source().visit(
[&](ByteBuffer const& buffer) { request.set_body(buffer); }, [&](ByteBuffer const& buffer) -> ErrorOr<void> {
[&](NonnullRefPtr<FileAPI::Blob> const& blob) { request.set_body(blob->m_byte_buffer); }, request.set_body(buffer);
[](auto&) {}); return {};
},
[&](NonnullRefPtr<FileAPI::Blob> const& blob) -> ErrorOr<void> {
auto byte_buffer = TRY(ByteBuffer::copy(blob->bytes()));
request.set_body(byte_buffer);
return {};
},
[](auto&) -> ErrorOr<void> {
return {};
}));
if (body_with_type->type.has_value()) if (body_with_type->type.has_value())
request.set_header("Content-Type", String { body_with_type->type->span() }); request.set_header("Content-Type", String { body_with_type->type->span() });
} }