OLD | NEW |
1 // Copyright (c) 2012 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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 if (entry.get() && entry->IsEntryOutdated()) { | 68 if (entry.get() && entry->IsEntryOutdated()) { |
69 entry = NULL; | 69 entry = NULL; |
70 } | 70 } |
71 | 71 |
72 // Create the entry if needed. | 72 // Create the entry if needed. |
73 if (entry.get() == NULL) { | 73 if (entry.get() == NULL) { |
74 entry = new URLRequestThrottlerEntry(this, url_id); | 74 entry = new URLRequestThrottlerEntry(this, url_id); |
75 | 75 |
76 // We only disable back-off throttling on an entry that we have | 76 // We only disable back-off throttling on an entry that we have |
77 // just constructed. This is to allow unit tests to explicitly override | 77 // just constructed. This is to allow unit tests to explicitly override |
78 // the entry for localhost URLs. Given that we do not attempt to | 78 // the entry for localhost URLs. |
79 // disable throttling for entries already handed out (see comment | |
80 // in AddToOptOutList), this is not a problem. | |
81 std::string host = url.host(); | 79 std::string host = url.host(); |
82 if (opt_out_hosts_.find(host) != opt_out_hosts_.end() || | 80 if (IsLocalhost(host)) { |
83 IsLocalhost(host)) { | |
84 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { | 81 if (!logged_for_localhost_disabled_ && IsLocalhost(host)) { |
85 logged_for_localhost_disabled_ = true; | 82 logged_for_localhost_disabled_ = true; |
86 net_log_.AddEvent(NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, | 83 net_log_.AddEvent(NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, |
87 NetLog::StringCallback("host", &host)); | 84 NetLog::StringCallback("host", &host)); |
88 } | 85 } |
89 | 86 |
90 // TODO(joi): Once sliding window is separate from back-off throttling, | 87 // TODO(joi): Once sliding window is separate from back-off throttling, |
91 // we can simply return a dummy implementation of | 88 // we can simply return a dummy implementation of |
92 // URLRequestThrottlerEntryInterface here that never blocks anything (and | 89 // URLRequestThrottlerEntryInterface here that never blocks anything. |
93 // not keep entries in url_entries_ for opted-out sites). | |
94 entry->DisableBackoffThrottling(); | 90 entry->DisableBackoffThrottling(); |
95 } | 91 } |
96 } | 92 } |
97 | 93 |
98 return entry; | 94 return entry; |
99 } | 95 } |
100 | 96 |
101 void URLRequestThrottlerManager::AddToOptOutList(const std::string& host) { | |
102 // There is an edge case here that we are not handling, to keep things | |
103 // simple. If a host starts adding the opt-out header to its responses | |
104 // after there are already one or more entries in url_entries_ for that | |
105 // host, the pre-existing entries may still perform back-off throttling. | |
106 // In practice, this would almost never occur. | |
107 if (opt_out_hosts_.find(host) == opt_out_hosts_.end()) { | |
108 UMA_HISTOGRAM_COUNTS("Throttling.SiteOptedOut", 1); | |
109 | |
110 net_log_.EndEvent(NetLog::TYPE_THROTTLING_DISABLED_FOR_HOST, | |
111 NetLog::StringCallback("host", &host)); | |
112 opt_out_hosts_.insert(host); | |
113 } | |
114 } | |
115 | |
116 void URLRequestThrottlerManager::OverrideEntryForTests( | 97 void URLRequestThrottlerManager::OverrideEntryForTests( |
117 const GURL& url, | 98 const GURL& url, |
118 URLRequestThrottlerEntry* entry) { | 99 URLRequestThrottlerEntry* entry) { |
119 // Normalize the url. | 100 // Normalize the url. |
120 std::string url_id = GetIdFromUrl(url); | 101 std::string url_id = GetIdFromUrl(url); |
121 | 102 |
122 // Periodically garbage collect old entries. | 103 // Periodically garbage collect old entries. |
123 GarbageCollectEntriesIfNecessary(); | 104 GarbageCollectEntriesIfNecessary(); |
124 | 105 |
125 url_entries_[url_id] = entry; | 106 url_entries_[url_id] = entry; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 void URLRequestThrottlerManager::OnNetworkChange() { | 175 void URLRequestThrottlerManager::OnNetworkChange() { |
195 // Remove all entries. Any entries that in-flight requests have a reference | 176 // Remove all entries. Any entries that in-flight requests have a reference |
196 // to will live until those requests end, and these entries may be | 177 // to will live until those requests end, and these entries may be |
197 // inconsistent with new entries for the same URLs, but since what we | 178 // inconsistent with new entries for the same URLs, but since what we |
198 // want is a clean slate for the new connection type, this is OK. | 179 // want is a clean slate for the new connection type, this is OK. |
199 url_entries_.clear(); | 180 url_entries_.clear(); |
200 requests_since_last_gc_ = 0; | 181 requests_since_last_gc_ = 0; |
201 } | 182 } |
202 | 183 |
203 } // namespace net | 184 } // namespace net |
OLD | NEW |