ReadableByteStreamController.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. u64 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. u64 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. u64 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. u64 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. u64 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. u64 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. u64 byte_length;
  55. };
  56. // https://streams.spec.whatwg.org/#readablebytestreamcontroller
  57. class ReadableByteStreamController : public Bindings::PlatformObject {
  58. WEB_PLATFORM_OBJECT(ReadableByteStreamController, Bindings::PlatformObject);
  59. JS_DECLARE_ALLOCATOR(ReadableByteStreamController);
  60. public:
  61. virtual ~ReadableByteStreamController() override = default;
  62. // IDL getter, returns current [[byobRequest]] (if any), and otherwise the [[byobRequest]] for the next pending pull into request
  63. JS::GCPtr<ReadableStreamBYOBRequest> byob_request();
  64. void set_byob_request(JS::GCPtr<ReadableStreamBYOBRequest> request) { m_byob_request = request; }
  65. // Raw [[byobRequest]] slot
  66. JS::GCPtr<ReadableStreamBYOBRequest const> raw_byob_request() const { return m_byob_request; }
  67. JS::GCPtr<ReadableStreamBYOBRequest> raw_byob_request() { return m_byob_request; }
  68. Optional<double> desired_size() const;
  69. WebIDL::ExceptionOr<void> close();
  70. void error(JS::Value error);
  71. WebIDL::ExceptionOr<void> enqueue(JS::Handle<WebIDL::ArrayBufferView>&);
  72. Optional<u64> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
  73. void set_auto_allocate_chunk_size(Optional<u64> value) { m_auto_allocate_chunk_size = value; }
  74. JS::GCPtr<CancelAlgorithm> cancel_algorithm() { return m_cancel_algorithm; }
  75. void set_cancel_algorithm(JS::GCPtr<CancelAlgorithm> value) { m_cancel_algorithm = value; }
  76. bool close_requested() const { return m_close_requested; }
  77. void set_close_requested(bool value) { m_close_requested = value; }
  78. bool pull_again() const { return m_pull_again; }
  79. void set_pull_again(bool value) { m_pull_again = value; }
  80. JS::GCPtr<PullAlgorithm> pull_algorithm() { return m_pull_algorithm; }
  81. void set_pull_algorithm(JS::GCPtr<PullAlgorithm> value) { m_pull_algorithm = value; }
  82. bool pulling() const { return m_pulling; }
  83. void set_pulling(bool value) { m_pulling = value; }
  84. SinglyLinkedList<PullIntoDescriptor>& pending_pull_intos() { return m_pending_pull_intos; }
  85. SinglyLinkedList<PullIntoDescriptor> const& pending_pull_intos() const { return m_pending_pull_intos; }
  86. SinglyLinkedList<ReadableByteStreamQueueEntry>& queue() { return m_queue; }
  87. double queue_total_size() const { return m_queue_total_size; }
  88. void set_queue_total_size(double size) { m_queue_total_size = size; }
  89. bool started() const { return m_started; }
  90. void set_started(bool value) { m_started = value; }
  91. double strategy_hwm() const { return m_strategy_hwm; }
  92. void set_strategy_hwm(double value) { m_strategy_hwm = value; }
  93. JS::GCPtr<ReadableStream const> stream() const { return m_stream; }
  94. JS::GCPtr<ReadableStream> stream() { return m_stream; }
  95. void set_stream(JS::GCPtr<ReadableStream> stream) { m_stream = stream; }
  96. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
  97. WebIDL::ExceptionOr<void> pull_steps(JS::NonnullGCPtr<ReadRequest>);
  98. WebIDL::ExceptionOr<void> release_steps();
  99. private:
  100. explicit ReadableByteStreamController(JS::Realm&);
  101. virtual void visit_edges(Cell::Visitor&) override;
  102. virtual void initialize(JS::Realm&) override;
  103. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-autoallocatechunksize
  104. // 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.
  105. Optional<u64> m_auto_allocate_chunk_size;
  106. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-byobrequest
  107. // A ReadableStreamBYOBRequest instance representing the current BYOB pull request, or null if there are no pending requests
  108. JS::GCPtr<ReadableStreamBYOBRequest> m_byob_request;
  109. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-cancelalgorithm
  110. // A promise-returning algorithm, taking one argument (the cancel reason), which communicates a requested cancelation to the underlying source
  111. JS::GCPtr<CancelAlgorithm> m_cancel_algorithm;
  112. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-closerequested
  113. // 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
  114. bool m_close_requested { false };
  115. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullagain
  116. // 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
  117. bool m_pull_again { false };
  118. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pullalgorithm
  119. // A promise-returning algorithm that pulls data from the underlying source
  120. JS::GCPtr<PullAlgorithm> m_pull_algorithm;
  121. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-pulling
  122. // 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
  123. bool m_pulling { false };
  124. // https://streams.spec.whatwg.org/#readablebytestreamcontroller-pendingpullintos
  125. // A list of pull-into descriptors
  126. SinglyLinkedList<PullIntoDescriptor> m_pending_pull_intos;
  127. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queue
  128. // A list representing the stream’s internal queue of chunks
  129. SinglyLinkedList<ReadableByteStreamQueueEntry> m_queue;
  130. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-queuetotalsize
  131. // The total size of all the chunks stored in [[queue]]
  132. double m_queue_total_size { 0 };
  133. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-started
  134. // A boolean flag indicating whether the underlying source has finished starting
  135. bool m_started { false };
  136. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-strategyhwm
  137. // 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
  138. double m_strategy_hwm { 0 };
  139. // https://streams.spec.whatwg.org/#readablestreamdefaultcontroller-stream
  140. // The ReadableStream instance controlled
  141. JS::GCPtr<ReadableStream> m_stream;
  142. };
  143. }