| 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_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
| 12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 const unsigned int URLRequestThrottlerManager::kMaximumNumberOfEntries = 1500; | 16 const unsigned int URLRequestThrottlerManager::kMaximumNumberOfEntries = 1500; |
| 17 const unsigned int URLRequestThrottlerManager::kRequestsBetweenCollecting = 200; | 17 const unsigned int URLRequestThrottlerManager::kRequestsBetweenCollecting = 200; |
| 18 | 18 |
| 19 URLRequestThrottlerManager::URLRequestThrottlerManager() | 19 URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 20 : requests_since_last_gc_(0), | 20 : requests_since_last_gc_(0), |
| 21 enable_thread_checks_(false), |
| 21 logged_for_localhost_disabled_(false), | 22 logged_for_localhost_disabled_(false), |
| 22 registered_from_thread_(base::kInvalidThreadId) { | 23 registered_from_thread_(base::kInvalidThreadId) { |
| 23 url_id_replacements_.ClearPassword(); | 24 url_id_replacements_.ClearPassword(); |
| 24 url_id_replacements_.ClearUsername(); | 25 url_id_replacements_.ClearUsername(); |
| 25 url_id_replacements_.ClearQuery(); | 26 url_id_replacements_.ClearQuery(); |
| 26 url_id_replacements_.ClearRef(); | 27 url_id_replacements_.ClearRef(); |
| 27 | 28 |
| 28 NetworkChangeNotifier::AddIPAddressObserver(this); | 29 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 29 NetworkChangeNotifier::AddConnectionTypeObserver(this); | 30 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 30 } | 31 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 } | 43 } |
| 43 ++i; | 44 ++i; |
| 44 } | 45 } |
| 45 | 46 |
| 46 // Delete all entries. | 47 // Delete all entries. |
| 47 url_entries_.clear(); | 48 url_entries_.clear(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 scoped_refptr<URLRequestThrottlerEntryInterface> | 51 scoped_refptr<URLRequestThrottlerEntryInterface> |
| 51 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { | 52 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { |
| 52 DCHECK(CalledOnValidThread()); | 53 DCHECK(!enable_thread_checks_ || CalledOnValidThread()); |
| 53 | 54 |
| 54 // Normalize the url. | 55 // Normalize the url. |
| 55 std::string url_id = GetIdFromUrl(url); | 56 std::string url_id = GetIdFromUrl(url); |
| 56 | 57 |
| 57 // Periodically garbage collect old entries. | 58 // Periodically garbage collect old entries. |
| 58 GarbageCollectEntriesIfNecessary(); | 59 GarbageCollectEntriesIfNecessary(); |
| 59 | 60 |
| 60 // Find the entry in the map or create a new NULL entry. | 61 // Find the entry in the map or create a new NULL entry. |
| 61 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; | 62 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; |
| 62 | 63 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 79 // in AddToOptOutList), this is not a problem. | 80 // in AddToOptOutList), this is not a problem. |
| 80 std::string host = url.host(); | 81 std::string host = url.host(); |
| 81 if (opt_out_hosts_.find(host) != opt_out_hosts_.end() || | 82 if (opt_out_hosts_.find(host) != opt_out_hosts_.end() || |
| 82 IsLocalhost(host)) { | 83 IsLocalhost(host)) { |
| 83 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { | 84 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { |
| 84 logged_for_localhost_disabled_ = true; | 85 logged_for_localhost_disabled_ = true; |
| 85 net_log_.AddEvent(NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, | 86 net_log_.AddEvent(NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, |
| 86 NetLog::StringCallback("host", &host)); | 87 NetLog::StringCallback("host", &host)); |
| 87 } | 88 } |
| 88 | 89 |
| 89 // If sliding window was separate from back-off throttling, we | 90 // TODO(joi): Once sliding window is separate from back-off throttling, |
| 90 // could simply return a dummy implementation of | 91 // we can simply return a dummy implementation of |
| 91 // URLRequestThrottlerEntryInterface here that never blocks | 92 // URLRequestThrottlerEntryInterface here that never blocks anything (and |
| 92 // anything (and not keep entries in url_entries_ for opted-out | 93 // not keep entries in url_entries_ for opted-out sites). |
| 93 // sites). | |
| 94 entry->DisableBackoffThrottling(); | 94 entry->DisableBackoffThrottling(); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 return entry; | 98 return entry; |
| 99 } | 99 } |
| 100 | 100 |
| 101 void URLRequestThrottlerManager::AddToOptOutList(const std::string& host) { | 101 void URLRequestThrottlerManager::AddToOptOutList(const std::string& host) { |
| 102 // There is an edge case here that we are not handling, to keep things | 102 // There is an edge case here that we are not handling, to keep things |
| 103 // simple. If a host starts adding the opt-out header to its responses | 103 // simple. If a host starts adding the opt-out header to its responses |
| (...skipping 20 matching lines...) Expand all Loading... |
| 124 | 124 |
| 125 url_entries_[url_id] = entry; | 125 url_entries_[url_id] = entry; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { | 128 void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { |
| 129 // Normalize the url. | 129 // Normalize the url. |
| 130 std::string url_id = GetIdFromUrl(url); | 130 std::string url_id = GetIdFromUrl(url); |
| 131 url_entries_.erase(url_id); | 131 url_entries_.erase(url_id); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void URLRequestThrottlerManager::set_enable_thread_checks(bool enable) { |
| 135 enable_thread_checks_ = enable; |
| 136 } |
| 137 |
| 138 bool URLRequestThrottlerManager::enable_thread_checks() const { |
| 139 return enable_thread_checks_; |
| 140 } |
| 141 |
| 134 void URLRequestThrottlerManager::set_net_log(NetLog* net_log) { | 142 void URLRequestThrottlerManager::set_net_log(NetLog* net_log) { |
| 135 DCHECK(net_log); | 143 DCHECK(net_log); |
| 136 net_log_ = BoundNetLog::Make(net_log, | 144 net_log_ = BoundNetLog::Make(net_log, |
| 137 NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING); | 145 NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING); |
| 138 } | 146 } |
| 139 | 147 |
| 140 NetLog* URLRequestThrottlerManager::net_log() const { | 148 NetLog* URLRequestThrottlerManager::net_log() const { |
| 141 return net_log_.net_log(); | 149 return net_log_.net_log(); |
| 142 } | 150 } |
| 143 | 151 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 void URLRequestThrottlerManager::OnNetworkChange() { | 194 void URLRequestThrottlerManager::OnNetworkChange() { |
| 187 // Remove all entries. Any entries that in-flight requests have a reference | 195 // Remove all entries. Any entries that in-flight requests have a reference |
| 188 // to will live until those requests end, and these entries may be | 196 // to will live until those requests end, and these entries may be |
| 189 // inconsistent with new entries for the same URLs, but since what we | 197 // inconsistent with new entries for the same URLs, but since what we |
| 190 // want is a clean slate for the new connection type, this is OK. | 198 // want is a clean slate for the new connection type, this is OK. |
| 191 url_entries_.clear(); | 199 url_entries_.clear(); |
| 192 requests_since_last_gc_ = 0; | 200 requests_since_last_gc_ = 0; |
| 193 } | 201 } |
| 194 | 202 |
| 195 } // namespace net | 203 } // namespace net |
| OLD | NEW |