WritableStream.h 6.3 KB

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