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_
10 #include "googleurl/src/gurl.h"
11 #include "net/base/net_export.h"
12 #include "net/proxy/proxy_bypass_rules.h"
13 #include "net/proxy/proxy_config_source.h"
14 #include "net/proxy/proxy_server.h"
24 // ProxyConfig describes a user's proxy settings.
26 // There are two categories of proxy settings:
27 // (1) Automatic (indicates the methods to obtain a PAC script)
28 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns)
30 // When both automatic and manual settings are specified, the Automatic ones
31 // take precedence over the manual ones.
33 // For more details see:
34 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
35 class NET_EXPORT ProxyConfig
{
37 // ProxyRules describes the "manual" proxy settings.
38 // TODO(eroman): Turn this into a class.
39 struct NET_EXPORT ProxyRules
{
43 TYPE_PROXY_PER_SCHEME
,
46 // Note that the default of TYPE_NO_RULES results in direct connections
47 // being made when using this ProxyConfig.
52 return type
== TYPE_NO_RULES
;
55 // Sets |result| with the proxy to use for |url| based on the current rules.
56 void Apply(const GURL
& url
, ProxyInfo
* result
) const;
58 // Parses the rules from a string, indicating which proxies to use.
60 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
62 // If the proxy to use depends on the scheme of the URL, can instead specify
63 // a semicolon separated list of:
65 // <url-scheme>"="<proxy-uri>
68 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http://
69 // URLs, and HTTP proxy "foopy2:80" for
71 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs.
72 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all
74 void ParseFromString(const std::string
& proxy_rules
);
76 // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp,
77 // &fallback_proxy}, or NULL if there is no proxy to use.
78 // Should only call this if the type is TYPE_PROXY_PER_SCHEME.
79 const ProxyServer
* MapUrlSchemeToProxy(const std::string
& url_scheme
) const;
81 // Returns true if |*this| describes the same configuration as |other|.
82 bool Equals(const ProxyRules
& other
) const;
84 // Exceptions for when not to use a proxy.
85 ProxyBypassRules bypass_rules
;
87 // Reverse the meaning of |bypass_rules|.
92 // Set if |type| is TYPE_SINGLE_PROXY.
93 ProxyServer single_proxy
;
95 // Set if |type| is TYPE_PROXY_PER_SCHEME.
96 ProxyServer proxy_for_http
;
97 ProxyServer proxy_for_https
;
98 ProxyServer proxy_for_ftp
;
100 // Used when there isn't a more specific per-scheme proxy server.
101 ProxyServer fallback_proxy
;
104 // Returns one of {&proxy_for_http, &proxy_for_https, &proxy_for_ftp}
105 // or NULL if it is a scheme that we don't have a mapping
106 // for. Should only call this if the type is TYPE_PROXY_PER_SCHEME.
107 ProxyServer
* MapUrlSchemeToProxyNoFallback(const std::string
& scheme
);
112 // Indicates an invalid proxy config.
113 static const ID kInvalidConfigID
= 0;
116 ProxyConfig(const ProxyConfig
& config
);
118 ProxyConfig
& operator=(const ProxyConfig
& config
);
120 // Used to numerically identify this configuration.
121 ID
id() const { return id_
; }
122 void set_id(ID id
) { id_
= id
; }
123 bool is_valid() const { return id_
!= kInvalidConfigID
; }
125 // Returns true if the given config is equivalent to this config. The
126 // comparison ignores differences in |id()| and |source()|.
127 bool Equals(const ProxyConfig
& other
) const;
129 // Returns true if this config contains any "automatic" settings. See the
130 // class description for what that means.
131 bool HasAutomaticSettings() const;
133 void ClearAutomaticSettings();
135 // Creates a Value dump of this configuration. The caller is responsible for
136 // deleting the returned value.
137 base::Value
* ToValue() const;
139 ProxyRules
& proxy_rules() {
143 const ProxyRules
& proxy_rules() const {
147 void set_pac_url(const GURL
& url
) {
151 const GURL
& pac_url() const {
155 void set_pac_mandatory(bool enable_pac_mandatory
) {
156 pac_mandatory_
= enable_pac_mandatory
;
159 bool pac_mandatory() const {
160 return pac_mandatory_
;
163 bool has_pac_url() const {
164 return pac_url_
.is_valid();
167 void set_auto_detect(bool enable_auto_detect
) {
168 auto_detect_
= enable_auto_detect
;
171 bool auto_detect() const {
175 void set_source(ProxyConfigSource source
) {
179 ProxyConfigSource
source() const {
183 // Helpers to construct some common proxy configurations.
185 static ProxyConfig
CreateDirect() {
186 return ProxyConfig();
189 static ProxyConfig
CreateAutoDetect() {
191 config
.set_auto_detect(true);
195 static ProxyConfig
CreateFromCustomPacURL(const GURL
& pac_url
) {
197 config
.set_pac_url(pac_url
);
198 // By default fall back to direct connection in case PAC script fails.
199 config
.set_pac_mandatory(false);
204 // True if the proxy configuration should be auto-detected.
207 // If non-empty, indicates the URL of the proxy auto-config file to use.
210 // If true, blocks all traffic in case fetching the pac script from |pac_url_|
211 // fails. Only valid if |pac_url_| is non-empty.
214 // Manual proxy settings.
215 ProxyRules proxy_rules_
;
217 // Source of proxy settings.
218 ProxyConfigSource source_
;
227 #endif // NET_PROXY_PROXY_CONFIG_H_