Blob.h 2.1 KB

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