WritableStreamDefaultController.h 4.3 KB

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