WritableStream.h 6.2 KB

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