| 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 "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/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 scoped_refptr<URLRequestThrottlerEntryInterface> | 23 scoped_refptr<URLRequestThrottlerEntryInterface> |
| 24 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { | 24 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { |
| 25 DCHECK(!enable_thread_checks_ || CalledOnValidThread()); | 25 DCHECK(!enable_thread_checks_ || CalledOnValidThread()); |
| 26 | 26 |
| 27 if (registered_from_thread_ == base::kInvalidThreadId) { | 27 if (registered_from_thread_ == base::kInvalidThreadId) { |
| 28 // We can't currently do this in the constructor as it is run on the | 28 // We can't currently do this in the constructor as it is run on the |
| 29 // UI thread and notifications go to the thread from which they are | 29 // UI thread and notifications go to the thread from which they are |
| 30 // registered. | 30 // registered. |
| 31 // TODO(joi): Clean this up once this is no longer a Singleton. | 31 // TODO(joi): Clean this up once this is no longer a Singleton. |
| 32 NetworkChangeNotifier::AddIPAddressObserver(this); | 32 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 33 NetworkChangeNotifier::AddOnlineStateObserver(this); | 33 NetworkChangeNotifier::AddConnectionStateObserver(this); |
| 34 registered_from_thread_ = base::PlatformThread::CurrentId(); | 34 registered_from_thread_ = base::PlatformThread::CurrentId(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Normalize the url. | 37 // Normalize the url. |
| 38 std::string url_id = GetIdFromUrl(url); | 38 std::string url_id = GetIdFromUrl(url); |
| 39 | 39 |
| 40 // Periodically garbage collect old entries. | 40 // Periodically garbage collect old entries. |
| 41 GarbageCollectEntriesIfNecessary(); | 41 GarbageCollectEntriesIfNecessary(); |
| 42 | 42 |
| 43 // Find the entry in the map or create a new NULL entry. | 43 // Find the entry in the map or create a new NULL entry. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } | 139 } |
| 140 | 140 |
| 141 NetLog* URLRequestThrottlerManager::net_log() const { | 141 NetLog* URLRequestThrottlerManager::net_log() const { |
| 142 return net_log_->net_log(); | 142 return net_log_->net_log(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void URLRequestThrottlerManager::OnIPAddressChanged() { | 145 void URLRequestThrottlerManager::OnIPAddressChanged() { |
| 146 OnNetworkChange(); | 146 OnNetworkChange(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { | 149 void URLRequestThrottlerManager::OnConnectionStateChanged( |
| 150 NetworkChangeNotifier::ConnectionState state) { |
| 150 OnNetworkChange(); | 151 OnNetworkChange(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 // TODO(joi): Turn throttling on by default when appropriate. | 154 // TODO(joi): Turn throttling on by default when appropriate. |
| 154 URLRequestThrottlerManager::URLRequestThrottlerManager() | 155 URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 155 : requests_since_last_gc_(0), | 156 : requests_since_last_gc_(0), |
| 156 enforce_throttling_(false), | 157 enforce_throttling_(false), |
| 157 enable_thread_checks_(false), | 158 enable_thread_checks_(false), |
| 158 logged_for_localhost_disabled_(false), | 159 logged_for_localhost_disabled_(false), |
| 159 registered_from_thread_(base::kInvalidThreadId) { | 160 registered_from_thread_(base::kInvalidThreadId) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 176 // Destruction is on main thread (AtExit), but real use is on I/O thread. | 177 // Destruction is on main thread (AtExit), but real use is on I/O thread. |
| 177 // The AtExit manager does not run until the I/O thread has finished | 178 // The AtExit manager does not run until the I/O thread has finished |
| 178 // processing. | 179 // processing. |
| 179 DetachFromThread(); | 180 DetachFromThread(); |
| 180 | 181 |
| 181 // We must currently skip this in the production case, where the destructor | 182 // We must currently skip this in the production case, where the destructor |
| 182 // does not run on the thread we registered from. | 183 // does not run on the thread we registered from. |
| 183 // TODO(joi): Fix once we are no longer a Singleton. | 184 // TODO(joi): Fix once we are no longer a Singleton. |
| 184 if (base::PlatformThread::CurrentId() == registered_from_thread_) { | 185 if (base::PlatformThread::CurrentId() == registered_from_thread_) { |
| 185 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 186 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 186 NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 187 NetworkChangeNotifier::RemoveConnectionStateObserver(this); |
| 187 } | 188 } |
| 188 | 189 |
| 189 // Since, for now, the manager object might conceivably go away before | 190 // Since, for now, the manager object might conceivably go away before |
| 190 // the entries, detach the entries' back-pointer to the manager. | 191 // the entries, detach the entries' back-pointer to the manager. |
| 191 // | 192 // |
| 192 // TODO(joi): Revisit whether to make entries non-refcounted. | 193 // TODO(joi): Revisit whether to make entries non-refcounted. |
| 193 UrlEntryMap::iterator i = url_entries_.begin(); | 194 UrlEntryMap::iterator i = url_entries_.begin(); |
| 194 while (i != url_entries_.end()) { | 195 while (i != url_entries_.end()) { |
| 195 if (i->second != NULL) { | 196 if (i->second != NULL) { |
| 196 i->second->DetachManager(); | 197 i->second->DetachManager(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 void URLRequestThrottlerManager::OnNetworkChange() { | 239 void URLRequestThrottlerManager::OnNetworkChange() { |
| 239 // Remove all entries. Any entries that in-flight requests have a reference | 240 // Remove all entries. Any entries that in-flight requests have a reference |
| 240 // to will live until those requests end, and these entries may be | 241 // to will live until those requests end, and these entries may be |
| 241 // inconsistent with new entries for the same URLs, but since what we | 242 // inconsistent with new entries for the same URLs, but since what we |
| 242 // want is a clean slate for the new connection state, this is OK. | 243 // want is a clean slate for the new connection state, this is OK. |
| 243 url_entries_.clear(); | 244 url_entries_.clear(); |
| 244 requests_since_last_gc_ = 0; | 245 requests_since_last_gc_ = 0; |
| 245 } | 246 } |
| 246 | 247 |
| 247 } // namespace net | 248 } // namespace net |
| OLD | NEW |