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 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 5 | #include "extensions/renderer/extension_throttle_manager.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 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" |
| 10 | #include "base/metrics/field_trial.h" |
| 11 | #include "base/metrics/histogram.h" |
| 12 | #include "base/strings/string_util.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 13 | #include "extensions/common/constants.h" |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 14 | #include "extensions/renderer/extension_url_loader_throttle.h" |
tfarina | 7ba5a62 | 2016-02-23 23:21:44 | [diff] [blame] | 15 | #include "net/base/url_util.h" |
Lucas Furukawa Gadani | 01188659 | 2019-10-23 17:12:24 | [diff] [blame] | 16 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 17 | #include "third_party/blink/public/platform/web_url.h" |
| 18 | #include "third_party/blink/public/platform/web_url_request.h" |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 19 | |
| 20 | namespace extensions { |
| 21 | |
| 22 | const unsigned int ExtensionThrottleManager::kMaximumNumberOfEntries = 1500; |
| 23 | const unsigned int ExtensionThrottleManager::kRequestsBetweenCollecting = 200; |
| 24 | |
| 25 | ExtensionThrottleManager::ExtensionThrottleManager() |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 26 | : requests_since_last_gc_(0) { |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 27 | url_id_replacements_.ClearPassword(); |
| 28 | url_id_replacements_.ClearUsername(); |
| 29 | url_id_replacements_.ClearQuery(); |
| 30 | url_id_replacements_.ClearRef(); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ExtensionThrottleManager::~ExtensionThrottleManager() { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 34 | base::AutoLock auto_lock(lock_); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 35 | // Delete all entries. |
| 36 | url_entries_.clear(); |
| 37 | } |
| 38 | |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 39 | std::unique_ptr<blink::URLLoaderThrottle> |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 40 | ExtensionThrottleManager::MaybeCreateURLLoaderThrottle( |
| 41 | const blink::WebURLRequest& request) { |
Maks Orlovich | ab27e24 | 2020-01-07 18:10:39 | [diff] [blame] | 42 | // TODO(https://crbug.com/1039700): This relies on the extension scheme |
| 43 | // getting special handling via ShouldTreatURLSchemeAsFirstPartyWhenTopLevel, |
| 44 | // which has problems. Once that's removed this should probably look at top |
| 45 | // level directly instead. |
| 46 | if (request.SiteForCookies().scheme() != extensions::kExtensionScheme) |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 47 | return nullptr; |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 48 | return std::make_unique<ExtensionURLLoaderThrottle>(this); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 49 | } |
| 50 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 51 | ExtensionThrottleEntry* ExtensionThrottleManager::RegisterRequestUrl( |
| 52 | const GURL& url) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 53 | // Internal function, no locking. |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 54 | |
| 55 | // Normalize the url. |
| 56 | std::string url_id = GetIdFromUrl(url); |
| 57 | |
| 58 | // Periodically garbage collect old entries. |
| 59 | GarbageCollectEntriesIfNecessary(); |
| 60 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 61 | // Find the entry in the map or create a new null entry. |
| 62 | std::unique_ptr<ExtensionThrottleEntry>& entry = url_entries_[url_id]; |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 63 | |
| 64 | // If the entry exists but could be garbage collected at this point, we |
| 65 | // start with a fresh entry so that we possibly back off a bit less |
| 66 | // aggressively (i.e. this resets the error count when the entry's URL |
| 67 | // hasn't been requested in long enough). |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 68 | if (entry && entry->IsEntryOutdated()) |
| 69 | entry.reset(); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 70 | |
| 71 | // Create the entry if needed. |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 72 | if (!entry) { |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 73 | if (backoff_policy_for_tests_) { |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 74 | entry = std::make_unique<ExtensionThrottleEntry>( |
| 75 | url_id, backoff_policy_for_tests_.get()); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 76 | } else { |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 77 | entry = std::make_unique<ExtensionThrottleEntry>(url_id); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 78 | } |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 79 | |
| 80 | // We only disable back-off throttling on an entry that we have |
| 81 | // just constructed. This is to allow unit tests to explicitly override |
| 82 | // the entry for localhost URLs. |
Rob Wu | f79b3ba | 2018-01-14 01:54:31 | [diff] [blame] | 83 | if (net::IsLocalhost(url)) { |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 84 | // TODO(joi): Once sliding window is separate from back-off throttling, |
| 85 | // we can simply return a dummy implementation of |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 86 | // ExtensionThrottleEntry here that never blocks anything. |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 87 | entry->DisableBackoffThrottling(); |
| 88 | } |
| 89 | } |
| 90 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 91 | return entry.get(); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 92 | } |
| 93 | |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 94 | bool ExtensionThrottleManager::ShouldRejectRequest(const GURL& request_url) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 95 | base::AutoLock auto_lock(lock_); |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 96 | return RegisterRequestUrl(request_url)->ShouldRejectRequest(); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool ExtensionThrottleManager::ShouldRejectRedirect( |
| 100 | const GURL& request_url, |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 101 | const net::RedirectInfo& redirect_info) { |
| 102 | { |
Chris Mumford | 475eede | 2018-07-25 20:55:03 | [diff] [blame] | 103 | // An entry GC when requests are outstanding can purge entries so check |
| 104 | // before use. |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 105 | base::AutoLock auto_lock(lock_); |
Chris Mumford | 475eede | 2018-07-25 20:55:03 | [diff] [blame] | 106 | auto it = url_entries_.find(GetIdFromUrl(request_url)); |
| 107 | if (it != url_entries_.end()) |
| 108 | it->second->UpdateWithResponse(redirect_info.status_code); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 109 | } |
Matt Menke | 2185050 | 2019-10-09 22:34:24 | [diff] [blame] | 110 | return ShouldRejectRequest(redirect_info.new_url); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void ExtensionThrottleManager::WillProcessResponse( |
| 114 | const GURL& response_url, |
Lucas Furukawa Gadani | 01188659 | 2019-10-23 17:12:24 | [diff] [blame] | 115 | const network::mojom::URLResponseHead& response_head) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 116 | if (response_head.network_accessed) { |
Chris Mumford | 475eede | 2018-07-25 20:55:03 | [diff] [blame] | 117 | // An entry GC when requests are outstanding can purge entries so check |
| 118 | // before use. |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 119 | base::AutoLock auto_lock(lock_); |
Chris Mumford | 475eede | 2018-07-25 20:55:03 | [diff] [blame] | 120 | auto it = url_entries_.find(GetIdFromUrl(response_url)); |
| 121 | if (it != url_entries_.end()) |
| 122 | it->second->UpdateWithResponse(response_head.headers->response_code()); |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 123 | } |
| 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) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 128 | base::AutoLock auto_lock(lock_); |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 129 | backoff_policy_for_tests_ = std::move(policy); |
xunjieli | e484e2d6 | 2015-06-19 14:31:21 | [diff] [blame] | 130 | } |
| 131 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 132 | void ExtensionThrottleManager::OverrideEntryForTests( |
| 133 | const GURL& url, |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 134 | std::unique_ptr<ExtensionThrottleEntry> entry) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 135 | base::AutoLock auto_lock(lock_); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 136 | // Normalize the url. |
| 137 | std::string url_id = GetIdFromUrl(url); |
| 138 | |
| 139 | // Periodically garbage collect old entries. |
| 140 | GarbageCollectEntriesIfNecessary(); |
| 141 | |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 142 | url_entries_[url_id] = std::move(entry); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void ExtensionThrottleManager::EraseEntryForTests(const GURL& url) { |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 146 | base::AutoLock auto_lock(lock_); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 147 | // Normalize the url. |
| 148 | std::string url_id = GetIdFromUrl(url); |
| 149 | url_entries_.erase(url_id); |
| 150 | } |
| 151 | |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 152 | void ExtensionThrottleManager::SetOnline(bool is_online) { |
Devlin Cronin | 5674beb | 2017-11-23 02:54:19 | [diff] [blame] | 153 | // When we switch from online to offline or change IP addresses, we |
| 154 | // clear all back-off history. This is a precaution in case the change in |
| 155 | // online state now lets us communicate without error with servers that |
| 156 | // we were previously getting 500 or 503 responses from (perhaps the |
| 157 | // responses are from a badly-written proxy that should have returned a |
| 158 | // 502 or 504 because it's upstream connection was down or it had no route |
| 159 | // to the server). |
| 160 | // Remove all entries. Any entries that in-flight requests have a reference |
| 161 | // to will live until those requests end, and these entries may be |
| 162 | // inconsistent with new entries for the same URLs, but since what we |
| 163 | // want is a clean slate for the new connection type, this is OK. |
Chris Mumford | 3f0eda9 | 2018-07-23 14:51:17 | [diff] [blame] | 164 | base::AutoLock auto_lock(lock_); |
Devlin Cronin | 5674beb | 2017-11-23 02:54:19 | [diff] [blame] | 165 | url_entries_.clear(); |
| 166 | requests_since_last_gc_ = 0; |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | std::string ExtensionThrottleManager::GetIdFromUrl(const GURL& url) const { |
| 170 | if (!url.is_valid()) |
| 171 | return url.possibly_invalid_spec(); |
| 172 | |
| 173 | GURL id = url.ReplaceComponents(url_id_replacements_); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 174 | return base::ToLowerASCII(id.spec()); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void ExtensionThrottleManager::GarbageCollectEntriesIfNecessary() { |
| 178 | requests_since_last_gc_++; |
| 179 | if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 180 | return; |
| 181 | requests_since_last_gc_ = 0; |
| 182 | |
| 183 | GarbageCollectEntries(); |
| 184 | } |
| 185 | |
| 186 | void ExtensionThrottleManager::GarbageCollectEntries() { |
Chris Mumford | c54b0c34 | 2018-07-23 21:29:46 | [diff] [blame] | 187 | base::EraseIf(url_entries_, [](const auto& entry) { |
| 188 | return entry.second->IsEntryOutdated(); |
| 189 | }); |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 190 | |
| 191 | // In case something broke we want to make sure not to grow indefinitely. |
| 192 | while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 193 | url_entries_.erase(url_entries_.begin()); |
| 194 | } |
| 195 | } |
| 196 | |
xunjieli | 413a6878 | 2015-06-16 17:15:43 | [diff] [blame] | 197 | } // namespace extensions |