From 622a4f29a782e022d2a394eb1f1d1dcf7184bc36 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Sun, 24 Jul 2022 15:43:33 +0200 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/FileAPI/Blob.cpp | 2 +- Userland/Libraries/LibWeb/FileAPI/Blob.h | 4 ++-- .../Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp index 9e1815511a..5f9a1bce82 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp @@ -76,7 +76,7 @@ ErrorOr Blob::process_blob_parts(Vector const& blob_parts) }, // 3. If element is a Blob, append the bytes it represents to bytes. [&](NonnullRefPtr const& blob) -> ErrorOr { - return bytes.try_append(blob->m_byte_buffer.bytes()); + return bytes.try_append(blob->bytes()); })); } return bytes; diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.h b/Userland/Libraries/LibWeb/FileAPI/Blob.h index 0035f243f5..4947323544 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.h +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.h @@ -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 process_blob_parts(Vector const& blob_parts); ByteBuffer m_byte_buffer {}; String m_type {}; - - friend class XHR::XMLHttpRequest; }; } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 9bd319bd1c..e16b9e756d 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -475,10 +475,19 @@ DOM::ExceptionOr XMLHttpRequest::send(Optional 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 const& blob) { request.set_body(blob->m_byte_buffer); }, - [](auto&) {}); + TRY_OR_RETURN_OOM(body_with_type->body.source().visit( + [&](ByteBuffer const& buffer) -> ErrorOr { + request.set_body(buffer); + return {}; + }, + [&](NonnullRefPtr const& blob) -> ErrorOr { + auto byte_buffer = TRY(ByteBuffer::copy(blob->bytes())); + request.set_body(byte_buffer); + return {}; + }, + [](auto&) -> ErrorOr { + return {}; + })); if (body_with_type->type.has_value()) request.set_header("Content-Type", String { body_with_type->type->span() }); }