WritableStream.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. JS_DECLARE_ALLOCATOR(WritableStream);
  31. public:
  32. enum class State {
  33. Writable,
  34. Closed,
  35. Erroring,
  36. Errored,
  37. };
  38. static WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> construct_impl(JS::Realm& realm, Optional<JS::Handle<JS::Object>> const& underlying_sink, QueuingStrategy const& = {});
  39. virtual ~WritableStream() = default;
  40. bool locked() const;
  41. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> abort(JS::Value reason);
  42. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> close();
  43. WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> get_writer();
  44. bool backpressure() const { return m_backpressure; }
  45. void set_backpressure(bool value) { m_backpressure = value; }
  46. JS::GCPtr<WebIDL::Promise const> close_request() const { return m_close_request; }
  47. JS::GCPtr<WebIDL::Promise> close_request() { return m_close_request; }
  48. void set_close_request(JS::GCPtr<WebIDL::Promise> value) { m_close_request = value; }
  49. JS::GCPtr<WritableStreamDefaultController const> controller() const { return m_controller; }
  50. JS::GCPtr<WritableStreamDefaultController> controller() { return m_controller; }
  51. void set_controller(JS::GCPtr<WritableStreamDefaultController> value) { m_controller = value; }
  52. JS::GCPtr<WebIDL::Promise const> in_flight_write_request() const { return m_in_flight_write_request; }
  53. void set_in_flight_write_request(JS::GCPtr<WebIDL::Promise> value) { m_in_flight_write_request = value; }
  54. JS::GCPtr<WebIDL::Promise const> in_flight_close_request() const { return m_in_flight_close_request; }
  55. void set_in_flight_close_request(JS::GCPtr<WebIDL::Promise> value) { m_in_flight_close_request = value; }
  56. Optional<PendingAbortRequest>& pending_abort_request() { return m_pending_abort_request; }
  57. void set_pending_abort_request(Optional<PendingAbortRequest>&& value) { m_pending_abort_request = move(value); }
  58. State state() const { return m_state; }
  59. void set_state(State value) { m_state = value; }
  60. JS::Value stored_error() const { return m_stored_error; }
  61. void set_stored_error(JS::Value value) { m_stored_error = value; }
  62. JS::GCPtr<WritableStreamDefaultWriter const> writer() const { return m_writer; }
  63. JS::GCPtr<WritableStreamDefaultWriter> writer() { return m_writer; }
  64. void set_writer(JS::GCPtr<WritableStreamDefaultWriter> value) { m_writer = value; }
  65. SinglyLinkedList<JS::NonnullGCPtr<WebIDL::Promise>>& write_requests() { return m_write_requests; }
  66. private:
  67. explicit WritableStream(JS::Realm&);
  68. virtual void initialize(JS::Realm&) override;
  69. virtual void visit_edges(Cell::Visitor&) override;
  70. // https://streams.spec.whatwg.org/#writablestream-backpressure
  71. // A boolean indicating the backpressure signal set by the controller
  72. bool m_backpressure { false };
  73. // https://streams.spec.whatwg.org/#writablestream-closerequest
  74. // The promise returned from the writer’s close() method
  75. JS::GCPtr<WebIDL::Promise> m_close_request;
  76. // https://streams.spec.whatwg.org/#writablestream-controller
  77. // A WritableStreamDefaultController created with the ability to control the state and queue of this stream
  78. JS::GCPtr<WritableStreamDefaultController> m_controller;
  79. // https://streams.spec.whatwg.org/#writablestream-detached
  80. // A boolean flag set to true when the stream is transferred
  81. bool m_detached { false };
  82. // https://streams.spec.whatwg.org/#writablestream-inflightwriterequest
  83. // 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
  84. JS::GCPtr<WebIDL::Promise> m_in_flight_write_request;
  85. // https://streams.spec.whatwg.org/#writablestream-inflightcloserequest
  86. // 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
  87. JS::GCPtr<WebIDL::Promise> m_in_flight_close_request;
  88. // https://streams.spec.whatwg.org/#writablestream-pendingabortrequest
  89. // A pending abort request
  90. Optional<PendingAbortRequest> m_pending_abort_request;
  91. // https://streams.spec.whatwg.org/#writablestream-state
  92. // A string containing the stream’s current state, used internally; one of "writable", "closed", "erroring", or "errored"
  93. State m_state { State::Writable };
  94. // https://streams.spec.whatwg.org/#writablestream-storederror
  95. // 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
  96. JS::Value m_stored_error { JS::js_undefined() };
  97. // https://streams.spec.whatwg.org/#writablestream-writer
  98. // A WritableStreamDefaultWriter instance, if the stream is locked to a writer, or undefined if it is not
  99. JS::GCPtr<WritableStreamDefaultWriter> m_writer;
  100. // https://streams.spec.whatwg.org/#writablestream-writerequests
  101. // A list of promises representing the stream’s internal queue of write requests not yet processed by the underlying sink
  102. SinglyLinkedList<JS::NonnullGCPtr<WebIDL::Promise>> m_write_requests;
  103. };
  104. }