OLD | NEW |
1 // Copyright (c) 2011 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" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // We only disable back-off throttling on an entry that we have | 58 // We only disable back-off throttling on an entry that we have |
59 // just constructed. This is to allow unit tests to explicitly override | 59 // just constructed. This is to allow unit tests to explicitly override |
60 // the entry for localhost URLs. Given that we do not attempt to | 60 // the entry for localhost URLs. Given that we do not attempt to |
61 // disable throttling for entries already handed out (see comment | 61 // disable throttling for entries already handed out (see comment |
62 // in AddToOptOutList), this is not a problem. | 62 // in AddToOptOutList), this is not a problem. |
63 std::string host = url.host(); | 63 std::string host = url.host(); |
64 if (opt_out_hosts_.find(host) != opt_out_hosts_.end() || | 64 if (opt_out_hosts_.find(host) != opt_out_hosts_.end() || |
65 IsLocalhost(host)) { | 65 IsLocalhost(host)) { |
66 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { | 66 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { |
67 logged_for_localhost_disabled_ = true; | 67 logged_for_localhost_disabled_ = true; |
68 net_log_->AddEvent( | 68 net_log_.AddEvent( |
69 NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, | 69 NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, |
70 make_scoped_refptr(new NetLogStringParameter("host", host))); | 70 make_scoped_refptr(new NetLogStringParameter("host", host))); |
71 } | 71 } |
72 | 72 |
73 // TODO(joi): Once sliding window is separate from back-off throttling, | 73 // TODO(joi): Once sliding window is separate from back-off throttling, |
74 // we can simply return a dummy implementation of | 74 // we can simply return a dummy implementation of |
75 // URLRequestThrottlerEntryInterface here that never blocks anything (and | 75 // URLRequestThrottlerEntryInterface here that never blocks anything (and |
76 // not keep entries in url_entries_ for opted-out sites). | 76 // not keep entries in url_entries_ for opted-out sites). |
77 entry->DisableBackoffThrottling(); | 77 entry->DisableBackoffThrottling(); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 return entry; | 81 return entry; |
82 } | 82 } |
83 | 83 |
84 void URLRequestThrottlerManager::AddToOptOutList(const std::string& host) { | 84 void URLRequestThrottlerManager::AddToOptOutList(const std::string& host) { |
85 // There is an edge case here that we are not handling, to keep things | 85 // There is an edge case here that we are not handling, to keep things |
86 // simple. If a host starts adding the opt-out header to its responses | 86 // simple. If a host starts adding the opt-out header to its responses |
87 // after there are already one or more entries in url_entries_ for that | 87 // after there are already one or more entries in url_entries_ for that |
88 // host, the pre-existing entries may still perform back-off throttling. | 88 // host, the pre-existing entries may still perform back-off throttling. |
89 // In practice, this would almost never occur. | 89 // In practice, this would almost never occur. |
90 if (opt_out_hosts_.find(host) == opt_out_hosts_.end()) { | 90 if (opt_out_hosts_.find(host) == opt_out_hosts_.end()) { |
91 UMA_HISTOGRAM_COUNTS("Throttling.SiteOptedOut", 1); | 91 UMA_HISTOGRAM_COUNTS("Throttling.SiteOptedOut", 1); |
92 | 92 |
93 net_log_->EndEvent( | 93 net_log_.EndEvent( |
94 NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, | 94 NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, |
95 make_scoped_refptr(new NetLogStringParameter("host", host))); | 95 make_scoped_refptr(new NetLogStringParameter("host", host))); |
96 opt_out_hosts_.insert(host); | 96 opt_out_hosts_.insert(host); |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 void URLRequestThrottlerManager::OverrideEntryForTests( | 100 void URLRequestThrottlerManager::OverrideEntryForTests( |
101 const GURL& url, | 101 const GURL& url, |
102 URLRequestThrottlerEntry* entry) { | 102 URLRequestThrottlerEntry* entry) { |
103 // Normalize the url. | 103 // Normalize the url. |
(...skipping 22 matching lines...) Expand all Loading... |
126 void URLRequestThrottlerManager::set_enforce_throttling(bool enforce) { | 126 void URLRequestThrottlerManager::set_enforce_throttling(bool enforce) { |
127 enforce_throttling_ = enforce; | 127 enforce_throttling_ = enforce; |
128 } | 128 } |
129 | 129 |
130 bool URLRequestThrottlerManager::enforce_throttling() { | 130 bool URLRequestThrottlerManager::enforce_throttling() { |
131 return enforce_throttling_; | 131 return enforce_throttling_; |
132 } | 132 } |
133 | 133 |
134 void URLRequestThrottlerManager::set_net_log(NetLog* net_log) { | 134 void URLRequestThrottlerManager::set_net_log(NetLog* net_log) { |
135 DCHECK(net_log); | 135 DCHECK(net_log); |
136 NetLog::Source source(NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING, | 136 net_log_ = BoundNetLog::Make(net_log, |
137 net_log->NextID()); | 137 NetLog::SOURCE_EXPONENTIAL_BACKOFF_THROTTLING); |
138 net_log_.reset(new BoundNetLog(source, net_log)); | |
139 } | 138 } |
140 | 139 |
141 NetLog* URLRequestThrottlerManager::net_log() const { | 140 NetLog* URLRequestThrottlerManager::net_log() const { |
142 return net_log_->net_log(); | 141 return net_log_.net_log(); |
143 } | 142 } |
144 | 143 |
145 void URLRequestThrottlerManager::OnIPAddressChanged() { | 144 void URLRequestThrottlerManager::OnIPAddressChanged() { |
146 OnNetworkChange(); | 145 OnNetworkChange(); |
147 } | 146 } |
148 | 147 |
149 void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { | 148 void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { |
150 OnNetworkChange(); | 149 OnNetworkChange(); |
151 } | 150 } |
152 | 151 |
153 // TODO(joi): Turn throttling on by default when appropriate. | 152 // TODO(joi): Turn throttling on by default when appropriate. |
154 URLRequestThrottlerManager::URLRequestThrottlerManager() | 153 URLRequestThrottlerManager::URLRequestThrottlerManager() |
155 : requests_since_last_gc_(0), | 154 : requests_since_last_gc_(0), |
156 enforce_throttling_(false), | 155 enforce_throttling_(false), |
157 enable_thread_checks_(false), | 156 enable_thread_checks_(false), |
158 logged_for_localhost_disabled_(false), | 157 logged_for_localhost_disabled_(false), |
159 registered_from_thread_(base::kInvalidThreadId) { | 158 registered_from_thread_(base::kInvalidThreadId) { |
160 // Construction/destruction is on main thread (because BrowserMain | 159 // Construction/destruction is on main thread (because BrowserMain |
161 // retrieves an instance to call InitializeOptions), but is from then on | 160 // retrieves an instance to call InitializeOptions), but is from then on |
162 // used on I/O thread. | 161 // used on I/O thread. |
163 DetachFromThread(); | 162 DetachFromThread(); |
164 | 163 |
165 url_id_replacements_.ClearPassword(); | 164 url_id_replacements_.ClearPassword(); |
166 url_id_replacements_.ClearUsername(); | 165 url_id_replacements_.ClearUsername(); |
167 url_id_replacements_.ClearQuery(); | 166 url_id_replacements_.ClearQuery(); |
168 url_id_replacements_.ClearRef(); | 167 url_id_replacements_.ClearRef(); |
169 | |
170 // Make sure there is always a net_log_ instance, even if it logs to | |
171 // nowhere. | |
172 net_log_.reset(new BoundNetLog()); | |
173 } | 168 } |
174 | 169 |
175 URLRequestThrottlerManager::~URLRequestThrottlerManager() { | 170 URLRequestThrottlerManager::~URLRequestThrottlerManager() { |
176 // Destruction is on main thread (AtExit), but real use is on I/O thread. | 171 // Destruction is on main thread (AtExit), but real use is on I/O thread. |
177 // The AtExit manager does not run until the I/O thread has finished | 172 // The AtExit manager does not run until the I/O thread has finished |
178 // processing. | 173 // processing. |
179 DetachFromThread(); | 174 DetachFromThread(); |
180 | 175 |
181 // We must currently skip this in the production case, where the destructor | 176 // We must currently skip this in the production case, where the destructor |
182 // does not run on the thread we registered from. | 177 // does not run on the thread we registered from. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 void URLRequestThrottlerManager::OnNetworkChange() { | 233 void URLRequestThrottlerManager::OnNetworkChange() { |
239 // Remove all entries. Any entries that in-flight requests have a reference | 234 // Remove all entries. Any entries that in-flight requests have a reference |
240 // to will live until those requests end, and these entries may be | 235 // to will live until those requests end, and these entries may be |
241 // inconsistent with new entries for the same URLs, but since what we | 236 // inconsistent with new entries for the same URLs, but since what we |
242 // want is a clean slate for the new connection state, this is OK. | 237 // want is a clean slate for the new connection state, this is OK. |
243 url_entries_.clear(); | 238 url_entries_.clear(); |
244 requests_since_last_gc_ = 0; | 239 requests_since_last_gc_ = 0; |
245 } | 240 } |
246 | 241 |
247 } // namespace net | 242 } // namespace net |
OLD | NEW |