/* * Copyright (c) 2022-2024, Kenneth Myhra * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Web::FileAPI { using BlobPart = Variant, GC::Root, String>; struct BlobPropertyBag { String type = String {}; Bindings::EndingType endings; }; [[nodiscard]] ErrorOr convert_line_endings_to_native(StringView string); [[nodiscard]] ErrorOr process_blob_parts(Vector const& blob_parts, Optional const& options = {}); [[nodiscard]] bool is_basic_latin(StringView view); class Blob : public Bindings::PlatformObject , public Bindings::Serializable { WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject); GC_DECLARE_ALLOCATOR(Blob); public: virtual ~Blob() override; [[nodiscard]] static GC::Ref create(JS::Realm&, ByteBuffer, String type); [[nodiscard]] static GC::Ref create(JS::Realm&, Optional> const& blob_parts = {}, Optional const& options = {}); static WebIDL::ExceptionOr> construct_impl(JS::Realm&, Optional> const& blob_parts = {}, Optional const& options = {}); // https://w3c.github.io/FileAPI/#dfn-size u64 size() const { return m_byte_buffer.size(); } // https://w3c.github.io/FileAPI/#dfn-type String const& type() const { return m_type; } WebIDL::ExceptionOr> slice(Optional start = {}, Optional end = {}, Optional const& content_type = {}); GC::Ref stream(); GC::Ref text(); GC::Ref array_buffer(); GC::Ref bytes(); ReadonlyBytes raw_bytes() const { return m_byte_buffer.bytes(); } GC::Ref get_stream(); virtual StringView interface_name() const override { return "Blob"sv; } virtual WebIDL::ExceptionOr serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override; virtual WebIDL::ExceptionOr deserialization_steps(ReadonlySpan const& record, size_t& position, HTML::DeserializationMemory&) override; protected: Blob(JS::Realm&, ByteBuffer, String type); Blob(JS::Realm&, ByteBuffer); virtual void initialize(JS::Realm&) override; WebIDL::ExceptionOr> slice_blob(Optional start = {}, Optional end = {}, Optional const& content_type = {}); ByteBuffer m_byte_buffer {}; String m_type {}; private: explicit Blob(JS::Realm&); }; }