summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/streams/writable_stream_native.h
blob: e37cac9a68c75d807e026228ec7327f78c1eead9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_WRITABLE_STREAM_NATIVE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_WRITABLE_STREAM_NATIVE_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/streams/writable_stream.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "v8/include/v8.h"

namespace blink {

class ExceptionState;
class ScriptState;
class StrategySizeAlgorithm;
class StreamAlgorithm;
class StreamPromiseResolver;
class StreamStartAlgorithm;
class UnderlyingSinkBase;
class WritableStreamDefaultController;
class WritableStreamDefaultWriter;

class CORE_EXPORT WritableStreamNative : public WritableStream {
 public:
  enum State : uint8_t {
    kWritable,
    kClosed,
    kErroring,
    kErrored,
  };

  // https://streams.spec.whatwg.org/#create-writable-stream
  // Unlike in the standard, |high_water_mark| and |size_algorithm| are
  // required parameters.
  static WritableStreamNative* Create(ScriptState*,
                                      StreamStartAlgorithm* start_algorithm,
                                      StreamAlgorithm* write_algorithm,
                                      StreamAlgorithm* close_algorithm,
                                      StreamAlgorithm* abort_algorithm,
                                      double high_water_mark,
                                      StrategySizeAlgorithm* size_algorithm,
                                      ExceptionState&);

  static WritableStreamNative* CreateWithCountQueueingStrategy(
      ScriptState*,
      UnderlyingSinkBase*,
      size_t high_water_mark);

  // Used by Create().
  WritableStreamNative();

  // Used when creating a stream from JavaScript.
  // https://streams.spec.whatwg.org/#ws-constructor
  WritableStreamNative(ScriptState*,
                       ScriptValue raw_underlying_sink,
                       ScriptValue raw_strategy,
                       ExceptionState&);
  ~WritableStreamNative() override;

  // IDL defined functions

  // https://streams.spec.whatwg.org/#ws-locked
  bool locked(ScriptState*, ExceptionState&) const override;

  // https://streams.spec.whatwg.org/#ws-abort
  ScriptPromise abort(ScriptState*, ExceptionState&) override;
  ScriptPromise abort(ScriptState*,
                      ScriptValue reason,
                      ExceptionState&) override;

  // https://streams.spec.whatwg.org/#ws-get-writer
  ScriptValue getWriter(ScriptState*, ExceptionState&) override;

  // Inherited methods used internally.

  // https://streams.spec.whatwg.org/#is-writable-stream-locked
  // TODO(ricea): Delete this variant once the V8 extras implementation is
  // removed.
  base::Optional<bool> IsLocked(ScriptState*, ExceptionState&) const override {
    return IsLocked(this);
  }

  // This version can't fail.
  static bool IsLocked(const WritableStreamNative* stream) {
    return stream->writer_;
  }

  void Serialize(ScriptState*, MessagePort*, ExceptionState&) override;

  static WritableStreamNative* Deserialize(ScriptState*,
                                           MessagePort*,
                                           ExceptionState&);

  //
  // Methods used by ReadableStreamNative::PipeTo
  //

  // https://streams.spec.whatwg.org/#acquire-writable-stream-default-writer
  static WritableStreamDefaultWriter*
  AcquireDefaultWriter(ScriptState*, WritableStreamNative*, ExceptionState&);

  //
  // Methods used by WritableStreamDefaultWriter.
  //

  // https://streams.spec.whatwg.org/#writable-stream-abort
  static v8::Local<v8::Promise> Abort(ScriptState*,
                                      WritableStreamNative*,
                                      v8::Local<v8::Value> reason);

