1 // Copyright 2013 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 #include "chrome/browser/chromeos/ui_proxy_config.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/proxy_config/proxy_config_dictionary.h"
10 #include "net/proxy/proxy_config.h"
11 #include "url/url_constants.h"
14 const char kSocksScheme
[] = "socks";
19 UIProxyConfig::UIProxyConfig()
21 state(ProxyPrefs::CONFIG_UNSET
),
22 user_modifiable(true) {
25 UIProxyConfig::~UIProxyConfig() {
28 void UIProxyConfig::SetPacUrl(const GURL
& pac_url
) {
29 mode
= UIProxyConfig::MODE_PAC_SCRIPT
;
30 automatic_proxy
.pac_url
= pac_url
;
33 void UIProxyConfig::SetSingleProxy(const net::ProxyServer
& server
) {
34 mode
= UIProxyConfig::MODE_SINGLE_PROXY
;
35 single_proxy
.server
= server
;
38 void UIProxyConfig::SetProxyForScheme(const std::string
& scheme
,
39 const net::ProxyServer
& server
) {
40 ManualProxy
* proxy
= MapSchemeToProxy(scheme
);
42 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme
<< "]";
45 mode
= UIProxyConfig::MODE_PROXY_PER_SCHEME
;
46 proxy
->server
= server
;
49 void UIProxyConfig::SetBypassRules(const net::ProxyBypassRules
& rules
) {
50 if (mode
!= UIProxyConfig::MODE_SINGLE_PROXY
&&
51 mode
!= UIProxyConfig::MODE_PROXY_PER_SCHEME
) {
52 NOTREACHED() << "Cannot set bypass rules for proxy mode [" << mode
<< "]";
58 bool UIProxyConfig::FromNetProxyConfig(const net::ProxyConfig
& net_config
) {
59 *this = UIProxyConfig(); // Reset to default.
60 const net::ProxyConfig::ProxyRules
& rules
= net_config
.proxy_rules();
62 case net::ProxyConfig::ProxyRules::TYPE_NO_RULES
:
63 if (!net_config
.HasAutomaticSettings()) {
64 mode
= UIProxyConfig::MODE_DIRECT
;
65 } else if (net_config
.auto_detect()) {
66 mode
= UIProxyConfig::MODE_AUTO_DETECT
;
67 } else if (net_config
.has_pac_url()) {
68 mode
= UIProxyConfig::MODE_PAC_SCRIPT
;
69 automatic_proxy
.pac_url
= net_config
.pac_url();
74 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY
:
75 if (rules
.single_proxies
.IsEmpty())
77 mode
= MODE_SINGLE_PROXY
;
78 single_proxy
.server
= rules
.single_proxies
.Get();
79 bypass_rules
= rules
.bypass_rules
;
81 case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME
:
82 // Make sure we have valid server for at least one of the protocols.
83 if (rules
.proxies_for_http
.IsEmpty() &&
84 rules
.proxies_for_https
.IsEmpty() &&
85 rules
.proxies_for_ftp
.IsEmpty() &&
86 rules
.fallback_proxies
.IsEmpty()) {
89 mode
= MODE_PROXY_PER_SCHEME
;
90 if (!rules
.proxies_for_http
.IsEmpty())
91 http_proxy
.server
= rules
.proxies_for_http
.Get();
92 if (!rules
.proxies_for_https
.IsEmpty())
93 https_proxy
.server
= rules
.proxies_for_https
.Get();
94 if (!rules
.proxies_for_ftp
.IsEmpty())
95 ftp_proxy
.server
= rules
.proxies_for_ftp
.Get();
96 if (!rules
.fallback_proxies
.IsEmpty())
97 socks_proxy
.server
= rules
.fallback_proxies
.Get();
98 bypass_rules
= rules
.bypass_rules
;
101 NOTREACHED() << "Unrecognized proxy config mode";
107 base::DictionaryValue
* UIProxyConfig::ToPrefProxyConfig() const {
110 return ProxyConfigDictionary::CreateDirect();
112 case MODE_AUTO_DETECT
: {
113 return ProxyConfigDictionary::CreateAutoDetect();
115 case MODE_PAC_SCRIPT
: {
116 return ProxyConfigDictionary::CreatePacScript(
117 automatic_proxy
.pac_url
.spec(), false);
119 case MODE_SINGLE_PROXY
: {
121 if (single_proxy
.server
.is_valid())
122 spec
= single_proxy
.server
.ToURI();
123 return ProxyConfigDictionary::CreateFixedServers(
124 spec
, bypass_rules
.ToString());
126 case MODE_PROXY_PER_SCHEME
: {
128 ProxyConfigDictionary::EncodeAndAppendProxyServer(
129 url::kHttpScheme
, http_proxy
.server
, &spec
);
130 ProxyConfigDictionary::EncodeAndAppendProxyServer(
131 url::kHttpsScheme
, https_proxy
.server
, &spec
);
132 ProxyConfigDictionary::EncodeAndAppendProxyServer(
133 url::kFtpScheme
, ftp_proxy
.server
, &spec
);
134 ProxyConfigDictionary::EncodeAndAppendProxyServer(
135 kSocksScheme
, socks_proxy
.server
, &spec
);
136 return ProxyConfigDictionary::CreateFixedServers(
137 spec
, bypass_rules
.ToString());
142 NOTREACHED() << "Unrecognized proxy config mode for preference";
146 UIProxyConfig::ManualProxy
* UIProxyConfig::MapSchemeToProxy(
147 const std::string
& scheme
) {
148 if (scheme
== url::kHttpScheme
)
150 if (scheme
== url::kHttpsScheme
)
152 if (scheme
== url::kFtpScheme
)
154 if (scheme
== kSocksScheme
)
156 NOTREACHED() << "Invalid scheme: " << scheme
;
160 } // namespace chromeos