| 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 "content/common/net/url_fetcher_core.h" | 5 #include "content/common/net/url_fetcher_core.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util_proxy.h" | 8 #include "base/file_util_proxy.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
| 14 #include "content/public/common/url_fetcher_delegate.h" | 14 #include "content/public/common/url_fetcher_delegate.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 19 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "net/url_request/url_request_throttler_manager.h" | 21 #include "net/url_request/url_request_throttler_manager.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 static const int kBufferSize = 4096; | 25 static const int kBufferSize = 4096; |
| 25 static const int kUploadProgressTimerInterval = 100; | 26 static const int kUploadProgressTimerInterval = 100; |
| 26 | 27 |
| 27 URLFetcherCore::Registry::Registry() {} | 28 URLFetcherCore::Registry::Registry() {} |
| 28 URLFetcherCore::Registry::~Registry() {} | 29 URLFetcherCore::Registry::~Registry() {} |
| (...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 } | 600 } |
| 600 return write_complete; | 601 return write_complete; |
| 601 } | 602 } |
| 602 | 603 |
| 603 void URLFetcherCore::OnReadCompleted(net::URLRequest* request, | 604 void URLFetcherCore::OnReadCompleted(net::URLRequest* request, |
| 604 int bytes_read) { | 605 int bytes_read) { |
| 605 DCHECK(request == request_); | 606 DCHECK(request == request_); |
| 606 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 607 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 607 | 608 |
| 608 url_ = request->url(); | 609 url_ = request->url(); |
| 609 url_throttler_entry_ = | 610 net::URLRequestThrottlerManager* throttler_manager = |
| 610 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl(url_); | 611 request->context()->throttler_manager(); |
| 612 if (throttler_manager) { |
| 613 url_throttler_entry_ = throttler_manager->RegisterRequestUrl(url_); |
| 614 } |
| 611 | 615 |
| 612 bool waiting_on_write = false; | 616 bool waiting_on_write = false; |
| 613 do { | 617 do { |
| 614 if (!request_->status().is_success() || bytes_read <= 0) | 618 if (!request_->status().is_success() || bytes_read <= 0) |
| 615 break; | 619 break; |
| 616 | 620 |
| 617 current_response_bytes_ += bytes_read; | 621 current_response_bytes_ += bytes_read; |
| 618 InformDelegateDownloadProgress(); | 622 InformDelegateDownloadProgress(); |
| 619 InformDelegateDownloadDataIfNecessary(bytes_read); | 623 InformDelegateDownloadDataIfNecessary(bytes_read); |
| 620 | 624 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 657 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 654 base::TimeDelta backoff_delay; | 658 base::TimeDelta backoff_delay; |
| 655 | 659 |
| 656 // Checks the response from server. | 660 // Checks the response from server. |
| 657 if (response_code_ >= 500 || | 661 if (response_code_ >= 500 || |
| 658 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { | 662 status_.error() == net::ERR_TEMPORARILY_THROTTLED) { |
| 659 // When encountering a server error, we will send the request again | 663 // When encountering a server error, we will send the request again |
| 660 // after backoff time. | 664 // after backoff time. |
| 661 ++num_retries_; | 665 ++num_retries_; |
| 662 | 666 |
| 663 // Note that backoff_delay may be 0 because (a) the URLRequestThrottler | 667 // Note that backoff_delay may be 0 because (a) the |
| 664 // code does not necessarily back off on the first error, and (b) it | 668 // URLRequestThrottlerManager and related code does not |
| 665 // only backs off on some of the 5xx status codes. | 669 // necessarily back off on the first error, (b) it only backs off |
| 670 // on some of the 5xx status codes, (c) not all URLRequestContexts |
| 671 // have a throttler manager. |
| 666 base::TimeTicks backoff_release_time = GetBackoffReleaseTime(); | 672 base::TimeTicks backoff_release_time = GetBackoffReleaseTime(); |
| 667 backoff_delay = backoff_release_time - base::TimeTicks::Now(); | 673 backoff_delay = backoff_release_time - base::TimeTicks::Now(); |
| 668 if (backoff_delay < base::TimeDelta()) | 674 if (backoff_delay < base::TimeDelta()) |
| 669 backoff_delay = base::TimeDelta(); | 675 backoff_delay = base::TimeDelta(); |
| 670 | 676 |
| 671 if (automatically_retry_on_5xx_ && num_retries_ <= max_retries_) { | 677 if (automatically_retry_on_5xx_ && num_retries_ <= max_retries_) { |
| 672 StartOnIOThread(); | 678 StartOnIOThread(); |
| 673 return; | 679 return; |
| 674 } | 680 } |
| 675 } else { | 681 } else { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 | 793 |
| 788 request_->Start(); | 794 request_->Start(); |
| 789 } | 795 } |
| 790 | 796 |
| 791 void URLFetcherCore::StartURLRequestWhenAppropriate() { | 797 void URLFetcherCore::StartURLRequestWhenAppropriate() { |
| 792 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 798 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 793 | 799 |
| 794 if (was_cancelled_) | 800 if (was_cancelled_) |
| 795 return; | 801 return; |
| 796 | 802 |
| 803 DCHECK(request_context_getter_); |
| 804 |
| 805 int64 delay = 0LL; |
| 797 if (original_url_throttler_entry_ == NULL) { | 806 if (original_url_throttler_entry_ == NULL) { |
| 798 original_url_throttler_entry_ = | 807 net::URLRequestThrottlerManager* manager = |
| 799 net::URLRequestThrottlerManager::GetInstance()->RegisterRequestUrl( | 808 request_context_getter_->GetURLRequestContext()->throttler_manager(); |
| 800 original_url_); | 809 if (manager) { |
| 810 original_url_throttler_entry_ = |
| 811 manager->RegisterRequestUrl(original_url_); |
| 812 } |
| 813 } |
| 814 if (original_url_throttler_entry_ != NULL) { |
| 815 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( |
| 816 GetBackoffReleaseTime()); |
| 801 } | 817 } |
| 802 | 818 |
| 803 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( | |
| 804 GetBackoffReleaseTime()); | |
| 805 if (delay == 0) { | 819 if (delay == 0) { |
| 806 StartURLRequest(); | 820 StartURLRequest(); |
| 807 } else { | 821 } else { |
| 808 MessageLoop::current()->PostDelayedTask( | 822 MessageLoop::current()->PostDelayedTask( |
| 809 FROM_HERE, base::Bind(&URLFetcherCore::StartURLRequest, this), | 823 FROM_HERE, base::Bind(&URLFetcherCore::StartURLRequest, this), |
| 810 base::TimeDelta::FromMilliseconds(delay)); | 824 base::TimeDelta::FromMilliseconds(delay)); |
| 811 } | 825 } |
| 812 } | 826 } |
| 813 | 827 |
| 814 void URLFetcherCore::CancelURLRequest() { | 828 void URLFetcherCore::CancelURLRequest() { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 } | 939 } |
| 926 | 940 |
| 927 void URLFetcherCore::ReleaseRequest() { | 941 void URLFetcherCore::ReleaseRequest() { |
| 928 upload_progress_checker_timer_.reset(); | 942 upload_progress_checker_timer_.reset(); |
| 929 request_.reset(); | 943 request_.reset(); |
| 930 g_registry.Get().RemoveURLFetcherCore(this); | 944 g_registry.Get().RemoveURLFetcherCore(this); |
| 931 } | 945 } |
| 932 | 946 |
| 933 base::TimeTicks URLFetcherCore::GetBackoffReleaseTime() { | 947 base::TimeTicks URLFetcherCore::GetBackoffReleaseTime() { |
| 934 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 948 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 935 DCHECK(original_url_throttler_entry_ != NULL); | |
| 936 | 949 |
| 937 base::TimeTicks original_url_backoff = | 950 if (original_url_throttler_entry_) { |
| 938 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 951 base::TimeTicks original_url_backoff = |
| 939 base::TimeTicks destination_url_backoff; | 952 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
| 940 if (url_throttler_entry_ != NULL && | 953 base::TimeTicks destination_url_backoff; |
| 941 original_url_throttler_entry_ != url_throttler_entry_) { | 954 if (url_throttler_entry_ != NULL && |
| 942 destination_url_backoff = | 955 original_url_throttler_entry_ != url_throttler_entry_) { |
| 943 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | 956 destination_url_backoff = |
| 957 url_throttler_entry_->GetExponentialBackoffReleaseTime(); |
| 958 } |
| 959 |
| 960 return original_url_backoff > destination_url_backoff ? |
| 961 original_url_backoff : destination_url_backoff; |
| 962 } else { |
| 963 return base::TimeTicks(); |
| 944 } | 964 } |
| 945 | |
| 946 return original_url_backoff > destination_url_backoff ? | |
| 947 original_url_backoff : destination_url_backoff; | |
| 948 } | 965 } |
| 949 | 966 |
| 950 } // namespace content | 967 } // namespace content |
| OLD | NEW |