Serenity Operating System
at master 64 lines 2.2 kB view raw
1/* 2 * Copyright (c) 2022-2023, 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/Forward.h> 14#include <LibWeb/WebIDL/ExceptionOr.h> 15 16namespace Web::FileAPI { 17 18using BlobPart = Variant<JS::Handle<JS::Object>, JS::Handle<Blob>, String>; 19 20struct BlobPropertyBag { 21 String type = String {}; 22 Bindings::EndingType endings; 23}; 24 25[[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string); 26[[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {}); 27[[nodiscard]] bool is_basic_latin(StringView view); 28 29class Blob : public Bindings::PlatformObject { 30 WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject); 31 32public: 33 virtual ~Blob() override; 34 35 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, ByteBuffer, String type); 36 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); 37 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {}); 38 39 // https://w3c.github.io/FileAPI/#dfn-size 40 u64 size() const { return m_byte_buffer.size(); } 41 // https://w3c.github.io/FileAPI/#dfn-type 42 String const& type() const { return m_type; } 43 44 WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {}); 45 46 WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text(); 47 JS::Promise* array_buffer(); 48 49 ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); } 50 51protected: 52 Blob(JS::Realm&, ByteBuffer, String type); 53 Blob(JS::Realm&, ByteBuffer); 54 55 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; 56 57private: 58 explicit Blob(JS::Realm&); 59 60 ByteBuffer m_byte_buffer {}; 61 String m_type {}; 62}; 63 64}