ReadableStream.h 3.5 KB

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