WritableStreamDefaultController.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. virtual void visit_edges(Visitor&) override;
  41. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-abortalgorithm
  42. // A promise-returning algorithm, taking one argument (the abort reason), which communicates a requested abort to the underlying sink
  43. Optional<AbortAlgorithm> m_abort_algorithm;
  44. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-closealgorithm
  45. // A promise-returning algorithm which communicates a requested close to the underlying sink
  46. Optional<CloseAlgorithm> m_close_algorithm;
  47. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-queue
  48. // A list representing the stream’s internal queue of chunks
  49. SinglyLinkedList<ValueWithSize> m_queue;
  50. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-queuetotalsize
  51. // The total size of all the chunks stored in [[queue]]
  52. double m_queue_total_size { 0 };
  53. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-signal
  54. // An AbortSignal that can be used to abort the pending write or close operation when the stream is aborted.
  55. JS::GCPtr<DOM::AbortSignal> m_signal;
  56. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-started
  57. // A boolean flag indicating whether the underlying sink has finished starting
  58. bool m_started { false };
  59. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-strategyhwm
  60. // 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
  61. size_t m_strategy_hwm { 0 };
  62. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-strategysizealgorithm
  63. // An algorithm to calculate the size of enqueued chunks, as part of the stream’s queuing strategy
  64. Optional<SizeAlgorithm> m_strategy_size_algorithm;
  65. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-stream
  66. // The WritableStream instance controlled
  67. JS::GCPtr<WritableStream> m_stream;
  68. // https://streams.spec.whatwg.org/#writablestreamdefaultcontroller-writealgorithm
  69. // A promise-returning algorithm, taking one argument (the chunk to write), which writes data to the underlying sink
  70. Optional<WriteAlgorithm> m_write_algorithm;
  71. };
  72. }