Move android_app_version_* into an inner variables dict.
[chromium-blink-merge.git] / net / proxy / proxy_list.cc
blob524cc1583ebd60d7a41b9ace1af1ad3bd52f6c1e
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.
5 #include "net/proxy/proxy_list.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/strings/string_tokenizer.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "net/proxy/proxy_server.h"
14 using base::TimeDelta;
15 using base::TimeTicks;
17 namespace net {
19 ProxyList::ProxyList() {
22 ProxyList::~ProxyList() {
25 void ProxyList::Set(const std::string& proxy_uri_list) {
26 proxies_.clear();
27 base::StringTokenizer str_tok(proxy_uri_list, ";");
28 while (str_tok.GetNext()) {
29 ProxyServer uri = ProxyServer::FromURI(
30 str_tok.token_begin(), str_tok.token_end(), ProxyServer::SCHEME_HTTP);
31 // Silently discard malformed inputs.
32 if (uri.is_valid())
33 proxies_.push_back(uri);
37 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
38 proxies_.clear();
39 AddProxyServer(proxy_server);
42 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
43 if (proxy_server.is_valid())
44 proxies_.push_back(proxy_server);
47 void ProxyList::DeprioritizeBadProxies(
48 const ProxyRetryInfoMap& proxy_retry_info) {
49 // Partition the proxy list in two:
50 // (1) the known bad proxies
51 // (2) everything else
52 std::vector<ProxyServer> good_proxies;
53 std::vector<ProxyServer> bad_proxies_to_try;
55 std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
56 for (; iter != proxies_.end(); ++iter) {
57 ProxyRetryInfoMap::const_iterator bad_proxy =
58 proxy_retry_info.find(iter->ToURI());
59 if (bad_proxy != proxy_retry_info.end()) {
60 // This proxy is bad. Check if it's time to retry.
61 if (bad_proxy->second.bad_until >= TimeTicks::Now()) {
62 // still invalid.
63 if (bad_proxy->second.try_while_bad)
64 bad_proxies_to_try.push_back(*iter);
65 continue;
68 good_proxies.push_back(*iter);
71 // "proxies_ = good_proxies + bad_proxies"
72 proxies_.swap(good_proxies);
73 proxies_.insert(proxies_.end(), bad_proxies_to_try.begin(),
74 bad_proxies_to_try.end());
77 void ProxyList::RemoveProxiesWithoutScheme(int scheme_bit_field) {
78 for (std::vector<ProxyServer>::iterator it = proxies_.begin();
79 it != proxies_.end(); ) {
80 if (!(scheme_bit_field & it->scheme())) {
81 it = proxies_.erase(it);
82 continue;
84 ++it;
88 void ProxyList::Clear() {
89 proxies_.clear();
92 bool ProxyList::IsEmpty() const {
93 return proxies_.empty();
96 size_t ProxyList::size() const {
97 return proxies_.size();
100 // Returns true if |*this| lists the same proxies as |other|.
101 bool ProxyList::Equals(const ProxyList& other) const {
102 if (size() != other.size())
103 return false;
104 return proxies_ == other.proxies_;
107 const ProxyServer& ProxyList::Get() const {
108 DCHECK(!proxies_.empty());
109 return proxies_[0];
112 const std::vector<ProxyServer>& ProxyList::GetAll() const {
113 return proxies_;
116 void ProxyList::SetFromPacString(const std::string& pac_string) {
117 base::StringTokenizer entry_tok(pac_string, ";");
118 proxies_.clear();
119 while (entry_tok.GetNext()) {
120 ProxyServer uri = ProxyServer::FromPacString(
121 entry_tok.token_begin(), entry_tok.token_end());
122 // Silently discard malformed inputs.
123 if (uri.is_valid())
124 proxies_.push_back(uri);
127 // If we failed to parse anything from the PAC results list, fallback to
128 // DIRECT (this basically means an error in the PAC script).
129 if (proxies_.empty()) {
130 proxies_.push_back(ProxyServer::Direct());
134 std::string ProxyList::ToPacString() const {
135 std::string proxy_list;
136 std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
137 for (; iter != proxies_.end(); ++iter) {
138 if (!proxy_list.empty())
139 proxy_list += ";";
140 proxy_list += iter->ToPacString();
142 return proxy_list.empty() ? std::string() : proxy_list;
145 base::ListValue* ProxyList::ToValue() const {
146 base::ListValue* list = new base::ListValue();
147 for (size_t i = 0; i < proxies_.size(); ++i)
148 list->AppendString(proxies_[i].ToURI());
149 return list;
152 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
153 int net_error,
154 const BoundNetLog& net_log) {
156 // TODO(eroman): It would be good if instead of removing failed proxies
157 // from the list, we simply annotated them with the error code they failed
158 // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
159 // be given this information by the network transaction.
161 // The advantage of this approach is when the network transaction
162 // fails, we could output the full list of proxies that were attempted, and
163 // why each one of those failed (as opposed to just the last failure).
165 // And also, before failing the transaction wholesale, we could go back and
166 // retry the "bad proxies" which we never tried to begin with.
167 // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
168 // them from the list, so we would know what they were).
170 if (proxies_.empty()) {
171 NOTREACHED();
172 return false;
174 // By default, proxies are not retried for 5 minutes.
175 UpdateRetryInfoOnFallback(proxy_retry_info,
176 TimeDelta::FromMinutes(5),
177 true,
178 ProxyServer(),
179 net_error,
180 net_log);
182 // Remove this proxy from our list.
183 proxies_.erase(proxies_.begin());
184 return !proxies_.empty();
187 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
188 base::TimeDelta retry_delay,
189 bool try_while_bad,
190 const ProxyServer& proxy_to_retry,
191 int net_error,
192 const BoundNetLog& net_log) const {
193 // Mark this proxy as bad.
194 std::string proxy_key = proxy_to_retry.ToURI();
195 ProxyRetryInfoMap::iterator iter = proxy_retry_info->find(proxy_key);
196 if (iter != proxy_retry_info->end()) {
197 // TODO(nsylvain): This is not the first time we get this. We should
198 // double the retry time. Bug 997660.
199 iter->second.bad_until = TimeTicks::Now() + iter->second.current_delay;
200 } else {
201 ProxyRetryInfo retry_info;
202 retry_info.current_delay = retry_delay;
203 retry_info.bad_until = TimeTicks().Now() + retry_info.current_delay;
204 retry_info.try_while_bad = try_while_bad;
205 retry_info.net_error = net_error;
206 (*proxy_retry_info)[proxy_key] = retry_info;
208 net_log.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK,
209 NetLog::StringCallback("bad_proxy", &proxy_key));
212 void ProxyList::UpdateRetryInfoOnFallback(
213 ProxyRetryInfoMap* proxy_retry_info,
214 base::TimeDelta retry_delay,
215 bool reconsider,
216 const ProxyServer& another_proxy_to_bypass,
217 int net_error,
218 const BoundNetLog& net_log) const {
219 DCHECK(retry_delay != base::TimeDelta());
221 if (proxies_.empty()) {
222 NOTREACHED();
223 return;
226 if (!proxies_[0].is_direct()) {
227 AddProxyToRetryList(proxy_retry_info,
228 retry_delay,
229 reconsider,
230 proxies_[0],
231 net_error,
232 net_log);
234 // If an additional proxy to bypass is specified, add it to the retry map
235 // as well.
236 if (another_proxy_to_bypass.is_valid()) {
237 AddProxyToRetryList(proxy_retry_info,
238 retry_delay,
239 reconsider,
240 another_proxy_to_bypass,
241 net_error,
242 net_log);
247 } // namespace net