| 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 "mojo/services/network/url_loader_impl.h" | 5 #include "mojo/services/network/url_loader_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/common/common_type_converters.h" | 9 #include "mojo/common/common_type_converters.h" |
| 10 #include "mojo/services/network/network_context.h" | 10 #include "mojo/services/network/network_context.h" |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 320 |
| 321 void URLLoaderImpl::WaitToReadMore() { | 321 void URLLoaderImpl::WaitToReadMore() { |
| 322 handle_watcher_.Start(response_body_stream_.get(), | 322 handle_watcher_.Start(response_body_stream_.get(), |
| 323 MOJO_HANDLE_SIGNAL_WRITABLE, | 323 MOJO_HANDLE_SIGNAL_WRITABLE, |
| 324 MOJO_DEADLINE_INDEFINITE, | 324 MOJO_DEADLINE_INDEFINITE, |
| 325 base::Bind(&URLLoaderImpl::OnResponseBodyStreamReady, | 325 base::Bind(&URLLoaderImpl::OnResponseBodyStreamReady, |
| 326 weak_ptr_factory_.GetWeakPtr())); | 326 weak_ptr_factory_.GetWeakPtr())); |
| 327 } | 327 } |
| 328 | 328 |
| 329 void URLLoaderImpl::ReadMore() { | 329 void URLLoaderImpl::ReadMore() { |
| 330 DCHECK(!pending_write_); | 330 DCHECK(!pending_write_.get()); |
| 331 | 331 |
| 332 pending_write_ = new PendingWriteToDataPipe(response_body_stream_.Pass()); | 332 pending_write_ = new PendingWriteToDataPipe(response_body_stream_.Pass()); |
| 333 | 333 |
| 334 uint32_t num_bytes; | 334 uint32_t num_bytes; |
| 335 MojoResult result = pending_write_->BeginWrite(&num_bytes); | 335 MojoResult result = pending_write_->BeginWrite(&num_bytes); |
| 336 if (result == MOJO_RESULT_SHOULD_WAIT) { | 336 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 337 // The pipe is full. We need to wait for it to have more space. | 337 // The pipe is full. We need to wait for it to have more space. |
| 338 response_body_stream_ = pending_write_->Complete(num_bytes); | 338 response_body_stream_ = pending_write_->Complete(num_bytes); |
| 339 pending_write_ = NULL; | 339 pending_write_ = NULL; |
| 340 WaitToReadMore(); | 340 WaitToReadMore(); |
| 341 return; | 341 return; |
| 342 } | 342 } |
| 343 if (result != MOJO_RESULT_OK) { | 343 if (result != MOJO_RESULT_OK) { |
| 344 // The response body stream is in a bad state. Bail. | 344 // The response body stream is in a bad state. Bail. |
| 345 // TODO(darin): How should this be communicated to our client? | 345 // TODO(darin): How should this be communicated to our client? |
| 346 return; | 346 return; |
| 347 } | 347 } |
| 348 CHECK_GT(static_cast<uint32_t>(std::numeric_limits<int>::max()), num_bytes); | 348 CHECK_GT(static_cast<uint32_t>(std::numeric_limits<int>::max()), num_bytes); |
| 349 | 349 |
| 350 scoped_refptr<net::IOBuffer> buf = new DependentIOBuffer(pending_write_); | 350 scoped_refptr<net::IOBuffer> buf = |
| 351 new DependentIOBuffer(pending_write_.get()); |
| 351 | 352 |
| 352 int bytes_read; | 353 int bytes_read; |
| 353 url_request_->Read(buf, static_cast<int>(num_bytes), &bytes_read); | 354 url_request_->Read(buf.get(), static_cast<int>(num_bytes), &bytes_read); |
| 354 | 355 |
| 355 // Drop our reference to the buffer. | 356 // Drop our reference to the buffer. |
| 356 buf = NULL; | 357 buf = NULL; |
| 357 | 358 |
| 358 if (url_request_->status().is_io_pending()) { | 359 if (url_request_->status().is_io_pending()) { |
| 359 // Wait for OnReadCompleted. | 360 // Wait for OnReadCompleted. |
| 360 } else if (url_request_->status().is_success() && bytes_read > 0) { | 361 } else if (url_request_->status().is_success() && bytes_read > 0) { |
| 361 DidRead(static_cast<uint32_t>(bytes_read), true); | 362 DidRead(static_cast<uint32_t>(bytes_read), true); |
| 362 } else { | 363 } else { |
| 363 pending_write_->Complete(0); | 364 pending_write_->Complete(0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 374 if (completed_synchronously) { | 375 if (completed_synchronously) { |
| 375 base::MessageLoop::current()->PostTask( | 376 base::MessageLoop::current()->PostTask( |
| 376 FROM_HERE, | 377 FROM_HERE, |
| 377 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); | 378 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); |
| 378 } else { | 379 } else { |
| 379 ReadMore(); | 380 ReadMore(); |
| 380 } | 381 } |
| 381 } | 382 } |
| 382 | 383 |
| 383 } // namespace mojo | 384 } // namespace mojo |
| OLD | NEW |