ReadableStream.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // https://streams.spec.whatwg.org/#readablestream
  23. class ReadableStream final : public Bindings::PlatformObject {
  24. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  25. public:
  26. enum class State {
  27. Readable,
  28. Closed,
  29. Errored,
  30. };
  31. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source, QueuingStrategy const& = {});
  32. virtual ~ReadableStream() override;
  33. bool locked() const;
  34. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value reason);
  35. WebIDL::ExceptionOr<ReadableStreamReader> get_reader(ReadableStreamGetReaderOptions const& = {});
  36. Optional<ReadableStreamController>& controller() { return m_controller; }
  37. void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }
  38. JS::Value stored_error() const { return m_stored_error; }
  39. void set_stored_error(JS::Value value) { m_stored_error = value; }
  40. Optional<ReadableStreamReader> const& reader() const { return m_reader; }
  41. void set_reader(Optional<ReadableStreamReader> value) { m_reader = move(value); }
  42. bool is_disturbed() const;
  43. void set_disturbed(bool value) { m_disturbed = value; }
  44. bool is_readable() const;
  45. bool is_closed() const;
  46. bool is_errored() const;
  47. bool is_locked() const;
  48. State state() const { return m_state; }
  49. void set_state(State value) { m_state = value; }
  50. private:
  51. explicit ReadableStream(JS::Realm&);
  52. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  53. virtual void visit_edges(Cell::Visitor&) override;
  54. // https://streams.spec.whatwg.org/#readablestream-controller
  55. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  56. Optional<ReadableStreamController> m_controller;
  57. // https://streams.spec.whatwg.org/#readablestream-detached
  58. // A boolean flag set to true when the stream is transferred
  59. bool m_detached { false };
  60. // https://streams.spec.whatwg.org/#readablestream-disturbed
  61. // A boolean flag set to true when the stream has been read from or canceled
  62. bool m_disturbed { false };
  63. // https://streams.spec.whatwg.org/#readablestream-reader
  64. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  65. Optional<ReadableStreamReader> m_reader;
  66. // https://streams.spec.whatwg.org/#readablestream-state
  67. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  68. State m_state { State::Readable };
  69. // https://streams.spec.whatwg.org/#readablestream-storederror
  70. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  71. JS::Value m_stored_error { JS::js_undefined() };
  72. };
  73. }