Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / net / proxy / proxy_info.h
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_PROXY_INFO_H_
6 #define NET_PROXY_PROXY_INFO_H_
7
8 #include <string>
9
10 #include "base/time/time.h"
11 #include "net/base/net_export.h"
12 #include "net/base/net_log.h"
13 #include "net/proxy/proxy_config.h"
14 #include "net/proxy/proxy_list.h"
15 #include "net/proxy/proxy_retry_info.h"
16 #include "net/proxy/proxy_server.h"
17
18 namespace net {
19
20 // This object holds proxy information returned by ResolveProxy.
21 class NET_EXPORT ProxyInfo {
22  public:
23   ProxyInfo();
24   ~ProxyInfo();
25   // Default copy-constructor and assignment operator are OK!
26
27   // Uses the same proxy server as the given |proxy_info|.
28   void Use(const ProxyInfo& proxy_info);
29
30   // Uses a direct connection.
31   void UseDirect();
32
33   // Uses a direct connection. did_bypass_proxy() will return true to indicate
34   // that the direct connection is the result of configured proxy bypass rules.
35   void UseDirectWithBypassedProxy();
36
37   // Uses a specific proxy server, of the form:
38   //   proxy-uri = [<scheme> "://"] <hostname> [":" <port>]
39   // This may optionally be a semi-colon delimited list of <proxy-uri>.
40   // It is OK to have LWS between entries.
41   void UseNamedProxy(const std::string& proxy_uri_list);
42
43   // Sets the proxy list to a single entry, |proxy_server|.
44   void UseProxyServer(const ProxyServer& proxy_server);
45
46   // Parses from the given PAC result.
47   void UsePacString(const std::string& pac_string);
48
49   // Use the proxies from the given list.
50   void UseProxyList(const ProxyList& proxy_list);
51
52   // Returns true if this proxy info specifies a direct connection.
53   bool is_direct() const {
54     // We don't implicitly fallback to DIRECT unless it was added to the list.
55     if (is_empty())
56       return false;
57     return proxy_list_.Get().is_direct();
58   }
59
60   bool is_direct_only() const {
61     return is_direct() && proxy_list_.size() == 1 && proxy_retry_info_.empty();
62   }
63
64   // Returns true if the first valid proxy server is an https proxy.
65   bool is_https() const {
66     if (is_empty())
67       return false;
68     return proxy_server().is_https();
69   }
70
71   // Returns true if the first valid proxy server is an http proxy.
72   bool is_http() const {
73     if (is_empty())
74       return false;
75     return proxy_server().is_http();
76   }
77
78   // Returns true if the first valid proxy server is a quic proxy.
79   bool is_quic() const {
80     if (is_empty())
81       return false;
82     return proxy_server().is_quic();
83   }
84
85   // Returns true if the first valid proxy server is a socks server.
86   bool is_socks() const {
87     if (is_empty())
88       return false;
89     return proxy_server().is_socks();
90   }
91
92   // Returns true if this proxy info has no proxies left to try.
93   bool is_empty() const {
94     return proxy_list_.IsEmpty();
95   }
96
97   // Returns true if this proxy resolution is using a direct connection due to
98   // proxy bypass rules.
99   bool did_bypass_proxy() const {
100     return did_bypass_proxy_;
101   }
102
103   // Returns true if the proxy resolution was done using a PAC script.
104   bool did_use_pac_script() const {
105     return did_use_pac_script_;
106   }
107
108   // Returns the first valid proxy server. is_empty() must be false to be able
109   // to call this function.
110   const ProxyServer& proxy_server() const { return proxy_list_.Get(); }
111
112   // Returns the source for configuration settings used for proxy resolution.
113   ProxyConfigSource config_source() const { return config_source_; }
114
115   // See description in ProxyList::ToPacString().
116   std::string ToPacString() const;
117
118   // Marks the current proxy as bad. |net_error| should contain the network
119   // error encountered when this proxy was tried, if any. If this fallback
120   // is not because of a network error, then |OK| should be passed in (eg. for
121   // reasons such as local policy). Returns true if there is another proxy is
122   // available to try in proxy list_.
123   bool Fallback(int net_error, const BoundNetLog& net_log);
124
125   // De-prioritizes the proxies that we have cached as not working, by moving
126   // them to the end of the proxy list.
127   void DeprioritizeBadProxies(const ProxyRetryInfoMap& proxy_retry_info);
128
129   // Deletes any entry which doesn't have one of the specified proxy schemes.
130   void RemoveProxiesWithoutScheme(int scheme_bit_field);
131
132   ProxyConfig::ID config_id() const { return config_id_; }
133
134   base::TimeTicks proxy_resolve_start_time() const {
135     return proxy_resolve_start_time_;
136   }
137
138   base::TimeTicks proxy_resolve_end_time() const {
139     return proxy_resolve_end_time_;
140   }
141
142  private:
143   friend class ProxyService;
144
145   const ProxyRetryInfoMap& proxy_retry_info() const {
146     return proxy_retry_info_;
147   }
148
149   // Reset proxy and config settings.
150   void Reset();
151
152   // The ordered list of proxy servers (including DIRECT attempts) remaining to
153   // try. If proxy_list_ is empty, then there is nothing left to fall back to.
154   ProxyList proxy_list_;
155
156   // List of proxies that have been tried already.
157   ProxyRetryInfoMap proxy_retry_info_;
158
159   // This value identifies the proxy config used to initialize this object.
160   ProxyConfig::ID config_id_;
161
162   // The source of the proxy settings used,
163   ProxyConfigSource config_source_;
164
165   // Whether the proxy result represent a proxy bypass.
166   bool did_bypass_proxy_;
167
168   // Whether we used a PAC script for resolving the proxy.
169   bool did_use_pac_script_;
170
171   // How long it took to resolve the proxy.  Times are both null if proxy was
172   // determined synchronously without running a PAC.
173   base::TimeTicks proxy_resolve_start_time_;
174   base::TimeTicks proxy_resolve_end_time_;
175 };
176
177 }  // namespace net
178
179 #endif  // NET_PROXY_PROXY_INFO_H_