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