OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_fetcher_core.h" | 5 #include "net/url_request/url_fetcher_core.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 424 |
425 bool waiting_on_write = false; | 425 bool waiting_on_write = false; |
426 do { | 426 do { |
427 if (!request_->status().is_success() || bytes_read <= 0) | 427 if (!request_->status().is_success() || bytes_read <= 0) |
428 break; | 428 break; |
429 | 429 |
430 current_response_bytes_ += bytes_read; | 430 current_response_bytes_ += bytes_read; |
431 InformDelegateDownloadProgress(); | 431 InformDelegateDownloadProgress(); |
432 InformDelegateDownloadDataIfNecessary(bytes_read); | 432 InformDelegateDownloadDataIfNecessary(bytes_read); |
433 | 433 |
434 const int result = response_writer_->Write( | 434 const int result = WriteBuffer(new DrainableIOBuffer(buffer_, bytes_read)); |
435 buffer_, bytes_read, | |
436 base::Bind(&URLFetcherCore::DidWriteBuffer, this)); | |
437 if (result < 0) { | 435 if (result < 0) { |
438 // Write failed or waiting for write completion. | 436 // Write failed or waiting for write completion. |
439 if (result == ERR_IO_PENDING) | 437 if (result == ERR_IO_PENDING) |
440 waiting_on_write = true; | 438 waiting_on_write = true; |
441 break; | 439 break; |
442 } | 440 } |
443 } while (request_->Read(buffer_, kBufferSize, &bytes_read)); | 441 } while (request_->Read(buffer_, kBufferSize, &bytes_read)); |
444 | 442 |
445 const URLRequestStatus status = request_->status(); | 443 const URLRequestStatus status = request_->status(); |
446 | 444 |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 return; | 813 return; |
816 } | 814 } |
817 DCHECK(is_chunked_upload_); | 815 DCHECK(is_chunked_upload_); |
818 DCHECK(request_.get()); | 816 DCHECK(request_.get()); |
819 DCHECK(!content.empty()); | 817 DCHECK(!content.empty()); |
820 request_->AppendChunkToUpload(content.data(), | 818 request_->AppendChunkToUpload(content.data(), |
821 static_cast<int>(content.length()), | 819 static_cast<int>(content.length()), |
822 is_last_chunk); | 820 is_last_chunk); |
823 } | 821 } |
824 | 822 |
825 void URLFetcherCore::DidWriteBuffer(int result) { | 823 int URLFetcherCore::WriteBuffer(scoped_refptr<DrainableIOBuffer> data) { |
826 if (result < 0) { | 824 while (data->BytesRemaining() > 0) { |
| 825 const int result = response_writer_->Write( |
| 826 data, data->BytesRemaining(), |
| 827 base::Bind(&URLFetcherCore::DidWriteBuffer, this, data)); |
| 828 if (result < 0) |
| 829 return result; |
| 830 data->DidConsume(result); |
| 831 } |
| 832 return OK; |
| 833 } |
| 834 |
| 835 void URLFetcherCore::DidWriteBuffer(scoped_refptr<DrainableIOBuffer> data, |
| 836 int result) { |
| 837 if (result >= 0) { // Continue writing. |
| 838 data->DidConsume(result); |
| 839 result = WriteBuffer(data); |
| 840 if (result == ERR_IO_PENDING) |
| 841 return; |
| 842 } |
| 843 |
| 844 if (result < 0) { // Handle errors. |
827 delegate_task_runner_->PostTask( | 845 delegate_task_runner_->PostTask( |
828 FROM_HERE, | 846 FROM_HERE, |
829 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, this)); | 847 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, this)); |
830 return; | 848 return; |
831 } | 849 } |
832 // Finished writing buffer_. Read some more. | 850 // Finished writing buffer_. Read some more. |
| 851 DCHECK_EQ(0, data->BytesRemaining()); |
833 ReadResponse(); | 852 ReadResponse(); |
834 } | 853 } |
835 | 854 |
836 void URLFetcherCore::ReadResponse() { | 855 void URLFetcherCore::ReadResponse() { |
837 // Some servers may treat HEAD requests as GET requests. To free up the | 856 // Some servers may treat HEAD requests as GET requests. To free up the |
838 // network connection as soon as possible, signal that the request has | 857 // network connection as soon as possible, signal that the request has |
839 // completed immediately, without trying to read any data back (all we care | 858 // completed immediately, without trying to read any data back (all we care |
840 // about is the response code and headers, which we already have). | 859 // about is the response code and headers, which we already have). |
841 int bytes_read = 0; | 860 int bytes_read = 0; |
842 if (request_->status().is_success() && | 861 if (request_->status().is_success() && |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 } | 929 } |
911 | 930 |
912 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( | 931 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( |
913 scoped_ptr<std::string> download_data) { | 932 scoped_ptr<std::string> download_data) { |
914 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 933 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
915 if (delegate_) | 934 if (delegate_) |
916 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); | 935 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); |
917 } | 936 } |
918 | 937 |
919 } // namespace net | 938 } // namespace net |
OLD | NEW |