ReadableStream.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022, 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 <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/Bindings/ReadableStreamPrototype.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/Streams/QueuingStrategy.h>
  13. namespace Web::Streams {
  14. // https://streams.spec.whatwg.org/#typedefdef-readablestreamreader
  15. using ReadableStreamReader = Variant<JS::NonnullGCPtr<ReadableStreamDefaultReader>, JS::NonnullGCPtr<ReadableStreamBYOBReader>>;
  16. // https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
  17. using ReadableStreamController = Variant<JS::NonnullGCPtr<ReadableStreamDefaultController>, JS::NonnullGCPtr<ReadableByteStreamController>>;
  18. // https://streams.spec.whatwg.org/#dictdef-readablestreamgetreaderoptions
  19. struct ReadableStreamGetReaderOptions {
  20. Optional<Bindings::ReadableStreamReaderMode> mode;
  21. };
  22. struct ReadableStreamPair {
  23. // Define a couple container-like methods so this type may be used as the return type of the IDL `tee` implementation.
  24. size_t size() const { return 2; }
  25. JS::NonnullGCPtr<ReadableStream>& at(size_t index)
  26. {
  27. if (index == 0)
  28. return first;
  29. if (index == 1)
  30. return second;
  31. VERIFY_NOT_REACHED();
  32. }
  33. JS::NonnullGCPtr<ReadableStream> first;
  34. JS::NonnullGCPtr<ReadableStream> second;
  35. };
  36. // https://streams.spec.whatwg.org/#readablestream
  37. class ReadableStream final : public Bindings::PlatformObject {
  38. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  39. JS_DECLARE_ALLOCATOR(ReadableStream);
  40. public:
  41. enum class State {
  42. Readable,
  43. Closed,
  44. Errored,
  45. };
  46. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source, QueuingStrategy const& = {});
  47. virtual ~ReadableStream() override;
  48. bool locked() const;
  49. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value reason);
  50. WebIDL::ExceptionOr<ReadableStreamReader> get_reader(ReadableStreamGetReaderOptions const& = {});
  51. WebIDL::ExceptionOr<ReadableStreamPair> tee();
  52. Optional<ReadableStreamController>& controller() { return m_controller; }
  53. void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }
  54. JS::Value stored_error() const { return m_stored_error; }
  55. void set_stored_error(JS::Value value) { m_stored_error = value; }
  56. Optional<ReadableStreamReader> const& reader() const { return m_reader; }
  57. void set_reader(Optional<ReadableStreamReader> value) { m_reader = move(value); }
  58. bool is_disturbed() const;
  59. void set_disturbed(bool value) { m_disturbed = value; }
  60. bool is_readable() const;
  61. bool is_closed() const;
  62. bool is_errored() const;
  63. bool is_locked() const;
  64. State state() const { return m_state; }
  65. void set_state(State value) { m_state = value; }
  66. private:
  67. explicit ReadableStream(JS::Realm&);
  68. virtual void initialize(JS::Realm&) override;
  69. virtual void visit_edges(Cell::Visitor&) override;
  70. // https://streams.spec.whatwg.org/#readablestream-controller
  71. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  72. Optional<ReadableStreamController> m_controller;
  73. // https://streams.spec.whatwg.org/#readablestream-detached
  74. // A boolean flag set to true when the stream is transferred
  75. bool m_detached { false };
  76. // https://streams.spec.whatwg.org/#readablestream-disturbed
  77. // A boolean flag set to true when the stream has been read from or canceled
  78. bool m_disturbed { false };
  79. // https://streams.spec.whatwg.org/#readablestream-reader
  80. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  81. Optional<ReadableStreamReader> m_reader;
  82. // https://streams.spec.whatwg.org/#readablestream-state
  83. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  84. State m_state { State::Readable };
  85. // https://streams.spec.whatwg.org/#readablestream-storederror
  86. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  87. JS::Value m_stored_error { JS::js_undefined() };
  88. };
  89. }