WritableStreamDefaultController.h 4.5 KB

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