| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/ui_proxy_config.h" | |
| 6 | |
| 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" | |
| 12 | |
| 13 namespace { | |
| 14 const char kSocksScheme[] = "socks"; | |
| 15 } | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 UIProxyConfig::UIProxyConfig() | |
| 20 : mode(MODE_DIRECT), | |
| 21 state(ProxyPrefs::CONFIG_UNSET), | |
| 22 user_modifiable(true) { | |
| 23 } | |
| 24 | |
| 25 UIProxyConfig::~UIProxyConfig() { | |
| 26 } | |
| 27 | |
| 28 void UIProxyConfig::SetPacUrl(const GURL& pac_url) { | |
| 29 mode = UIProxyConfig::MODE_PAC_SCRIPT; | |
| 30 automatic_proxy.pac_url = pac_url; | |
| 31 } | |
| 32 | |
| 33 void UIProxyConfig::SetSingleProxy(const net::ProxyServer& server) { | |
| 34 mode = UIProxyConfig::MODE_SINGLE_PROXY; | |
| 35 single_proxy.server = server; | |
| 36 } | |
| 37 | |
| 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; | |
| 44 } | |
| 45 mode = UIProxyConfig::MODE_PROXY_PER_SCHEME; | |
| 46 proxy->server = server; | |
| 47 } | |
| 48 | |
| 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; | |
| 54 } | |
| 55 bypass_rules = rules; | |
| 56 } | |
| 57 | |
| 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; | |
| 72 } | |
| 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; | |
| 88 } | |
| 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; | |
| 103 } | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 base::DictionaryValue* UIProxyConfig::ToPrefProxyConfig() const { | |
| 108 switch (mode) { | |
| 109 case MODE_DIRECT: { | |
| 110 return ProxyConfigDictionary::CreateDirect(); | |
| 111 } | |
| 112 case MODE_AUTO_DETECT: { | |
| 113 return ProxyConfigDictionary::CreateAutoDetect(); | |
| 114 } | |
| 115 case MODE_PAC_SCRIPT: { | |
| 116 return ProxyConfigDictionary::CreatePacScript( | |
| 117 automatic_proxy.pac_url.spec(), false); | |
| 118 } | |
| 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()); | |
| 125 } | |
| 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()); | |
| 138 } | |
| 139 default: | |
| 140 break; | |
| 141 } | |
| 142 NOTREACHED() << "Unrecognized proxy config mode for preference"; | |
| 143 return NULL; | |
| 144 } | |
| 145 | |
| 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; | |
| 158 } | |
| 159 | |
| 160 } // namespace chromeos | |
| OLD | NEW |