Blob.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/BlobPrototype.h>
  12. #include <LibWeb/Bindings/Wrappable.h>
  13. #include <LibWeb/DOM/ExceptionOr.h>
  14. #include <LibWeb/Forward.h>
  15. #include <LibWeb/HTML/Window.h>
  16. namespace Web::FileAPI {
  17. using BlobPart = Variant<JS::Handle<JS::Object>, NonnullRefPtr<Blob>, String>;
  18. struct BlobPropertyBag {
  19. String type = String::empty();
  20. Bindings::EndingType endings;
  21. };
  22. [[nodiscard]] ErrorOr<String> convert_line_endings_to_native(String const& string);
  23. [[nodiscard]] ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts, Optional<BlobPropertyBag> const& options = {});
  24. [[nodiscard]] bool is_basic_latin(StringView view);
  25. class Blob
  26. : public RefCounted<Blob>
  27. , public Weakable<Blob>
  28. , public Bindings::Wrappable {
  29. public:
  30. using WrapperType = Bindings::BlobWrapper;
  31. Blob(ByteBuffer byte_buffer, String type);
  32. virtual ~Blob() override;
  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(HTML::Window&, 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. ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
  43. protected:
  44. Blob(ByteBuffer byte_buffer);
  45. private:
  46. Blob() = default;
  47. ByteBuffer m_byte_buffer {};
  48. String m_type {};
  49. };
  50. }