blob: bc3a810aae74d3923ad9e6b471b45ac1f9bf51e9 [file] [log] [blame]
xunjieli413a68782015-06-16 17:15:431// 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
dchenge59eca1602015-12-18 17:48:007#include <utility>
8
xunjieli413a68782015-06-16 17:15:439#include "base/logging.h"
dchengf5d241082016-04-21 03:43:1110#include "base/memory/ptr_util.h"
xunjieli413a68782015-06-16 17:15:4311#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"
tfarina7ba5a622016-02-23 23:21:4416#include "net/base/url_util.h"
xunjieli413a68782015-06-16 17:15:4317#include "net/log/net_log.h"
mikecirone8b85c432016-09-08 19:11:0018#include "net/log/net_log_event_type.h"
19#include "net/log/net_log_source_type.h"
xunjieli413a68782015-06-16 17:15:4320#include "net/url_request/url_request.h"
21
22namespace extensions {
23
24const unsigned int ExtensionThrottleManager::kMaximumNumberOfEntries = 1500;
25const unsigned int ExtensionThrottleManager::kRequestsBetweenCollecting = 200;
26
27ExtensionThrottleManager::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
42ExtensionThrottleManager::~ExtensionThrottleManager() {
gab370841f22017-06-01 14:38:2643 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
xunjieli413a68782015-06-16 17:15:4344 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
dchengf5d241082016-04-21 03:43:1161std::unique_ptr<content::ResourceThrottle>
xunjieli413a68782015-06-16 17:15:4362ExtensionThrottleManager::MaybeCreateThrottle(const net::URLRequest* request) {
63 if (request->first_party_for_cookies().scheme() !=
64 extensions::kExtensionScheme) {
65 return nullptr;
66 }
ricea5e273372016-08-22 02:51:0367 return base::MakeUnique<extensions::ExtensionRequestLimitingThrottle>(request,
68 this);
xunjieli413a68782015-06-16 17:15:4369}
70
71scoped_refptr<ExtensionThrottleEntryInterface>
72ExtensionThrottleManager::RegisterRequestUrl(const GURL& url) {
gab370841f22017-06-01 14:38:2673#if DCHECK_IS_ON()
74 DCHECK(!enable_thread_checks_ || sequence_checker_.CalledOnValidSequence());
75#endif // DCHECK_IS_ON()
xunjieli413a68782015-06-16 17:15:4376
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) {
xunjielie484e2d62015-06-19 14:31:2196 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 }
xunjieli413a68782015-06-16 17:15:43104
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;
mikecirone8b85c432016-09-08 19:11:00112 net_log_.AddEvent(net::NetLogEventType::THROTTLING_DISABLED_FOR_HOST,
xunjieli413a68782015-06-16 17:15:43113 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
xunjielie484e2d62015-06-19 14:31:21126void ExtensionThrottleManager::SetBackoffPolicyForTests(
dchengf5d241082016-04-21 03:43:11127 std::unique_ptr<net::BackoffEntry::Policy> policy) {
dchenge59eca1602015-12-18 17:48:00128 backoff_policy_for_tests_ = std::move(policy);
xunjielie484e2d62015-06-19 14:31:21129}
130
xunjieli413a68782015-06-16 17:15:43131void 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
143void ExtensionThrottleManager::EraseEntryForTests(const GURL& url) {
144 // Normalize the url.
145 std::string url_id = GetIdFromUrl(url);
146 url_entries_.erase(url_id);
147}
148
149void ExtensionThrottleManager::SetIgnoreUserGestureLoadFlagForTests(
150 bool ignore_user_gesture_load_flag_for_tests) {
151 ignore_user_gesture_load_flag_for_tests_ = true;
152}
153
154void ExtensionThrottleManager::set_enable_thread_checks(bool enable) {
155 enable_thread_checks_ = enable;
156}
157
158bool ExtensionThrottleManager::enable_thread_checks() const {
159 return enable_thread_checks_;
160}
161
162void ExtensionThrottleManager::set_net_log(net::NetLog* net_log) {
163 DCHECK(net_log);
tfarina428341112016-09-22 13:38:20164 net_log_ = net::NetLogWithSource::Make(
mikecirone8b85c432016-09-08 19:11:00165 net_log, net::NetLogSourceType::EXPONENTIAL_BACKOFF_THROTTLING);
xunjieli413a68782015-06-16 17:15:43166}
167
168net::NetLog* ExtensionThrottleManager::net_log() const {
169 return net_log_.net_log();
170}
171
172void ExtensionThrottleManager::OnIPAddressChanged() {
173 OnNetworkChange();
174}
175
176void ExtensionThrottleManager::OnConnectionTypeChanged(
177 net::NetworkChangeNotifier::ConnectionType type) {
178 OnNetworkChange();
179}
180
181std::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_);
brettw8e2106d2015-08-11 19:30:22186 return base::ToLowerASCII(id.spec());
xunjieli413a68782015-06-16 17:15:43187}
188
189void 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
198void 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
214void 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