| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/net/url_fetcher.h" | 5 #include "chrome/common/net/url_fetcher.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // us. If our caller hasn't had time to fully construct us and take a | 51 // us. If our caller hasn't had time to fully construct us and take a |
| 52 // reference, the IO thread could interrupt things, run a task, Release() | 52 // reference, the IO thread could interrupt things, run a task, Release() |
| 53 // us, and destroy us, leaving the caller with an already-destroyed object | 53 // us, and destroy us, leaving the caller with an already-destroyed object |
| 54 // when construction finishes. | 54 // when construction finishes. |
| 55 void Start(); | 55 void Start(); |
| 56 | 56 |
| 57 // Stops any in-progress load and ensures no callback will happen. It is | 57 // Stops any in-progress load and ensures no callback will happen. It is |
| 58 // safe to call this multiple times. | 58 // safe to call this multiple times. |
| 59 void Stop(); | 59 void Stop(); |
| 60 | 60 |
| 61 // Reports that the received content was malformed. | 61 // Reports that the received content was malformed (i.e. failed parsing |
| 62 // or validation). This makes the throttling logic that does exponential |
| 63 // back-off when servers are having problems treat the current request as |
| 64 // a failure. Your call to this method will be ignored if your request is |
| 65 // already considered a failure based on the HTTP response code or response |
| 66 // headers. |
| 62 void ReceivedContentWasMalformed(); | 67 void ReceivedContentWasMalformed(); |
| 63 | 68 |
| 64 // Overridden from net::URLRequest::Delegate: | 69 // Overridden from net::URLRequest::Delegate: |
| 65 virtual void OnResponseStarted(net::URLRequest* request); | 70 virtual void OnResponseStarted(net::URLRequest* request); |
| 66 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); | 71 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read); |
| 67 | 72 |
| 68 URLFetcher::Delegate* delegate() const { return delegate_; } | 73 URLFetcher::Delegate* delegate() const { return delegate_; } |
| 69 static void CancelAll(); | 74 static void CancelAll(); |
| 70 | 75 |
| 71 private: | 76 private: |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 status_ = status; | 803 status_ = status; |
| 799 | 804 |
| 800 // Checks the response from server. | 805 // Checks the response from server. |
| 801 if (response_code_ >= 500 || | 806 if (response_code_ >= 500 || |
| 802 status.os_error() == net::ERR_TEMPORARILY_THROTTLED) { | 807 status.os_error() == net::ERR_TEMPORARILY_THROTTLED) { |
| 803 // When encountering a server error, we will send the request again | 808 // When encountering a server error, we will send the request again |
| 804 // after backoff time. | 809 // after backoff time. |
| 805 ++num_retries_; | 810 ++num_retries_; |
| 806 // Restarts the request if we still need to notify the delegate. | 811 // Restarts the request if we still need to notify the delegate. |
| 807 if (delegate_) { | 812 if (delegate_) { |
| 813 // Note that backoff_delay_ may be 0 because (a) the URLRequestThrottler |
| 814 // code does not necessarily back off on the first error, and (b) it |
| 815 // only backs off on some of the 5xx status codes. |
| 808 fetcher_->backoff_delay_ = backoff_release_time_ - base::TimeTicks::Now(); | 816 fetcher_->backoff_delay_ = backoff_release_time_ - base::TimeTicks::Now(); |
| 809 if (fetcher_->backoff_delay_ < base::TimeDelta()) | 817 if (fetcher_->backoff_delay_ < base::TimeDelta()) |
| 810 fetcher_->backoff_delay_ = base::TimeDelta(); | 818 fetcher_->backoff_delay_ = base::TimeDelta(); |
| 811 | 819 |
| 812 if (fetcher_->automatically_retry_on_5xx_ && | 820 if (fetcher_->automatically_retry_on_5xx_ && |
| 813 num_retries_ <= fetcher_->max_retries()) { | 821 num_retries_ <= fetcher_->max_retries()) { |
| 814 io_message_loop_proxy_->PostTask( | 822 io_message_loop_proxy_->PostTask( |
| 815 FROM_HERE, | 823 FROM_HERE, |
| 816 NewRunnableMethod(this, &Core::StartURLRequestWhenAppropriate)); | 824 NewRunnableMethod(this, &Core::StartURLRequestWhenAppropriate)); |
| 817 } else { | 825 } else { |
| 818 InformDelegateFetchIsComplete(); | 826 InformDelegateFetchIsComplete(); |
| 819 } | 827 } |
| 820 } | 828 } |
| 821 } else { | 829 } else { |
| 822 if (delegate_) { | 830 if (delegate_) { |
| 823 fetcher_->backoff_delay_ = base::TimeDelta(); | 831 fetcher_->backoff_delay_ = base::TimeDelta(); |
| 824 InformDelegateFetchIsComplete(); | 832 InformDelegateFetchIsComplete(); |
| 825 } | 833 } |
| 826 } | 834 } |
| 827 } | 835 } |
| 828 | 836 |
| 829 void URLFetcher::Core::InformDelegateFetchIsComplete() { | 837 void URLFetcher::Core::InformDelegateFetchIsComplete() { |
| 830 delegate_->OnURLFetchComplete(fetcher_); | 838 delegate_->OnURLFetchComplete(fetcher_); |
| 831 } | 839 } |
| 832 | 840 |
| 833 void URLFetcher::Core::NotifyMalformedContent() { | 841 void URLFetcher::Core::NotifyMalformedContent() { |
| 834 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 842 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 835 if (url_throttler_entry_ != NULL) | 843 if (url_throttler_entry_ != NULL) { |
| 836 url_throttler_entry_->ReceivedContentWasMalformed(); | 844 int status_code = response_code_; |
| 845 if (status_code == kInvalidHttpResponseCode) { |
| 846 // The status code will generally be known by the time clients |
| 847 // call the |ReceivedContentWasMalformed()| function (which ends up |
| 848 // calling the current function) but if it's not, we need to assume |
| 849 // the response was successful so that the total failure count |
| 850 // used to calculate exponential back-off goes up. |
| 851 status_code = 200; |
| 852 } |
| 853 url_throttler_entry_->ReceivedContentWasMalformed(status_code); |
| 854 } |
| 837 } | 855 } |
| 838 | 856 |
| 839 void URLFetcher::Core::ReleaseRequest() { | 857 void URLFetcher::Core::ReleaseRequest() { |
| 840 request_.reset(); | 858 request_.reset(); |
| 841 g_registry.Get().RemoveURLFetcherCore(this); | 859 g_registry.Get().RemoveURLFetcherCore(this); |
| 842 } | 860 } |
| 843 | 861 |
| 844 base::TimeTicks URLFetcher::Core::GetBackoffReleaseTime() { | 862 base::TimeTicks URLFetcher::Core::GetBackoffReleaseTime() { |
| 845 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 863 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 846 DCHECK(original_url_throttler_entry_ != NULL); | 864 DCHECK(original_url_throttler_entry_ != NULL); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 } | 1033 } |
| 1016 | 1034 |
| 1017 // static | 1035 // static |
| 1018 int URLFetcher::GetNumFetcherCores() { | 1036 int URLFetcher::GetNumFetcherCores() { |
| 1019 return Core::g_registry.Get().size(); | 1037 return Core::g_registry.Get().size(); |
| 1020 } | 1038 } |
| 1021 | 1039 |
| 1022 URLFetcher::Delegate* URLFetcher::delegate() const { | 1040 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 1023 return core_->delegate(); | 1041 return core_->delegate(); |
| 1024 } | 1042 } |
| OLD | NEW |