| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 resolver()->resolve(parsed); | 97 resolver()->resolve(parsed); |
| 98 else | 98 else |
| 99 resolver()->reject(trycatch.Exception()); | 99 resolver()->reject(trycatch.Exception()); |
| 100 } | 100 } |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 } // namespace | 103 } // namespace |
| 104 | 104 |
| 105 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) | 105 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) |
| 106 { | 106 { |
| 107 if (bodyUsed()) | 107 if (isBodyLocked() || bodyUsed()) |
| 108 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 108 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 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 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 119 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 120 ScriptPromise promise = resolver->promise(); | 120 ScriptPromise promise = resolver->promise(); |
| 121 if (bodyBuffer()) { | 121 if (bodyBuffer()) { |
| 122 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); | 122 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); |
| 123 } else { | 123 } else { |
| 124 resolver->resolve(DOMArrayBuffer::create(0u, 1)); | 124 resolver->resolve(DOMArrayBuffer::create(0u, 1)); |
| 125 } | 125 } |
| 126 return promise; | 126 return promise; |
| 127 } | 127 } |
| 128 | 128 |
| 129 ScriptPromise Body::blob(ScriptState* scriptState) | 129 ScriptPromise Body::blob(ScriptState* scriptState) |
| 130 { | 130 { |
| 131 if (bodyUsed()) | 131 if (isBodyLocked() || bodyUsed()) |
| 132 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 132 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 133 | 133 |
| 134 // See above comment. | 134 // See above comment. |
| 135 if (!scriptState->executionContext()) | 135 if (!scriptState->executionContext()) |
| 136 return ScriptPromise(); | 136 return ScriptPromise(); |
| 137 | 137 |
| 138 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 138 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 139 ScriptPromise promise = resolver->promise(); | 139 ScriptPromise promise = resolver->promise(); |
| 140 if (bodyBuffer()) { | 140 if (bodyBuffer()) { |
| 141 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); | 141 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); |
| 142 } else { | 142 } else { |
| 143 OwnPtr<BlobData> blobData = BlobData::create(); | 143 OwnPtr<BlobData> blobData = BlobData::create(); |
| 144 blobData->setContentType(mimeType()); | 144 blobData->setContentType(mimeType()); |
| 145 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); | 145 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); |
| 146 } | 146 } |
| 147 return promise; | 147 return promise; |
| 148 | 148 |
| 149 } | 149 } |
| 150 | 150 |
| 151 ScriptPromise Body::json(ScriptState* scriptState) | 151 ScriptPromise Body::json(ScriptState* scriptState) |
| 152 { | 152 { |
| 153 if (bodyUsed()) | 153 if (isBodyLocked() || bodyUsed()) |
| 154 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 154 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 155 | 155 |
| 156 // See above comment. | 156 // See above comment. |
| 157 if (!scriptState->executionContext()) | 157 if (!scriptState->executionContext()) |
| 158 return ScriptPromise(); | 158 return ScriptPromise(); |
| 159 | 159 |
| 160 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 160 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 161 ScriptPromise promise = resolver->promise(); | 161 ScriptPromise promise = resolver->promise(); |
| 162 if (bodyBuffer()) { | 162 if (bodyBuffer()) { |
| 163 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyJsonConsumer(resolver)); | 163 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyJsonConsumer(resolver)); |
| 164 } else { | 164 } else { |
| 165 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); | 165 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); |
| 166 } | 166 } |
| 167 return promise; | 167 return promise; |
| 168 } | 168 } |
| 169 | 169 |
| 170 ScriptPromise Body::text(ScriptState* scriptState) | 170 ScriptPromise Body::text(ScriptState* scriptState) |
| 171 { | 171 { |
| 172 if (bodyUsed()) | 172 if (isBodyLocked() || bodyUsed()) |
| 173 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 173 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 174 | 174 |
| 175 // See above comment. | 175 // See above comment. |
| 176 if (!scriptState->executionContext()) | 176 if (!scriptState->executionContext()) |
| 177 return ScriptPromise(); | 177 return ScriptPromise(); |
| 178 | 178 |
| 179 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 179 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 180 ScriptPromise promise = resolver->promise(); | 180 ScriptPromise promise = resolver->promise(); |
| 181 if (bodyBuffer()) { | 181 if (bodyBuffer()) { |
| 182 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyTextConsumer(resolver)); | 182 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyTextConsumer(resolver)); |
| 183 } else { | 183 } else { |
| 184 resolver->resolve(String()); | 184 resolver->resolve(String()); |
| 185 } | 185 } |
| 186 return promise; | 186 return promise; |
| 187 } | 187 } |
| 188 | 188 |
| 189 ReadableByteStream* Body::body() | 189 ReadableByteStream* Body::body() |
| 190 { | 190 { |
| 191 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; |
| 192 } |
| 193 |
| 194 ReadableByteStream* Body::bodyWithUseCounter() |
| 195 { |
| 191 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); | 196 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); |
| 192 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; | 197 return body(); |
| 193 } | 198 } |
| 194 | 199 |
| 195 bool Body::bodyUsed() | 200 bool Body::bodyUsed() |
| 196 { | 201 { |
| 197 return m_bodyPassed || (body() && body()->isLocked()); | 202 return body() && body()->isDisturbed(); |
| 203 } |
| 204 |
| 205 bool Body::isBodyLocked() |
| 206 { |
| 207 return body() && body()->isLocked(); |
| 198 } | 208 } |
| 199 | 209 |
| 200 bool Body::hasPendingActivity() const | 210 bool Body::hasPendingActivity() const |
| 201 { | 211 { |
| 202 if (executionContext()->activeDOMObjectsAreStopped()) | 212 if (executionContext()->activeDOMObjectsAreStopped()) |
| 203 return false; | 213 return false; |
| 204 if (!bodyBuffer()) | 214 if (!bodyBuffer()) |
| 205 return false; | 215 return false; |
| 206 return bodyBuffer()->hasPendingActivity(); | 216 return bodyBuffer()->hasPendingActivity(); |
| 207 } | 217 } |
| 208 | 218 |
| 209 Body::Body(ExecutionContext* context) : ActiveDOMObject(context), m_bodyPassed(f
alse) | 219 Body::Body(ExecutionContext* context) : ActiveDOMObject(context) |
| 210 { | 220 { |
| 211 suspendIfNeeded(); | 221 suspendIfNeeded(); |
| 212 } | 222 } |
| 213 | 223 |
| 214 } // namespace blink | 224 } // namespace blink |
| OLD | NEW |