| 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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // When the main thread sends a V8::TerminateExecution() signal to a worker | 266 // When the main thread sends a V8::TerminateExecution() signal to a worker |
| 267 // thread, any V8 API on the worker thread starts returning an empty | 267 // thread, any V8 API on the worker thread starts returning an empty |
| 268 // handle. This can happen in Body::readAsync. To avoid the situation, we | 268 // handle. This can happen in Body::readAsync. To avoid the situation, we |
| 269 // first check the ExecutionContext and return immediately if it's already | 269 // first check the ExecutionContext and return immediately if it's already |
| 270 // gone (which means that the V8::TerminateExecution() signal has been sent | 270 // gone (which means that the V8::TerminateExecution() signal has been sent |
| 271 // to this worker thread). | 271 // to this worker thread). |
| 272 ExecutionContext* executionContext = scriptState->executionContext(); | 272 ExecutionContext* executionContext = scriptState->executionContext(); |
| 273 if (!executionContext) | 273 if (!executionContext) |
| 274 return ScriptPromise(); | 274 return ScriptPromise(); |
| 275 | 275 |
| 276 setBodyUsed(); | 276 lockBody(PassBody); |
| 277 m_responseType = type; | 277 m_responseType = type; |
| 278 | 278 |
| 279 ASSERT(!m_resolver); | 279 ASSERT(!m_resolver); |
| 280 m_resolver = ScriptPromiseResolver::create(scriptState); | 280 m_resolver = ScriptPromiseResolver::create(scriptState); |
| 281 ScriptPromise promise = m_resolver->promise(); | 281 ScriptPromise promise = m_resolver->promise(); |
| 282 | 282 |
| 283 if (streamAccessed()) { | 283 if (streamAccessed()) { |
| 284 m_streamSource->createDrainingStream()->readAllAndCreateBlobHandle(mimeT
ype(), new BlobHandleReceiver(this)); | 284 m_streamSource->createDrainingStream()->readAllAndCreateBlobHandle(mimeT
ype(), new BlobHandleReceiver(this)); |
| 285 } else if (buffer()) { | 285 } else if (buffer()) { |
| 286 buffer()->readAllAndCreateBlobHandle(mimeType(), new BlobHandleReceiver(
this)); | 286 buffer()->readAllAndCreateBlobHandle(mimeType(), new BlobHandleReceiver(
this)); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 { | 366 { |
| 367 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); | 367 UseCounter::count(executionContext(), UseCounter::FetchBodyStream); |
| 368 return m_stream; | 368 return m_stream; |
| 369 } | 369 } |
| 370 | 370 |
| 371 bool Body::bodyUsed() const | 371 bool Body::bodyUsed() const |
| 372 { | 372 { |
| 373 return m_bodyUsed || m_stream->isLocked(); | 373 return m_bodyUsed || m_stream->isLocked(); |
| 374 } | 374 } |
| 375 | 375 |
| 376 void Body::setBodyUsed() | 376 void Body::lockBody(LockBodyOption option) |
| 377 { | 377 { |
| 378 ASSERT(!m_bodyUsed); | 378 ASSERT(!bodyUsed()); |
| 379 if (option == PassBody) |
| 380 m_bodyUsed = true; |
| 379 ASSERT(!m_stream->isLocked()); | 381 ASSERT(!m_stream->isLocked()); |
| 380 TrackExceptionState exceptionState; | 382 TrackExceptionState exceptionState; |
| 381 m_streamReader = m_stream->getBytesReader(executionContext(), exceptionState
); | 383 m_stream->getBytesReader(executionContext(), exceptionState); |
| 382 ASSERT(!exceptionState.hadException()); | 384 ASSERT(!exceptionState.hadException()); |
| 383 m_bodyUsed = true; | |
| 384 } | 385 } |
| 385 | 386 |
| 386 bool Body::streamAccessed() const | 387 bool Body::streamAccessed() const |
| 387 { | 388 { |
| 388 return m_streamSource->state() != m_streamSource->Initial; | 389 return m_streamSource->state() != m_streamSource->Initial; |
| 389 } | 390 } |
| 390 | 391 |
| 392 void Body::refreshBody() |
| 393 { |
| 394 m_streamSource = new ReadableStreamSource(this); |
| 395 m_stream = new ReadableByteStream(m_streamSource, new ReadableByteStream::St
rictStrategy); |
| 396 m_streamSource->startStream(m_stream); |
| 397 } |
| 398 |
| 391 BodyStreamBuffer* Body::createDrainingStream() | 399 BodyStreamBuffer* Body::createDrainingStream() |
| 392 { | 400 { |
| 393 return m_streamSource->createDrainingStream(); | 401 return m_streamSource->createDrainingStream(); |
| 394 } | 402 } |
| 395 | 403 |
| 396 void Body::stop() | 404 void Body::stop() |
| 397 { | 405 { |
| 398 // Canceling the load will call didFail which will remove the resolver. | 406 // Canceling the load will call didFail which will remove the resolver. |
| 399 if (m_loader) | 407 if (m_loader) |
| 400 m_loader->cancel(); | 408 m_loader->cancel(); |
| 401 } | 409 } |
| 402 | 410 |
| 403 bool Body::hasPendingActivity() const | 411 bool Body::hasPendingActivity() const |
| 404 { | 412 { |
| 405 if (executionContext()->activeDOMObjectsAreStopped()) | 413 if (executionContext()->activeDOMObjectsAreStopped()) |
| 406 return false; | 414 return false; |
| 407 if (m_resolver) | 415 if (m_resolver) |
| 408 return true; | 416 return true; |
| 409 if (m_stream->isLocked()) | 417 if (m_stream->isLocked()) |
| 410 return true; | 418 return true; |
| 411 return false; | 419 return false; |
| 412 } | 420 } |
| 413 | 421 |
| 414 DEFINE_TRACE(Body) | 422 DEFINE_TRACE(Body) |
| 415 { | 423 { |
| 416 visitor->trace(m_resolver); | 424 visitor->trace(m_resolver); |
| 417 visitor->trace(m_stream); | 425 visitor->trace(m_stream); |
| 418 visitor->trace(m_streamSource); | 426 visitor->trace(m_streamSource); |
| 419 visitor->trace(m_streamReader); | |
| 420 ActiveDOMObject::trace(visitor); | 427 ActiveDOMObject::trace(visitor); |
| 421 } | 428 } |
| 422 | 429 |
| 423 Body::Body(ExecutionContext* context) | 430 Body::Body(ExecutionContext* context) |
| 424 : ActiveDOMObject(context) | 431 : ActiveDOMObject(context) |
| 425 , m_bodyUsed(false) | 432 , m_bodyUsed(false) |
| 426 , m_responseType(ResponseType::ResponseUnknown) | 433 , m_responseType(ResponseType::ResponseUnknown) |
| 427 , m_streamSource(new ReadableStreamSource(this)) | 434 , m_streamSource(new ReadableStreamSource(this)) |
| 428 , m_stream(new ReadableByteStream(m_streamSource, new ReadableByteStream::St
rictStrategy)) | 435 , m_stream(new ReadableByteStream(m_streamSource, new ReadableByteStream::St
rictStrategy)) |
| 429 { | 436 { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> except
ion) | 505 void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> except
ion) |
| 499 { | 506 { |
| 500 if (!m_resolver) | 507 if (!m_resolver) |
| 501 return; | 508 return; |
| 502 m_stream->error(DOMException::create(NetworkError, "network error")); | 509 m_stream->error(DOMException::create(NetworkError, "network error")); |
| 503 m_resolver->reject(exception); | 510 m_resolver->reject(exception); |
| 504 m_resolver.clear(); | 511 m_resolver.clear(); |
| 505 } | 512 } |
| 506 | 513 |
| 507 } // namespace blink | 514 } // namespace blink |
| OLD | NEW |