Body.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Fetch {
  12. enum class PackageDataType {
  13. ArrayBuffer,
  14. Blob,
  15. FormData,
  16. JSON,
  17. Text,
  18. };
  19. // https://fetch.spec.whatwg.org/#body-mixin
  20. class BodyMixin {
  21. public:
  22. virtual ~BodyMixin();
  23. virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const = 0;
  24. virtual JS::GCPtr<Infrastructure::Body> body_impl() = 0;
  25. virtual JS::GCPtr<Infrastructure::Body const> body_impl() const = 0;
  26. virtual Bindings::PlatformObject& as_platform_object() = 0;
  27. virtual Bindings::PlatformObject const& as_platform_object() const = 0;
  28. [[nodiscard]] bool is_unusable() const;
  29. [[nodiscard]] JS::GCPtr<Streams::ReadableStream> body() const;
  30. [[nodiscard]] bool body_used() const;
  31. // JS API functions
  32. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> array_buffer() const;
  33. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> blob() const;
  34. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> form_data() const;
  35. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> json() const;
  36. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text() const;
  37. };
  38. [[nodiscard]] WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm&, ByteBuffer, PackageDataType, Optional<MimeSniff::MimeType> const&);
  39. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> consume_body(JS::Realm&, BodyMixin const&, PackageDataType);
  40. }