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

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

Issue 712043002: Make ReadableStream implementation up to date. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month 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/ReadableStreamTest.cpp » ('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/UnderlyingSource.h" 15 #include "core/streams/UnderlyingSource.h"
16 16
17 namespace blink { 17 namespace blink {
18 18
19 namespace {
20
21 class ConstUndefined : public ScriptFunction {
22 public:
23 static v8::Handle<v8::Function> create(ScriptState* scriptState)
24 {
25 return (new ConstUndefined(scriptState))->bindToV8Function();
26 }
27
28 private:
29 explicit ConstUndefined(ScriptState* scriptState) : ScriptFunction(scriptSta te) { }
30 ScriptValue call(ScriptValue value) override
31 {
32 return ScriptValue(scriptState(), v8::Undefined(scriptState()->isolate() ));
33 }
34 };
35
36 } // namespace
37
19 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou rce* source) 38 ReadableStream::ReadableStream(ExecutionContext* executionContext, UnderlyingSou rce* source)
20 : m_source(source) 39 : m_source(source)
21 , m_isStarted(false) 40 , m_isStarted(false)
22 , m_isDraining(false) 41 , m_isDraining(false)
23 , m_isPulling(false) 42 , m_isPulling(false)
24 , m_isSchedulingPull(false)
25 , m_state(Waiting) 43 , m_state(Waiting)
26 , m_wait(new WaitPromise(executionContext, this, WaitPromise::Ready)) 44 , m_wait(new WaitPromise(executionContext, this, WaitPromise::Ready))
27 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed)) 45 , m_closed(new ClosedPromise(executionContext, this, ClosedPromise::Closed))
28 { 46 {
29 } 47 }
30 48
31 ReadableStream::~ReadableStream() 49 ReadableStream::~ReadableStream()
32 { 50 {
33 } 51 }
34 52
35 String ReadableStream::stateString() const 53 String ReadableStream::stateString() const
36 { 54 {
37 switch (m_state) { 55 switch (m_state) {
38 case Readable: 56 case Readable:
39 return "readable"; 57 return "readable";
40 case Waiting: 58 case Waiting:
41 return "waiting"; 59 return "waiting";
42 case Closed: 60 case Closed:
43 return "closed"; 61 return "closed";
44 case Errored: 62 case Errored:
45 return "errored"; 63 return "errored";
46 } 64 }
47 ASSERT(false); 65 ASSERT(false);
48 return String(); 66 return String();
49 } 67 }
50 68
51 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize) 69 bool ReadableStream::enqueuePreliminaryCheck(size_t chunkSize)
52 { 70 {
71 // This is a bit different from what spec says: it says we should throw
72 // an exception here. But sometimes a caller is not in any JavaScript
73 // context, and we don't want to throw an exception in such a case.
53 if (m_state == Errored || m_state == Closed || m_isDraining) 74 if (m_state == Errored || m_state == Closed || m_isDraining)
54 return false; 75 return false;
55 76
56 // FIXME: Query strategy.
57 return true; 77 return true;
58 } 78 }
59 79
60 bool ReadableStream::enqueuePostAction(size_t totalQueueSize) 80 bool ReadableStream::enqueuePostAction(size_t totalQueueSize)
61 { 81 {
62 m_isPulling = false; 82 m_isPulling = false;
63 83
64 // FIXME: Set needsMore correctly. 84 // FIXME: Set |shouldApplyBackpressure| correctly.
65 bool needsMore = true; 85 bool shouldApplyBackpressure = false;
66 86
67 if (m_state == Waiting) { 87 if (m_state == Waiting) {
68 m_state = Readable; 88 m_state = Readable;
69 m_wait->resolve(V8UndefinedType()); 89 m_wait->resolve(V8UndefinedType());
70 } 90 }
71 91
72 return needsMore; 92 return !shouldApplyBackpressure;
73 } 93 }
74 94
75 void ReadableStream::close() 95 void ReadableStream::close()
76 { 96 {
77 if (m_state == Waiting) { 97 if (m_state == Waiting) {
78 m_wait->resolve(V8UndefinedType()); 98 m_wait->resolve(V8UndefinedType());
79 m_closed->resolve(V8UndefinedType()); 99 m_closed->resolve(V8UndefinedType());
80 m_state = Closed; 100 m_state = Closed;
81 } else if (m_state == Readable) { 101 } else if (m_state == Readable) {
82 m_isDraining = true; 102 m_isDraining = true;
(...skipping 15 matching lines...) Expand all
98 return; 118 return;
99 } 119 }
100 } 120 }
101 121
102 void ReadableStream::readPostAction() 122 void ReadableStream::readPostAction()
103 { 123 {
104 ASSERT(m_state == Readable); 124 ASSERT(m_state == Readable);
105 if (isQueueEmpty()) { 125 if (isQueueEmpty()) {
106 if (m_isDraining) { 126 if (m_isDraining) {
107 m_state = Closed; 127 m_state = Closed;
108 m_wait->reset();
109 m_wait->resolve(V8UndefinedType());
110 m_closed->resolve(V8UndefinedType()); 128 m_closed->resolve(V8UndefinedType());
111 } else { 129 } else {
112 m_state = Waiting; 130 m_state = Waiting;
113 m_wait->reset(); 131 m_wait->reset();
114 callOrSchedulePull();
115 } 132 }
116 } 133 }
134 callPullIfNeeded();
117 } 135 }
118 136
119 ScriptPromise ReadableStream::wait(ScriptState* scriptState) 137 ScriptPromise ReadableStream::wait(ScriptState* scriptState)
120 { 138 {
121 if (m_state == Waiting)
122 callOrSchedulePull();
123 return m_wait->promise(scriptState->world()); 139 return m_wait->promise(scriptState->world());
124 } 140 }
125 141
126 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso n) 142 ScriptPromise ReadableStream::cancel(ScriptState* scriptState, ScriptValue reaso n)
127 { 143 {
128 if (m_state == Errored) {
129 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(s criptState);
130 ScriptPromise promise = resolver->promise();
131 resolver->reject(m_exception);
132 return promise;
133 }
134 if (m_state == Closed) 144 if (m_state == Closed)
135 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te())); 145 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te()));
146 if (m_state == Errored)
147 return ScriptPromise::rejectWithDOMException(scriptState, m_exception);
136 148
137 if (m_state == Waiting) { 149 ASSERT(m_state == Readable || m_state == Waiting);
150 if (m_state == Waiting)
138 m_wait->resolve(V8UndefinedType()); 151 m_wait->resolve(V8UndefinedType());
139 } else {
140 ASSERT(m_state == Readable);
141 m_wait->reset();
142 m_wait->resolve(V8UndefinedType());
143 }
144
145 clearQueue(); 152 clearQueue();
146 m_state = Closed; 153 m_state = Closed;
147 m_closed->resolve(V8UndefinedType()); 154 m_closed->resolve(V8UndefinedType());
148 return m_source->cancelSource(scriptState, reason); 155 return m_source->cancelSource(scriptState, reason).then(ConstUndefined::crea te(scriptState));
149 } 156 }
150 157
151 ScriptPromise ReadableStream::closed(ScriptState* scriptState) 158 ScriptPromise ReadableStream::closed(ScriptState* scriptState)
152 { 159 {
153 return m_closed->promise(scriptState->world()); 160 return m_closed->promise(scriptState->world());
154 } 161 }
155 162
156 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception) 163 void ReadableStream::error(PassRefPtrWillBeRawPtr<DOMException> exception)
157 { 164 {
158 if (m_state == Readable) { 165 switch (m_state) {
159 clearQueue(); 166 case Waiting:
160 m_wait->reset();
161 }
162
163 if (m_state == Waiting || m_state == Readable) {
164 m_state = Errored; 167 m_state = Errored;
165 m_exception = exception; 168 m_exception = exception;
166 if (m_wait->state() == m_wait->Pending) 169 m_wait->reject(m_exception);
167 m_wait->reject(m_exception);
168 m_closed->reject(m_exception); 170 m_closed->reject(m_exception);
171 break;
172 case Readable:
173 clearQueue();
174 m_state = Errored;
175 m_exception = exception;
176 m_wait->reset();
177 m_wait->reject(m_exception);
178 m_closed->reject(m_exception);
179 break;
180 default:
181 break;
169 } 182 }
170 } 183 }
171 184
172 void ReadableStream::didSourceStart() 185 void ReadableStream::didSourceStart()
173 { 186 {
174 m_isStarted = true; 187 m_isStarted = true;
175 if (m_isSchedulingPull) 188 callPullIfNeeded();
176 m_source->pullSource();
177 } 189 }
178 190
179 void ReadableStream::callOrSchedulePull() 191 void ReadableStream::callPullIfNeeded()
180 { 192 {
181 if (m_isPulling) 193 if (m_isPulling || m_isDraining || !m_isStarted || m_state == Closed || m_st ate == Errored)
194 return;
195 // FIXME: Set shouldApplyBackpressure correctly.
196 bool shouldApplyBackpressure = false;
197 if (shouldApplyBackpressure)
182 return; 198 return;
183 m_isPulling = true; 199 m_isPulling = true;
184 if (m_isStarted) 200 m_source->pullSource();
185 m_source->pullSource();
186 else
187 m_isSchedulingPull = true;
188 } 201 }
189 202
190 void ReadableStream::trace(Visitor* visitor) 203 void ReadableStream::trace(Visitor* visitor)
191 { 204 {
192 visitor->trace(m_source); 205 visitor->trace(m_source);
193 visitor->trace(m_wait); 206 visitor->trace(m_wait);
194 visitor->trace(m_closed); 207 visitor->trace(m_closed);
195 visitor->trace(m_exception); 208 visitor->trace(m_exception);
196 } 209 }
197 210
198 } // namespace blink 211 } // namespace blink
199 212
OLDNEW
« no previous file with comments | « Source/core/streams/ReadableStream.h ('k') | Source/core/streams/ReadableStreamTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698