/src/serenity/Userland/Libraries/LibWeb/FileAPI/Blob.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2024, Kenneth Myhra <kennethmyhra@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/NonnullRefPtr.h> |
10 | | #include <AK/Vector.h> |
11 | | #include <LibWeb/Bindings/BlobPrototype.h> |
12 | | #include <LibWeb/Bindings/PlatformObject.h> |
13 | | #include <LibWeb/Bindings/Serializable.h> |
14 | | #include <LibWeb/Forward.h> |
15 | | #include <LibWeb/WebIDL/ExceptionOr.h> |
16 | | |
17 | | namespace Web::FileAPI { |
18 | | |
19 | | using BlobPart = Variant<JS::Handle<WebIDL::BufferSource>, JS::Handle<Blob>, String>; |
20 | | |
21 | | struct BlobPropertyBag { |
22 | | String type = String {}; |
23 | | Bindings::EndingType endings; |
24 | | }; |
25 | | |
26 | | [[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string); |
27 | | [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {}); |
28 | | [[nodiscard]] bool is_basic_latin(StringView view); |
29 | | |
30 | | class Blob |
31 | | : public Bindings::PlatformObject |
32 | | , public Bindings::Serializable { |
33 | | WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject); |
34 | | JS_DECLARE_ALLOCATOR(Blob); |
35 | | |
36 | | public: |
37 | | virtual ~Blob() override; |
38 | | |
39 | | [[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, String type); |
40 | | [[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); |
41 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); |
42 | | |
43 | | // https://w3c.github.io/FileAPI/#dfn-size |
44 | 0 | u64 size() const { return m_byte_buffer.size(); } |
45 | | // https://w3c.github.io/FileAPI/#dfn-type |
46 | 0 | String const& type() const { return m_type; } |
47 | | |
48 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {}); |
49 | | |
50 | | JS::NonnullGCPtr<Streams::ReadableStream> stream(); |
51 | | JS::NonnullGCPtr<WebIDL::Promise> text(); |
52 | | JS::NonnullGCPtr<WebIDL::Promise> array_buffer(); |
53 | | JS::NonnullGCPtr<WebIDL::Promise> bytes(); |
54 | | |
55 | 0 | ReadonlyBytes raw_bytes() const { return m_byte_buffer.bytes(); } |
56 | | |
57 | | JS::NonnullGCPtr<Streams::ReadableStream> get_stream(); |
58 | | |
59 | 0 | virtual StringView interface_name() const override { return "Blob"sv; } |
60 | | |
61 | | virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override; |
62 | | virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override; |
63 | | |
64 | | protected: |
65 | | Blob(JS::Realm&, ByteBuffer, String type); |
66 | | Blob(JS::Realm&, ByteBuffer); |
67 | | |
68 | | virtual void initialize(JS::Realm&) override; |
69 | | |
70 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice_blob(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {}); |
71 | | |
72 | | ByteBuffer m_byte_buffer {}; |
73 | | String m_type {}; |
74 | | |
75 | | private: |
76 | | explicit Blob(JS::Realm&); |
77 | | }; |
78 | | |
79 | | } |