ReadableStreamDefaultController.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/Optional.h>
  9. #include <AK/SinglyLinkedList.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/Streams/AbstractOperations.h>
  14. #include <LibWeb/WebIDL/Promise.h>
  15. namespace Web::Streams {
  16. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller
  17. class ReadableStreamDefaultController : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(ReadableStreamDefaultController, Bindings::PlatformObject);
  19. JS_DECLARE_ALLOCATOR(ReadableStreamDefaultController);
  20. public:
  21. explicit ReadableStreamDefaultController(JS::Realm&);
  22. virtual ~ReadableStreamDefaultController() override = default;
  23. Optional<double> desired_size();
  24. WebIDL::ExceptionOr<void> close();
  25. WebIDL::ExceptionOr<void> enqueue(JS::Value chunk);
  26. void error(JS::Value error);
  27. JS::GCPtr<CancelAlgorithm> cancel_algorithm() { return m_cancel_algorithm; }
  28. void set_cancel_algorithm(JS::GCPtr<CancelAlgorithm> value) { m_cancel_algorithm = value; }
  29. bool close_requested() const { return m_close_requested; }
  30. void set_close_requested(bool value) { m_close_requested = value; }
  31. bool pull_again() const { return m_pull_again; }
  32. void set_pull_again(bool value) { m_pull_again = value; }
  33. JS::GCPtr<PullAlgorithm> pull_algorithm() { return m_pull_algorithm; }
  34. void set_pull_algorithm(JS::GCPtr<PullAlgorithm> value) { m_pull_algorithm = value; }
  35. bool pulling() const { return m_pulling; }
  36. void set_pulling(bool value) { m_pulling = value; }
  37. SinglyLinkedList<ValueWithSize>& queue() { return m_queue; }
  38. double queue_total_size() const { return m_queue_total_size; }
  39. void set_queue_total_size(double value) { m_queue_total_size = value; }
  40. bool started() const { return m_started; }
  41. void set_started(bool value) { m_started = value; }
  42. double strategy_hwm() const { return m_strategy_hwm; }
  43. void set_strategy_hwm(double value) { m_strategy_hwm = value; }
  44. JS::GCPtr<SizeAlgorithm> strategy_size_algorithm() { return m_strategy_size_algorithm; }
  45. void set_strategy_size_algorithm(JS::GCPtr<SizeAlgorithm> value) { m_strategy_size_algorithm = value; }
  46. JS::GCPtr<ReadableStream> stream() { return m_stream; }
  47. void set_stream(JS::GCPtr<ReadableStream> value) { m_stream = value; }
  48. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
  49. WebIDL::ExceptionOr<void> pull_steps(ReadRequest&);
  50. WebIDL::ExceptionOr<void> release_steps();
  51. private:
  52. virtual void initialize(JS::Realm&) override;
  53. virtual void visit_edges(Cell::Visitor&) override;
  54. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-cancelalgorithm
  55. // A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
  56. JS::GCPtr<CancelAlgorithm> m_cancel_algorithm;
  57. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-closerequested
  58. // A boolean flag indicating whether the stream has been closed by its underlying source, but still has chunks in its internal queue that have not yet been read
  59. bool m_close_requested { false };
  60. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullagain
  61. // A boolean flag set to true if the stream’s mechanisms requested a call to the underlying source's pull algorithm to pull more data, but the pull could not yet be done since a previous call is still executing
  62. bool m_pull_again { false };
  63. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullalgorithm
  64. // A promise-returning algorithm that pulls data from the underlying source
  65. JS::GCPtr<PullAlgorithm> m_pull_algorithm;
  66. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pulling
  67. // A boolean flag set to true while the underlying source's pull algorithm is executing and the returned promise has not yet fulfilled, used to prevent reentrant calls
  68. bool m_pulling { false };
  69. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queue
  70. // A list representing the stream’s internal queue of chunks
  71. SinglyLinkedList<ValueWithSize> m_queue;
  72. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queuetotalsize
  73. // The total size of all the chunks stored in [[queue]]
  74. double m_queue_total_size { 0 };
  75. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-started
  76. // A boolean flag indicating whether the underlying source has finished starting
  77. bool m_started { false };
  78. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategyhwm
  79. // A number supplied to the constructor as part of the stream’s queuing strategy, indicating the point at which the stream will apply backpressure to its underlying source
  80. double m_strategy_hwm { 0 };
  81. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategysizealgorithm
  82. // An algorithm to calculate the size of enqueued chunks, as part of the stream’s queuing strategy
  83. JS::GCPtr<SizeAlgorithm> m_strategy_size_algorithm;
  84. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-stream
  85. // The ReadableStream instance controlled
  86. JS::GCPtr<ReadableStream> m_stream;
  87. };
  88. }