Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/ui_proxy_config.h" | |
| 6 | |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h" | |
| 11 #include "chrome/browser/prefs/proxy_config_dictionary.h" | |
| 12 #include "net/proxy/proxy_config.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 UIProxyConfig::UIProxyConfig() | |
| 17 : mode(MODE_DIRECT), | |
| 18 state(ProxyPrefs::CONFIG_UNSET), | |
| 19 user_modifiable(true) {} | |
|
stevenjb
2013/05/15 17:44:14
nit: } on separate line outside of class declarati
pneubeck (no reviews)
2013/05/15 20:24:48
Done.
| |
| 20 | |
| 21 UIProxyConfig::~UIProxyConfig() {} | |
| 22 | |
| 23 void UIProxyConfig::SetPacUrl(const GURL& pac_url) { | |
| 24 mode = UIProxyConfig::MODE_PAC_SCRIPT; | |
| 25 automatic_proxy.pac_url = pac_url; | |
| 26 } | |
| 27 | |
| 28 void UIProxyConfig::SetSingleProxy(const net::ProxyServer& server) { | |
| 29 mode = UIProxyConfig::MODE_SINGLE_PROXY; | |
| 30 single_proxy.server = server; | |
| 31 } | |
| 32 | |
| 33 void UIProxyConfig::SetProxyForScheme(const std::string& scheme, | |
| 34 const net::ProxyServer& server) { | |
| 35 ManualProxy* proxy = MapSchemeToProxy(scheme); | |
| 36 if (!proxy) { | |
| 37 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme << "]"; | |
| 38 return; | |
| 39 } | |
| 40 mode = UIProxyConfig::MODE_PROXY_PER_SCHEME; | |
| 41 proxy->server = server; | |
| 42 } | |
| 43 | |
| 44 void UIProxyConfig::SetBypassRules(const net::ProxyBypassRules& rules) { | |
| 45 if (mode != UIProxyConfig::MODE_SINGLE_PROXY && | |
| 46 mode != UIProxyConfig::MODE_PROXY_PER_SCHEME) { | |
| 47 NOTREACHED() << "Cannot set bypass rules for proxy mode [" << mode << "]"; | |
| 48 return; | |
| 49 } | |
| 50 bypass_rules = rules; | |
| 51 } | |
| 52 | |
| 53 bool UIProxyConfig::FromNetProxyConfig(const net::ProxyConfig& net_config) { | |
| 54 *this = UIProxyConfig(); // Reset to default. | |
| 55 const net::ProxyConfig::ProxyRules& rules = net_config.proxy_rules(); | |
| 56 switch (rules.type) { | |
|
stevenjb
2013/05/15 17:44:14
nit: if/else is generally more readable than compl
| |
| 57 case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: | |
| 58 if (!net_config.HasAutomaticSettings()) { | |
| 59 mode = UIProxyConfig::MODE_DIRECT; | |
| 60 } else if (net_config.auto_detect()) { | |
| 61 mode = UIProxyConfig::MODE_AUTO_DETECT; | |
| 62 } else if (net_config.has_pac_url()) { | |
| 63 mode = UIProxyConfig::MODE_PAC_SCRIPT; | |
| 64 automatic_proxy.pac_url = net_config.pac_url(); | |
| 65 } else { | |
| 66 return false; | |
| 67 } | |
| 68 return true; | |
| 69 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: | |
| 70 if (rules.single_proxies.IsEmpty()) | |
| 71 return false; | |
| 72 mode = MODE_SINGLE_PROXY; | |
| 73 single_proxy.server = rules.single_proxies.Get(); | |
| 74 bypass_rules = rules.bypass_rules; | |
| 75 return true; | |
| 76 case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: | |
| 77 // Make sure we have valid server for at least one of the protocols. | |
| 78 if (rules.proxies_for_http.IsEmpty() && | |
| 79 rules.proxies_for_https.IsEmpty() && | |
| 80 rules.proxies_for_ftp.IsEmpty() && | |
| 81 rules.fallback_proxies.IsEmpty()) { | |
| 82 return false; | |
| 83 } | |
| 84 mode = MODE_PROXY_PER_SCHEME; | |
| 85 if (!rules.proxies_for_http.IsEmpty()) | |
| 86 http_proxy.server = rules.proxies_for_http.Get(); | |
| 87 if (!rules.proxies_for_https.IsEmpty()) | |
| 88 https_proxy.server = rules.proxies_for_https.Get(); | |
| 89 if (!rules.proxies_for_ftp.IsEmpty()) | |
| 90 ftp_proxy.server = rules.proxies_for_ftp.Get(); | |
| 91 if (!rules.fallback_proxies.IsEmpty()) | |
| 92 socks_proxy.server = rules.fallback_proxies.Get(); | |
| 93 bypass_rules = rules.bypass_rules; | |
| 94 return true; | |
| 95 default: | |
| 96 NOTREACHED() << "Unrecognized proxy config mode"; | |
| 97 break; | |
| 98 } | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 base::DictionaryValue* UIProxyConfig::ToPrefProxyConfig() { | |
| 103 switch (mode) { | |
|
stevenjb
2013/05/15 17:44:14
nit: if/else would be better here also imho. Other
| |
| 104 case MODE_DIRECT: { | |
| 105 return ProxyConfigDictionary::CreateDirect(); | |
| 106 } | |
| 107 case MODE_AUTO_DETECT: { | |
| 108 return ProxyConfigDictionary::CreateAutoDetect(); | |
| 109 } | |
| 110 case MODE_PAC_SCRIPT: { | |
| 111 return ProxyConfigDictionary::CreatePacScript( | |
| 112 automatic_proxy.pac_url.spec(), false); | |
| 113 } | |
| 114 case MODE_SINGLE_PROXY: { | |
| 115 std::string spec; | |
| 116 if (single_proxy.server.is_valid()) | |
| 117 spec = single_proxy.server.ToURI(); | |
| 118 return ProxyConfigDictionary::CreateFixedServers( | |
| 119 spec, bypass_rules.ToString()); | |
| 120 } | |
| 121 case MODE_PROXY_PER_SCHEME: { | |
| 122 std::string spec; | |
| 123 EncodeAndAppendProxyServer("http", http_proxy.server, &spec); | |
| 124 EncodeAndAppendProxyServer("https", https_proxy.server, &spec); | |
| 125 EncodeAndAppendProxyServer("ftp", ftp_proxy.server, &spec); | |
| 126 EncodeAndAppendProxyServer("socks", socks_proxy.server, &spec); | |
| 127 return ProxyConfigDictionary::CreateFixedServers( | |
| 128 spec, bypass_rules.ToString()); | |
| 129 } | |
| 130 default: | |
| 131 break; | |
| 132 } | |
| 133 NOTREACHED() << "Unrecognized proxy config mode for preference"; | |
| 134 return NULL; | |
| 135 } | |
| 136 | |
| 137 UIProxyConfig::ManualProxy* UIProxyConfig::MapSchemeToProxy( | |
| 138 const std::string& scheme) { | |
| 139 if (scheme == "http") | |
| 140 return &http_proxy; | |
| 141 if (scheme == "https") | |
| 142 return &https_proxy; | |
| 143 if (scheme == "ftp") | |
| 144 return &ftp_proxy; | |
| 145 if (scheme == "socks") | |
| 146 return &socks_proxy; | |
| 147 NOTREACHED() << "Invalid scheme: " << scheme; | |
| 148 return NULL; | |
| 149 } | |
| 150 | |
| 151 bool UIProxyConfig::DeserializeForDevice(const std::string& input) { | |
| 152 enterprise_management::DeviceProxySettingsProto proxy_proto; | |
| 153 if (!proxy_proto.ParseFromString(input)) | |
| 154 return false; | |
| 155 | |
| 156 const std::string& mode_string(proxy_proto.proxy_mode()); | |
| 157 if (mode_string == ProxyPrefs::kDirectProxyModeName) { | |
| 158 mode = MODE_DIRECT; | |
| 159 } else if (mode_string == ProxyPrefs::kAutoDetectProxyModeName) { | |
| 160 mode = MODE_AUTO_DETECT; | |
| 161 } else if (mode_string == ProxyPrefs::kPacScriptProxyModeName) { | |
| 162 mode = MODE_PAC_SCRIPT; | |
| 163 if (proxy_proto.has_proxy_pac_url()) | |
| 164 automatic_proxy.pac_url = GURL(proxy_proto.proxy_pac_url()); | |
| 165 } else if (mode_string == ProxyPrefs::kFixedServersProxyModeName) { | |
| 166 net::ProxyConfig::ProxyRules rules; | |
| 167 rules.ParseFromString(proxy_proto.proxy_server()); | |
| 168 switch (rules.type) { | |
|
stevenjb
2013/05/15 17:44:14
nit: if/else
| |
| 169 case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: | |
| 170 return false; | |
| 171 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: | |
| 172 if (rules.single_proxies.IsEmpty()) | |
| 173 return false; | |
| 174 mode = MODE_SINGLE_PROXY; | |
| 175 single_proxy.server = rules.single_proxies.Get(); | |
| 176 return true; | |
| 177 case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: | |
| 178 // Make sure we have valid server for at least one of the protocols. | |
| 179 if (rules.proxies_for_http.IsEmpty() && | |
| 180 rules.proxies_for_https.IsEmpty() && | |
| 181 rules.proxies_for_ftp.IsEmpty() && | |
| 182 rules.fallback_proxies.IsEmpty()) { | |
| 183 return false; | |
| 184 } | |
| 185 mode = MODE_PROXY_PER_SCHEME; | |
| 186 if (!rules.proxies_for_http.IsEmpty()) | |
| 187 http_proxy.server = rules.proxies_for_http.Get(); | |
| 188 if (!rules.proxies_for_https.IsEmpty()) | |
| 189 https_proxy.server = rules.proxies_for_https.Get(); | |
| 190 if (!rules.proxies_for_ftp.IsEmpty()) | |
| 191 ftp_proxy.server = rules.proxies_for_ftp.Get(); | |
| 192 if (!rules.fallback_proxies.IsEmpty()) | |
| 193 socks_proxy.server = rules.fallback_proxies.Get(); | |
| 194 break; | |
| 195 } | |
| 196 } else { | |
| 197 NOTREACHED() << "Unrecognized proxy config mode"; | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 201 if (proxy_proto.has_proxy_bypass_list()) | |
| 202 bypass_rules.ParseFromString(proxy_proto.proxy_bypass_list()); | |
| 203 | |
| 204 return true; | |
| 205 } | |
| 206 | |
| 207 bool UIProxyConfig::SerializeForNetwork(std::string* output) { | |
| 208 scoped_ptr<base::DictionaryValue> proxy_dict_ptr(ToPrefProxyConfig()); | |
| 209 if (!proxy_dict_ptr.get()) | |
| 210 return false; | |
| 211 | |
| 212 // Return empty string for direct mode for portal check to work correctly. | |
| 213 base::DictionaryValue *dict = proxy_dict_ptr.get(); | |
| 214 ProxyConfigDictionary proxy_dict(dict); | |
| 215 ProxyPrefs::ProxyMode mode; | |
| 216 if (proxy_dict.GetMode(&mode)) { | |
| 217 if (mode == ProxyPrefs::MODE_DIRECT) { | |
| 218 output->clear(); | |
| 219 return true; | |
| 220 } | |
| 221 } | |
| 222 base::JSONWriter::Write(dict, output); | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 // static | |
| 227 void UIProxyConfig::EncodeAndAppendProxyServer(const std::string& url_scheme, | |
| 228 const net::ProxyServer& server, | |
| 229 std::string* spec) { | |
| 230 if (!server.is_valid()) | |
| 231 return; | |
| 232 | |
| 233 if (!spec->empty()) | |
| 234 *spec += ';'; | |
| 235 | |
| 236 if (!url_scheme.empty()) { | |
| 237 *spec += url_scheme; | |
| 238 *spec += "="; | |
| 239 } | |
| 240 *spec += server.ToURI(); | |
| 241 } | |
| 242 | |
| 243 } // namespace chromeos | |
| OLD | NEW |