ReadableStreamDefaultController.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public:
  20. explicit ReadableStreamDefaultController(JS::Realm&);
  21. virtual ~ReadableStreamDefaultController() override = default;
  22. Optional<float> desired_size();
  23. WebIDL::ExceptionOr<void> close();
  24. WebIDL::ExceptionOr<void> enqueue(JS::Value chunk);
  25. void error(JS::Value error);
  26. auto& cancel_algorithm() { return m_cancel_algorithm; }
  27. void set_cancel_algorithm(Optional<CancelAlgorithm> value) { m_cancel_algorithm = move(value); }
  28. bool close_requested() const { return m_close_requested; }
  29. void set_close_requested(bool value) { m_close_requested = value; }
  30. bool pull_again() const { return m_pull_again; }
  31. void set_pull_again(bool value) { m_pull_again = value; }
  32. auto& pull_algorithm() { return m_pull_algorithm; }
  33. void set_pull_algorithm(Optional<PullAlgorithm> value) { m_pull_algorithm = move(value); }
  34. bool pulling() const { return m_pulling; }
  35. void set_pulling(bool value) { m_pulling = value; }
  36. SinglyLinkedList<ValueWithSize>& queue() { return m_queue; }
  37. double queue_total_size() const { return m_queue_total_size; }
  38. void set_queue_total_size(double value) { m_queue_total_size = value; }
  39. bool started() const { return m_started; }
  40. void set_started(bool value) { m_started = value; }
  41. size_t strategy_hwm() const { return m_strategy_hwm; }
  42. void set_strategy_hwm(size_t value) { m_strategy_hwm = value; }
  43. auto& strategy_size_algorithm() { return m_strategy_size_algorithm; }
  44. void set_strategy_size_algorithm(Optional<SizeAlgorithm> value) { m_strategy_size_algorithm = move(value); }
  45. JS::GCPtr<ReadableStream> stream() { return m_stream; }
  46. void set_stream(JS::GCPtr<ReadableStream> value) { m_stream = value; }
  47. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
  48. WebIDL::ExceptionOr<void> pull_steps(ReadRequest&);
  49. WebIDL::ExceptionOr<void> release_steps();
  50. private:
  51. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-cancelalgorithm
  54. // A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
  55. Optional<CancelAlgorithm> m_cancel_algorithm;
  56. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-closerequested
  57. // 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
  58. bool m_close_requested { false };
  59. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullagain
  60. // 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
  61. bool m_pull_again { false };
  62. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullalgorithm
  63. // A promise-returning algorithm that pulls data from the underlying source
  64. Optional<PullAlgorithm> m_pull_algorithm;
  65. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pulling
  66. // 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
  67. bool m_pulling { false };
  68. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queue
  69. // A list representing the stream’s internal queue of chunks
  70. SinglyLinkedList<ValueWithSize> m_queue;
  71. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queuetotalsize
  72. // The total size of all the chunks stored in [[queue]]
  73. double m_queue_total_size { 0 };
  74. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-started
  75. // A boolean flag indicating whether the underlying source has finished starting
  76. bool m_started { false };
  77. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategyhwm
  78. // 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
  79. size_t m_strategy_hwm { 0 };
  80. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategysizealgorithm
  81. // An algorithm to calculate the size of enqueued chunks, as part of the stream’s queuing strategy
  82. Optional<SizeAlgorithm> m_strategy_size_algorithm;
  83. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-stream
  84. // The ReadableStream instance controlled
  85. JS::GCPtr<ReadableStream> m_stream;
  86. };
  87. }