Import Mac cursor images from Blink repo
[chromium-blink-merge.git] / net / proxy / proxy_config.h
blobe558174a4658bc558afaa331035b766548274e92
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 #ifndef NET_PROXY_PROXY_CONFIG_H_
6 #define NET_PROXY_PROXY_CONFIG_H_
8 #include <string>
10 #include "net/base/net_export.h"
11 #include "net/proxy/proxy_bypass_rules.h"
12 #include "net/proxy/proxy_config_source.h"
13 #include "net/proxy/proxy_list.h"
14 #include "net/proxy/proxy_server.h"
15 #include "url/gurl.h"
17 namespace base {
18 class Value;
21 namespace net {
23 class ProxyInfo;
25 // ProxyConfig describes a user's proxy settings.
27 // There are two categories of proxy settings:
28 // (1) Automatic (indicates the methods to obtain a PAC script)
29 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns)
31 // When both automatic and manual settings are specified, the Automatic ones
32 // take precedence over the manual ones.
34 // For more details see:
35 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
36 class NET_EXPORT ProxyConfig {
37 public:
38 // ProxyRules describes the "manual" proxy settings.
39 // TODO(eroman): Turn this into a class.
40 // TODO(marq): Update the enum names; "TYPE_SINGLE_PROXY" really means
41 // the same set of proxies are used for all requests.
42 struct NET_EXPORT ProxyRules {
43 enum Type {
44 TYPE_NO_RULES,
45 TYPE_SINGLE_PROXY,
46 TYPE_PROXY_PER_SCHEME,
49 // Note that the default of TYPE_NO_RULES results in direct connections
50 // being made when using this ProxyConfig.
51 ProxyRules();
52 ~ProxyRules();
54 bool empty() const {
55 return type == TYPE_NO_RULES;
58 // Sets |result| with the proxies to use for |url| based on the current
59 // rules.
60 void Apply(const GURL& url, ProxyInfo* result) const;
62 // Parses the rules from a string, indicating which proxies to use.
64 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
66 // proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
68 // url-scheme = "http" | "https" | "ftp" | "socks"
70 // scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
72 // proxy-rules = scheme-proxies[";"<scheme-proxies>]
74 // Thus, the proxy-rules string should be a semicolon-separated list of
75 // ordered proxies that apply to a particular URL scheme. Unless specified,
76 // the proxy scheme for proxy-uris is assumed to be http.
78 // Some special cases:
79 // * If the scheme is omitted from the first proxy list, that list applies
80 // to all URL schemes and subsequent lists are ignored.
81 // * If a scheme is omitted from any proxy list after a list where a scheme
82 // has been provided, the list without a scheme is ignored.
83 // * If the url-scheme is set to 'socks', that sets a fallback list that
84 // to all otherwise unspecified url-schemes, however the default proxy-
85 // scheme for proxy urls in the 'socks' list is understood to be
86 // socks4:// if unspecified.
88 // For example:
89 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http://
90 // URLs, and HTTP proxy "foopy2:80" for
91 // ftp:// URLs.
92 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs.
93 // "foopy:80,bar,direct://" -- use HTTP proxy "foopy:80" for all URLs,
94 // failing over to "bar" if "foopy:80" is
95 // unavailable, and after that using no
96 // proxy.
97 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all
98 // URLs.
99 // "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
100 // and fail over to the SOCKS5 proxy
101 // "bar.com" if "foop" is unavailable.
102 // "http=foopy,direct:// -- use HTTP proxy "foopy" for http URLs,
103 // and use no proxy if "foopy" is
104 // unavailable.
105 // "http=foopy;socks=foopy2 -- use HTTP proxy "foopy" for http URLs,
106 // and use socks4://foopy2 for all other
107 // URLs.
108 void ParseFromString(const std::string& proxy_rules);
110 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp,
111 // &fallback_proxies}, or NULL if there is no proxy to use.
112 // Should only call this if the type is TYPE_PROXY_PER_SCHEME.
113 const ProxyList* MapUrlSchemeToProxyList(
114 const std::string& url_scheme) const;
116 // Returns true if |*this| describes the same configuration as |other|.
117 bool Equals(const ProxyRules& other) const;
119 // Exceptions for when not to use a proxy.
120 ProxyBypassRules bypass_rules;
122 // Reverse the meaning of |bypass_rules|.
123 bool reverse_bypass;
125 Type type;
127 // Set if |type| is TYPE_SINGLE_PROXY.
128 ProxyList single_proxies;
130 // Set if |type| is TYPE_PROXY_PER_SCHEME.
131 ProxyList proxies_for_http;
132 ProxyList proxies_for_https;
133 ProxyList proxies_for_ftp;
135 // Used when a fallback has been defined and the url to be proxied doesn't
136 // match any of the standard schemes.
137 ProxyList fallback_proxies;
139 private:
140 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp}
141 // or NULL if it is a scheme that we don't have a mapping
142 // for. Should only call this if the type is TYPE_PROXY_PER_SCHEME.
143 ProxyList* MapUrlSchemeToProxyListNoFallback(const std::string& scheme);
146 typedef int ID;
148 // Indicates an invalid proxy config.
149 static const ID kInvalidConfigID = 0;
151 ProxyConfig();
152 ProxyConfig(const ProxyConfig& config);
153 ~ProxyConfig();
154 ProxyConfig& operator=(const ProxyConfig& config);
156 // Used to numerically identify this configuration.
157 ID id() const { return id_; }
158 void set_id(ID id) { id_ = id; }
159 bool is_valid() const { return id_ != kInvalidConfigID; }
161 // Returns true if the given config is equivalent to this config. The
162 // comparison ignores differences in |id()| and |source()|.
163 bool Equals(const ProxyConfig& other) const;
165 // Returns true if this config contains any "automatic" settings. See the
166 // class description for what that means.
167 bool HasAutomaticSettings() const;
169 void ClearAutomaticSettings();
171 // Creates a Value dump of this configuration. The caller is responsible for
172 // deleting the returned value.
173 base::DictionaryValue* ToValue() const;
175 ProxyRules& proxy_rules() {
176 return proxy_rules_;
179 const ProxyRules& proxy_rules() const {
180 return proxy_rules_;
183 void set_pac_url(const GURL& url) {
184 pac_url_ = url;
187 const GURL& pac_url() const {
188 return pac_url_;
191 void set_pac_mandatory(bool enable_pac_mandatory) {
192 pac_mandatory_ = enable_pac_mandatory;
195 bool pac_mandatory() const {
196 return pac_mandatory_;
199 bool has_pac_url() const {
200 return pac_url_.is_valid();
203 void set_auto_detect(bool enable_auto_detect) {
204 auto_detect_ = enable_auto_detect;
207 bool auto_detect() const {
208 return auto_detect_;
211 void set_source(ProxyConfigSource source) {
212 source_ = source;
215 ProxyConfigSource source() const {
216 return source_;
219 // Helpers to construct some common proxy configurations.
221 static ProxyConfig CreateDirect() {
222 return ProxyConfig();
225 static ProxyConfig CreateAutoDetect() {
226 ProxyConfig config;
227 config.set_auto_detect(true);
228 return config;
231 static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) {
232 ProxyConfig config;
233 config.set_pac_url(pac_url);
234 // By default fall back to direct connection in case PAC script fails.
235 config.set_pac_mandatory(false);
236 return config;
239 private:
240 // True if the proxy configuration should be auto-detected.
241 bool auto_detect_;
243 // If non-empty, indicates the URL of the proxy auto-config file to use.
244 GURL pac_url_;
246 // If true, blocks all traffic in case fetching the pac script from |pac_url_|
247 // fails. Only valid if |pac_url_| is non-empty.
248 bool pac_mandatory_;
250 // Manual proxy settings.
251 ProxyRules proxy_rules_;
253 // Source of proxy settings.
254 ProxyConfigSource source_;
256 ID id_;
259 } // namespace net
263 #endif // NET_PROXY_PROXY_CONFIG_H_