Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / net / proxy / proxy_list.cc
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
5 #include "net/proxy/proxy_list.h"
6
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"
13
14 using base::TimeDelta;
15 using base::TimeTicks;
16
17 namespace net {
18
19 ProxyList::ProxyList() {
20 }
21
22 ProxyList::~ProxyList() {
23 }
24
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);
34   }
35 }
36
37 void ProxyList::SetSingleProxyServer(const ProxyServer& proxy_server) {
38   proxies_.clear();
39   AddProxyServer(proxy_server);
40 }
41
42 void ProxyList::AddProxyServer(const ProxyServer& proxy_server) {
43   if (proxy_server.is_valid())
44     proxies_.push_back(proxy_server);
45 }
46
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;
54
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;
66       }
67     }
68     good_proxies.push_back(*iter);
69   }
70
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());
75 }
76
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;
83     }
84     ++it;
85   }
86 }
87
88 void ProxyList::Clear() {
89   proxies_.clear();
90 }
91
92 bool ProxyList::IsEmpty() const {
93   return proxies_.empty();
94 }
95
96 size_t ProxyList::size() const {
97   return proxies_.size();
98 }
99
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_;
105 }
106
107 const ProxyServer& ProxyList::Get() const {
108   DCHECK(!proxies_.empty());
109   return proxies_[0];
110 }
111
112 void ProxyList::SetFromPacString(const std::string& pac_string) {
113   base::StringTokenizer entry_tok(pac_string, ";");
114   proxies_.clear();
115   while (entry_tok.GetNext()) {
116     ProxyServer uri = ProxyServer::FromPacString(
117         entry_tok.token_begin(), entry_tok.token_end());
118     // Silently discard malformed inputs.
119     if (uri.is_valid())
120       proxies_.push_back(uri);
121   }
122
123   // If we failed to parse anything from the PAC results list, fallback to
124   // DIRECT (this basically means an error in the PAC script).
125   if (proxies_.empty()) {
126     proxies_.push_back(ProxyServer::Direct());
127   }
128 }
129
130 std::string ProxyList::ToPacString() const {
131   std::string proxy_list;
132   std::vector<ProxyServer>::const_iterator iter = proxies_.begin();
133   for (; iter != proxies_.end(); ++iter) {
134     if (!proxy_list.empty())
135       proxy_list += ";";
136     proxy_list += iter->ToPacString();
137   }
138   return proxy_list.empty() ? std::string() : proxy_list;
139 }
140
141 base::ListValue* ProxyList::ToValue() const {
142   base::ListValue* list = new base::ListValue();
143   for (size_t i = 0; i < proxies_.size(); ++i)
144     list->AppendString(proxies_[i].ToURI());
145   return list;
146 }
147
148 bool ProxyList::Fallback(ProxyRetryInfoMap* proxy_retry_info,
149                          const BoundNetLog& net_log) {
150
151   // TODO(eroman): It would be good if instead of removing failed proxies
152   // from the list, we simply annotated them with the error code they failed
153   // with. Of course, ProxyService::ReconsiderProxyAfterError() would need to
154   // be given this information by the network transaction.
155   //
156   // The advantage of this approach is when the network transaction
157   // fails, we could output the full list of proxies that were attempted, and
158   // why each one of those failed (as opposed to just the last failure).
159   //
160   // And also, before failing the transaction wholesale, we could go back and
161   // retry the "bad proxies" which we never tried to begin with.
162   // (RemoveBadProxies would annotate them as 'expected bad' rather then delete
163   // them from the list, so we would know what they were).
164
165   if (proxies_.empty()) {
166     NOTREACHED();
167     return false;
168   }
169   // By default, proxies are not retried for 5 minutes.
170   UpdateRetryInfoOnFallback(proxy_retry_info,
171                             TimeDelta::FromMinutes(5),
172                             true,
173                             ProxyServer(),
174                             net_log);
175
176   // Remove this proxy from our list.
177   proxies_.erase(proxies_.begin());
178   return !proxies_.empty();
179 }
180
181 void ProxyList::AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
182                                     base::TimeDelta retry_delay,
183                                     bool try_while_bad,
184                                     const ProxyServer& proxy_to_retry,
185                                     const BoundNetLog& net_log) const {
186   // Mark this proxy as bad.
187   std::string proxy_key = proxy_to_retry.ToURI();
188   ProxyRetryInfoMap::iterator iter = proxy_retry_info->find(proxy_key);
189   if (iter != proxy_retry_info->end()) {
190     // TODO(nsylvain): This is not the first time we get this. We should
191     // double the retry time. Bug 997660.
192     iter->second.bad_until = TimeTicks::Now() + iter->second.current_delay;
193   } else {
194     ProxyRetryInfo retry_info;
195     retry_info.current_delay = retry_delay;
196     retry_info.bad_until = TimeTicks().Now() + retry_info.current_delay;
197     retry_info.try_while_bad = try_while_bad;
198     (*proxy_retry_info)[proxy_key] = retry_info;
199   }
200   net_log.AddEvent(NetLog::TYPE_PROXY_LIST_FALLBACK,
201                    NetLog::StringCallback("bad_proxy", &proxy_key));
202 }
203
204 void ProxyList::UpdateRetryInfoOnFallback(
205     ProxyRetryInfoMap* proxy_retry_info,
206     base::TimeDelta retry_delay,
207     bool reconsider,
208     const ProxyServer& another_proxy_to_bypass,
209     const BoundNetLog& net_log) const {
210   DCHECK(retry_delay != base::TimeDelta());
211
212   if (proxies_.empty()) {
213     NOTREACHED();
214     return;
215   }
216
217   if (!proxies_[0].is_direct()) {
218     AddProxyToRetryList(proxy_retry_info, retry_delay, reconsider, proxies_[0],
219                         net_log);
220
221     // If an additional proxy to bypass is specified, add it to the retry map
222     // as well.
223     if (another_proxy_to_bypass.is_valid()) {
224       AddProxyToRetryList(proxy_retry_info, retry_delay, reconsider,
225                           another_proxy_to_bypass, net_log);
226     }
227   }
228 }
229
230 }  // namespace net