Blob.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/DeprecatedString.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/Vector.h>
  10. #include <LibWeb/Bindings/BlobPrototype.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::FileAPI {
  15. using BlobPart = Variant<JS::Handle<JS::Object>, JS::Handle<Blob>, DeprecatedString>;
  16. struct BlobPropertyBag {
  17. DeprecatedString type = DeprecatedString::empty();
  18. Bindings::EndingType endings;
  19. };
  20. [[nodiscard]] ErrorOr<DeprecatedString> convert_line_endings_to_native(DeprecatedString const& 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 : public Bindings::PlatformObject {
  24. WEB_PLATFORM_OBJECT(Blob, Bindings::PlatformObject);
  25. public:
  26. virtual ~Blob() override;
  27. static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, DeprecatedString type);
  28. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  29. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
  30. // https://w3c.github.io/FileAPI/#dfn-size
  31. u64 size() const { return m_byte_buffer.size(); }
  32. // https://w3c.github.io/FileAPI/#dfn-type
  33. DeprecatedString const& type() const { return m_type; }
  34. WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> slice(Optional<i64> start = {}, Optional<i64> end = {}, Optional<DeprecatedString> const& content_type = {});
  35. JS::Promise* text();
  36. JS::Promise* array_buffer();
  37. ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
  38. protected:
  39. Blob(JS::Realm&, ByteBuffer, DeprecatedString type);
  40. Blob(JS::Realm&, ByteBuffer);
  41. private:
  42. explicit Blob(JS::Realm&);
  43. ByteBuffer m_byte_buffer {};
  44. DeprecatedString m_type {};
  45. };
  46. }