Blob.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
  24. class Blob
  25. : public RefCounted<Blob>
  26. , public Weakable<Blob>
  27. , public Bindings::Wrappable {
  28. public:
  29. using WrapperType = Bindings::BlobWrapper;
  30. Blob(ByteBuffer byte_buffer, String type);
  31. static DOM::ExceptionOr<NonnullRefPtr<Blob>> create(Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  32. static DOM::ExceptionOr<NonnullRefPtr<Blob>> create_with_global_object(Bindings::WindowObject&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  33. u64 size() const { return m_byte_buffer.size(); }
  34. String type() const& { return m_type; }
  35. DOM::ExceptionOr<NonnullRefPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<String> const& content_type = {});
  36. JS::Promise* text();
  37. JS::Promise* array_buffer();
  38. virtual JS::Object* create_wrapper(JS::GlobalObject&);
  39. ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
  40. private:
  41. Blob() = default;
  42. ByteBuffer m_byte_buffer {};
  43. String m_type {};
  44. };
  45. }