WritableStreamDefaultController.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/SinglyLinkedList.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/Streams/AbstractOperations.h>
  10. namespace Web::Streams {
  11. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller
  12. class WritableStreamDefaultController final : public Bindings::PlatformObject {
  13. WEB_PLATFORM_OBJECT(WritableStreamDefaultController, Bindings::PlatformObject);
  14. JS_DECLARE_ALLOCATOR(WritableStreamDefaultController);
  15. public:
  16. virtual ~WritableStreamDefaultController() override = default;
  17. WebIDL::ExceptionOr<void> error(JS::Value error);
  18. JS::NonnullGCPtr<DOM::AbortSignal> signal() { return *m_signal; }
  19. void set_signal(JS::NonnullGCPtr<DOM::AbortSignal> value) { m_signal = value; }
  20. JS::GCPtr<AbortAlgorithm> abort_algorithm() { return m_abort_algorithm; }
  21. void set_abort_algorithm(JS::GCPtr<AbortAlgorithm> value) { m_abort_algorithm = value; }
  22. JS::GCPtr<CloseAlgorithm> close_algorithm() { return m_close_algorithm; }
  23. void set_close_algorithm(JS::GCPtr<CloseAlgorithm> value) { m_close_algorithm = value; }
  24. SinglyLinkedList<ValueWithSize>& queue() { return m_queue; }
  25. double queue_total_size() const { return m_queue_total_size; }
  26. void set_queue_total_size(double value) { m_queue_total_size = value; }
  27. bool started() const { return m_started; }
  28. void set_started(bool value) { m_started = value; }
  29. size_t strategy_hwm() const { return m_strategy_hwm; }
  30. void set_strategy_hwm(size_t value) { m_strategy_hwm = value; }
  31. JS::GCPtr<SizeAlgorithm> strategy_size_algorithm() { return m_strategy_size_algorithm; }
  32. void set_strategy_size_algorithm(JS::GCPtr<SizeAlgorithm> value) { m_strategy_size_algorithm = value; }
  33. JS::NonnullGCPtr<WritableStream> stream() { return *m_stream; }
  34. void set_stream(JS::NonnullGCPtr<WritableStream> value) { m_stream = value; }
  35. JS::GCPtr<WriteAlgorithm> write_algorithm() { return m_write_algorithm; }
  36. void set_write_algorithm(JS::GCPtr<WriteAlgorithm> value) { m_write_algorithm = value; }
  37. WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>> abort_steps(JS::Value reason);
  38. void error_steps();
  39. private:
  40. explicit WritableStreamDefaultController(JS::Realm&);
  41. virtual void visit_edges(Visitor&) override;
  42. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-abortalgorithm
  43. // A promise-returning algorithm, taking one argument (the abort reason), which communicates a requested abort to the underlying sink
  44. JS::GCPtr<AbortAlgorithm> m_abort_algorithm;
  45. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-closealgorithm
  46. // A promise-returning algorithm which communicates a requested close to the underlying sink
  47. JS::GCPtr<CloseAlgorithm> m_close_algorithm;
  48. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-queue
  49. // A list representing the stream’s internal queue of chunks
  50. SinglyLinkedList<ValueWithSize> m_queue;
  51. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-queuetotalsize
  52. // The total size of all the chunks stored in [[queue]]
  53. double m_queue_total_size { 0 };
  54. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-signal
  55. // An AbortSignal that can be used to abort the pending write or close operation when the stream is aborted.
  56. JS::GCPtr<DOM::AbortSignal> m_signal;
  57. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-started
  58. // A boolean flag indicating whether the underlying sink has finished starting
  59. bool m_started { false };
  60. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-strategyhwm
  61. // A number supplied by the creator of the stream as part of the stream’s queuing strategy, indicating the point at which the stream will apply backpressure to its underlying sink
  62. size_t m_strategy_hwm { 0 };
  63. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-strategysizealgorithm
  64. // An algorithm to calculate the size of enqueued chunks, as part of the stream’s queuing strategy
  65. JS::GCPtr<SizeAlgorithm> m_strategy_size_algorithm;
  66. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-stream
  67. // The WritableStream instance controlled
  68. JS::GCPtr<WritableStream> m_stream;
  69. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-writealgorithm
  70. // A promise-returning algorithm, taking one argument (the chunk to write), which writes data to the underlying sink
  71. JS::GCPtr<WriteAlgorithm> m_write_algorithm;
  72. };
  73. }