summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/streams/readable_stream_default_controller.h
blob: ca53eca0890db321e2b48bad302682715a486ff9 (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
// 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_READABLE_STREAM_DEFAULT_CONTROLLER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_READABLE_STREAM_DEFAULT_CONTROLLER_H_

#include "base/optional.h"
#include "third_party/blink/renderer/core/streams/readable_stream_controller.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "v8/include/v8.h"

namespace blink {

class ExceptionState;
class QueueWithSizes;
class ReadableStream;
class ScriptState;
class ScriptValue;
class StrategySizeAlgorithm;
class StreamAlgorithm;
class StreamPromiseResolver;
class StreamStartAlgorithm;

class ReadableStreamDefaultController : public ReadableStreamController {
  DEFINE_WRAPPERTYPEINFO();

 public:
  ReadableStreamDefaultController();

  // https://streams.spec.whatwg.org/#rs-default-controller-desired-size
  base::Optional<double> desiredSize() const { return GetDesiredSize(); }

  // https://streams.spec.whatwg.org/#rs-default-controller-close
  void close(ScriptState*, ExceptionState&);

  // https://streams.spec.whatwg.org/#rs-default-controller-enqueue
  void enqueue(ScriptState*, ExceptionState&);
  void enqueue(ScriptState*, ScriptValue chunk, ExceptionState&);

  // https://streams.spec.whatwg.org/#rs-default-controller-error
  void error(ScriptState*);
  void error(ScriptState*, ScriptValue e);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-close
  static void Close(ScriptState*, ReadableStreamDefaultController*);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-enqueue
  static void Enqueue(ScriptState*,
                      ReadableStreamDefaultController*,
                      v8::Local<v8::Value> chunk,
                      ExceptionState&);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-error
  static void Error(ScriptState*,
                    ReadableStreamDefaultController*,
                    v8::Local<v8::Value> e);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-get-desired-size
  base::Optional<double> GetDesiredSize() const;

  //
  // Used by TransformStream
  //
  // https://streams.spec.whatwg.org/#readable-stream-default-controller-can-close-or-enqueue
  static bool CanCloseOrEnqueue(const ReadableStreamDefaultController*);

  // https://streams.spec.whatwg.org/#rs-default-controller-has-backpressure
  static bool HasBackpressure(const ReadableStreamDefaultController*);

  static const char* EnqueueExceptionMessage(
      const ReadableStreamDefaultController*);

  bool IsDefaultController() const override { return true; }
  bool IsByteStreamController() const override { return false; }

  void Trace(Visitor*) const override;

  // https://streams.spec.whatwg.org/#rs-default-controller-private-cancel
  v8::Local<v8::Promise> CancelSteps(ScriptState*,
                                     v8::Local<v8::Value> reason) override;

  // https://streams.spec.whatwg.org/#rs-default-controller-private-pull
  StreamPromiseResolver* PullSteps(ScriptState*) override;

 private:
  friend class ReadableStream;
  friend class ReadableStreamDefaultReader;

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-call-pull-if-needed
  static void CallPullIfNeeded(ScriptState*, ReadableStreamDefaultController*);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-should-call-pull
  static bool ShouldCallPull(const ReadableStreamDefaultController*);

  // https://streams.spec.whatwg.org/#readable-stream-default-controller-clear-algorithms
  static void ClearAlgorithms(ReadableStreamDefaultController*);

  // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller
  static void SetUp(ScriptState*,
                    ReadableStream*,
                    ReadableStreamDefaultController*,
                    StreamStartAlgorithm* start_algorithm,
                    StreamAlgorithm* pull_algorithm,
                    StreamAlgorithm* cancel_algorithm,
                    double high_water_mark,
                    StrategySizeAlgorithm* size_algorithm,
                    ExceptionState&);

  // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller-from-underlying-source
  static void SetUpFromUnderlyingSource(ScriptState*,
                                        ReadableStream*,
                                        v8::Local<v8::Object> underlying_source,
                                        double high_water_mark,
                                        StrategySizeAlgorithm* size_algorithm,
                                        ExceptionState&);

  // Boolean flags are grouped together to reduce object size. Verbs have been
  // added to the names in the standard to match Blink style.
  bool is_close_requested_ = false;
  bool will_pull_again_ = false;
  bool is_pulling_ = false;
  bool is_started_ = false;
  Member<StreamAlgorithm> cancel_algorithm_;
  Member<ReadableStream> controlled_readable_stream_;
  Member<StreamAlgorithm> pull_algorithm_;
  Member<QueueWithSizes> queue_;
  double strategy_high_water_mark_ = 0.0;
  Member<StrategySizeAlgorithm> strategy_size_algorithm_;
};

template <>
struct DowncastTraits<ReadableStreamDefaultController> {
  static bool AllowFrom(const ReadableStreamController& controller) {
    return controller.IsDefaultController();
  }
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STREAMS_READABLE_STREAM_DEFAULT_CONTROLLER_H_