OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/streams/ReadableStream.h" | 6 #include "core/streams/ReadableStream.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou
rce* source) | 38 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou
rce* source) |
39 : ActiveDOMObject(executionContext) | 39 : ActiveDOMObject(executionContext) |
40 , m_source(source) | 40 , m_source(source) |
41 , m_isStarted(false) | 41 , m_isStarted(false) |
42 , m_isDraining(false) | 42 , m_isDraining(false) |
43 , m_isPulling(false) | 43 , m_isPulling(false) |
44 , m_state(Waiting) | 44 , m_state(Waiting) |
45 , m_wait(new WaitPromise(executionContext, this, WaitPromise::Ready)) | 45 , m_ready(new WaitPromise(executionContext, this, WaitPromise::Ready)) |
46 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed)) | 46 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed)) |
47 { | 47 { |
48 suspendIfNeeded(); | 48 suspendIfNeeded(); |
49 } | 49 } |
50 | 50 |
51 ReadableStream::~ReadableStream() | 51 ReadableStream::~ReadableStream() |
52 { | 52 { |
53 } | 53 } |
54 | 54 |
55 String ReadableStream::stateString() const | 55 String ReadableStream::stateString() const |
(...skipping 28 matching lines...) Expand all Loading... |
84 m_isPulling = false; | 84 m_isPulling = false; |
85 | 85 |
86 bool shouldApplyBackpressure = this->shouldApplyBackpressure(); | 86 bool shouldApplyBackpressure = this->shouldApplyBackpressure(); |
87 // this->shouldApplyBackpressure may call this->error(). | 87 // this->shouldApplyBackpressure may call this->error(). |
88 if (m_state == Errored) | 88 if (m_state == Errored) |
89 return false; | 89 return false; |
90 | 90 |
91 if (m_state == Waiting) { | 91 if (m_state == Waiting) { |
92 // ReadableStream::hasPendingActivity return value gets false when | 92 // ReadableStream::hasPendingActivity return value gets false when |
93 // |m_state| is changed to Closed or Errored from Waiting or Readable. | 93 // |m_state| is changed to Closed or Errored from Waiting or Readable. |
94 // On the other hand, the wrappers should be kept alive when |m_wait| | 94 // On the other hand, the wrappers should be kept alive when |m_ready| |
95 // and |m_close| resolution and rejection are called. Hence we call | 95 // and |m_close| resolution and rejection are called. Hence we call |
96 // ScriptPromiseProperty::resolve and ScriptPromiseProperty::reject | 96 // ScriptPromiseProperty::resolve and ScriptPromiseProperty::reject |
97 // *before* changing state, no matter if the state change actually | 97 // *before* changing state, no matter if the state change actually |
98 // changes hasPendingActivity return value. | 98 // changes hasPendingActivity return value. |
99 m_wait->resolve(ToV8UndefinedGenerator()); | 99 m_ready->resolve(ToV8UndefinedGenerator()); |
100 m_state = Readable; | 100 m_state = Readable; |
101 } | 101 } |
102 | 102 |
103 return !shouldApplyBackpressure; | 103 return !shouldApplyBackpressure; |
104 } | 104 } |
105 | 105 |
106 void ReadableStream::close() | 106 void ReadableStream::close() |
107 { | 107 { |
108 if (m_state == Waiting) { | 108 if (m_state == Waiting) { |
109 m_wait->resolve(ToV8UndefinedGenerator()); | 109 m_ready->resolve(ToV8UndefinedGenerator()); |
110 m_closed->resolve(ToV8UndefinedGenerator()); | 110 m_closed->resolve(ToV8UndefinedGenerator()); |
111 m_state = Closed; | 111 m_state = Closed; |
112 } else if (m_state == Readable) { | 112 } else if (m_state == Readable) { |
113 m_isDraining = true; | 113 m_isDraining = true; |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState) | 117 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState) |
118 { | 118 { |
119 if (m_state == Waiting) { | 119 if (m_state == Waiting) { |
(...skipping 11 matching lines...) Expand all Loading... |
131 } | 131 } |
132 | 132 |
133 void ReadableStream::readPostAction() | 133 void ReadableStream::readPostAction() |
134 { | 134 { |
135 ASSERT(m_state == Readable); | 135 ASSERT(m_state == Readable); |
136 if (isQueueEmpty()) { | 136 if (isQueueEmpty()) { |
137 if (m_isDraining) { | 137 if (m_isDraining) { |
138 m_closed->resolve(ToV8UndefinedGenerator()); | 138 m_closed->resolve(ToV8UndefinedGenerator()); |
139 m_state = Closed; | 139 m_state = Closed; |
140 } else { | 140 } else { |
141 m_wait->reset(); | 141 m_ready->reset(); |
142 m_state = Waiting; | 142 m_state = Waiting; |
143 } | 143 } |
144 } | 144 } |
145 callPullIfNeeded(); | 145 callPullIfNeeded(); |
146 } | 146 } |
147 | 147 |
148 ScriptPromise ReadableStream::wait(ScriptState* scriptState) | 148 ScriptPromise ReadableStream::ready(ScriptState* scriptState) |
149 { | 149 { |
150 return m_wait->promise(scriptState->world()); | 150 return m_ready->promise(scriptState->world()); |
151 } | 151 } |
152 | 152 |
153 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso
n) | 153 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso
n) |
154 { | 154 { |
155 if (m_state == Closed) | 155 if (m_state == Closed) |
156 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola
te())); | 156 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola
te())); |
157 if (m_state == Errored) | 157 if (m_state == Errored) |
158 return ScriptPromise::rejectWithDOMException(scriptState, m_exception); | 158 return ScriptPromise::rejectWithDOMException(scriptState, m_exception); |
159 | 159 |
160 ASSERT(m_state == Readable || m_state == Waiting); | 160 ASSERT(m_state == Readable || m_state == Waiting); |
161 if (m_state == Waiting) | 161 if (m_state == Waiting) |
162 m_wait->resolve(ToV8UndefinedGenerator()); | 162 m_ready->resolve(ToV8UndefinedGenerator()); |
163 clearQueue(); | 163 clearQueue(); |
164 m_closed->resolve(ToV8UndefinedGenerator()); | 164 m_closed->resolve(ToV8UndefinedGenerator()); |
165 m_state = Closed; | 165 m_state = Closed; |
166 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea
te(scriptState)); | 166 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea
te(scriptState)); |
167 } | 167 } |
168 | 168 |
169 ScriptPromise ReadableStream::closed(ScriptState* scriptState) | 169 ScriptPromise ReadableStream::closed(ScriptState* scriptState) |
170 { | 170 { |
171 return m_closed->promise(scriptState->world()); | 171 return m_closed->promise(scriptState->world()); |
172 } | 172 } |
173 | 173 |
174 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception) | 174 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception) |
175 { | 175 { |
176 switch (m_state) { | 176 switch (m_state) { |
177 case Waiting: | 177 case Waiting: |
178 m_exception = exception; | 178 m_exception = exception; |
179 m_wait->reject(m_exception); | 179 m_ready->reject(m_exception); |
180 m_closed->reject(m_exception); | 180 m_closed->reject(m_exception); |
181 m_state = Errored; | 181 m_state = Errored; |
182 break; | 182 break; |
183 case Readable: | 183 case Readable: |
184 clearQueue(); | 184 clearQueue(); |
185 m_exception = exception; | 185 m_exception = exception; |
186 m_wait->reset(); | 186 m_ready->reset(); |
187 m_wait->reject(m_exception); | 187 m_ready->reject(m_exception); |
188 m_closed->reject(m_exception); | 188 m_closed->reject(m_exception); |
189 m_state = Errored; | 189 m_state = Errored; |
190 break; | 190 break; |
191 default: | 191 default: |
192 break; | 192 break; |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 void ReadableStream::didSourceStart() | 196 void ReadableStream::didSourceStart() |
197 { | 197 { |
(...skipping 15 matching lines...) Expand all Loading... |
213 } | 213 } |
214 | 214 |
215 bool ReadableStream::hasPendingActivity() const | 215 bool ReadableStream::hasPendingActivity() const |
216 { | 216 { |
217 return m_state == Waiting || m_state == Readable; | 217 return m_state == Waiting || m_state == Readable; |
218 } | 218 } |
219 | 219 |
220 void ReadableStream::trace(Visitor* visitor) | 220 void ReadableStream::trace(Visitor* visitor) |
221 { | 221 { |
222 visitor->trace(m_source); | 222 visitor->trace(m_source); |
223 visitor->trace(m_wait); | 223 visitor->trace(m_ready); |
224 visitor->trace(m_closed); | 224 visitor->trace(m_closed); |
225 visitor->trace(m_exception); | 225 visitor->trace(m_exception); |
226 } | 226 } |
227 | 227 |
228 } // namespace blink | 228 } // namespace blink |
OLD | NEW |