WritableStreamDefaultController.h 4.4 KB

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