| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/NonnullRefPtr.h>
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/String.h>
 | 
					
						
							|  |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/BlobWrapper.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/WindowObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Wrappable.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::FileAPI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using BlobPart = Variant<JS::Handle<JS::Object>, NonnullRefPtr<Blob>, String>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct BlobPropertyBag { | 
					
						
							|  |  |  |     String type = String::empty(); | 
					
						
							|  |  |  |     Bindings::EndingType endings; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 16:05:25 +02:00
										 |  |  | [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  | class Blob | 
					
						
							|  |  |  |     : public RefCounted<Blob> | 
					
						
							|  |  |  |     , public Weakable<Blob> | 
					
						
							|  |  |  |     , public Bindings::Wrappable { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     using WrapperType = Bindings::BlobWrapper; | 
					
						
							| 
									
										
										
										
											2022-07-17 00:34:25 +01:00
										 |  |  |     Blob(ByteBuffer byte_buffer, String type); | 
					
						
							| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static DOM::ExceptionOr<NonnullRefPtr<Blob>> create(Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | 
					
						
							|  |  |  |     static DOM::ExceptionOr<NonnullRefPtr<Blob>> create_with_global_object(Bindings::WindowObject&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 23:08:55 +02:00
										 |  |  |     // https://w3c.github.io/FileAPI/#dfn-size
 | 
					
						
							| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  |     u64 size() const { return m_byte_buffer.size(); } | 
					
						
							| 
									
										
										
										
											2022-07-25 23:08:55 +02:00
										 |  |  |     // https://w3c.github.io/FileAPI/#dfn-type
 | 
					
						
							| 
									
										
										
										
											2022-07-26 11:29:07 +02:00
										 |  |  |     String const& type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     DOM::ExceptionOr<NonnullRefPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     JS::Promise* text(); | 
					
						
							|  |  |  |     JS::Promise* array_buffer(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-14 14:32:04 +02:00
										 |  |  |     virtual JS::Object* create_wrapper(JS::GlobalObject&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 15:43:33 +02:00
										 |  |  |     ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 21:32:18 +02:00
										 |  |  | protected: | 
					
						
							|  |  |  |     Blob(ByteBuffer byte_buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 18:31:17 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     Blob() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ByteBuffer m_byte_buffer {}; | 
					
						
							|  |  |  |     String m_type {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |