| 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/string_util.h" | 10 #include "base/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::GetInstance() { | 19 URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 20 return Singleton<URLRequestThrottlerManager>::get(); | 20 : requests_since_last_gc_(0), |
| 21 enforce_throttling_(true), |
| 22 enable_thread_checks_(false), |
| 23 logged_for_localhost_disabled_(false), |
| 24 registered_from_thread_(base::kInvalidThreadId) { |
| 25 url_id_replacements_.ClearPassword(); |
| 26 url_id_replacements_.ClearUsername(); |
| 27 url_id_replacements_.ClearQuery(); |
| 28 url_id_replacements_.ClearRef(); |
| 29 |
| 30 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 31 NetworkChangeNotifier::AddOnlineStateObserver(this); |
| 32 } |
| 33 |
| 34 URLRequestThrottlerManager::~URLRequestThrottlerManager() { |
| 35 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 36 NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
| 37 |
| 38 // Since, for now, the manager object might conceivably go away before |
| 39 // the entries, detach the entries' back-pointer to the manager. |
| 40 // |
| 41 // TODO(joi): Revisit whether to make entries non-refcounted. |
| 42 UrlEntryMap::iterator i = url_entries_.begin(); |
| 43 while (i != url_entries_.end()) { |
| 44 if (i->second != NULL) { |
| 45 i->second->DetachManager(); |
| 46 } |
| 47 ++i; |
| 48 } |
| 49 |
| 50 // Delete all entries. |
| 51 url_entries_.clear(); |
| 21 } | 52 } |
| 22 | 53 |
| 23 scoped_refptr<URLRequestThrottlerEntryInterface> | 54 scoped_refptr<URLRequestThrottlerEntryInterface> |
| 24 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { | 55 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { |
| 25 DCHECK(!enable_thread_checks_ || CalledOnValidThread()); | 56 DCHECK(!enable_thread_checks_ || CalledOnValidThread()); |
| 26 | 57 |
| 27 if (registered_from_thread_ == base::kInvalidThreadId) { | |
| 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 | |
| 30 // registered. | |
| 31 // TODO(joi): Clean this up once this is no longer a Singleton. | |
| 32 NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 33 NetworkChangeNotifier::AddOnlineStateObserver(this); | |
| 34 registered_from_thread_ = base::PlatformThread::CurrentId(); | |
| 35 } | |
| 36 | |
| 37 // Normalize the url. | 58 // Normalize the url. |
| 38 std::string url_id = GetIdFromUrl(url); | 59 std::string url_id = GetIdFromUrl(url); |
| 39 | 60 |
| 40 // Periodically garbage collect old entries. | 61 // Periodically garbage collect old entries. |
| 41 GarbageCollectEntriesIfNecessary(); | 62 GarbageCollectEntriesIfNecessary(); |
| 42 | 63 |
| 43 // Find the entry in the map or create a new NULL entry. | 64 // Find the entry in the map or create a new NULL entry. |
| 44 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; | 65 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; |
| 45 | 66 |
| 46 // If the entry exists but could be garbage collected at this point, we | 67 // If the entry exists but could be garbage collected at this point, we |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 163 } |
| 143 | 164 |
| 144 void URLRequestThrottlerManager::OnIPAddressChanged() { | 165 void URLRequestThrottlerManager::OnIPAddressChanged() { |
| 145 OnNetworkChange(); | 166 OnNetworkChange(); |
| 146 } | 167 } |
| 147 | 168 |
| 148 void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { | 169 void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { |
| 149 OnNetworkChange(); | 170 OnNetworkChange(); |
| 150 } | 171 } |
| 151 | 172 |
| 152 URLRequestThrottlerManager::URLRequestThrottlerManager() | |
| 153 : requests_since_last_gc_(0), | |
| 154 enforce_throttling_(true), | |
| 155 enable_thread_checks_(false), | |
| 156 logged_for_localhost_disabled_(false), | |
| 157 registered_from_thread_(base::kInvalidThreadId) { | |
| 158 // Construction/destruction is on main thread (because BrowserMain | |
| 159 // retrieves an instance to call InitializeOptions), but is from then on | |
| 160 // used on I/O thread. | |
| 161 DetachFromThread(); | |
| 162 | |
| 163 url_id_replacements_.ClearPassword(); | |
| 164 url_id_replacements_.ClearUsername(); | |
| 165 url_id_replacements_.ClearQuery(); | |
| 166 url_id_replacements_.ClearRef(); | |
| 167 } | |
| 168 | |
| 169 URLRequestThrottlerManager::~URLRequestThrottlerManager() { | |
| 170 // Destruction is on main thread (AtExit), but real use is on I/O thread. | |
| 171 // The AtExit manager does not run until the I/O thread has finished | |
| 172 // processing. | |
| 173 DetachFromThread(); | |
| 174 | |
| 175 // We must currently skip this in the production case, where the destructor | |
| 176 // does not run on the thread we registered from. | |
| 177 // TODO(joi): Fix once we are no longer a Singleton. | |
| 178 if (base::PlatformThread::CurrentId() == registered_from_thread_) { | |
| 179 NetworkChangeNotifier::RemoveIPAddressObserver(this); | |
| 180 NetworkChangeNotifier::RemoveOnlineStateObserver(this); | |
| 181 } | |
| 182 | |
| 183 // Since, for now, the manager object might conceivably go away before | |
| 184 // the entries, detach the entries' back-pointer to the manager. | |
| 185 // | |
| 186 // TODO(joi): Revisit whether to make entries non-refcounted. | |
| 187 UrlEntryMap::iterator i = url_entries_.begin(); | |
| 188 while (i != url_entries_.end()) { | |
| 189 if (i->second != NULL) { | |
| 190 i->second->DetachManager(); | |
| 191 } | |
| 192 ++i; | |
| 193 } | |
| 194 | |
| 195 // Delete all entries. | |
| 196 url_entries_.clear(); | |
| 197 } | |
| 198 | |
| 199 std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { | 173 std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { |
| 200 if (!url.is_valid()) | 174 if (!url.is_valid()) |
| 201 return url.possibly_invalid_spec(); | 175 return url.possibly_invalid_spec(); |
| 202 | 176 |
| 203 GURL id = url.ReplaceComponents(url_id_replacements_); | 177 GURL id = url.ReplaceComponents(url_id_replacements_); |
| 204 return StringToLowerASCII(id.spec()).c_str(); | 178 return StringToLowerASCII(id.spec()).c_str(); |
| 205 } | 179 } |
| 206 | 180 |
| 207 void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { | 181 void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { |
| 208 requests_since_last_gc_++; | 182 requests_since_last_gc_++; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 232 void URLRequestThrottlerManager::OnNetworkChange() { | 206 void URLRequestThrottlerManager::OnNetworkChange() { |
| 233 // Remove all entries. Any entries that in-flight requests have a reference | 207 // Remove all entries. Any entries that in-flight requests have a reference |
| 234 // to will live until those requests end, and these entries may be | 208 // to will live until those requests end, and these entries may be |
| 235 // inconsistent with new entries for the same URLs, but since what we | 209 // inconsistent with new entries for the same URLs, but since what we |
| 236 // want is a clean slate for the new connection state, this is OK. | 210 // want is a clean slate for the new connection state, this is OK. |
| 237 url_entries_.clear(); | 211 url_entries_.clear(); |
| 238 requests_since_last_gc_ = 0; | 212 requests_since_last_gc_ = 0; |
| 239 } | 213 } |
| 240 | 214 |
| 241 } // namespace net | 215 } // namespace net |
| OLD | NEW |