xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "extensions/browser/extension_throttle_manager.h" |
| 6 | |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 9 | #include "base/logging.h" |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 11 | #include "base/metrics/field_trial.h" |
| 12 | #include "base/metrics/histogram.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | #include "extensions/browser/extension_request_limiting_throttle.h" |
| 15 | #include "extensions/common/constants.h" |
tfarina | 7ba5a62 | 2016-02-23 23:21:44 | [diff] [blame] | 16 | #include "net/base/url_util.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 17 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 18 | #include "net/log/net_log_event_type.h" |
| 19 | #include "net/log/net_log_source_type.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 20 | #include "net/url_request/url_request.h" |
| 21 | |
| 22 | namespace extensions { |
| 23 | |
| 24 | const unsigned int ExtensionThrottleManager::kMaximumNumberOfEntries = 1500; |
| 25 | const unsigned int ExtensionThrottleManager::kRequestsBetweenCollecting = 200; |
| 26 | |
| 27 | ExtensionThrottleManager::ExtensionThrottleManager() |
| 28 | : requests_since_last_gc_(0), |
| 29 | enable_thread_checks_(false), |
| 30 | logged_for_localhost_disabled_(false), |
| 31 | registered_from_thread_(base::kInvalidThreadId), |
| 32 | ignore_user_gesture_load_flag_for_tests_(false) { |
| 33 | url_id_replacements_.ClearPassword(); |
| 34 | url_id_replacements_.ClearUsername(); |
| 35 | url_id_replacements_.ClearQuery(); |
| 36 | url_id_replacements_.ClearRef(); |
| 37 | |
| 38 | net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 39 | net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 40 | } |
| 41 | |
| 42 | ExtensionThrottleManager::~ExtensionThrottleManager() { |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 43 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 44 | net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 45 | net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 46 | |
| 47 | // Since the manager object might conceivably go away before the |
| 48 | // entries, detach the entries' back-pointer to the manager. |
| 49 | UrlEntryMap::iterator i = url_entries_.begin(); |
| 50 | while (i != url_entries_.end()) { |
| 51 | if (i->second.get() != NULL) { |
| 52 | i->second->DetachManager(); |
| 53 | } |
| 54 | ++i; |
| 55 | } |
| 56 | |
| 57 | // Delete all entries. |
| 58 | url_entries_.clear(); |
| 59 | } |
| 60 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 61 | std::unique_ptr<content::ResourceThrottle> |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 62 | ExtensionThrottleManager::MaybeCreateThrottle(const net::URLRequest* request) { |
| 63 | if (request->first_party_for_cookies().scheme() != |
| 64 | extensions::kExtensionScheme) { |
| 65 | return nullptr; |
| 66 | } |
ricea | 5e27337 | 2016-08-22 02:51:03 | [diff] [blame] | 67 | return base::MakeUnique<extensions::ExtensionRequestLimitingThrottle>(request, |
| 68 | this); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | scoped_refptr<ExtensionThrottleEntryInterface> |
| 72 | ExtensionThrottleManager::RegisterRequestUrl(const GURL& url) { |
gab | 370841f2 | 2017-06-01 14:38:26 | [diff] [blame] | 73 | #if DCHECK_IS_ON() |
| 74 | DCHECK(!enable_thread_checks_ || sequence_checker_.CalledOnValidSequence()); |
| 75 | #endif // DCHECK_IS_ON() |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 76 | |
| 77 | // Normalize the url. |
| 78 | std::string url_id = GetIdFromUrl(url); |
| 79 | |
| 80 | // Periodically garbage collect old entries. |
| 81 | GarbageCollectEntriesIfNecessary(); |
| 82 | |
| 83 | // Find the entry in the map or create a new NULL entry. |
| 84 | scoped_refptr<ExtensionThrottleEntry>& entry = url_entries_[url_id]; |
| 85 | |
| 86 | // If the entry exists but could be garbage collected at this point, we |
| 87 | // start with a fresh entry so that we possibly back off a bit less |
| 88 | // aggressively (i.e. this resets the error count when the entry's URL |
| 89 | // hasn't been requested in long enough). |
| 90 | if (entry.get() && entry->IsEntryOutdated()) { |
| 91 | entry = NULL; |
| 92 | } |
| 93 | |
| 94 | // Create the entry if needed. |
| 95 | if (entry.get() == NULL) { |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 96 | if (backoff_policy_for_tests_) { |
| 97 | entry = new ExtensionThrottleEntry( |
| 98 | this, url_id, backoff_policy_for_tests_.get(), |
| 99 | ignore_user_gesture_load_flag_for_tests_); |
| 100 | } else { |
| 101 | entry = new ExtensionThrottleEntry( |
| 102 | this, url_id, ignore_user_gesture_load_flag_for_tests_); |
| 103 | } |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 104 | |
| 105 | // We only disable back-off throttling on an entry that we have |
| 106 | // just constructed. This is to allow unit tests to explicitly override |
| 107 | // the entry for localhost URLs. |
| 108 | std::string host = url.host(); |
| 109 | if (net::IsLocalhost(host)) { |
| 110 | if (!logged_for_localhost_disabled_ && net::IsLocalhost(host)) { |
| 111 | logged_for_localhost_disabled_ = true; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 112 | net_log_.AddEvent(net::NetLogEventType::THROTTLING_DISABLED_FOR_HOST, |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 113 | net::NetLog::StringCallback("host", &host)); |
| 114 | } |
| 115 | |
| 116 | // TODO(joi): Once sliding window is separate from back-off throttling, |
| 117 | // we can simply return a dummy implementation of |
| 118 | // ExtensionThrottleEntryInterface here that never blocks anything. |
| 119 | entry->DisableBackoffThrottling(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return entry; |
| 124 | } |
| 125 | |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 126 | void ExtensionThrottleManager::SetBackoffPolicyForTests( |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 127 | std::unique_ptr<net::BackoffEntry::Policy> policy) { |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 128 | backoff_policy_for_tests_ = std::move(policy); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 129 | } |
| 130 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 131 | void ExtensionThrottleManager::OverrideEntryForTests( |
| 132 | const GURL& url, |
| 133 | ExtensionThrottleEntry* entry) { |
| 134 | // Normalize the url. |
| 135 | std::string url_id = GetIdFromUrl(url); |
| 136 | |
| 137 | // Periodically garbage collect old entries. |
| 138 | GarbageCollectEntriesIfNecessary(); |
| 139 | |
| 140 | url_entries_[url_id] = entry; |
| 141 | } |
| 142 | |
| 143 | void ExtensionThrottleManager::EraseEntryForTests(const GURL& url) { |
| 144 | // Normalize the url. |
| 145 | std::string url_id = GetIdFromUrl(url); |
| 146 | url_entries_.erase(url_id); |
| 147 | } |
| 148 | |
| 149 | void ExtensionThrottleManager::SetIgnoreUserGestureLoadFlagForTests( |
| 150 | bool ignore_user_gesture_load_flag_for_tests) { |
| 151 | ignore_user_gesture_load_flag_for_tests_ = true; |
| 152 | } |
| 153 | |
| 154 | void ExtensionThrottleManager::set_enable_thread_checks(bool enable) { |
| 155 | enable_thread_checks_ = enable; |
| 156 | } |
| 157 | |
| 158 | bool ExtensionThrottleManager::enable_thread_checks() const { |
| 159 | return enable_thread_checks_; |
| 160 | } |
| 161 | |
| 162 | void ExtensionThrottleManager::set_net_log(net::NetLog* net_log) { |
| 163 | DCHECK(net_log); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 164 | net_log_ = net::NetLogWithSource::Make( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 165 | net_log, net::NetLogSourceType::EXPONENTIAL_BACKOFF_THROTTLING); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | net::NetLog* ExtensionThrottleManager::net_log() const { |
| 169 | return net_log_.net_log(); |
| 170 | } |
| 171 | |
| 172 | void ExtensionThrottleManager::OnIPAddressChanged() { |
| 173 | OnNetworkChange(); |
| 174 | } |
| 175 | |
| 176 | void ExtensionThrottleManager::OnConnectionTypeChanged( |
| 177 | net::NetworkChangeNotifier::ConnectionType type) { |
| 178 | OnNetworkChange(); |
| 179 | } |
| 180 | |
| 181 | std::string ExtensionThrottleManager::GetIdFromUrl(const GURL& url) const { |
| 182 | if (!url.is_valid()) |
| 183 | return url.possibly_invalid_spec(); |
| 184 | |
| 185 | GURL id = url.ReplaceComponents(url_id_replacements_); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 186 | return base::ToLowerASCII(id.spec()); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void ExtensionThrottleManager::GarbageCollectEntriesIfNecessary() { |
| 190 | requests_since_last_gc_++; |
| 191 | if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 192 | return; |
| 193 | requests_since_last_gc_ = 0; |
| 194 | |
| 195 | GarbageCollectEntries(); |
| 196 | } |
| 197 | |
| 198 | void ExtensionThrottleManager::GarbageCollectEntries() { |
| 199 | UrlEntryMap::iterator i = url_entries_.begin(); |
| 200 | while (i != url_entries_.end()) { |
| 201 | if ((i->second)->IsEntryOutdated()) { |
| 202 | url_entries_.erase(i++); |
| 203 | } else { |
| 204 | ++i; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // In case something broke we want to make sure not to grow indefinitely. |
| 209 | while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 210 | url_entries_.erase(url_entries_.begin()); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void ExtensionThrottleManager::OnNetworkChange() { |
| 215 | // Remove all entries. Any entries that in-flight requests have a reference |
| 216 | // to will live until those requests end, and these entries may be |
| 217 | // inconsistent with new entries for the same URLs, but since what we |
| 218 | // want is a clean slate for the new connection type, this is OK. |
| 219 | url_entries_.clear(); |
| 220 | requests_since_last_gc_ = 0; |
| 221 | } |
| 222 | |
| 223 | } // namespace extensions |