Blob.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2022-2024, 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/Vector.h>
  9. #include <LibWeb/Bindings/BlobPrototype.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Bindings/Serializable.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::FileAPI {
  15. using BlobPart = Variant<JS::Handle<WebIDL::BufferSource>, JS::Handle<Blob>, String>;
  16. struct BlobPropertyBag {
  17. String type = String {};
  18. Bindings::EndingType endings;
  19. };
  20. [[nodiscard]] ErrorOr<String> convert_line_endings_to_native(StringView string);
  21. [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
  22. [[nodiscard]] bool is_basic_latin(StringView view);
  23. class Blob
  24. : public Bindings::PlatformObject
  25. , public Bindings::Serializable {
  26. WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject);
  27. JS_DECLARE_ALLOCATOR(Blob);
  28. public:
  29. virtual ~Blob() override;
  30. [[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, String type);
  31. [[nodiscard]] static JS::NonnullGCPtr<Blob> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  32. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  33. // https://w3c.github.io/FileAPI/#dfn-size
  34. u64 size() const { return m_byte_buffer.size(); }
  35. // https://w3c.github.io/FileAPI/#dfn-type
  36. String const& type() const { return m_type; }
  37. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
  38. WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> stream();
  39. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text();
  40. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> array_buffer();
  41. ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<Streams::ReadableStream>> get_stream();
  43. virtual StringView interface_name() const override { return "Blob"sv; }
  44. virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
  45. virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
  46. protected:
  47. Blob(JS::Realm&, ByteBuffer, String type);
  48. Blob(JS::Realm&, ByteBuffer);
  49. virtual void initialize(JS::Realm&) override;
  50. ByteBuffer m_byte_buffer {};
  51. String m_type {};
  52. private:
  53. explicit Blob(JS::Realm&);
  54. };
  55. }