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 "modules/fetch/Body.h" | 6 #include "modules/fetch/Body.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 | 109 |
110 // When the main thread sends a V8::TerminateExecution() signal to a worker | 110 // When the main thread sends a V8::TerminateExecution() signal to a worker |
111 // thread, any V8 API on the worker thread starts returning an empty | 111 // thread, any V8 API on the worker thread starts returning an empty |
112 // handle. This can happen in Body::readAsync. To avoid the situation, we | 112 // handle. This can happen in Body::readAsync. To avoid the situation, we |
113 // first check the ExecutionContext and return immediately if it's already | 113 // first check the ExecutionContext and return immediately if it's already |
114 // gone (which means that the V8::TerminateExecution() signal has been sent | 114 // gone (which means that the V8::TerminateExecution() signal has been sent |
115 // to this worker thread). | 115 // to this worker thread). |
116 if (!scriptState->executionContext()) | 116 if (!scriptState->executionContext()) |
117 return ScriptPromise(); | 117 return ScriptPromise(); |
118 | 118 |
119 if (m_opaque) | |
120 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); | |
121 | |
119 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 122 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
120 ScriptPromise promise = resolver->promise(); | 123 ScriptPromise promise = resolver->promise(); |
121 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); | 124 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); |
122 return promise; | 125 return promise; |
123 } | 126 } |
124 | 127 |
125 ScriptPromise Body::blob(ScriptState* scriptState) | 128 ScriptPromise Body::blob(ScriptState* scriptState) |
126 { | 129 { |
127 if (bodyUsed()) | 130 if (bodyUsed()) |
128 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); | 131 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); |
129 | 132 |
130 // See above comment. | 133 // See above comment. |
131 if (!scriptState->executionContext()) | 134 if (!scriptState->executionContext()) |
132 return ScriptPromise(); | 135 return ScriptPromise(); |
133 | 136 |
137 if (m_opaque) | |
philipj_slow
2015/09/02 12:12:48
The monkey patching says that this step should hap
Mike West
2015/09/02 13:17:42
That's fair. We should do this before the `bodyUse
| |
138 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); | |
139 | |
134 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 140 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
135 ScriptPromise promise = resolver->promise(); | 141 ScriptPromise promise = resolver->promise(); |
136 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); | 142 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); |
137 return promise; | 143 return promise; |
138 | 144 |
139 } | 145 } |
140 | 146 |
141 ScriptPromise Body::json(ScriptState* scriptState) | 147 ScriptPromise Body::json(ScriptState* scriptState) |
142 { | 148 { |
143 if (bodyUsed()) | 149 if (bodyUsed()) |
144 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); | 150 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); |
145 | 151 |
146 // See above comment. | 152 // See above comment. |
147 if (!scriptState->executionContext()) | 153 if (!scriptState->executionContext()) |
148 return ScriptPromise(); | 154 return ScriptPromise(); |
149 | 155 |
156 if (m_opaque) | |
157 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); | |
158 | |
150 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 159 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
151 ScriptPromise promise = resolver->promise(); | 160 ScriptPromise promise = resolver->promise(); |
152 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsString(), new BodyJsonConsumer(resolver)); | 161 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsString(), new BodyJsonConsumer(resolver)); |
153 return promise; | 162 return promise; |
154 } | 163 } |
155 | 164 |
156 ScriptPromise Body::text(ScriptState* scriptState) | 165 ScriptPromise Body::text(ScriptState* scriptState) |
157 { | 166 { |
158 if (bodyUsed()) | 167 if (bodyUsed()) |
159 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); | 168 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); |
160 | 169 |
161 // See above comment. | 170 // See above comment. |
162 if (!scriptState->executionContext()) | 171 if (!scriptState->executionContext()) |
163 return ScriptPromise(); | 172 return ScriptPromise(); |
164 | 173 |
174 if (m_opaque) | |
175 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "The body is opaque.")); | |
176 | |
165 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 177 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
166 ScriptPromise promise = resolver->promise(); | 178 ScriptPromise promise = resolver->promise(); |
167 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsString(), new BodyTextConsumer(resolver)); | 179 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader: :createLoaderAsString(), new BodyTextConsumer(resolver)); |
168 return promise; | 180 return promise; |
169 } | 181 } |
170 | 182 |
171 ReadableByteStream* Body::body() | 183 ReadableByteStream* Body::body() |
172 { | 184 { |
173 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); | 185 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); |
174 return bodyBuffer()->stream(); | 186 return bodyBuffer()->stream(); |
175 } | 187 } |
176 | 188 |
177 bool Body::bodyUsed() | 189 bool Body::bodyUsed() |
178 { | 190 { |
179 return m_bodyPassed || body()->isLocked(); | 191 return m_bodyPassed || body()->isLocked(); |
180 } | 192 } |
181 | 193 |
182 bool Body::hasPendingActivity() const | 194 bool Body::hasPendingActivity() const |
183 { | 195 { |
184 if (executionContext()->activeDOMObjectsAreStopped()) | 196 if (executionContext()->activeDOMObjectsAreStopped()) |
185 return false; | 197 return false; |
186 return bodyBuffer()->hasPendingActivity(); | 198 return bodyBuffer()->hasPendingActivity(); |
187 } | 199 } |
188 | 200 |
189 Body::Body(ExecutionContext* context) : ActiveDOMObject(context), m_bodyPassed(f alse) | 201 Body::Body(ExecutionContext* context) |
202 : ActiveDOMObject(context) | |
203 , m_bodyPassed(false) | |
204 , m_opaque(false) | |
190 { | 205 { |
191 suspendIfNeeded(); | 206 suspendIfNeeded(); |
192 } | 207 } |
193 | 208 |
194 } // namespace blink | 209 } // namespace blink |
OLD | NEW |