| 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 13 matching lines...) Expand all Loading... |
| 24 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "net/url_request/url_request_throttler_manager.h" | 25 #include "net/url_request/url_request_throttler_manager.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const int kBufferSize = 4096; | 29 const int kBufferSize = 4096; |
| 30 const int kUploadProgressTimerInterval = 100; | 30 const int kUploadProgressTimerInterval = 100; |
| 31 bool g_interception_enabled = false; | 31 bool g_interception_enabled = false; |
| 32 bool g_ignore_certificate_requests = false; | 32 bool g_ignore_certificate_requests = false; |
| 33 | 33 |
| 34 void EmptyCompletionCallback(int result) {} |
| 35 |
| 34 } // namespace | 36 } // namespace |
| 35 | 37 |
| 36 namespace net { | 38 namespace net { |
| 37 | 39 |
| 38 // URLFetcherCore::Registry --------------------------------------------------- | 40 // URLFetcherCore::Registry --------------------------------------------------- |
| 39 | 41 |
| 40 URLFetcherCore::Registry::Registry() {} | 42 URLFetcherCore::Registry::Registry() {} |
| 41 URLFetcherCore::Registry::~Registry() {} | 43 URLFetcherCore::Registry::~Registry() {} |
| 42 | 44 |
| 43 void URLFetcherCore::Registry::AddURLFetcherCore(URLFetcherCore* core) { | 45 void URLFetcherCore::Registry::AddURLFetcherCore(URLFetcherCore* core) { |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 if (throttler_manager) { | 420 if (throttler_manager) { |
| 419 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_); | 421 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_); |
| 420 } | 422 } |
| 421 | 423 |
| 422 do { | 424 do { |
| 423 if (!request_->status().is_success() || bytes_read <= 0) | 425 if (!request_->status().is_success() || bytes_read <= 0) |
| 424 break; | 426 break; |
| 425 | 427 |
| 426 current_response_bytes_ += bytes_read; | 428 current_response_bytes_ += bytes_read; |
| 427 InformDelegateDownloadProgress(); | 429 InformDelegateDownloadProgress(); |
| 428 InformDelegateDownloadDataIfNecessary(bytes_read); | |
| 429 | 430 |
| 430 const int result = | 431 const int result = |
| 431 WriteBuffer(new DrainableIOBuffer(buffer_.get(), bytes_read)); | 432 WriteBuffer(new DrainableIOBuffer(buffer_.get(), bytes_read)); |
| 432 if (result < 0) { | 433 if (result < 0) { |
| 433 // Write failed or waiting for write completion. | 434 // Write failed or waiting for write completion. |
| 434 return; | 435 return; |
| 435 } | 436 } |
| 436 } while (request_->Read(buffer_.get(), kBufferSize, &bytes_read)); | 437 } while (request_->Read(buffer_.get(), kBufferSize, &bytes_read)); |
| 437 | 438 |
| 438 const URLRequestStatus status = request_->status(); | 439 const URLRequestStatus status = request_->status(); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 | 644 |
| 644 // Release the reference to the request context. There could be multiple | 645 // Release the reference to the request context. There could be multiple |
| 645 // references to URLFetcher::Core at this point so it may take a while to | 646 // references to URLFetcher::Core at this point so it may take a while to |
| 646 // delete the object, but we cannot delay the destruction of the request | 647 // delete the object, but we cannot delay the destruction of the request |
| 647 // context. | 648 // context. |
| 648 request_context_getter_ = NULL; | 649 request_context_getter_ = NULL; |
| 649 first_party_for_cookies_ = GURL(); | 650 first_party_for_cookies_ = GURL(); |
| 650 url_request_data_key_ = NULL; | 651 url_request_data_key_ = NULL; |
| 651 url_request_create_data_callback_.Reset(); | 652 url_request_create_data_callback_.Reset(); |
| 652 was_cancelled_ = true; | 653 was_cancelled_ = true; |
| 653 response_writer_.reset(); | |
| 654 } | 654 } |
| 655 | 655 |
| 656 void URLFetcherCore::OnCompletedURLRequest( | 656 void URLFetcherCore::OnCompletedURLRequest( |
| 657 base::TimeDelta backoff_delay) { | 657 base::TimeDelta backoff_delay) { |
| 658 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 658 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 659 | 659 |
| 660 // Save the status and backoff_delay so that delegates can read it. | 660 // Save the status and backoff_delay so that delegates can read it. |
| 661 if (delegate_) { | 661 if (delegate_) { |
| 662 backoff_delay_ = backoff_delay; | 662 backoff_delay_ = backoff_delay; |
| 663 InformDelegateFetchIsComplete(); | 663 InformDelegateFetchIsComplete(); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 is_last_chunk); | 794 is_last_chunk); |
| 795 } | 795 } |
| 796 | 796 |
| 797 int URLFetcherCore::WriteBuffer(scoped_refptr<DrainableIOBuffer> data) { | 797 int URLFetcherCore::WriteBuffer(scoped_refptr<DrainableIOBuffer> data) { |
| 798 while (data->BytesRemaining() > 0) { | 798 while (data->BytesRemaining() > 0) { |
| 799 const int result = response_writer_->Write( | 799 const int result = response_writer_->Write( |
| 800 data.get(), | 800 data.get(), |
| 801 data->BytesRemaining(), | 801 data->BytesRemaining(), |
| 802 base::Bind(&URLFetcherCore::DidWriteBuffer, this, data)); | 802 base::Bind(&URLFetcherCore::DidWriteBuffer, this, data)); |
| 803 if (result < 0) { | 803 if (result < 0) { |
| 804 if (result != ERR_IO_PENDING) { | 804 if (result != ERR_IO_PENDING) |
| 805 CancelURLRequest(result); | 805 DidWriteBuffer(data, result); |
| 806 delegate_task_runner_->PostTask( | |
| 807 FROM_HERE, | |
| 808 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, this)); | |
| 809 } | |
| 810 return result; | 806 return result; |
| 811 } | 807 } |
| 812 data->DidConsume(result); | 808 data->DidConsume(result); |
| 813 } | 809 } |
| 814 return OK; | 810 return OK; |
| 815 } | 811 } |
| 816 | 812 |
| 817 void URLFetcherCore::DidWriteBuffer(scoped_refptr<DrainableIOBuffer> data, | 813 void URLFetcherCore::DidWriteBuffer(scoped_refptr<DrainableIOBuffer> data, |
| 818 int result) { | 814 int result) { |
| 819 if (result < 0) { // Handle errors. | 815 if (result < 0) { // Handle errors. |
| 820 CancelURLRequest(result); | 816 CancelURLRequest(result); |
| 817 response_writer_->Finish(base::Bind(&EmptyCompletionCallback)); |
| 821 delegate_task_runner_->PostTask( | 818 delegate_task_runner_->PostTask( |
| 822 FROM_HERE, | 819 FROM_HERE, |
| 823 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, this)); | 820 base::Bind(&URLFetcherCore::InformDelegateFetchIsComplete, this)); |
| 824 return; | 821 return; |
| 825 } | 822 } |
| 826 | 823 |
| 827 // Continue writing. | 824 // Continue writing. |
| 828 data->DidConsume(result); | 825 data->DidConsume(result); |
| 829 if (WriteBuffer(data) < 0) | 826 if (WriteBuffer(data) < 0) |
| 830 return; | 827 return; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 885 this, current_response_bytes_, total_response_bytes_)); | 882 this, current_response_bytes_, total_response_bytes_)); |
| 886 } | 883 } |
| 887 | 884 |
| 888 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( | 885 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( |
| 889 int64 current, int64 total) { | 886 int64 current, int64 total) { |
| 890 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 887 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 891 if (delegate_) | 888 if (delegate_) |
| 892 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); | 889 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); |
| 893 } | 890 } |
| 894 | 891 |
| 895 void URLFetcherCore::InformDelegateDownloadDataIfNecessary(int bytes_read) { | |
| 896 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 897 if (delegate_ && delegate_->ShouldSendDownloadData()) { | |
| 898 scoped_ptr<std::string> download_data( | |
| 899 new std::string(buffer_->data(), bytes_read)); | |
| 900 delegate_task_runner_->PostTask( | |
| 901 FROM_HERE, | |
| 902 base::Bind( | |
| 903 &URLFetcherCore::InformDelegateDownloadDataInDelegateThread, | |
| 904 this, base::Passed(&download_data))); | |
| 905 } | |
| 906 } | |
| 907 | |
| 908 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( | |
| 909 scoped_ptr<std::string> download_data) { | |
| 910 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | |
| 911 if (delegate_) | |
| 912 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); | |
| 913 } | |
| 914 | |
| 915 } // namespace net | 892 } // namespace net |
| OLD | NEW |