| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/proxy_cros_settings_provider.h" | 5 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 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" | 8 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | |
| 12 | 9 |
| 13 namespace chromeos { | 10 namespace chromeos { |
| 14 | 11 |
| 15 static const char kProxyPacUrl[] = "cros.session.proxy.pacurl"; | 12 // Names of proxy preferences. |
| 16 static const char kProxySingleHttp[] = "cros.session.proxy.singlehttp"; | 13 const char kProxyPacUrl[] = "cros.session.proxy.pacurl"; |
| 17 static const char kProxySingleHttpPort[] = "cros.session.proxy.singlehttpport"; | 14 const char kProxySingleHttp[] = "cros.session.proxy.singlehttp"; |
| 18 static const char kProxyHttpUrl[] = "cros.session.proxy.httpurl"; | 15 const char kProxySingleHttpPort[] = "cros.session.proxy.singlehttpport"; |
| 19 static const char kProxyHttpPort[] = "cros.session.proxy.httpport"; | 16 const char kProxyHttpUrl[] = "cros.session.proxy.httpurl"; |
| 20 static const char kProxyHttpsUrl[] = "cros.session.proxy.httpsurl"; | 17 const char kProxyHttpPort[] = "cros.session.proxy.httpport"; |
| 21 static const char kProxyHttpsPort[] = "cros.session.proxy.httpsport"; | 18 const char kProxyHttpsUrl[] = "cros.session.proxy.httpsurl"; |
| 22 static const char kProxyType[] = "cros.session.proxy.type"; | 19 const char kProxyHttpsPort[] = "cros.session.proxy.httpsport"; |
| 23 static const char kProxySingle[] = "cros.session.proxy.single"; | 20 const char kProxyType[] = "cros.session.proxy.type"; |
| 24 static const char kProxyFtpUrl[] = "cros.session.proxy.ftpurl"; | 21 const char kProxySingle[] = "cros.session.proxy.single"; |
| 25 static const char kProxyFtpPort[] = "cros.session.proxy.ftpport"; | 22 const char kProxyFtpUrl[] = "cros.session.proxy.ftpurl"; |
| 26 static const char kProxySocks[] = "cros.session.proxy.socks"; | 23 const char kProxyFtpPort[] = "cros.session.proxy.ftpport"; |
| 27 static const char kProxySocksPort[] = "cros.session.proxy.socksport"; | 24 const char kProxySocks[] = "cros.session.proxy.socks"; |
| 28 static const char kProxyIgnoreList[] = "cros.session.proxy.ignorelist"; | 25 const char kProxySocksPort[] = "cros.session.proxy.socksport"; |
| 26 const char kProxyIgnoreList[] = "cros.session.proxy.ignorelist"; |
| 29 | 27 |
| 30 static const char* const kProxySettings[] = { | 28 namespace { |
| 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 | 29 |
| 47 //------------------ ProxyCrosSettingsProvider: public methods ----------------- | 30 base::Value* CreateServerHostValue( |
| 48 | 31 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) { |
| 49 ProxyCrosSettingsProvider::ProxyCrosSettingsProvider(Profile* profile) | 32 return proxy.server.is_valid() ? |
| 50 : profile_(profile) { | 33 base::Value::CreateStringValue(proxy.server.host_port_pair().host()) : |
| 34 NULL; |
| 51 } | 35 } |
| 52 | 36 |
| 53 void ProxyCrosSettingsProvider::SetCurrentNetwork(const std::string& network) { | 37 base::Value* CreateServerPortValue( |
| 54 GetConfigService()->UISetCurrentNetwork(network); | 38 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) { |
| 55 for (size_t i = 0; i < arraysize(kProxySettings); ++i) | 39 return proxy.server.is_valid() ? |
| 56 CrosSettings::Get()->FireObservers(kProxySettings[i]); | 40 base::Value::CreateIntegerValue(proxy.server.host_port_pair().port()) : |
| 41 NULL; |
| 57 } | 42 } |
| 58 | 43 |
| 59 void ProxyCrosSettingsProvider::MakeActiveNetworkCurrent() { | 44 net::ProxyServer CreateProxyServerFromHost( |
| 60 GetConfigService()->UIMakeActiveNetworkCurrent(); | 45 const std::string& host, |
| 61 for (size_t i = 0; i < arraysize(kProxySettings); ++i) | 46 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy, |
| 62 CrosSettings::Get()->FireObservers(kProxySettings[i]); | 47 net::ProxyServer::Scheme scheme) { |
| 48 uint16 port = 0; |
| 49 if (proxy.server.is_valid()) |
| 50 port = proxy.server.host_port_pair().port(); |
| 51 if (host.length() == 0 && port == 0) |
| 52 return net::ProxyServer(); |
| 53 if (port == 0) |
| 54 port = net::ProxyServer::GetDefaultPortForScheme(scheme); |
| 55 net::HostPortPair host_port_pair(host, port); |
| 56 return net::ProxyServer(scheme, host_port_pair); |
| 63 } | 57 } |
| 64 | 58 |
| 65 void ProxyCrosSettingsProvider::DoSet(const std::string& path, | 59 net::ProxyServer CreateProxyServerFromPort( |
| 66 Value* in_value) { | 60 uint16 port, |
| 61 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy, |
| 62 net::ProxyServer::Scheme scheme) { |
| 63 std::string host; |
| 64 if (proxy.server.is_valid()) |
| 65 host = proxy.server.host_port_pair().host(); |
| 66 if (host.length() == 0 && port == 0) |
| 67 return net::ProxyServer(); |
| 68 net::HostPortPair host_port_pair(host, port); |
| 69 return net::ProxyServer(scheme, host_port_pair); |
| 70 } |
| 71 |
| 72 } |
| 73 |
| 74 void ProxyCrosSettingsParser::SetProxyPrefValue(Profile* profile, |
| 75 const std::string& path, |
| 76 const base::Value* in_value) { |
| 67 if (!in_value) { | 77 if (!in_value) { |
| 78 NOTREACHED(); |
| 68 return; | 79 return; |
| 69 } | 80 } |
| 70 | 81 |
| 71 chromeos::ProxyConfigServiceImpl* config_service = GetConfigService(); | 82 chromeos::ProxyConfigServiceImpl* config_service = |
| 83 profile->GetProxyConfigTracker(); |
| 72 // Retrieve proxy config. | 84 // Retrieve proxy config. |
| 73 chromeos::ProxyConfigServiceImpl::ProxyConfig config; | 85 chromeos::ProxyConfigServiceImpl::ProxyConfig config; |
| 74 config_service->UIGetProxyConfig(&config); | 86 config_service->UIGetProxyConfig(&config); |
| 75 | 87 |
| 76 if (path == kProxyPacUrl) { | 88 if (path == kProxyPacUrl) { |
| 77 std::string val; | 89 std::string val; |
| 78 if (in_value->GetAsString(&val)) { | 90 if (in_value->GetAsString(&val)) { |
| 79 GURL url(val); | 91 GURL url(val); |
| 80 if (url.is_valid()) | 92 if (url.is_valid()) |
| 81 config_service->UISetProxyConfigToPACScript(url); | 93 config_service->UISetProxyConfigToPACScript(url); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 if (in_value->GetAsInteger(&val)) { | 216 if (in_value->GetAsInteger(&val)) { |
| 205 std::string host = config.socks_proxy.server.host_port_pair().host(); | 217 std::string host = config.socks_proxy.server.host_port_pair().host(); |
| 206 config_service->UISetProxyConfigToProxyPerScheme("socks", | 218 config_service->UISetProxyConfigToProxyPerScheme("socks", |
| 207 CreateProxyServerFromPort(val, config.socks_proxy, | 219 CreateProxyServerFromPort(val, config.socks_proxy, |
| 208 StartsWithASCII(host, "socks5://", false) ? | 220 StartsWithASCII(host, "socks5://", false) ? |
| 209 net::ProxyServer::SCHEME_SOCKS5 : | 221 net::ProxyServer::SCHEME_SOCKS5 : |
| 210 net::ProxyServer::SCHEME_SOCKS4)); | 222 net::ProxyServer::SCHEME_SOCKS4)); |
| 211 } | 223 } |
| 212 } else if (path == kProxyIgnoreList) { | 224 } else if (path == kProxyIgnoreList) { |
| 213 net::ProxyBypassRules bypass_rules; | 225 net::ProxyBypassRules bypass_rules; |
| 214 if (in_value->GetType() == Value::TYPE_LIST) { | 226 if (in_value->GetType() == base::Value::TYPE_LIST) { |
| 215 const ListValue* list_value = static_cast<const ListValue*>(in_value); | 227 const ListValue* list_value = static_cast<const ListValue*>(in_value); |
| 216 for (size_t x = 0; x < list_value->GetSize(); x++) { | 228 for (size_t x = 0; x < list_value->GetSize(); x++) { |
| 217 std::string val; | 229 std::string val; |
| 218 if (list_value->GetString(x, &val)) { | 230 if (list_value->GetString(x, &val)) { |
| 219 bypass_rules.AddRuleFromString(val); | 231 bypass_rules.AddRuleFromString(val); |
| 220 } | 232 } |
| 221 } | 233 } |
| 222 config_service->UISetProxyConfigBypassRules(bypass_rules); | 234 config_service->UISetProxyConfigBypassRules(bypass_rules); |
| 223 } | 235 } |
| 224 } | 236 } |
| 225 } | 237 } |
| 226 | 238 |
| 227 bool ProxyCrosSettingsProvider::Get(const std::string& path, | 239 bool ProxyCrosSettingsParser::GetProxyPrefValue(Profile* profile, |
| 228 Value** out_value) const { | 240 const std::string& path, |
| 241 base::Value** out_value) { |
| 229 bool found = false; | 242 bool found = false; |
| 230 bool managed = false; | 243 bool managed = false; |
| 231 std::string controlled_by; | 244 std::string controlled_by; |
| 232 Value* data = NULL; | 245 base::Value* data = NULL; |
| 233 chromeos::ProxyConfigServiceImpl* config_service = GetConfigService(); | 246 chromeos::ProxyConfigServiceImpl* config_service = |
| 247 profile->GetProxyConfigTracker(); |
| 234 chromeos::ProxyConfigServiceImpl::ProxyConfig config; | 248 chromeos::ProxyConfigServiceImpl::ProxyConfig config; |
| 235 config_service->UIGetProxyConfig(&config); | 249 config_service->UIGetProxyConfig(&config); |
| 236 | 250 |
| 237 if (path == kProxyPacUrl) { | 251 if (path == kProxyPacUrl) { |
| 238 // Only show pacurl for pac-script mode. | 252 // Only show pacurl for pac-script mode. |
| 239 if (config.mode == | 253 if (config.mode == |
| 240 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT && | 254 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT && |
| 241 config.automatic_proxy.pac_url.is_valid()) { | 255 config.automatic_proxy.pac_url.is_valid()) { |
| 242 data = Value::CreateStringValue(config.automatic_proxy.pac_url.spec()); | 256 data = |
| 257 base::Value::CreateStringValue(config.automatic_proxy.pac_url.spec()); |
| 243 } | 258 } |
| 244 found = true; | 259 found = true; |
| 245 } else if (path == kProxySingleHttp) { | 260 } else if (path == kProxySingleHttp) { |
| 246 data = CreateServerHostValue(config.single_proxy); | 261 data = CreateServerHostValue(config.single_proxy); |
| 247 found = true; | 262 found = true; |
| 248 } else if (path == kProxySingleHttpPort) { | 263 } else if (path == kProxySingleHttpPort) { |
| 249 data = CreateServerPortValue(config.single_proxy); | 264 data = CreateServerPortValue(config.single_proxy); |
| 250 found = true; | 265 found = true; |
| 251 } else if (path == kProxyHttpUrl) { | 266 } else if (path == kProxyHttpUrl) { |
| 252 data = CreateServerHostValue(config.http_proxy); | 267 data = CreateServerHostValue(config.http_proxy); |
| 253 found = true; | 268 found = true; |
| 254 } else if (path == kProxyHttpsUrl) { | 269 } else if (path == kProxyHttpsUrl) { |
| 255 data = CreateServerHostValue(config.https_proxy); | 270 data = CreateServerHostValue(config.https_proxy); |
| 256 found = true; | 271 found = true; |
| 257 } else if (path == kProxyType) { | 272 } else if (path == kProxyType) { |
| 258 if (config.mode == | 273 if (config.mode == |
| 259 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT || | 274 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT || |
| 260 config.mode == | 275 config.mode == |
| 261 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT) { | 276 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT) { |
| 262 data = Value::CreateIntegerValue(3); | 277 data = base::Value::CreateIntegerValue(3); |
| 263 } else if (config.mode == | 278 } else if (config.mode == |
| 264 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY || | 279 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY || |
| 265 config.mode == | 280 config.mode == |
| 266 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME) { | 281 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME) { |
| 267 data = Value::CreateIntegerValue(2); | 282 data = base::Value::CreateIntegerValue(2); |
| 268 } else { | 283 } else { |
| 269 data = Value::CreateIntegerValue(1); | 284 data = base::Value::CreateIntegerValue(1); |
| 270 } | 285 } |
| 271 switch (config.state) { | 286 switch (config.state) { |
| 272 case ProxyPrefs::CONFIG_POLICY: | 287 case ProxyPrefs::CONFIG_POLICY: |
| 273 controlled_by = "policyManagedPrefsBannerText"; | 288 controlled_by = "policyManagedPrefsBannerText"; |
| 274 break; | 289 break; |
| 275 case ProxyPrefs::CONFIG_EXTENSION: | 290 case ProxyPrefs::CONFIG_EXTENSION: |
| 276 controlled_by = "extensionManagedPrefsBannerText"; | 291 controlled_by = "extensionManagedPrefsBannerText"; |
| 277 break; | 292 break; |
| 278 case ProxyPrefs::CONFIG_OTHER_PRECEDE: | 293 case ProxyPrefs::CONFIG_OTHER_PRECEDE: |
| 279 controlled_by = "unmodifiablePrefsBannerText"; | 294 controlled_by = "unmodifiablePrefsBannerText"; |
| 280 break; | 295 break; |
| 281 default: | 296 default: |
| 282 if (!config.user_modifiable) | 297 if (!config.user_modifiable) |
| 283 controlled_by = "enableSharedProxiesBannerText"; | 298 controlled_by = "enableSharedProxiesBannerText"; |
| 284 break; | 299 break; |
| 285 } | 300 } |
| 286 found = true; | 301 found = true; |
| 287 } else if (path == kProxySingle) { | 302 } else if (path == kProxySingle) { |
| 288 data = Value::CreateBooleanValue(config.mode == | 303 data = base::Value::CreateBooleanValue(config.mode == |
| 289 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY); | 304 chromeos::ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY); |
| 290 found = true; | 305 found = true; |
| 291 } else if (path == kProxyFtpUrl) { | 306 } else if (path == kProxyFtpUrl) { |
| 292 data = CreateServerHostValue(config.ftp_proxy); | 307 data = CreateServerHostValue(config.ftp_proxy); |
| 293 found = true; | 308 found = true; |
| 294 } else if (path == kProxySocks) { | 309 } else if (path == kProxySocks) { |
| 295 data = CreateServerHostValue(config.socks_proxy); | 310 data = CreateServerHostValue(config.socks_proxy); |
| 296 found = true; | 311 found = true; |
| 297 } else if (path == kProxyHttpPort) { | 312 } else if (path == kProxyHttpPort) { |
| 298 data = CreateServerPortValue(config.http_proxy); | 313 data = CreateServerPortValue(config.http_proxy); |
| 299 found = true; | 314 found = true; |
| 300 } else if (path == kProxyHttpsPort) { | 315 } else if (path == kProxyHttpsPort) { |
| 301 data = CreateServerPortValue(config.https_proxy); | 316 data = CreateServerPortValue(config.https_proxy); |
| 302 found = true; | 317 found = true; |
| 303 } else if (path == kProxyFtpPort) { | 318 } else if (path == kProxyFtpPort) { |
| 304 data = CreateServerPortValue(config.ftp_proxy); | 319 data = CreateServerPortValue(config.ftp_proxy); |
| 305 found = true; | 320 found = true; |
| 306 } else if (path == kProxySocksPort) { | 321 } else if (path == kProxySocksPort) { |
| 307 data = CreateServerPortValue(config.socks_proxy); | 322 data = CreateServerPortValue(config.socks_proxy); |
| 308 found = true; | 323 found = true; |
| 309 } else if (path == kProxyIgnoreList) { | 324 } else if (path == kProxyIgnoreList) { |
| 310 ListValue* list = new ListValue(); | 325 ListValue* list = new ListValue(); |
| 311 net::ProxyBypassRules::RuleList bypass_rules = config.bypass_rules.rules(); | 326 net::ProxyBypassRules::RuleList bypass_rules = config.bypass_rules.rules(); |
| 312 for (size_t x = 0; x < bypass_rules.size(); x++) { | 327 for (size_t x = 0; x < bypass_rules.size(); x++) { |
| 313 list->Append(Value::CreateStringValue(bypass_rules[x]->ToString())); | 328 list->Append(base::Value::CreateStringValue(bypass_rules[x]->ToString())); |
| 314 } | 329 } |
| 315 *out_value = list; | 330 *out_value = list; |
| 316 return true; | 331 return true; |
| 317 } | 332 } |
| 318 if (found) { | 333 if (found) { |
| 319 DictionaryValue* dict = new DictionaryValue; | 334 DictionaryValue* dict = new DictionaryValue; |
| 320 if (!data) | 335 if (!data) |
| 321 data = Value::CreateStringValue(""); | 336 data = base::Value::CreateStringValue(""); |
| 322 dict->Set("value", data); | 337 dict->Set("value", data); |
| 323 dict->SetBoolean("managed", managed); | 338 dict->SetBoolean("managed", managed); |
| 324 if (path == kProxyType) { | 339 if (path == kProxyType) { |
| 325 dict->SetString("controlledBy", controlled_by); | 340 dict->SetString("controlledBy", controlled_by); |
| 326 dict->SetBoolean("disabled", !config.user_modifiable); | 341 dict->SetBoolean("disabled", !config.user_modifiable); |
| 327 } | 342 } |
| 328 *out_value = dict; | 343 *out_value = dict; |
| 329 return true; | 344 return true; |
| 330 } else { | 345 } else { |
| 331 *out_value = NULL; | 346 *out_value = NULL; |
| 332 return false; | 347 return false; |
| 333 } | 348 } |
| 334 } | 349 } |
| 335 | 350 |
| 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 | 351 } // namespace chromeos |
| OLD | NEW |