2022-07-10 18:31:17 +02:00
|
|
|
/*
|
2024-02-23 18:39:28 +01:00
|
|
|
* Copyright (c) 2022-2024, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2022-07-10 18:31:17 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/Bindings/BlobPrototype.h>
|
2022-09-04 11:44:38 +02:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2024-02-23 18:39:28 +01:00
|
|
|
#include <LibWeb/Bindings/Serializable.h>
|
2022-07-10 18:31:17 +02:00
|
|
|
#include <LibWeb/Forward.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2022-07-10 18:31:17 +02:00
|
|
|
|
|
|
|
namespace Web::FileAPI {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
using BlobPart = Variant<GC::Root<WebIDL::BufferSource>, GC::Root<Blob>, String>;
|
2022-07-10 18:31:17 +02:00
|
|
|
|
|
|
|
struct BlobPropertyBag {
|
2023-02-25 10:27:38 +01:00
|
|
|
String type = String {};
|
2022-07-10 18:31:17 +02:00
|
|
|
Bindings::EndingType endings;
|
|
|
|
};
|
|
|
|
|
2023-02-25 10:27:38 +01:00
|
|
|
[[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string);
|
2022-07-30 10:36:18 +02:00
|
|
|
[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
|
2022-08-01 19:33:27 +02:00
|
|
|
[[nodiscard]] bool is_basic_latin(StringView view);
|
2022-07-24 16:05:25 +02:00
|
|
|
|
2024-02-23 18:39:28 +01:00
|
|
|
class Blob
|
|
|
|
: public Bindings::PlatformObject
|
|
|
|
, public Bindings::Serializable {
|
2022-09-04 11:44:38 +02:00
|
|
|
WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(Blob);
|
2022-07-10 18:31:17 +02:00
|
|
|
|
|
|
|
public:
|
2022-08-28 13:42:07 +02:00
|
|
|
virtual ~Blob() override;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<Blob> create(JS::Realm&, ByteBuffer, String type);
|
|
|
|
[[nodiscard]] static GC::Ref<Blob> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
|
|
|
|
static WebIDL::ExceptionOr<GC::Ref<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
|
2022-07-10 18:31:17 +02:00
|
|
|
|
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
|
2023-02-25 10:27:38 +01:00
|
|
|
String const& type() const { return m_type; }
|
2022-07-10 18:31:17 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
|
2022-07-10 18:31:17 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Streams::ReadableStream> stream();
|
|
|
|
GC::Ref<WebIDL::Promise> text();
|
|
|
|
GC::Ref<WebIDL::Promise> array_buffer();
|
|
|
|
GC::Ref<WebIDL::Promise> bytes();
|
2022-07-10 18:31:17 +02:00
|
|
|
|
2024-07-23 23:48:01 -07:00
|
|
|
ReadonlyBytes raw_bytes() const { return m_byte_buffer.bytes(); }
|
2022-07-24 15:43:33 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Streams::ReadableStream> get_stream();
|
2023-09-09 13:27:22 +12:00
|
|
|
|
2024-02-23 18:39:28 +01:00
|
|
|
virtual StringView interface_name() const override { return "Blob"sv; }
|
|
|
|
|
2024-03-10 19:13:45 +01:00
|
|
|
virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
|
2024-03-10 19:48:33 +01:00
|
|
|
virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
|
2024-02-23 18:39:28 +01:00
|
|
|
|
2022-07-24 21:32:18 +02:00
|
|
|
protected:
|
2023-02-25 10:27:38 +01:00
|
|
|
Blob(JS::Realm&, ByteBuffer, String type);
|
2022-09-25 18:08:29 -06:00
|
|
|
Blob(JS::Realm&, ByteBuffer);
|
2022-07-24 21:32:18 +02:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-01-10 06:28:20 -05:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ref<Blob>> slice_blob(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
|
2024-08-23 00:36:22 +01:00
|
|
|
|
2022-07-10 18:31:17 +02:00
|
|
|
ByteBuffer m_byte_buffer {};
|
2023-02-25 10:27:38 +01:00
|
|
|
String m_type {};
|
2024-02-23 22:14:18 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit Blob(JS::Realm&);
|
2022-07-10 18:31:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|