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) { |
| 20 } |
| 21 |
| 22 UIProxyConfig::~UIProxyConfig() { |
| 23 } |
| 24 |
| 25 void UIProxyConfig::SetPacUrl(const GURL& pac_url) { |
| 26 mode = UIProxyConfig::MODE_PAC_SCRIPT; |
| 27 automatic_proxy.pac_url = pac_url; |
| 28 } |
| 29 |
| 30 void UIProxyConfig::SetSingleProxy(const net::ProxyServer& server) { |
| 31 mode = UIProxyConfig::MODE_SINGLE_PROXY; |
| 32 single_proxy.server = server; |
| 33 } |
| 34 |
| 35 void UIProxyConfig::SetProxyForScheme(const std::string& scheme, |
| 36 const net::ProxyServer& server) { |
| 37 ManualProxy* proxy = MapSchemeToProxy(scheme); |
| 38 if (!proxy) { |
| 39 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme << "]"; |
| 40 return; |
| 41 } |
| 42 mode = UIProxyConfig::MODE_PROXY_PER_SCHEME; |
| 43 proxy->server = server; |
| 44 } |
| 45 |
| 46 void UIProxyConfig::SetBypassRules(const net::ProxyBypassRules& rules) { |
| 47 if (mode != UIProxyConfig::MODE_SINGLE_PROXY && |
| 48 mode != UIProxyConfig::MODE_PROXY_PER_SCHEME) { |
| 49 NOTREACHED() << "Cannot set bypass rules for proxy mode [" << mode << "]"; |
| 50 return; |
| 51 } |
| 52 bypass_rules = rules; |
| 53 } |
| 54 |
| 55 bool UIProxyConfig::FromNetProxyConfig(const net::ProxyConfig& net_config) { |
| 56 *this = UIProxyConfig(); // Reset to default. |
| 57 const net::ProxyConfig::ProxyRules& rules = net_config.proxy_rules(); |
| 58 switch (rules.type) { |
| 59 case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: |
| 60 if (!net_config.HasAutomaticSettings()) { |
| 61 mode = UIProxyConfig::MODE_DIRECT; |
| 62 } else if (net_config.auto_detect()) { |
| 63 mode = UIProxyConfig::MODE_AUTO_DETECT; |
| 64 } else if (net_config.has_pac_url()) { |
| 65 mode = UIProxyConfig::MODE_PAC_SCRIPT; |
| 66 automatic_proxy.pac_url = net_config.pac_url(); |
| 67 } else { |
| 68 return false; |
| 69 } |
| 70 return true; |
| 71 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: |
| 72 if (rules.single_proxies.IsEmpty()) |
| 73 return false; |
| 74 mode = MODE_SINGLE_PROXY; |
| 75 single_proxy.server = rules.single_proxies.Get(); |
| 76 bypass_rules = rules.bypass_rules; |
| 77 return true; |
| 78 case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: |
| 79 // Make sure we have valid server for at least one of the protocols. |
| 80 if (rules.proxies_for_http.IsEmpty() && |
| 81 rules.proxies_for_https.IsEmpty() && |
| 82 rules.proxies_for_ftp.IsEmpty() && |
| 83 rules.fallback_proxies.IsEmpty()) { |
| 84 return false; |
| 85 } |
| 86 mode = MODE_PROXY_PER_SCHEME; |
| 87 if (!rules.proxies_for_http.IsEmpty()) |
| 88 http_proxy.server = rules.proxies_for_http.Get(); |
| 89 if (!rules.proxies_for_https.IsEmpty()) |
| 90 https_proxy.server = rules.proxies_for_https.Get(); |
| 91 if (!rules.proxies_for_ftp.IsEmpty()) |
| 92 ftp_proxy.server = rules.proxies_for_ftp.Get(); |
| 93 if (!rules.fallback_proxies.IsEmpty()) |
| 94 socks_proxy.server = rules.fallback_proxies.Get(); |
| 95 bypass_rules = rules.bypass_rules; |
| 96 return true; |
| 97 default: |
| 98 NOTREACHED() << "Unrecognized proxy config mode"; |
| 99 break; |
| 100 } |
| 101 return false; |
| 102 } |
| 103 |
| 104 base::DictionaryValue* UIProxyConfig::ToPrefProxyConfig() { |
| 105 switch (mode) { |
| 106 case MODE_DIRECT: { |
| 107 return ProxyConfigDictionary::CreateDirect(); |
| 108 } |
| 109 case MODE_AUTO_DETECT: { |
| 110 return ProxyConfigDictionary::CreateAutoDetect(); |
| 111 } |
| 112 case MODE_PAC_SCRIPT: { |
| 113 return ProxyConfigDictionary::CreatePacScript( |
| 114 automatic_proxy.pac_url.spec(), false); |
| 115 } |
| 116 case MODE_SINGLE_PROXY: { |
| 117 std::string spec; |
| 118 if (single_proxy.server.is_valid()) |
| 119 spec = single_proxy.server.ToURI(); |
| 120 return ProxyConfigDictionary::CreateFixedServers( |
| 121 spec, bypass_rules.ToString()); |
| 122 } |
| 123 case MODE_PROXY_PER_SCHEME: { |
| 124 std::string spec; |
| 125 EncodeAndAppendProxyServer("http", http_proxy.server, &spec); |
| 126 EncodeAndAppendProxyServer("https", https_proxy.server, &spec); |
| 127 EncodeAndAppendProxyServer("ftp", ftp_proxy.server, &spec); |
| 128 EncodeAndAppendProxyServer("socks", socks_proxy.server, &spec); |
| 129 return ProxyConfigDictionary::CreateFixedServers( |
| 130 spec, bypass_rules.ToString()); |
| 131 } |
| 132 default: |
| 133 break; |
| 134 } |
| 135 NOTREACHED() << "Unrecognized proxy config mode for preference"; |
| 136 return NULL; |
| 137 } |
| 138 |
| 139 UIProxyConfig::ManualProxy* UIProxyConfig::MapSchemeToProxy( |
| 140 const std::string& scheme) { |
| 141 if (scheme == "http") |
| 142 return &http_proxy; |
| 143 if (scheme == "https") |
| 144 return &https_proxy; |
| 145 if (scheme == "ftp") |
| 146 return &ftp_proxy; |
| 147 if (scheme == "socks") |
| 148 return &socks_proxy; |
| 149 NOTREACHED() << "Invalid scheme: " << scheme; |
| 150 return NULL; |
| 151 } |
| 152 |
| 153 bool UIProxyConfig::SerializeForNetwork(std::string* output) { |
| 154 scoped_ptr<base::DictionaryValue> proxy_dict_ptr(ToPrefProxyConfig()); |
| 155 if (!proxy_dict_ptr.get()) |
| 156 return false; |
| 157 |
| 158 // Return empty string for direct mode for portal check to work correctly. |
| 159 base::DictionaryValue *dict = proxy_dict_ptr.get(); |
| 160 ProxyConfigDictionary proxy_dict(dict); |
| 161 ProxyPrefs::ProxyMode mode; |
| 162 if (proxy_dict.GetMode(&mode)) { |
| 163 if (mode == ProxyPrefs::MODE_DIRECT) { |
| 164 output->clear(); |
| 165 return true; |
| 166 } |
| 167 } |
| 168 base::JSONWriter::Write(dict, output); |
| 169 return true; |
| 170 } |
| 171 |
| 172 // static |
| 173 void UIProxyConfig::EncodeAndAppendProxyServer(const std::string& url_scheme, |
| 174 const net::ProxyServer& server, |
| 175 std::string* spec) { |
| 176 if (!server.is_valid()) |
| 177 return; |
| 178 |
| 179 if (!spec->empty()) |
| 180 *spec += ';'; |
| 181 |
| 182 if (!url_scheme.empty()) { |
| 183 *spec += url_scheme; |
| 184 *spec += "="; |
| 185 } |
| 186 *spec += server.ToURI(); |
| 187 } |
| 188 |
| 189 } // namespace chromeos |
OLD | NEW |