  // https://streams.spec.whatwg.org/#writable-stream-add-write-request
  static v8::Local<v8::Promise> AddWriteRequest(ScriptState*,
                                                WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-close-queued-or-in-flight
  static bool CloseQueuedOrInFlight(const WritableStreamNative*);

  //
  // Methods used by WritableStreamDefaultController.
  //

  // https://streams.spec.whatwg.org/#writable-stream-deal-with-rejection
  static void DealWithRejection(ScriptState*,
                                WritableStreamNative*,
                                v8::Local<v8::Value> error);

  // https://streams.spec.whatwg.org/#writable-stream-start-erroring
  static void StartErroring(ScriptState*,
                            WritableStreamNative*,
                            v8::Local<v8::Value> reason);

  // https://streams.spec.whatwg.org/#writable-stream-finish-erroring
  static void FinishErroring(ScriptState*, WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write
  static void FinishInFlightWrite(ScriptState*, WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write-with-error
  static void FinishInFlightWriteWithError(ScriptState*,
                                           WritableStreamNative*,
                                           v8::Local<v8::Value> error);

  // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close
  static void FinishInFlightClose(ScriptState*, WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close-with-error
  static void FinishInFlightCloseWithError(ScriptState*,
                                           WritableStreamNative*,
                                           v8::Local<v8::Value> error);

  // https://streams.spec.whatwg.org/#writable-stream-mark-close-request-in-flight
  static void MarkCloseRequestInFlight(WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-mark-first-write-request-in-flight
  static void MarkFirstWriteRequestInFlight(WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-update-backpressure
  static void UpdateBackpressure(ScriptState*,
                                 WritableStreamNative*,
                                 bool backpressure);

  // Accessors for use by other stream classes.
  State GetState() const { return state_; }
  bool IsErrored() const { return state_ == kErrored; }
  bool IsErroring() const { return state_ == kErroring; }
  bool IsWritable() const { return state_ == kWritable; }

  bool HasBackpressure() const { return has_backpressure_; }

  const StreamPromiseResolver* InFlightWriteRequest() const {
    return in_flight_write_request_;
  }

  bool IsClosingOrClosed() const {
    return CloseQueuedOrInFlight(this) || state_ == kClosed;
  }

  v8::Local<v8::Value> GetStoredError(v8::Isolate*) const;

  WritableStreamDefaultController* Controller() {
    return writable_stream_controller_;
  }
  const WritableStreamDefaultController* Controller() const {
    return writable_stream_controller_;
  }

  const WritableStreamDefaultWriter* Writer() const { return writer_; }

  void SetCloseRequest(StreamPromiseResolver*);
  void SetController(WritableStreamDefaultController*);
  void SetWriter(WritableStreamDefaultWriter*);

  void Trace(Visitor*) override;

 private:
  using PromiseQueue = HeapDeque<Member<StreamPromiseResolver>>;

  class PendingAbortRequest;

  // https://streams.spec.whatwg.org/#writable-stream-has-operation-marked-in-flight
  static bool HasOperationMarkedInFlight(const WritableStreamNative*);

  // https://streams.spec.whatwg.org/#writable-stream-reject-close-and-closed-promise-if-needed
  static void RejectCloseAndClosedPromiseIfNeeded(ScriptState*,
                                                  WritableStreamNative*);

  // Functions that don't appear in the standard.

  // Rejects all the promises in |queue| with value |e|.
  static void RejectPromises(ScriptState*,
                             PromiseQueue* queue,
                             v8::Local<v8::Value> e);

  // Member variables correspond 1:1 to the internal slots in the standard.
  // See https://streams.spec.whatwg.org/#ws-internal-slots

  // |has_backpressure_| corresponds to the [[backpressure]] slot in the
  // |standard.
  bool has_backpressure_ = false;

  // |state_| is here out of order so it doesn't require 7 bytes of padding.
  State state_ = kWritable;

  Member<StreamPromiseResolver> close_request_;
  Member<StreamPromiseResolver> in_flight_write_request_;
  Member<StreamPromiseResolver> in_flight_close_request_;
  Member<PendingAbortRequest> pending_abort_request_;
  TraceWrapperV8Reference<v8::Value> stored_error_;
  Member<WritableStreamDefaultController> writable_stream_controller_;
  Member<WritableStreamDefaultWriter> writer_;
  PromiseQueue write_requests_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_WRITABLE_STREAM_NATIVE_H_