ReadableByteStreamController.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/Optional.h>
  8. #include <AK/SinglyLinkedList.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/Streams/AbstractOperations.h>
  11. namespace Web::Streams {
  12. enum class ReaderType {
  13. Default,
  14. Byob,
  15. None,
  16. };
  17. // https://streams.spec.whatwg.org/#pull-into-descriptor
  18. struct PullIntoDescriptor {
  19. // https://streams.spec.whatwg.org/#pull-into-descriptor-buffer
  20. // An ArrayBuffer
  21. JS::NonnullGCPtr<JS::ArrayBuffer> buffer;
  22. // https://streams.spec.whatwg.org/#pull-into-descriptor-buffer-byte-length
  23. // A positive integer representing the initial byte length of buffer
  24. u32 buffer_byte_length;
  25. // https://streams.spec.whatwg.org/#pull-into-descriptor-byte-offset
  26. // A nonnegative integer byte offset into the buffer where the underlying byte source will start writing
  27. u32 byte_offset;
  28. // https://streams.spec.whatwg.org/#pull-into-descriptor-byte-length
  29. // A positive integer number of bytes which can be written into the buffer
  30. u32 byte_length;
  31. // https://streams.spec.whatwg.org/#pull-into-descriptor-bytes-filled
  32. // A nonnegative integer number of bytes that have been written into the buffer so far
  33. u32 bytes_filled;
  34. // https://streams.spec.whatwg.org/#pull-into-descriptor-element-size
  35. // A positive integer representing the number of bytes that can be written into the buffer at a time, using views of the type described by the view constructor
  36. u32 element_size;
  37. // https://streams.spec.whatwg.org/#pull-into-descriptor-view-constructor
  38. // A typed array constructor or %DataView%, which will be used for constructing a view with which to write into the buffer
  39. JS::NonnullGCPtr<JS::NativeFunction> view_constructor;
  40. // https://streams.spec.whatwg.org/#pull-into-descriptor-reader-type
  41. // Either "default" or "byob", indicating what type of readable stream reader initiated this request, or "none" if the initiating reader was released
  42. ReaderType reader_type;
  43. };
  44. // https://streams.spec.whatwg.org/#readable-byte-stream-queue-entry
  45. struct ReadableByteStreamQueueEntry {
  46. // https://streams.spec.whatwg.org/#readable-byte-stream-queue-entry-buffer
  47. // An ArrayBuffer, which will be a transferred version of the one originally supplied by the underlying byte source
  48. JS::NonnullGCPtr<JS::ArrayBuffer> buffer;
  49. // https://streams.spec.whatwg.org/#readable-byte-stream-queue-entry-byte-offset
  50. // A nonnegative integer number giving the byte offset derived from the view originally supplied by the underlying byte source
  51. u32 byte_offset;
  52. // https://streams.spec.whatwg.org/#readable-byte-stream-queue-entry-byte-length
  53. // A nonnegative integer number giving the byte length derived from the view originally supplied by the underlying byte source
  54. u32 byte_length;
  55. };
  56. // https://streams.spec.whatwg.org/#readablebytestreamcontroller
  57. class ReadableByteStreamController : public Bindings::PlatformObject {
  58. WEB_PLATFORM_OBJECT(ReadableByteStreamController, Bindings::PlatformObject);
  59. public:
  60. virtual ~ReadableByteStreamController() override = default;
  61. JS::GCPtr<ReadableStreamBYOBRequest> byob_request() { return m_byob_request; }
  62. void set_byob_request(JS::GCPtr<ReadableStreamBYOBRequest> request) { m_byob_request = request; }
  63. Optional<double> desired_size() const;
  64. Optional<u32> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
  65. void set_auto_allocate_chunk_size(Optional<u32> value) { m_auto_allocate_chunk_size = value; }
  66. auto& cancel_algorithm() { return m_cancel_algorithm; }
  67. void set_cancel_algorithm(Optional<CancelAlgorithm> value) { m_cancel_algorithm = move(value); }
  68. bool close_requested() const { return m_close_requested; }
  69. void set_close_requested(bool value) { m_close_requested = value; }
  70. bool pull_again() const { return m_pull_again; }
  71. void set_pull_again(bool value) { m_pull_again = value; }
  72. auto& pull_algorithm() { return m_pull_algorithm; }
  73. void set_pull_algorithm(Optional<PullAlgorithm> value) { m_pull_algorithm = move(value); }
  74. bool pulling() const { return m_pulling; }
  75. void set_pulling(bool value) { m_pulling = value; }
  76. SinglyLinkedList<PullIntoDescriptor>& pending_pull_intos() { return m_pending_pull_intos; }
  77. SinglyLinkedList<ReadableByteStreamQueueEntry>& queue() { return m_queue; }
  78. u32 queue_total_size() const { return m_queue_total_size; }
  79. void set_queue_total_size(u32 size) { m_queue_total_size = size; }
  80. bool started() const { return m_started; }
  81. void set_started(bool value) { m_started = value; }
  82. u32 strategy_hwm() const { return m_strategy_hwm; }
  83. void set_strategy_hwm(u32 value) { m_strategy_hwm = value; }
  84. JS::GCPtr<ReadableStream const> stream() const { return m_stream; }
  85. JS::GCPtr<ReadableStream> stream() { return m_stream; }
  86. void set_stream(JS::GCPtr<ReadableStream> stream) { m_stream = stream; }
  87. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
  88. WebIDL::ExceptionOr<void> pull_steps(NonnullRefPtr<ReadRequest>);
  89. WebIDL::ExceptionOr<void> release_steps();
  90. private:
  91. explicit ReadableByteStreamController(JS::Realm&);
  92. virtual void visit_edges(Cell::Visitor&) override;
  93. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-autoallocatechunksize
  94. // A positive integer, when the automatic buffer allocation feature is enabled. In that case, this value specifies the size of buffer to allocate. It is undefined otherwise.
  95. Optional<u32> m_auto_allocate_chunk_size;
  96. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-byobrequest
  97. // A ReadableStreamBYOBRequest instance representing the current BYOB pull request, or null if there are no pending requests
  98. JS::GCPtr<ReadableStreamBYOBRequest> m_byob_request;
  99. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-cancelalgorithm
  100. // A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
  101. Optional<CancelAlgorithm> m_cancel_algorithm;
  102. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-closerequested
  103. // A boolean flag indicating whether the stream has been closed by its underlying source, but still has chunks in its internal queue that have not yet been read
  104. bool m_close_requested { false };
  105. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullagain
  106. // A boolean flag set to true if the stream’s mechanisms requested a call to the underlying source's pull algorithm to pull more data, but the pull could not yet be done since a previous call is still executing
  107. bool m_pull_again { false };
  108. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullalgorithm
  109. // A promise-returning algorithm that pulls data from the underlying source
  110. Optional<PullAlgorithm> m_pull_algorithm;
  111. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pulling
  112. // A boolean flag set to true while the underlying source's pull algorithm is executing and the returned promise has not yet fulfilled, used to prevent reentrant calls
  113. bool m_pulling { false };
  114. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-pendingpullintos
  115. // A list of pull-into descriptors
  116. SinglyLinkedList<PullIntoDescriptor> m_pending_pull_intos;
  117. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queue
  118. // A list representing the stream’s internal queue of chunks
  119. SinglyLinkedList<ReadableByteStreamQueueEntry> m_queue;
  120. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queuetotalsize
  121. // The total size of all the chunks stored in [[queue]]
  122. u32 m_queue_total_size { 0 };
  123. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-started
  124. // A boolean flag indicating whether the underlying source has finished starting
  125. bool m_started { false };
  126. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategyhwm
  127. // A number supplied to the constructor as part of the stream’s queuing strategy, indicating the point at which the stream will apply backpressure to its underlying source
  128. u32 m_strategy_hwm { 0 };
  129. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-stream
  130. // The ReadableStream instance controlled
  131. JS::GCPtr<ReadableStream> m_stream;
  132. };
  133. }