summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2022-07-14 14:32:04 +0200
committerLinus Groh <mail@linusgroh.de>2022-07-17 00:23:19 +0100
commit5806eeec08675487eb6ce1e06b257bfec381fb21 (patch)
tree3c9aba85618863384e7d805db9c6fe52b8735f69 /Userland/Libraries
parentdf8c49f6bf1783825764e28854d4b6d52a8b71c1 (diff)
downloadserenity-5806eeec08675487eb6ce1e06b257bfec381fb21.zip
LibWeb: Add support for XHR response type Blob
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/FileAPI/Blob.cpp5
-rw-r--r--Userland/Libraries/LibWeb/FileAPI/Blob.h2
-rw-r--r--Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp8
3 files changed, 13 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp
index e6c6618490..cf29064b41 100644
--- a/Userland/Libraries/LibWeb/FileAPI/Blob.cpp
+++ b/Userland/Libraries/LibWeb/FileAPI/Blob.cpp
@@ -194,4 +194,9 @@ JS::Promise* Blob::array_buffer()
return promise;
}
+JS::Object* Blob::create_wrapper(JS::GlobalObject& global_object)
+{
+ return wrap(global_object, *this);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/FileAPI/Blob.h b/Userland/Libraries/LibWeb/FileAPI/Blob.h
index 5498a99ccc..8930642ebc 100644
--- a/Userland/Libraries/LibWeb/FileAPI/Blob.h
+++ b/Userland/Libraries/LibWeb/FileAPI/Blob.h
@@ -47,6 +47,8 @@ public:
JS::Promise* text();
JS::Promise* array_buffer();
+ virtual JS::Object* create_wrapper(JS::GlobalObject&);
+
private:
Blob() = default;
static DOM::ExceptionOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index aee9f73894..62a14d6f09 100644
--- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -26,6 +26,7 @@
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
+#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Origin.h>
@@ -117,8 +118,11 @@ DOM::ExceptionOr<JS::Value> 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) {
- // FIXME: Implement this once we have 'Blob'.
- return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "XHR Blob type not implemented" };
+ auto blob_part_or_error = try_make_ref_counted<FileAPI::Blob>(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<FileAPI::BlobPart> { blob_part_or_error.release_value() }));
+ 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.
else if (m_response_type == Bindings::XMLHttpRequestResponseType::Document) {