mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +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:
parent
417a385db1
commit
622a4f29a7
3 changed files with 16 additions and 7 deletions
|
@ -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.
|
||||
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
||||
return bytes.try_append(blob->m_byte_buffer.bytes());
|
||||
return bytes.try_append(blob->bytes());
|
||||
}));
|
||||
}
|
||||
return bytes;
|
||||
|
|
|
@ -48,14 +48,14 @@ public:
|
|||
|
||||
virtual JS::Object* create_wrapper(JS::GlobalObject&);
|
||||
|
||||
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
|
||||
|
||||
private:
|
||||
Blob() = default;
|
||||
static ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
|
||||
|
||||
ByteBuffer m_byte_buffer {};
|
||||
String m_type {};
|
||||
|
||||
friend class XHR::XMLHttpRequest;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
request.set_method(m_method);
|
||||
if (body_with_type.has_value()) {
|
||||
body_with_type->body.source().visit(
|
||||
[&](ByteBuffer const& buffer) { request.set_body(buffer); },
|
||||
[&](NonnullRefPtr<FileAPI::Blob> const& blob) { request.set_body(blob->m_byte_buffer); },
|
||||
[](auto&) {});
|
||||
TRY_OR_RETURN_OOM(body_with_type->body.source().visit(
|
||||
[&](ByteBuffer const& buffer) -> ErrorOr<void> {
|
||||
request.set_body(buffer);
|
||||
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())
|
||||
request.set_header("Content-Type", String { body_with_type->type->span() });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue