Blob.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.h>
  11. #include <LibWeb/Bindings/BlobWrapper.h>
  12. #include <LibWeb/Bindings/WindowObject.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/DOM/ExceptionOr.h>
  15. #include <LibWeb/Forward.h>
  16. #include <LibWeb/HTML/Window.h>
  17. namespace Web::FileAPI {
  18. using BlobPart = Variant<JS::Handle<JS::Object>, NonnullRefPtr<Blob>, String>;
  19. struct BlobPropertyBag {
  20. String type = String::empty();
  21. Bindings::EndingType endings;
  22. };
  23. [[nodiscard]] ErrorOr<String> convert_line_endings_to_native(String const& string);
  24. [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
  25. [[nodiscard]] bool is_basic_latin(StringView view);
  26. class Blob
  27. : public RefCounted<Blob>
  28. , public Weakable<Blob>
  29. , public Bindings::Wrappable {
  30. public:
  31. using WrapperType = Bindings::BlobWrapper;
  32. Blob(ByteBuffer byte_buffer, String type);
  33. static DOM::ExceptionOr<NonnullRefPtr<Blob>> create(Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  34. static DOM::ExceptionOr<NonnullRefPtr<Blob>> create_with_global_object(Bindings::WindowObject&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  35. // https://w3c.github.io/FileAPI/#dfn-size
  36. u64 size() const { return m_byte_buffer.size(); }
  37. // https://w3c.github.io/FileAPI/#dfn-type
  38. String const& type() const { return m_type; }
  39. DOM::ExceptionOr<NonnullRefPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
  40. JS::Promise* text();
  41. JS::Promise* array_buffer();
  42. virtual JS::Object* create_wrapper(JS::GlobalObject&);
  43. ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
  44. protected:
  45. Blob(ByteBuffer byte_buffer);
  46. private:
  47. Blob() = default;
  48. ByteBuffer m_byte_buffer {};
  49. String m_type {};
  50. };
  51. }