Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(746)

Side by Side Diff: Source/core/streams/ReadableStream.cpp

Issue 901013002: Revert of Introduce ExclusiveStreamReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/streams/ReadableStream.h ('k') | Source/core/streams/ReadableStream.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "bindings/core/v8/V8Binding.h" 11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/dom/DOMException.h" 12 #include "core/dom/DOMException.h"
13 #include "core/dom/ExceptionCode.h" 13 #include "core/dom/ExceptionCode.h"
14 #include "core/dom/ExecutionContext.h" 14 #include "core/dom/ExecutionContext.h"
15 #include "core/streams/ExclusiveStreamReader.h"
16 #include "core/streams/UnderlyingSource.h" 15 #include "core/streams/UnderlyingSource.h"
17 16
18 namespace blink { 17 namespace blink {
19 18
20 namespace { 19 namespace {
21 20
22 class ConstUndefined : public ScriptFunction { 21 class ConstUndefined : public ScriptFunction {
23 public: 22 public:
24 static v8::Handle<v8::Function> create(ScriptState* scriptState) 23 static v8::Handle<v8::Function> create(ScriptState* scriptState)
25 { 24 {
26 return (new ConstUndefined(scriptState))->bindToV8Function(); 25 return (new ConstUndefined(scriptState))->bindToV8Function();
27 } 26 }
28 27
29 private: 28 private:
30 explicit ConstUndefined(ScriptState* scriptState) : ScriptFunction(scriptSta te) { } 29 explicit ConstUndefined(ScriptState* scriptState) : ScriptFunction(scriptSta te) { }
31 ScriptValue call(ScriptValue value) override 30 ScriptValue call(ScriptValue value) override
32 { 31 {
33 return ScriptValue(scriptState(), v8::Undefined(scriptState()->isolate() )); 32 return ScriptValue(scriptState(), v8::Undefined(scriptState()->isolate() ));
34 } 33 }
35 }; 34 };
36 35
37 class ResolveWithReady : public ScriptFunction {
38 public:
39 static v8::Handle<v8::Function> create(ScriptState* scriptState, ReadableStr eam* stream)
40 {
41 return (new ResolveWithReady(scriptState, stream))->bindToV8Function();
42 }
43
44 void trace(Visitor* visitor)
45 {
46 visitor->trace(m_stream);
47 ScriptFunction::trace(visitor);
48 }
49
50 private:
51 ResolveWithReady(ScriptState* scriptState, ReadableStream* stream)
52 : ScriptFunction(scriptState)
53 , m_stream(stream) { }
54
55 ScriptValue call(ScriptValue value) override
56 {
57 return ScriptValue(scriptState(), m_stream->ready(scriptState()).v8Value ());
58 }
59
60 Member<ReadableStream> m_stream;
61 };
62
63 } // namespace 36 } // namespace
64 37
65 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou rce* source) 38 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou rce* source)
66 : ActiveDOMObject(executionContext) 39 : ActiveDOMObject(executionContext)
67 , m_source(source) 40 , m_source(source)
68 , m_isStarted(false) 41 , m_isStarted(false)
69 , m_isDraining(false) 42 , m_isDraining(false)
70 , m_isPulling(false) 43 , m_isPulling(false)
71 , m_state(Waiting) 44 , m_state(Waiting)
72 , m_ready(new WaitPromise(executionContext, this, WaitPromise::Ready)) 45 , m_ready(new WaitPromise(executionContext, this, WaitPromise::Ready))
73 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed)) 46 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed))
74 { 47 {
75 suspendIfNeeded(); 48 suspendIfNeeded();
76 } 49 }
77 50
78 ReadableStream::~ReadableStream() 51 ReadableStream::~ReadableStream()
79 { 52 {
80 } 53 }
81 54
82 String ReadableStream::stateString() const 55 String ReadableStream::stateString() const
83 { 56 {
84 if (m_reader) 57 switch (m_state) {
58 case Readable:
59 return "readable";
60 case Waiting:
85 return "waiting"; 61 return "waiting";
86 62 case Closed:
87 return stateToString(m_state); 63 return "closed";
64 case Errored:
65 return "errored";
66 }
67 ASSERT(false);
68 return String();
88 } 69 }
89 70
90 bool ReadableStream::enqueuePreliminaryCheck() 71 bool ReadableStream::enqueuePreliminaryCheck()
91 { 72 {
92 // This is a bit different from what spec says: it says we should throw 73 // This is a bit different from what spec says: it says we should throw
93 // an exception here. But sometimes a caller is not in any JavaScript 74 // an exception here. But sometimes a caller is not in any JavaScript
94 // context, and we don't want to throw an exception in such a case. 75 // context, and we don't want to throw an exception in such a case.
95 if (m_state == Errored || m_state == Closed || m_isDraining) 76 if (m_state == Errored || m_state == Closed || m_isDraining)
96 return false; 77 return false;
97 78
(...skipping 22 matching lines...) Expand all
120 } 101 }
121 102
122 return !shouldApplyBackpressure; 103 return !shouldApplyBackpressure;
123 } 104 }
124 105
125 void ReadableStream::close() 106 void ReadableStream::close()
126 { 107 {
127 if (m_state == Waiting) { 108 if (m_state == Waiting) {
128 m_ready->resolve(ToV8UndefinedGenerator()); 109 m_ready->resolve(ToV8UndefinedGenerator());
129 m_closed->resolve(ToV8UndefinedGenerator()); 110 m_closed->resolve(ToV8UndefinedGenerator());
130 if (m_reader)
131 m_reader->releaseLock();
132 m_state = Closed; 111 m_state = Closed;
133 } else if (m_state == Readable) { 112 } else if (m_state == Readable) {
134 m_isDraining = true; 113 m_isDraining = true;
135 } 114 }
136 } 115 }
137 116
138 void ReadableStream::readInternalPreliminaryCheck(ExceptionState& exceptionState ) 117 void ReadableStream::readPreliminaryCheck(ExceptionState& exceptionState)
139 { 118 {
140 if (m_state == Waiting) { 119 if (m_state == Waiting) {
141 exceptionState.throwTypeError("read is called while state is waiting"); 120 exceptionState.throwTypeError("read is called while state is waiting");
142 return; 121 return;
143 } 122 }
144 if (m_state == Closed) { 123 if (m_state == Closed) {
145 exceptionState.throwTypeError("read is called while state is closed"); 124 exceptionState.throwTypeError("read is called while state is closed");
146 return; 125 return;
147 } 126 }
148 if (m_state == Errored) { 127 if (m_state == Errored) {
149 exceptionState.throwDOMException(m_exception->code(), m_exception->messa ge()); 128 exceptionState.throwDOMException(m_exception->code(), m_exception->messa ge());
150 return; 129 return;
151 } 130 }
152 } 131 }
153 132
154 void ReadableStream::readInternalPostAction() 133 void ReadableStream::readPostAction()
155 { 134 {
156 ASSERT(m_state == Readable); 135 ASSERT(m_state == Readable);
157 if (isQueueEmpty()) { 136 if (isQueueEmpty()) {
158 if (m_isDraining) { 137 if (m_isDraining) {
138 m_closed->resolve(ToV8UndefinedGenerator());
159 m_state = Closed; 139 m_state = Closed;
160 m_closed->resolve(ToV8UndefinedGenerator());
161 if (m_reader)
162 m_reader->releaseLock();
163 } else { 140 } else {
164 m_ready->reset(); 141 m_ready->reset();
165 m_state = Waiting; 142 m_state = Waiting;
166 } 143 }
167 } 144 }
168 callPullIfNeeded(); 145 callPullIfNeeded();
169 } 146 }
170 147
171 ScriptValue ReadableStream::read(ScriptState* scriptState, ExceptionState& excep tionState)
172 {
173 if (m_reader) {
174 exceptionState.throwTypeError("this stream is locked to an ExclusiveStre amReader");
175 return ScriptValue();
176 }
177 return readInternal(scriptState, exceptionState);
178 }
179
180 ScriptPromise ReadableStream::ready(ScriptState* scriptState) 148 ScriptPromise ReadableStream::ready(ScriptState* scriptState)
181 { 149 {
182 if (m_reader) {
183 return m_reader->released(scriptState).then(ResolveWithReady::create(scr iptState, this));
184 }
185
186 if (m_state == Waiting) {
187 return readyInternal(scriptState).then(ResolveWithReady::create(scriptSt ate, this));
188 }
189 return readyInternal(scriptState);
190 }
191
192 ScriptPromise ReadableStream::readyInternal(ScriptState* scriptState)
193 {
194 return m_ready->promise(scriptState->world()); 150 return m_ready->promise(scriptState->world());
195 } 151 }
196 152
197 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso n) 153 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso n)
198 { 154 {
199 if (m_reader)
200 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "this stream is locked to an ExclusiveStreamReader") );
201 if (m_state == Closed) 155 if (m_state == Closed)
202 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te())); 156 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te()));
203 if (m_state == Errored) 157 if (m_state == Errored)
204 return ScriptPromise::rejectWithDOMException(scriptState, m_exception); 158 return ScriptPromise::rejectWithDOMException(scriptState, m_exception);
205 159
206 ASSERT(m_state == Readable || m_state == Waiting); 160 ASSERT(m_state == Readable || m_state == Waiting);
207 if (m_state == Waiting) 161 if (m_state == Waiting)
208 m_ready->resolve(ToV8UndefinedGenerator()); 162 m_ready->resolve(ToV8UndefinedGenerator());
209 clearQueue(); 163 clearQueue();
210 m_closed->resolve(ToV8UndefinedGenerator()); 164 m_closed->resolve(ToV8UndefinedGenerator());
211 m_state = Closed; 165 m_state = Closed;
212 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea te(scriptState)); 166 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea te(scriptState));
213 } 167 }
214 168
215 ScriptPromise ReadableStream::closed(ScriptState* scriptState) 169 ScriptPromise ReadableStream::closed(ScriptState* scriptState)
216 { 170 {
217 return m_closed->promise(scriptState->world()); 171 return m_closed->promise(scriptState->world());
218 } 172 }
219 173
220 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception) 174 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception)
221 { 175 {
222 switch (m_state) { 176 switch (m_state) {
223 case Waiting: 177 case Waiting:
224 m_exception = exception; 178 m_exception = exception;
225 m_ready->reject(m_exception); 179 m_ready->reject(m_exception);
226 m_closed->reject(m_exception); 180 m_closed->reject(m_exception);
227 m_state = Errored; 181 m_state = Errored;
228 if (m_reader)
229 m_reader->releaseLock();
230 break; 182 break;
231 case Readable: 183 case Readable:
232 clearQueue(); 184 clearQueue();
233 m_exception = exception; 185 m_exception = exception;
234 m_ready->reset(); 186 m_ready->reset();
235 m_ready->reject(m_exception); 187 m_ready->reject(m_exception);
236 m_closed->reject(m_exception); 188 m_closed->reject(m_exception);
237 m_state = Errored; 189 m_state = Errored;
238 if (m_reader)
239 m_reader->releaseLock();
240 break; 190 break;
241 default: 191 default:
242 break; 192 break;
243 } 193 }
244 } 194 }
245 195
246 void ReadableStream::didSourceStart() 196 void ReadableStream::didSourceStart()
247 { 197 {
248 m_isStarted = true; 198 m_isStarted = true;
249 callPullIfNeeded(); 199 callPullIfNeeded();
250 } 200 }
251 201
252 ExclusiveStreamReader* ReadableStream::getReader(ExceptionState& exceptionState)
253 {
254 if (m_state == Closed) {
255 exceptionState.throwTypeError("this stream is already closed");
256 return nullptr;
257 }
258 if (m_state == Errored) {
259 exceptionState.throwDOMException(m_exception->code(), m_exception->messa ge());
260 return nullptr;
261 }
262 if (m_reader) {
263 exceptionState.throwTypeError("already locked to an ExclusiveStreamReade r");
264 return nullptr;
265 }
266 return new ExclusiveStreamReader(this);
267 }
268
269 void ReadableStream::setReader(ExclusiveStreamReader* reader)
270 {
271 ASSERT((reader && !m_reader) || (!reader && m_reader));
272 m_reader = reader;
273 }
274
275 void ReadableStream::callPullIfNeeded() 202 void ReadableStream::callPullIfNeeded()
276 { 203 {
277 if (m_isPulling || m_isDraining || !m_isStarted || m_state == Closed || m_st ate == Errored) 204 if (m_isPulling || m_isDraining || !m_isStarted || m_state == Closed || m_st ate == Errored)
278 return; 205 return;
279 206
280 bool shouldApplyBackpressure = this->shouldApplyBackpressure(); 207 bool shouldApplyBackpressure = this->shouldApplyBackpressure();
281 // this->shouldApplyBackpressure may call this->error(). 208 // this->shouldApplyBackpressure may call this->error().
282 if (shouldApplyBackpressure || m_state == Errored) 209 if (shouldApplyBackpressure || m_state == Errored)
283 return; 210 return;
284 m_isPulling = true; 211 m_isPulling = true;
285 m_source->pullSource(); 212 m_source->pullSource();
286 } 213 }
287 214
288 bool ReadableStream::hasPendingActivity() const 215 bool ReadableStream::hasPendingActivity() const
289 { 216 {
290 return m_state == Waiting || m_state == Readable; 217 return m_state == Waiting || m_state == Readable;
291 } 218 }
292 219
293 void ReadableStream::trace(Visitor* visitor) 220 void ReadableStream::trace(Visitor* visitor)
294 { 221 {
295 visitor->trace(m_source); 222 visitor->trace(m_source);
296 visitor->trace(m_ready); 223 visitor->trace(m_ready);
297 visitor->trace(m_closed); 224 visitor->trace(m_closed);
298 visitor->trace(m_exception); 225 visitor->trace(m_exception);
299 visitor->trace(m_reader);
300 ActiveDOMObject::trace(visitor); 226 ActiveDOMObject::trace(visitor);
301 } 227 }
302 228
303 String ReadableStream::stateToString(State state)
304 {
305 switch (state) {
306 case Readable:
307 return "readable";
308 case Waiting:
309 return "waiting";
310 case Closed:
311 return "closed";
312 case Errored:
313 return "errored";
314 }
315 ASSERT(false);
316 return String();
317 }
318
319 } // namespace blink 229 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/streams/ReadableStream.h ('k') | Source/core/streams/ReadableStream.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698