WritableStream.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Forward.h>
  8. #include <AK/SinglyLinkedList.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/Streams/QueuingStrategy.h>
  13. #include <LibWeb/WebIDL/Promise.h>
  14. namespace Web::Streams {
  15. // https://streams.spec.whatwg.org/#pending-abort-request
  16. struct PendingAbortRequest {
  17. // https://streams.spec.whatwg.org/#pending-abort-request-promise
  18. // A promise returned from WritableStreamAbort
  19. JS::NonnullGCPtr<WebIDL::Promise> promise;
  20. // https://streams.spec.whatwg.org/#pending-abort-request-reason
  21. // A JavaScript value that was passed as the abort reason to WritableStreamAbort
  22. JS::Value reason;
  23. // https://streams.spec.whatwg.org/#pending-abort-request-was-already-erroring
  24. // A boolean indicating whether or not the stream was in the "erroring" state when WritableStreamAbort was called, which impacts the outcome of the abort request
  25. bool was_already_erroring;
  26. };
  27. // https://streams.spec.whatwg.org/#writablestream
  28. class WritableStream final : public Bindings::PlatformObject {
  29. WEB_PLATFORM_OBJECT(WritableStream, Bindings::PlatformObject);
  30. public:
  31. enum class State {
  32. Writable,
  33. Closed,
  34. Erroring,
  35. Errored,
  36. };
  37. static WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_sink, QueuingStrategy const& = {});
  38. virtual ~WritableStream() = default;
  39. bool locked() const;
  40. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> abort(JS::Value reason);
  41. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> close();
  42. WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> get_writer();
  43. bool backpressure() const { return m_backpressure; }
  44. void set_backpressure(bool value) { m_backpressure = value; }
  45. JS::GCPtr<WebIDL::Promise const> close_request() const { return m_close_request; }
  46. JS::GCPtr<WebIDL::Promise> close_request() { return m_close_request; }
  47. void set_close_request(JS::GCPtr<WebIDL::Promise> value) { m_close_request = value; }
  48. JS::GCPtr<WritableStreamDefaultController const> controller() const { return m_controller; }
  49. JS::GCPtr<WritableStreamDefaultController> controller() { return m_controller; }
  50. void set_controller(JS::GCPtr<WritableStreamDefaultController> value) { m_controller = value; }
  51. JS::GCPtr<WebIDL::Promise const> in_flight_write_request() const { return m_in_flight_write_request; }
  52. void set_in_flight_write_request(JS::GCPtr<WebIDL::Promise> value) { m_in_flight_write_request = value; }
  53. JS::GCPtr<WebIDL::Promise const> in_flight_close_request() const { return m_in_flight_close_request; }
  54. void set_in_flight_close_request(JS::GCPtr<WebIDL::Promise> value) { m_in_flight_close_request = value; }
  55. Optional<PendingAbortRequest>& pending_abort_request() { return m_pending_abort_request; }
  56. void set_pending_abort_request(Optional<PendingAbortRequest>&& value) { m_pending_abort_request = move(value); }
  57. State state() const { return m_state; }
  58. void set_state(State value) { m_state = value; }
  59. JS::Value stored_error() const { return m_stored_error; }
  60. void set_stored_error(JS::Value value) { m_stored_error = value; }
  61. JS::GCPtr<WritableStreamDefaultWriter const> writer() const { return m_writer; }
  62. JS::GCPtr<WritableStreamDefaultWriter> writer() { return m_writer; }
  63. void set_writer(JS::GCPtr<WritableStreamDefaultWriter> value) { m_writer = value; }
  64. SinglyLinkedList<JS::NonnullGCPtr<WebIDL::Promise>>& write_requests() { return m_write_requests; }
  65. private:
  66. explicit WritableStream(JS::Realm&);
  67. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  68. virtual void visit_edges(Cell::Visitor&) override;
  69. // https://streams.spec.whatwg.org/#writablestream-backpressure
  70. // A boolean indicating the backpressure signal set by the controller
  71. bool m_backpressure { false };
  72. // https://streams.spec.whatwg.org/#writablestream-closerequest
  73. // The promise returned from the writer’s close() method
  74. JS::GCPtr<WebIDL::Promise> m_close_request;
  75. // https://streams.spec.whatwg.org/#writablestream-controller
  76. // A WritableStreamDefaultController created with the ability to control the state and queue of this stream
  77. JS::GCPtr<WritableStreamDefaultController> m_controller;
  78. // https://streams.spec.whatwg.org/#writablestream-detached
  79. // A boolean flag set to true when the stream is transferred
  80. bool m_detached { false };
  81. // https://streams.spec.whatwg.org/#writablestream-inflightwriterequest
  82. // A slot set to the promise for the current in-flight write operation while the underlying sink's write algorithm is executing and has not yet fulfilled, used to prevent reentrant calls
  83. JS::GCPtr<WebIDL::Promise> m_in_flight_write_request;
  84. // https://streams.spec.whatwg.org/#writablestream-inflightcloserequest
  85. // A slot set to the promise for the current in-flight close operation while the underlying sink's close algorithm is executing and has not yet fulfilled, used to prevent the abort() method from interrupting close
  86. JS::GCPtr<WebIDL::Promise> m_in_flight_close_request;
  87. // https://streams.spec.whatwg.org/#writablestream-pendingabortrequest
  88. // A pending abort request
  89. Optional<PendingAbortRequest> m_pending_abort_request;
  90. // https://streams.spec.whatwg.org/#writablestream-state
  91. // A string containing the stream’s current state, used internally; one of "writable", "closed", "erroring", or "errored"
  92. State m_state { State::Writable };
  93. // https://streams.spec.whatwg.org/#writablestream-storederror
  94. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on the stream while in the "errored" state
  95. JS::Value m_stored_error { JS::js_undefined() };
  96. // https://streams.spec.whatwg.org/#writablestream-writer
  97. // A WritableStreamDefaultWriter instance, if the stream is locked to a writer, or undefined if it is not
  98. JS::GCPtr<WritableStreamDefaultWriter> m_writer;
  99. // https://streams.spec.whatwg.org/#writablestream-writerequests
  100. // A list of promises representing the stream’s internal queue of write requests not yet processed by the underlying sink
  101. SinglyLinkedList<JS::NonnullGCPtr<WebIDL::Promise>> m_write_requests;
  102. };
  103. }