| 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 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 108 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 108 if (!promise.isEmpty()) |
| 109 return promise; |
| 109 | 110 |
| 110 // When the main thread sends a V8::TerminateExecution() signal to a worker | 111 // 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 | 112 // 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 | 113 // handle. This can happen in Body::readAsync. To avoid the situation, we |
| 113 // first check the ExecutionContext and return immediately if it's already | 114 // first check the ExecutionContext and return immediately if it's already |
| 114 // gone (which means that the V8::TerminateExecution() signal has been sent | 115 // gone (which means that the V8::TerminateExecution() signal has been sent |
| 115 // to this worker thread). | 116 // to this worker thread). |
| 116 if (!scriptState->executionContext()) | 117 if (!scriptState->executionContext()) |
| 117 return ScriptPromise(); | 118 return ScriptPromise(); |
| 118 | 119 |
| 119 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 120 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 120 ScriptPromise promise = resolver->promise(); | 121 promise = resolver->promise(); |
| 121 if (bodyBuffer()) { | 122 if (bodyBuffer()) { |
| 122 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); | 123 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); |
| 123 } else { | 124 } else { |
| 124 resolver->resolve(DOMArrayBuffer::create(0u, 1)); | 125 resolver->resolve(DOMArrayBuffer::create(0u, 1)); |
| 125 } | 126 } |
| 126 return promise; | 127 return promise; |
| 127 } | 128 } |
| 128 | 129 |
| 129 ScriptPromise Body::blob(ScriptState* scriptState) | 130 ScriptPromise Body::blob(ScriptState* scriptState) |
| 130 { | 131 { |
| 131 if (bodyUsed()) | 132 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 132 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 133 if (!promise.isEmpty()) |
| 134 return promise; |
| 133 | 135 |
| 134 // See above comment. | 136 // See above comment. |
| 135 if (!scriptState->executionContext()) | 137 if (!scriptState->executionContext()) |
| 136 return ScriptPromise(); | 138 return ScriptPromise(); |
| 137 | 139 |
| 138 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 140 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 139 ScriptPromise promise = resolver->promise(); | 141 promise = resolver->promise(); |
| 140 if (bodyBuffer()) { | 142 if (bodyBuffer()) { |
| 141 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); | 143 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); |
| 142 } else { | 144 } else { |
| 143 OwnPtr<BlobData> blobData = BlobData::create(); | 145 OwnPtr<BlobData> blobData = BlobData::create(); |
| 144 blobData->setContentType(mimeType()); | 146 blobData->setContentType(mimeType()); |
| 145 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); | 147 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); |
| 146 } | 148 } |
| 147 return promise; | 149 return promise; |
| 148 | 150 |
| 149 } | 151 } |
| 150 | 152 |
| 151 ScriptPromise Body::json(ScriptState* scriptState) | 153 ScriptPromise Body::json(ScriptState* scriptState) |
| 152 { | 154 { |
| 153 if (bodyUsed()) | 155 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 154 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 156 if (!promise.isEmpty()) |
| 157 return promise; |
| 155 | 158 |
| 156 // See above comment. | 159 // See above comment. |
| 157 if (!scriptState->executionContext()) | 160 if (!scriptState->executionContext()) |
| 158 return ScriptPromise(); | 161 return ScriptPromise(); |
| 159 | 162 |
| 160 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 163 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 161 ScriptPromise promise = resolver->promise(); | 164 promise = resolver->promise(); |
| 162 if (bodyBuffer()) { | 165 if (bodyBuffer()) { |
| 163 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyJsonConsumer(resolver)); | 166 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyJsonConsumer(resolver)); |
| 164 } else { | 167 } else { |
| 165 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); | 168 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); |
| 166 } | 169 } |
| 167 return promise; | 170 return promise; |
| 168 } | 171 } |
| 169 | 172 |
| 170 ScriptPromise Body::text(ScriptState* scriptState) | 173 ScriptPromise Body::text(ScriptState* scriptState) |
| 171 { | 174 { |
| 172 if (bodyUsed()) | 175 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
| 173 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 176 if (!promise.isEmpty()) |
| 177 return promise; |
| 174 | 178 |
| 175 // See above comment. | 179 // See above comment. |
| 176 if (!scriptState->executionContext()) | 180 if (!scriptState->executionContext()) |
| 177 return ScriptPromise(); | 181 return ScriptPromise(); |
| 178 | 182 |
| 179 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 183 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 180 ScriptPromise promise = resolver->promise(); | 184 promise = resolver->promise(); |
| 181 if (bodyBuffer()) { | 185 if (bodyBuffer()) { |
| 182 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyTextConsumer(resolver)); | 186 bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoa
der::createLoaderAsString(), new BodyTextConsumer(resolver)); |
| 183 } else { | 187 } else { |
| 184 resolver->resolve(String()); | 188 resolver->resolve(String()); |
| 185 } | 189 } |
| 186 return promise; | 190 return promise; |
| 187 } | 191 } |
| 188 | 192 |
| 189 ReadableByteStream* Body::body() | 193 ReadableByteStream* Body::body() |
| 190 { | 194 { |
| 191 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); | 195 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); |
| 192 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; | 196 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; |
| 193 } | 197 } |
| 194 | 198 |
| 195 bool Body::bodyUsed() | 199 bool Body::bodyUsed() |
| 196 { | 200 { |
| 197 return m_bodyPassed || (body() && body()->isLocked()); | 201 return m_bodyPassed || (body() && body()->isLocked()); |
| 198 } | 202 } |
| 199 | 203 |
| 200 bool Body::hasPendingActivity() const | 204 bool Body::hasPendingActivity() const |
| 201 { | 205 { |
| 202 if (executionContext()->activeDOMObjectsAreStopped()) | 206 if (executionContext()->activeDOMObjectsAreStopped()) |
| 203 return false; | 207 return false; |
| 204 if (!bodyBuffer()) | 208 if (!bodyBuffer()) |
| 205 return false; | 209 return false; |
| 206 return bodyBuffer()->hasPendingActivity(); | 210 return bodyBuffer()->hasPendingActivity(); |
| 207 } | 211 } |
| 208 | 212 |
| 209 Body::Body(ExecutionContext* context) : ActiveDOMObject(context), m_bodyPassed(f
alse) | 213 Body::Body(ExecutionContext* context) : ActiveDOMObject(context), m_bodyPassed(f
alse), m_opaque(false) |
| 210 { | 214 { |
| 211 suspendIfNeeded(); | 215 suspendIfNeeded(); |
| 212 } | 216 } |
| 213 | 217 |
| 218 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) |
| 219 { |
| 220 if (m_opaque) |
| 221 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "The body is opaque.")); |
| 222 if (bodyUsed()) |
| 223 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
| 224 return ScriptPromise(); |
| 225 } |
| 226 |
| 214 } // namespace blink | 227 } // namespace blink |
| OLD | NEW |