| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/proxy_cros_settings_provider.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/cros_settings.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/browser_list.h" | |
| 12 | |
| 13 namespace chromeos { | |
| 14 | |
| 15 static const char kProxyPacUrl[] = "cros.session.proxy.pacurl"; | |
| 16 static const char kProxySingleHttp[] = "cros.session.proxy.singlehttp"; | |
| 17 static const char kProxySingleHttpPort[] = "cros.session.proxy.singlehttpport"; | |
| 18 static const char kProxyHttpUrl[] = "cros.session.proxy.httpurl"; | |
| 19 static const char kProxyHttpPort[] = "cros.session.proxy.httpport"; | |
| 20 static const char kProxyHttpsUrl[] = "cros.session.proxy.httpsurl"; | |
| 21 static const char kProxyHttpsPort[] = "cros.session.proxy.httpsport"; | |
| 22 static const char kProxyType[] = "cros.session.proxy.type"; | |
| 23 static const char kProxySingle[] = "cros.session.proxy.single"; | |
| 24 static const char kProxyFtpUrl[] = "cros.session.proxy.ftpurl"; | |
| 25 static const char kProxyFtpPort[] = "cros.session.proxy.ftpport"; | |
| 26 static const char kProxySocks[] = "cros.session.proxy.socks"; | |
| 27 static const char kProxySocksPort[] = "cros.session.proxy.socksport"; | |
| 28 static const char kProxyIgnoreList[] = "cros.session.proxy.ignorelist"; | |
| 29 | |
| 30 static const char* const kProxySettings[] = { | |
| 31 kProxyPacUrl, | |
| 32 kProxySingleHttp, | |
| 33 kProxySingleHttpPort, | |
| 34 kProxyHttpUrl, | |
| 35 kProxyHttpPort, | |
| 36 kProxyHttpsUrl, | |
| 37 kProxyHttpsPort, | |
| 38 kProxyType, | |
| 39 kProxySingle, | |
| 40 kProxyFtpUrl, | |
| 41 kProxyFtpPort, | |
| 42 kProxySocks, | |
| 43 kProxySocksPort, | |
| 44 kProxyIgnoreList, | |
| 45 }; | |
| 46 | |
| 47 //------------------ ProxyCrosSettingsProvider: public methods ----------------- | |
| 48 | |
| 49 ProxyCrosSettingsProvider::ProxyCrosSettingsProvider(Profile* profile) | |
| 50 : profile_(profile) { | |
| 51 } | |
| 52 | |
| 53 void ProxyCrosSettingsProvider::SetCurrentNetwork(const std::string& network) { | |
| 54 GetConfigService()->UISetCurrentNetwork(network); | |
| 55 for (size_t i = 0; i < arraysize(kProxySettings); ++i) | |
| 56 CrosSettings::Get()->FireObservers(kProxySettings[i]); | |
| 57 } | |
| 58 | |
| 59 void ProxyCrosSettingsProvider::MakeActiveNetworkCurrent() { | |
| 60 GetConfigService()->UIMakeActiveNetworkCurrent(); | |
| 61 for (size_t i = 0; i < arraysize(kProxySettings); ++i) | |
| 62 CrosSettings::Get()->FireObservers(kProxySettings[i]); | |
| 63 } | |
| 64 | |
| 65 void ProxyCrosSettingsProvider::DoSet(const std::string& path, | |
| 66 Value* in_value) { | |
| 67 if (!in_value) { | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 chromeos::ProxyConfigServiceImpl* config_service = GetConfigService(); | |
| 72 // Retrieve proxy config. | |
| 73 chromeos::ProxyConfigServiceImpl::ProxyConfig config; | |
| 74 config_service->UIGetProxyConfig(&config); | |
| 75 | |
| 76 if (path == kProxyPacUrl) { | |
| 77 std::string val; | |
| 78 if (in_value->GetAsString(&val)) { | |
| 79 GURL url(val); | |
| 80 if (url.is_valid()) | |
| 81 config_service->UISetProxyConfigToPACScript(url); | |
| 82 else | |
| 83 config_service->UISetProxyConfigToAutoDetect(); | |
| 84 } | |
| 85 } else if (path == kProxySingleHttp) { | |
| 86 std::string val; | |
| 87 if (in_value->GetAsString(&val)) { | |
| 88 config_service->UISetProxyConfigToSingleProxy(CreateProxyServerFromHost( | |
| 89 val, config.single_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 90 } | |
| 91 } else if (path == kProxySingleHttpPort) { | |
| 92 int val; | |
| 93 if (in_value->GetAsInteger(&val)) { | |
| 94 config_service->UISetProxyConfigToSingleProxy(CreateProxyServerFromPort( | |
| 95 val, config.single_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 96 } | |
| 97 } else if (path == kProxyHttpUrl) { | |
| 98 std::string val; | |
| 99 if (in_value->GetAsString(&val)) { | |
| 100 config_service->UISetProxyConfigToProxyPerScheme("http", | |
| 101 CreateProxyServerFromHost( | |
| 102 val, config.http_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 103 } | |
| 104 } else if (path == kProxyHttpPort) { | |
| 105 int val; | |
| 106 if (in_value->GetAsInteger(&val)) { | |
| 107 config_service->UISetProxyConfigToProxyPerScheme("http", | |
| 108 CreateProxyServerFromPort( | |
| 109 val, config.http_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 110 } | |
| 111 } else if (path == kProxyHttpsUrl) { | |
| 112 std::string val; | |
| 113 if (in_value->GetAsString(&val)) { | |
| 114 config_service->UISetProxyConfigToProxyPerScheme("https", | |
| 115 CreateProxyServerFromHost( | |
| 116 val, config.https_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 117 } | |
| 118 } else if (path == kProxyHttpsPort) { | |
| 119 int val; | |
| 120 if (in_value->GetAsInteger(&val)) { | |
| 121 config_service->UISetProxyConfigToProxyPerScheme("https", | |
| 122 CreateProxyServerFromPort( | |
| 123 val, config.https_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 124 } | |
| 125 } else if (path == kProxyType) { | |
| 126 int val; | |
| 127 if (in_value->GetAsInteger(&val)) { | |
| 128 if (val == 3) { | |
| 129 if (config.automatic_proxy.pac_url.is_valid()) | |
| 130 config_service->UISetProxyConfigToPACScript( | |
| 131 config.automatic_proxy.pac_url); | |
| 132 else | |
| 133 config_service->UISetProxyConfigToAutoDetect(); | |
| 134 } else if (val == 2) { | |
| 135 if (config.single_proxy.server.is_valid()) { | |
| 136 config_service->UISetProxyConfigToSingleProxy( | |
| 137 config.single_proxy.server); | |
| 138 } else { | |
| 139 bool set_config = false; | |
| 140 if (config.http_proxy.server.is_valid()) { | |
| 141 config_service->UISetProxyConfigToProxyPerScheme("http", | |
| 142 config.http_proxy.server); | |
| 143 set_config = true; | |
| 144 } | |
| 145 if (config.https_proxy.server.is_valid()) { | |
| 146 config_service->UISetProxyConfigToProxyPerScheme("https", | |
| 147 config.https_proxy.server); | |
| 148 set_config = true; | |
| 149 } | |
| 150 if (config.ftp_proxy.server.is_valid()) { | |
| 151 config_service->UISetProxyConfigToProxyPerScheme("ftp", | |
| 152 config.ftp_proxy.server); | |
| 153 set_config = true; | |
| 154 } | |
| 155 if (config.socks_proxy.server.is_valid()) { | |
| 156 config_service->UISetProxyConfigToProxyPerScheme("socks", | |
| 157 config.socks_proxy.server); | |
| 158 set_config = true; | |
| 159 } | |
| 160 if (!set_config) { | |
| 161 config_service->UISetProxyConfigToProxyPerScheme("http", | |
| 162 net::ProxyServer()); | |
| 163 } | |
| 164 } | |
| 165 } else { | |
| 166 config_service->UISetProxyConfigToDirect(); | |
| 167 } | |
| 168 } | |
| 169 } else if (path == kProxySingle) { | |
| 170 bool val; | |
| 171 if (in_value->GetAsBoolean(&val)) { | |
| 172 if (val) | |
| 173 config_service->UISetProxyConfigToSingleProxy( | |
| 174 config.single_proxy.server); | |
| 175 else | |
| 176 config_service->UISetProxyConfigToProxyPerScheme("http", | |
| 177 config.http_proxy.server); | |
| 178 } | |
| 179 } else if (path == kProxyFtpUrl) { | |
| 180 std::string val; | |
| 181 if (in_value->GetAsString(&val)) { | |
| 182 config_service->UISetProxyConfigToProxyPerScheme("ftp", | |
| 183 CreateProxyServerFromHost( | |
| 184 val, config.ftp_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 185 } | |
| 186 } else if (path == kProxyFtpPort) { | |
| 187 int val; | |
| 188 if (in_value->GetAsInteger(&val)) { | |
| 189 config_service->UISetProxyConfigToProxyPerScheme("ftp", | |
| 190 CreateProxyServerFromPort( | |
| 191 val, config.ftp_proxy, net::ProxyServer::SCHEME_HTTP)); | |
| 192 } | |
| 193 } else if (path == kProxySocks) { | |
| 194 std::string val; | |
| 195 if (in_value->GetAsString(&val)) { | |
| 196 config_service->UISetProxyConfigToProxyPerScheme("socks", | |
| 197 CreateProxyServerFromHost(val, config.socks_proxy, | |
| 198 StartsWithASCII(val, "socks5://", false) ? | |
| 199 net::ProxyServer::SCHEME_SOCKS5 : | |
| 200 net::ProxyServer::SCHEME_SOCKS4)); | |
| 201 } | |
| 202 } else if (path == kProxySocksPort) { | |
| 203 int val; | |
| 204 if (in_value->GetAsInteger(&val)) { | |
| 205 std::string host = config.socks_proxy.server.host_port_pair().host(); | |
| 206 config_service->UISetProxyConfigToProxyPerScheme("socks", | |
| 207 CreateProxyServerFromPort(val, config.socks_proxy, | |
| 208 StartsWithASCII(host, "socks5://", false) ? | |
| 209 net::ProxyServer::SCHEME_SOCKS5 : | |
| 210 net::ProxyServer::SCHEME_SOCKS4)); | |
| 211 } | |
| 212 } else if (path == kProxyIgnoreList) { | |
| 213 net::ProxyBypassRules bypass_rules; | |
| 214 if (in_value->GetType() == Value::TYPE_LIST) { | |
| 215 const ListValue* list_value = static_cast<const ListValue*>(in_value); | |
| 216 for (size_t x = 0; x < list_value->GetSize(); x++) { | |
| 217 std::string val; | |
| 218 if (list_value->GetString(x, &val)) { | |
| 219 bypass_rules.AddRuleFromString(val); | |
| 220 } | |
| 221 } | |
| 222 config_service->UISetProxyConfigBypassRules(bypass_rules); | |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 bool ProxyCrosSettingsProvider::Get(const std::string& path, | |
| 228 Value** out_value) const { | |
| 229 bool found = false; | |
| 230 bool managed = false; | |
| 231 std::string controlled_by; | |
| 232 Value* data = NULL; | |
| 233 chromeos::ProxyConfigServiceImpl* config_service = GetConfigService(); | |
| 234 chromeos::ProxyConfigServiceImpl::ProxyConfig config; | |
| 235 config_service->UIGetProxyConfig(&config); | |
| 236 | |
| 237 if (path == kProxyPacUrl) { | |
| 238 // Only show pacurl for pac-script mode. | |
| 239 if (config.mode == | |
| 240 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT && | |
| 241 config.automatic_proxy.pac_url.is_valid()) { | |
| 242 data = Value::CreateStringValue(config.automatic_proxy.pac_url.spec()); | |
| 243 } | |
| 244 found = true; | |
| 245 } else if (path == kProxySingleHttp) { | |
| 246 data = CreateServerHostValue(config.single_proxy); | |
| 247 found = true; | |
| 248 } else if (path == kProxySingleHttpPort) { | |
| 249 data = CreateServerPortValue(config.single_proxy); | |
| 250 found = true; | |
| 251 } else if (path == kProxyHttpUrl) { | |
| 252 data = CreateServerHostValue(config.http_proxy); | |
| 253 found = true; | |
| 254 } else if (path == kProxyHttpsUrl) { | |
| 255 data = CreateServerHostValue(config.https_proxy); | |
| 256 found = true; | |
| 257 } else if (path == kProxyType) { | |
| 258 if (config.mode == | |
| 259 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT || | |
| 260 config.mode == | |
| 261 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT) { | |
| 262 data = Value::CreateIntegerValue(3); | |
| 263 } else if (config.mode == | |
| 264 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY || | |
| 265 config.mode == | |
| 266 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME) { | |
| 267 data = Value::CreateIntegerValue(2); | |
| 268 } else { | |
| 269 data = Value::CreateIntegerValue(1); | |
| 270 } | |
| 271 switch (config.state) { | |
| 272 case ProxyPrefs::CONFIG_POLICY: | |
| 273 controlled_by = "policyManagedPrefsBannerText"; | |
| 274 break; | |
| 275 case ProxyPrefs::CONFIG_EXTENSION: | |
| 276 controlled_by = "extensionManagedPrefsBannerText"; | |
| 277 break; | |
| 278 case ProxyPrefs::CONFIG_OTHER_PRECEDE: | |
| 279 controlled_by = "unmodifiablePrefsBannerText"; | |
| 280 break; | |
| 281 default: | |
| 282 if (!config.user_modifiable) | |
| 283 controlled_by = "enableSharedProxiesBannerText"; | |
| 284 break; | |
| 285 } | |
| 286 found = true; | |
| 287 } else if (path == kProxySingle) { | |
| 288 data = Value::CreateBooleanValue(config.mode == | |
| 289 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY); | |
| 290 found = true; | |
| 291 } else if (path == kProxyFtpUrl) { | |
| 292 data = CreateServerHostValue(config.ftp_proxy); | |
| 293 found = true; | |
| 294 } else if (path == kProxySocks) { | |
| 295 data = CreateServerHostValue(config.socks_proxy); | |
| 296 found = true; | |
| 297 } else if (path == kProxyHttpPort) { | |
| 298 data = CreateServerPortValue(config.http_proxy); | |
| 299 found = true; | |
| 300 } else if (path == kProxyHttpsPort) { | |
| 301 data = CreateServerPortValue(config.https_proxy); | |
| 302 found = true; | |
| 303 } else if (path == kProxyFtpPort) { | |
| 304 data = CreateServerPortValue(config.ftp_proxy); | |
| 305 found = true; | |
| 306 } else if (path == kProxySocksPort) { | |
| 307 data = CreateServerPortValue(config.socks_proxy); | |
| 308 found = true; | |
| 309 } else if (path == kProxyIgnoreList) { | |
| 310 ListValue* list = new ListValue(); | |
| 311 net::ProxyBypassRules::RuleList bypass_rules = config.bypass_rules.rules(); | |
| 312 for (size_t x = 0; x < bypass_rules.size(); x++) { | |
| 313 list->Append(Value::CreateStringValue(bypass_rules[x]->ToString())); | |
| 314 } | |
| 315 *out_value = list; | |
| 316 return true; | |
| 317 } | |
| 318 if (found) { | |
| 319 DictionaryValue* dict = new DictionaryValue; | |
| 320 if (!data) | |
| 321 data = Value::CreateStringValue(""); | |
| 322 dict->Set("value", data); | |
| 323 dict->SetBoolean("managed", managed); | |
| 324 if (path == kProxyType) { | |
| 325 dict->SetString("controlledBy", controlled_by); | |
| 326 dict->SetBoolean("disabled", !config.user_modifiable); | |
| 327 } | |
| 328 *out_value = dict; | |
| 329 return true; | |
| 330 } else { | |
| 331 *out_value = NULL; | |
| 332 return false; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 bool ProxyCrosSettingsProvider::HandlesSetting(const std::string& path) const { | |
| 337 return ::StartsWithASCII(path, "cros.session.proxy", true); | |
| 338 } | |
| 339 | |
| 340 //----------------- ProxyCrosSettingsProvider: private methods ----------------- | |
| 341 | |
| 342 chromeos::ProxyConfigServiceImpl* | |
| 343 ProxyCrosSettingsProvider::GetConfigService() const { | |
| 344 return profile_->GetProxyConfigTracker(); | |
| 345 } | |
| 346 | |
| 347 net::ProxyServer ProxyCrosSettingsProvider::CreateProxyServerFromHost( | |
| 348 const std::string& host, | |
| 349 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy, | |
| 350 net::ProxyServer::Scheme scheme) const { | |
| 351 uint16 port = 0; | |
| 352 if (proxy.server.is_valid()) | |
| 353 port = proxy.server.host_port_pair().port(); | |
| 354 if (host.length() == 0 && port == 0) | |
| 355 return net::ProxyServer(); | |
| 356 if (port == 0) | |
| 357 port = net::ProxyServer::GetDefaultPortForScheme(scheme); | |
| 358 net::HostPortPair host_port_pair(host, port); | |
| 359 return net::ProxyServer(scheme, host_port_pair); | |
| 360 } | |
| 361 | |
| 362 net::ProxyServer ProxyCrosSettingsProvider::CreateProxyServerFromPort( | |
| 363 uint16 port, | |
| 364 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy, | |
| 365 net::ProxyServer::Scheme scheme) const { | |
| 366 std::string host; | |
| 367 if (proxy.server.is_valid()) | |
| 368 host = proxy.server.host_port_pair().host(); | |
| 369 if (host.length() == 0 && port == 0) | |
| 370 return net::ProxyServer(); | |
| 371 net::HostPortPair host_port_pair(host, port); | |
| 372 return net::ProxyServer(scheme, host_port_pair); | |
| 373 } | |
| 374 | |
| 375 Value* ProxyCrosSettingsProvider::CreateServerHostValue( | |
| 376 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) const { | |
| 377 return proxy.server.is_valid() ? | |
| 378 Value::CreateStringValue(proxy.server.host_port_pair().host()) : | |
| 379 NULL; | |
| 380 } | |
| 381 | |
| 382 Value* ProxyCrosSettingsProvider::CreateServerPortValue( | |
| 383 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) const { | |
| 384 return proxy.server.is_valid() ? | |
| 385 Value::CreateIntegerValue(proxy.server.host_port_pair().port()) : | |
| 386 NULL; | |
| 387 } | |
| 388 | |
| 389 } // namespace chromeos | |
| OLD | NEW |