Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / ui_proxy_config.cc
blob7f37803cc66c817e0d3fdde0f0af025b85d42442
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"
13 namespace {
14 const char kSocksScheme[] = "socks";
17 namespace chromeos {
19 UIProxyConfig::UIProxyConfig()
20 : mode(MODE_DIRECT),
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);
41 if (!proxy) {
42 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme << "]";
43 return;
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 << "]";
53 return;
55 bypass_rules = rules;
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();
61 switch (rules.type) {
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();
70 } else {
71 return false;
73 return true;
74 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
75 if (rules.single_proxies.IsEmpty())
76 return false;
77 mode = MODE_SINGLE_PROXY;
78 single_proxy.server = rules.single_proxies.Get();
79 bypass_rules = rules.bypass_rules;
80 return true;
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()) {
87 return false;
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;
99 return true;
100 default:
101 NOTREACHED() << "Unrecognized proxy config mode";
102 break;
104 return false;
107 base::DictionaryValue* UIProxyConfig::ToPrefProxyConfig() const {
108 switch (mode) {
109 case MODE_DIRECT: {
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: {
120 std::string spec;
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: {
127 std::string spec;
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());
139 default:
140 break;
142 NOTREACHED() << "Unrecognized proxy config mode for preference";
143 return NULL;
146 UIProxyConfig::ManualProxy* UIProxyConfig::MapSchemeToProxy(
147 const std::string& scheme) {
148 if (scheme == url::kHttpScheme)
149 return &http_proxy;
150 if (scheme == url::kHttpsScheme)
151 return &https_proxy;
152 if (scheme == url::kFtpScheme)
153 return &ftp_proxy;
154 if (scheme == kSocksScheme)
155 return &socks_proxy;
156 NOTREACHED() << "Invalid scheme: " << scheme;
157 return NULL;
160 } // namespace chromeos