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" |
11 #include "bindings/core/v8/V8ArrayBuffer.h" | 11 #include "bindings/core/v8/V8ArrayBuffer.h" |
12 #include "bindings/core/v8/V8ThrowException.h" | 12 #include "bindings/core/v8/V8ThrowException.h" |
13 #include "core/dom/DOMArrayBuffer.h" | 13 #include "core/dom/DOMArrayBuffer.h" |
14 #include "core/fileapi/Blob.h" | 14 #include "core/fileapi/Blob.h" |
15 #include "core/fileapi/FileReaderLoader.h" | 15 #include "core/fileapi/FileReaderLoader.h" |
16 #include "core/fileapi/FileReaderLoaderClient.h" | 16 #include "core/fileapi/FileReaderLoaderClient.h" |
17 #include "core/frame/UseCounter.h" | 17 #include "core/frame/UseCounter.h" |
18 #include "core/streams/ExclusiveStreamReader.h" | |
19 #include "core/streams/UnderlyingSource.h" | 18 #include "core/streams/UnderlyingSource.h" |
20 #include "modules/fetch/BodyStreamBuffer.h" | 19 #include "modules/fetch/BodyStreamBuffer.h" |
21 | 20 |
22 namespace blink { | 21 namespace blink { |
23 | 22 |
24 class Body::BlobHandleReceiver final : public BodyStreamBuffer::BlobHandleCreato
rClient { | 23 class Body::BlobHandleReceiver final : public BodyStreamBuffer::BlobHandleCreato
rClient { |
25 public: | 24 public: |
26 explicit BlobHandleReceiver(Body* body) | 25 explicit BlobHandleReceiver(Body* body) |
27 : m_body(body) | 26 : m_body(body) |
28 { | 27 { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // Created when createDrainingStream is called to drain the data. | 226 // Created when createDrainingStream is called to drain the data. |
228 Member<BodyStreamBuffer> m_drainingStreamBuffer; | 227 Member<BodyStreamBuffer> m_drainingStreamBuffer; |
229 Member<ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArrayBuffer>>> m_
stream; | 228 Member<ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArrayBuffer>>> m_
stream; |
230 State m_state; | 229 State m_state; |
231 // The count of the chunks which were enqueued to the ReadableStream. | 230 // The count of the chunks which were enqueued to the ReadableStream. |
232 size_t m_queueCount; | 231 size_t m_queueCount; |
233 }; | 232 }; |
234 | 233 |
235 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) | 234 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) |
236 { | 235 { |
237 if (bodyUsed()) | 236 if (m_bodyUsed) |
238 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); | 237 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Already read")); |
239 | 238 |
240 // When the main thread sends a V8::TerminateExecution() signal to a worker | 239 // When the main thread sends a V8::TerminateExecution() signal to a worker |
241 // thread, any V8 API on the worker thread starts returning an empty | 240 // thread, any V8 API on the worker thread starts returning an empty |
242 // handle. This can happen in Body::readAsync. To avoid the situation, we | 241 // handle. This can happen in Body::readAsync. To avoid the situation, we |
243 // first check the ExecutionContext and return immediately if it's already | 242 // first check the ExecutionContext and return immediately if it's already |
244 // gone (which means that the V8::TerminateExecution() signal has been sent | 243 // gone (which means that the V8::TerminateExecution() signal has been sent |
245 // to this worker thread). | 244 // to this worker thread). |
246 ExecutionContext* executionContext = scriptState->executionContext(); | 245 ExecutionContext* executionContext = scriptState->executionContext(); |
247 if (!executionContext) | 246 if (!executionContext) |
248 return ScriptPromise(); | 247 return ScriptPromise(); |
249 | 248 |
250 setBodyUsed(); | 249 m_bodyUsed = true; |
251 m_responseType = type; | 250 m_responseType = type; |
252 | 251 |
253 ASSERT(!m_resolver); | 252 ASSERT(!m_resolver); |
254 m_resolver = ScriptPromiseResolver::create(scriptState); | 253 m_resolver = ScriptPromiseResolver::create(scriptState); |
255 ScriptPromise promise = m_resolver->promise(); | 254 ScriptPromise promise = m_resolver->promise(); |
256 | 255 |
257 if (m_stream) { | 256 if (m_stream) { |
258 ASSERT(m_streamSource); | 257 ASSERT(m_streamSource); |
259 bool dataLost; | 258 bool dataLost; |
260 m_streamSource->createDrainingStream(&dataLost)->readAllAndCreateBlobHan
dle(contentTypeForBuffer(), new BlobHandleReceiver(this)); | 259 m_streamSource->createDrainingStream(&dataLost)->readAllAndCreateBlobHan
dle(contentTypeForBuffer(), new BlobHandleReceiver(this)); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 ASSERT(!m_streamSource); | 339 ASSERT(!m_streamSource); |
341 m_streamSource = new ReadableStreamSource(this); | 340 m_streamSource = new ReadableStreamSource(this); |
342 m_stream = new ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArray
Buffer>>(executionContext(), m_streamSource); | 341 m_stream = new ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArray
Buffer>>(executionContext(), m_streamSource); |
343 m_streamSource->startStream(m_stream); | 342 m_streamSource->startStream(m_stream); |
344 } | 343 } |
345 return m_stream; | 344 return m_stream; |
346 } | 345 } |
347 | 346 |
348 bool Body::bodyUsed() const | 347 bool Body::bodyUsed() const |
349 { | 348 { |
350 return m_bodyUsed || (m_stream && m_stream->isLocked()); | 349 return m_bodyUsed; |
351 } | 350 } |
352 | 351 |
353 void Body::setBodyUsed() | 352 void Body::setBodyUsed() |
354 { | 353 { |
355 ASSERT(!m_bodyUsed); | |
356 ASSERT(!m_stream || !m_stream->isLocked()); | |
357 // Note that technically we can set BodyUsed even when the stream is | |
358 // closed or errored, but getReader doesn't work then. | |
359 if (m_stream && m_stream->stateInternal() != ReadableStream::Closed && m_str
eam->stateInternal() != ReadableStream::Errored) { | |
360 TrackExceptionState exceptionState; | |
361 m_streamReader = m_stream->getReader(exceptionState); | |
362 ASSERT(!exceptionState.hadException()); | |
363 } | |
364 m_bodyUsed = true; | 354 m_bodyUsed = true; |
365 } | 355 } |
366 | 356 |
367 bool Body::streamAccessed() const | 357 bool Body::streamAccessed() const |
368 { | 358 { |
369 return m_stream; | 359 return m_stream; |
370 } | 360 } |
371 | 361 |
372 BodyStreamBuffer* Body::createDrainingStream(bool* dataLost) | 362 BodyStreamBuffer* Body::createDrainingStream(bool* dataLost) |
373 { | 363 { |
(...skipping 18 matching lines...) Expand all Loading... |
392 if (m_stream && m_stream->hasPendingActivity()) | 382 if (m_stream && m_stream->hasPendingActivity()) |
393 return true; | 383 return true; |
394 return false; | 384 return false; |
395 } | 385 } |
396 | 386 |
397 void Body::trace(Visitor* visitor) | 387 void Body::trace(Visitor* visitor) |
398 { | 388 { |
399 visitor->trace(m_resolver); | 389 visitor->trace(m_resolver); |
400 visitor->trace(m_stream); | 390 visitor->trace(m_stream); |
401 visitor->trace(m_streamSource); | 391 visitor->trace(m_streamSource); |
402 visitor->trace(m_streamReader); | |
403 ActiveDOMObject::trace(visitor); | 392 ActiveDOMObject::trace(visitor); |
404 } | 393 } |
405 | 394 |
406 Body::Body(ExecutionContext* context) | 395 Body::Body(ExecutionContext* context) |
407 : ActiveDOMObject(context) | 396 : ActiveDOMObject(context) |
408 , m_bodyUsed(false) | 397 , m_bodyUsed(false) |
409 , m_responseType(ResponseType::ResponseUnknown) | 398 , m_responseType(ResponseType::ResponseUnknown) |
410 { | 399 { |
411 } | 400 } |
412 | 401 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 | 474 |
486 void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> except
ion) | 475 void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> except
ion) |
487 { | 476 { |
488 if (!m_resolver) | 477 if (!m_resolver) |
489 return; | 478 return; |
490 m_resolver->reject(exception); | 479 m_resolver->reject(exception); |
491 m_resolver.clear(); | 480 m_resolver.clear(); |
492 } | 481 } |
493 | 482 |
494 } // namespace blink | 483 } // namespace blink |
OLD | NEW |