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 #ifndef NET_PROXY_RESOLUTION_PROXY_LIST_H_
6 #define NET_PROXY_RESOLUTION_PROXY_LIST_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "net/base/net_export.h"
15 #include "net/proxy_resolution/proxy_retry_info.h"
16 
17 namespace base {
18 class TimeDelta;
19 class Value;
20 }
21 
22 namespace net {
23 
24 class ProxyServer;
25 class NetLogWithSource;
26 
27 // This class is used to hold a list of proxies returned by GetProxyForUrl or
28 // manually configured. It handles proxy fallback if multiple servers are
29 // specified.
30 class NET_EXPORT_PRIVATE ProxyList {
31  public:
32   ProxyList();
33   ProxyList(const ProxyList& other);
34   ProxyList(ProxyList&& other);
35   ProxyList& operator=(const ProxyList& other);
36   ProxyList& operator=(ProxyList&& other);
37   ~ProxyList();
38 
39   // Initializes the proxy list to a string containing one or more proxy servers
40   // delimited by a semicolon.
41   void Set(const std::string& proxy_uri_list);
42 
43   // Set the proxy list to a single entry, |proxy_server|.
44   void SetSingleProxyServer(const ProxyServer& proxy_server);
45 
46   // Append a single proxy server to the end of the proxy list.
47   void AddProxyServer(const ProxyServer& proxy_server);
48 
49   // De-prioritizes the proxies that are cached as not working but are allowed
50   // to be reconsidered, by moving them to the end of the fallback list.
51   void DeprioritizeBadProxies(const ProxyRetryInfoMap& proxy_retry_info);
52 
53   // Delete any entry which doesn't have one of the specified proxy schemes.
54   // |scheme_bit_field| is a bunch of ProxyServer::Scheme bitwise ORed together.
55   void RemoveProxiesWithoutScheme(int scheme_bit_field);
56 
57   // Clear the proxy list.
58   void Clear();
59 
60   // Returns true if there is nothing left in the ProxyList.
61   bool IsEmpty() const;
62 
63   // Returns the number of proxy servers in this list.
64   size_t size() const;
65 
66   // Returns true if |*this| lists the same proxies as |other|.
67   bool Equals(const ProxyList& other) const;
68 
69   // Returns the first proxy server in the list. It is only valid to call
70   // this if !IsEmpty().
71   const ProxyServer& Get() const;
72 
73   // Returns all proxy servers in the list.
74   const std::vector<ProxyServer>& GetAll() const;
75 
76   // Sets the list by parsing the PAC result |pac_string|.
77   // Some examples for |pac_string|:
78   //   "DIRECT"
79   //   "PROXY foopy1"
80   //   "PROXY foopy1; SOCKS4 foopy2:1188"
81   // Does a best-effort parse, and silently discards any errors.
82   void SetFromPacString(const std::string& pac_string);
83 
84   // Returns a PAC-style semicolon-separated list of valid proxy servers.
85   // For example: "PROXY xxx.xxx.xxx.xxx:xx; SOCKS yyy.yyy.yyy:yy".
86   std::string ToPacString() const;
87 
88   // Returns a serialized value for the list.
89   base::Value ToValue() const;
90 
91   // Marks the current proxy server as bad and deletes it from the list. The
92   // list of known bad proxies is given by |proxy_retry_info|. |net_error|
93   // should contain the network error encountered when this proxy was tried, if
94   // any. If this fallback is not because of a network error, then |OK| should
95   // be passed in (eg. for reasons such as local policy). Returns true if there
96   // is another server available in the list.
97   bool Fallback(ProxyRetryInfoMap* proxy_retry_info,
98                 int net_error,
99                 const NetLogWithSource& net_log);
100 
101   // Updates |proxy_retry_info| to indicate that the first proxy in the list
102   // is bad. This is distinct from Fallback(), above, to allow updating proxy
103   // retry information without modifying a given transction's proxy list. Will
104   // retry after |retry_delay| if positive, and will use the default proxy retry
105   // duration otherwise. It may reconsider the proxy beforehand if |reconsider|
106   // is true. Additionally updates |proxy_retry_info| with
107   // |additional_proxies_to_bypass|. |net_error| should contain the network
108   // error countered when this proxy was tried, or OK if the proxy retry info is
109   // being updated for a non-network related reason (e.g. local policy).
110   void UpdateRetryInfoOnFallback(
111       ProxyRetryInfoMap* proxy_retry_info,
112       base::TimeDelta retry_delay,
113       bool reconsider,
114       const std::vector<ProxyServer>& additional_proxies_to_bypass,
115       int net_error,
116       const NetLogWithSource& net_log) const;
117 
118  private:
119   // Updates |proxy_retry_info| to indicate that the |proxy_to_retry| in
120   // |proxies_| is bad for |retry_delay|, but may be reconsidered earlier if
121   // |try_while_bad| is true. |net_error| should contain the network error
122   // countered when this proxy was tried, or OK if the proxy retry info is
123   // being updated for a non-network related reason (e.g. local policy).
124   void AddProxyToRetryList(ProxyRetryInfoMap* proxy_retry_info,
125                            base::TimeDelta retry_delay,
126                            bool try_while_bad,
127                            const ProxyServer& proxy_to_retry,
128                            int net_error,
129                            const NetLogWithSource& net_log) const;
130 
131   // List of proxies.
132   std::vector<ProxyServer> proxies_;
133 };
134 
135 }  // namespace net
136 
137 #endif  // NET_PROXY_RESOLUTION_PROXY_LIST_H_
138