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 "modules/fetch/Body.h" | 5 #include "modules/fetch/Body.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
9 #include "bindings/core/v8/V8ArrayBuffer.h" | 10 #include "bindings/core/v8/V8ArrayBuffer.h" |
10 #include "bindings/core/v8/V8ThrowException.h" | 11 #include "bindings/core/v8/V8ThrowException.h" |
11 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
12 #include "core/dom/DOMTypedArray.h" | 13 #include "core/dom/DOMTypedArray.h" |
13 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
14 #include "core/fileapi/Blob.h" | 15 #include "core/fileapi/Blob.h" |
15 #include "core/frame/UseCounter.h" | 16 #include "core/frame/UseCounter.h" |
| 17 #include "core/streams/ReadableStreamController.h" |
| 18 #include "core/streams/ReadableStreamOperations.h" |
| 19 #include "core/streams/UnderlyingSourceBase.h" |
16 #include "modules/fetch/BodyStreamBuffer.h" | 20 #include "modules/fetch/BodyStreamBuffer.h" |
17 #include "modules/fetch/FetchDataLoader.h" | 21 #include "modules/fetch/FetchDataLoader.h" |
18 #include "public/platform/WebDataConsumerHandle.h" | 22 #include "public/platform/WebDataConsumerHandle.h" |
19 #include "wtf/OwnPtr.h" | 23 #include "wtf/OwnPtr.h" |
20 #include "wtf/PassRefPtr.h" | 24 #include "wtf/PassRefPtr.h" |
21 #include "wtf/RefPtr.h" | 25 #include "wtf/RefPtr.h" |
22 | 26 |
23 namespace blink { | 27 namespace blink { |
24 | 28 |
25 namespace { | 29 namespace { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 v8::Local<v8::String> inputString = v8String(isolate, string); | 97 v8::Local<v8::String> inputString = v8String(isolate, string); |
94 v8::TryCatch trycatch(isolate); | 98 v8::TryCatch trycatch(isolate); |
95 v8::Local<v8::Value> parsed; | 99 v8::Local<v8::Value> parsed; |
96 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) | 100 if (v8Call(v8::JSON::Parse(isolate, inputString), parsed, trycatch)) |
97 resolver()->resolve(parsed); | 101 resolver()->resolve(parsed); |
98 else | 102 else |
99 resolver()->reject(trycatch.Exception()); | 103 resolver()->reject(trycatch.Exception()); |
100 } | 104 } |
101 }; | 105 }; |
102 | 106 |
| 107 class UnderlyingSourceFromDataConsumerHandle final : public UnderlyingSourceBase
, public WebDataConsumerHandle::Client { |
| 108 EAGERLY_FINALIZE(); |
| 109 DECLARE_EAGER_FINALIZATION_OPERATOR_NEW(); |
| 110 public: |
| 111 UnderlyingSourceFromDataConsumerHandle(ScriptState* scriptState, PassOwnPtr<
WebDataConsumerHandle> handle) |
| 112 : UnderlyingSourceBase(scriptState) |
| 113 , m_scriptState(scriptState) |
| 114 , m_reader(handle->obtainReader(this)) |
| 115 { |
| 116 } |
| 117 |
| 118 ScriptPromise pull(ScriptState* scriptState) override |
| 119 { |
| 120 didGetReadable(); |
| 121 return ScriptPromise::castUndefined(scriptState); |
| 122 } |
| 123 |
| 124 ScriptPromise cancel(ScriptState* scriptState, ScriptValue reason) override |
| 125 { |
| 126 m_reader = nullptr; |
| 127 return ScriptPromise::castUndefined(scriptState); |
| 128 } |
| 129 |
| 130 void didGetReadable() override |
| 131 { |
| 132 while (controller()->desiredSize() > 0) { |
| 133 size_t available = 0; |
| 134 const void* buffer = nullptr; |
| 135 WebDataConsumerHandle::Result result = m_reader->beginRead(&buffer,
WebDataConsumerHandle::FlagNone, &available); |
| 136 if (result == WebDataConsumerHandle::Ok) { |
| 137 RefPtr<ArrayBuffer> arrayBuffer = ArrayBuffer::create(available,
1); |
| 138 memcpy(arrayBuffer->data(), buffer, available); |
| 139 m_reader->endRead(available); |
| 140 controller()->enqueue(DOMUint8Array::create(arrayBuffer.release(
), 0, available)); |
| 141 } else if (result == WebDataConsumerHandle::ShouldWait) { |
| 142 break; |
| 143 } else if (result == WebDataConsumerHandle::Done) { |
| 144 m_reader = nullptr; |
| 145 controller()->close(); |
| 146 break; |
| 147 } else { |
| 148 m_reader = nullptr; |
| 149 controller()->error(V8ThrowException::createTypeError(m_scriptSt
ate->isolate(), "Network error")); |
| 150 break; |
| 151 } |
| 152 } |
| 153 } |
| 154 |
| 155 private: |
| 156 RefPtr<ScriptState> m_scriptState; |
| 157 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| 158 }; |
| 159 |
103 } // namespace | 160 } // namespace |
104 | 161 |
105 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) | 162 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) |
106 { | 163 { |
107 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 164 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
108 if (!promise.isEmpty()) | 165 if (!promise.isEmpty()) |
109 return promise; | 166 return promise; |
110 | 167 |
111 // When the main thread sends a V8::TerminateExecution() signal to a worker | 168 // When the main thread sends a V8::TerminateExecution() signal to a worker |
112 // thread, any V8 API on the worker thread starts returning an empty | 169 // thread, any V8 API on the worker thread starts returning an empty |
113 // handle. This can happen in this function. To avoid the situation, we | 170 // handle. This can happen in this function. To avoid the situation, we |
114 // first check the ExecutionContext and return immediately if it's already | 171 // first check the ExecutionContext and return immediately if it's already |
115 // gone (which means that the V8::TerminateExecution() signal has been sent | 172 // gone (which means that the V8::TerminateExecution() signal has been sent |
116 // to this worker thread). | 173 // to this worker thread). |
117 if (!scriptState->getExecutionContext()) | 174 if (!scriptState->getExecutionContext()) |
118 return ScriptPromise(); | 175 return ScriptPromise(); |
119 | 176 |
120 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 177 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
121 promise = resolver->promise(); | 178 promise = resolver->promise(); |
122 if (bodyBuffer()) { | 179 if (bodyBuffer()) { |
123 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsArrayBuffer(),
new BodyArrayBufferConsumer(resolver)); | 180 bodyBuffer()->startLoading(scriptState->getExecutionContext(), FetchData
Loader::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); |
124 } else { | 181 } else { |
125 resolver->resolve(DOMArrayBuffer::create(0u, 1)); | 182 resolver->resolve(DOMArrayBuffer::create(0u, 1)); |
126 } | 183 } |
127 return promise; | 184 return promise; |
128 } | 185 } |
129 | 186 |
130 ScriptPromise Body::blob(ScriptState* scriptState) | 187 ScriptPromise Body::blob(ScriptState* scriptState) |
131 { | 188 { |
132 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 189 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
133 if (!promise.isEmpty()) | 190 if (!promise.isEmpty()) |
134 return promise; | 191 return promise; |
135 | 192 |
136 // See above comment. | 193 // See above comment. |
137 if (!scriptState->getExecutionContext()) | 194 if (!scriptState->getExecutionContext()) |
138 return ScriptPromise(); | 195 return ScriptPromise(); |
139 | 196 |
140 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 197 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
141 promise = resolver->promise(); | 198 promise = resolver->promise(); |
142 if (bodyBuffer()) { | 199 if (bodyBuffer()) { |
143 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsBlobHandle(mim
eType()), new BodyBlobConsumer(resolver)); | 200 bodyBuffer()->startLoading(scriptState->getExecutionContext(), FetchData
Loader::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); |
144 } else { | 201 } else { |
145 OwnPtr<BlobData> blobData = BlobData::create(); | 202 OwnPtr<BlobData> blobData = BlobData::create(); |
146 blobData->setContentType(mimeType()); | 203 blobData->setContentType(mimeType()); |
147 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); | 204 resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release()
, 0))); |
148 } | 205 } |
149 return promise; | 206 return promise; |
150 | 207 |
151 } | 208 } |
152 | 209 |
153 ScriptPromise Body::json(ScriptState* scriptState) | 210 ScriptPromise Body::json(ScriptState* scriptState) |
154 { | 211 { |
155 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 212 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
156 if (!promise.isEmpty()) | 213 if (!promise.isEmpty()) |
157 return promise; | 214 return promise; |
158 | 215 |
159 // See above comment. | 216 // See above comment. |
160 if (!scriptState->getExecutionContext()) | 217 if (!scriptState->getExecutionContext()) |
161 return ScriptPromise(); | 218 return ScriptPromise(); |
162 | 219 |
163 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 220 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
164 promise = resolver->promise(); | 221 promise = resolver->promise(); |
165 if (bodyBuffer()) { | 222 if (bodyBuffer()) { |
166 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsString(), new
BodyJsonConsumer(resolver)); | 223 bodyBuffer()->startLoading(scriptState->getExecutionContext(), FetchData
Loader::createLoaderAsString(), new BodyJsonConsumer(resolver)); |
167 } else { | 224 } else { |
168 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); | 225 resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolat
e(), "Unexpected end of input")); |
169 } | 226 } |
170 return promise; | 227 return promise; |
171 } | 228 } |
172 | 229 |
173 ScriptPromise Body::text(ScriptState* scriptState) | 230 ScriptPromise Body::text(ScriptState* scriptState) |
174 { | 231 { |
175 ScriptPromise promise = rejectInvalidConsumption(scriptState); | 232 ScriptPromise promise = rejectInvalidConsumption(scriptState); |
176 if (!promise.isEmpty()) | 233 if (!promise.isEmpty()) |
177 return promise; | 234 return promise; |
178 | 235 |
179 // See above comment. | 236 // See above comment. |
180 if (!scriptState->getExecutionContext()) | 237 if (!scriptState->getExecutionContext()) |
181 return ScriptPromise(); | 238 return ScriptPromise(); |
182 | 239 |
183 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 240 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
184 promise = resolver->promise(); | 241 promise = resolver->promise(); |
185 if (bodyBuffer()) { | 242 if (bodyBuffer()) { |
186 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsString(), new
BodyTextConsumer(resolver)); | 243 bodyBuffer()->startLoading(scriptState->getExecutionContext(), FetchData
Loader::createLoaderAsString(), new BodyTextConsumer(resolver)); |
187 } else { | 244 } else { |
188 resolver->resolve(String()); | 245 resolver->resolve(String()); |
189 } | 246 } |
190 return promise; | 247 return promise; |
191 } | 248 } |
192 | 249 |
193 ScriptValue Body::bodyWithUseCounter(ScriptState* scriptState) | 250 ReadableByteStream* Body::body() |
| 251 { |
| 252 return bodyBuffer() ? bodyBuffer()->stream() : nullptr; |
| 253 } |
| 254 |
| 255 ReadableByteStream* Body::bodyWithUseCounter() |
194 { | 256 { |
195 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); | 257 UseCounter::count(getExecutionContext(), UseCounter::FetchBodyStream); |
196 if (!bodyBuffer()) | 258 return body(); |
197 return ScriptValue::createNull(scriptState); | 259 } |
198 ScriptValue stream = bodyBuffer()->stream(); | 260 |
199 ASSERT(stream.getScriptState() == scriptState); | 261 ScriptValue Body::v8ExtraStreamBody(ScriptState* scriptState) |
200 return stream; | 262 { |
| 263 if (bodyUsed() || isBodyLocked() || !bodyBuffer()) |
| 264 return ScriptValue(scriptState, v8::Undefined(scriptState->isolate())); |
| 265 OwnPtr<FetchDataConsumerHandle> handle = bodyBuffer()->releaseHandle(scriptS
tate->getExecutionContext()); |
| 266 return ReadableStreamOperations::createReadableStream(scriptState, |
| 267 new UnderlyingSourceFromDataConsumerHandle(scriptState, handle.release()
), |
| 268 ScriptValue(scriptState, v8::Undefined(scriptState->isolate()))); |
201 } | 269 } |
202 | 270 |
203 bool Body::bodyUsed() | 271 bool Body::bodyUsed() |
204 { | 272 { |
205 return bodyBuffer() && bodyBuffer()->isStreamDisturbed(); | 273 return bodyBuffer() && bodyBuffer()->isStreamDisturbed(); |
206 } | 274 } |
207 | 275 |
208 bool Body::isBodyLocked() | 276 bool Body::isBodyLocked() |
209 { | 277 { |
210 return bodyBuffer() && bodyBuffer()->isStreamLocked(); | 278 return bodyBuffer() && bodyBuffer()->isStreamLocked(); |
(...skipping 16 matching lines...) Expand all Loading... |
227 } | 295 } |
228 | 296 |
229 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) | 297 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) |
230 { | 298 { |
231 if (isBodyLocked() || bodyUsed()) | 299 if (isBodyLocked() || bodyUsed()) |
232 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 300 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
233 return ScriptPromise(); | 301 return ScriptPromise(); |
234 } | 302 } |
235 | 303 |
236 } // namespace blink | 304 } // namespace blink |
OLD | NEW |