diff --git a/Userland/Libraries/LibWeb/DOM/DOMException.h b/Userland/Libraries/LibWeb/DOM/DOMException.h index 95475e2829..d3f113e109 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMException.h +++ b/Userland/Libraries/LibWeb/DOM/DOMException.h @@ -13,6 +13,16 @@ namespace Web::DOM { +#define TRY_OR_RETURN_OOM(expression) \ + ({ \ + auto _temporary_result = (expression); \ + if (_temporary_result.is_error()) { \ + VERIFY(_temporary_result.error().code() == ENOMEM); \ + return DOM::UnknownError::create("Out of memory."sv); \ + } \ + _temporary_result.release_value(); \ + }) + // The following have a legacy code value but *don't* produce it as // DOMException.code value when used as name (and are therefore omitted here): // - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2) diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp index 1fc9d67b7d..880e0b3b88 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp @@ -28,7 +28,7 @@ DOM::ExceptionOr> Blob::create(Optional> co ByteBuffer byte_buffer {}; // 2. Let bytes be the result of processing blob parts given blobParts and options. if (blob_parts.has_value()) { - byte_buffer = TRY(process_blob_parts(blob_parts.value())); + byte_buffer = TRY_OR_RETURN_OOM(process_blob_parts(blob_parts.value())); } String type = String::empty(); @@ -50,14 +50,14 @@ DOM::ExceptionOr> Blob::create_with_global_object(Bindings:: } // https://w3c.github.io/FileAPI/#process-blob-parts -DOM::ExceptionOr Blob::process_blob_parts(Vector const& blob_parts) +ErrorOr Blob::process_blob_parts(Vector const& blob_parts) { // 1. Let bytes be an empty sequence of bytes. ByteBuffer bytes {}; // 2. For each element in parts: for (auto const& blob_part : blob_parts) { - auto error = blob_part.visit( + TRY(blob_part.visit( // 1. If element is a USVString, run the following sub-steps: [&](String const& string) -> ErrorOr { // NOTE: This step is handled by the lambda expression. @@ -79,9 +79,7 @@ DOM::ExceptionOr Blob::process_blob_parts(Vector const& bl // 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()); - }); - if (error.is_error()) - return DOM::UnknownError::create("Out of memory. Failed to process blob parts."sv); + })); } return bytes; } @@ -146,10 +144,8 @@ DOM::ExceptionOr> Blob::slice(Optional start, Optional< // a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart. // b. S.size = span. // c. S.type = relativeContentType. - auto byte_buffer_or_error = m_byte_buffer.slice(relative_start, span); - if (byte_buffer_or_error.is_error()) - return DOM::UnknownError::create("Out of memory."sv); - return adopt_ref(*new Blob(byte_buffer_or_error.release_value(), move(relative_content_type))); + auto byte_buffer = TRY_OR_RETURN_OOM(m_byte_buffer.slice(relative_start, span)); + return adopt_ref(*new Blob(move(byte_buffer), move(relative_content_type))); } // https://w3c.github.io/FileAPI/#dom-blob-text diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.h b/Userland/Libraries/LibWeb/FileAPI/Blob.h index e47176b08f..2b2a645406 100644 --- a/Userland/Libraries/LibWeb/FileAPI/Blob.h +++ b/Userland/Libraries/LibWeb/FileAPI/Blob.h @@ -50,7 +50,7 @@ public: private: Blob() = default; - static DOM::ExceptionOr process_blob_parts(Vector const& blob_parts); + static ErrorOr process_blob_parts(Vector const& blob_parts); ByteBuffer m_byte_buffer {}; String m_type {}; diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 62a14d6f09..5560ea798c 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -118,10 +118,8 @@ DOM::ExceptionOr XMLHttpRequest::response() } // 6. Otherwise, if this’s response type is "blob", set this’s response object to a new Blob object representing this’s received bytes with type set to the result of get a final MIME type for this. else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) { - auto blob_part_or_error = try_make_ref_counted(m_received_bytes, get_final_mime_type().type()); - if (blob_part_or_error.is_error()) - return DOM::UnknownError::create("Out of memory."sv); - auto blob = TRY(FileAPI::Blob::create(Vector { blob_part_or_error.release_value() })); + auto blob_part = TRY_OR_RETURN_OOM(try_make_ref_counted(m_received_bytes, get_final_mime_type().type())); + auto blob = TRY(FileAPI::Blob::create(Vector { move(blob_part) })); m_response_object = JS::make_handle(JS::Value(blob->create_wrapper(global_object))); } // 7. Otherwise, if this’s response type is "document", set a document response for this.