| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_parser.h" | 5 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chromeos/network/proxy/ui_proxy_config.h" | 13 #include "chromeos/network/proxy/ui_proxy_config.h" |
| 13 #include "chromeos/network/proxy/ui_proxy_config_service.h" | 14 #include "chromeos/network/proxy/ui_proxy_config_service.h" |
| 14 | 15 |
| 15 namespace chromeos { | 16 namespace chromeos { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 base::Value* CreateServerHostValue(const UIProxyConfig::ManualProxy& proxy) { | 20 std::unique_ptr<base::Value> CreateServerHostValue( |
| 20 return proxy.server.is_valid() | 21 const UIProxyConfig::ManualProxy& proxy) { |
| 21 ? new base::Value(proxy.server.host_port_pair().host()) | 22 return proxy.server.is_valid() ? base::MakeUnique<base::Value>( |
| 22 : NULL; | 23 proxy.server.host_port_pair().host()) |
| 24 : nullptr; |
| 23 } | 25 } |
| 24 | 26 |
| 25 base::Value* CreateServerPortValue(const UIProxyConfig::ManualProxy& proxy) { | 27 std::unique_ptr<base::Value> CreateServerPortValue( |
| 26 return proxy.server.is_valid() | 28 const UIProxyConfig::ManualProxy& proxy) { |
| 27 ? new base::Value(proxy.server.host_port_pair().port()) | 29 return proxy.server.is_valid() ? base::MakeUnique<base::Value>( |
| 28 : NULL; | 30 proxy.server.host_port_pair().port()) |
| 31 : nullptr; |
| 29 } | 32 } |
| 30 | 33 |
| 31 net::ProxyServer CreateProxyServer(std::string host, | 34 net::ProxyServer CreateProxyServer(std::string host, |
| 32 uint16_t port, | 35 uint16_t port, |
| 33 net::ProxyServer::Scheme scheme) { | 36 net::ProxyServer::Scheme scheme) { |
| 34 if (host.empty() && port == 0) | 37 if (host.empty() && port == 0) |
| 35 return net::ProxyServer(); | 38 return net::ProxyServer(); |
| 36 uint16_t default_port = net::ProxyServer::GetDefaultPortForScheme(scheme); | 39 uint16_t default_port = net::ProxyServer::GetDefaultPortForScheme(scheme); |
| 37 net::HostPortPair host_port_pair; | 40 net::HostPortPair host_port_pair; |
| 38 // Check if host is a valid URL or a string of valid format <server>::<port>. | 41 // Check if host is a valid URL or a string of valid format <server>::<port>. |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 config.SetBypassRules(bypass_rules); | 275 config.SetBypassRules(bypass_rules); |
| 273 } | 276 } |
| 274 } else { | 277 } else { |
| 275 LOG(WARNING) << "Unknown proxy settings path " << path; | 278 LOG(WARNING) << "Unknown proxy settings path " << path; |
| 276 return; | 279 return; |
| 277 } | 280 } |
| 278 | 281 |
| 279 config_service->SetProxyConfig(network_guid, config); | 282 config_service->SetProxyConfig(network_guid, config); |
| 280 } | 283 } |
| 281 | 284 |
| 285 // TODO(crbug.com/697817): Change |out_value| to be |
| 286 // std::unique_ptr<base::Value>*. |
| 282 bool GetProxyPrefValue(const std::string& network_guid, | 287 bool GetProxyPrefValue(const std::string& network_guid, |
| 283 const std::string& path, | 288 const std::string& path, |
| 284 UIProxyConfigService* config_service, | 289 UIProxyConfigService* config_service, |
| 285 base::Value** out_value) { | 290 base::Value** out_value) { |
| 286 std::string controlled_by; | 291 std::string controlled_by; |
| 287 base::Value* data = NULL; | 292 std::unique_ptr<base::Value> data; |
| 288 UIProxyConfig config; | 293 UIProxyConfig config; |
| 289 config_service->GetProxyConfig(network_guid, &config); | 294 config_service->GetProxyConfig(network_guid, &config); |
| 290 | 295 |
| 291 if (path == kProxyPacUrl) { | 296 if (path == kProxyPacUrl) { |
| 292 // Only show pacurl for pac-script mode. | 297 // Only show pacurl for pac-script mode. |
| 293 if (config.mode == UIProxyConfig::MODE_PAC_SCRIPT && | 298 if (config.mode == UIProxyConfig::MODE_PAC_SCRIPT && |
| 294 config.automatic_proxy.pac_url.is_valid()) { | 299 config.automatic_proxy.pac_url.is_valid()) { |
| 295 data = new base::Value(config.automatic_proxy.pac_url.spec()); | 300 data = |
| 301 base::MakeUnique<base::Value>(config.automatic_proxy.pac_url.spec()); |
| 296 } | 302 } |
| 297 } else if (path == kProxySingleHttp) { | 303 } else if (path == kProxySingleHttp) { |
| 298 data = CreateServerHostValue(config.single_proxy); | 304 data = CreateServerHostValue(config.single_proxy); |
| 299 } else if (path == kProxySingleHttpPort) { | 305 } else if (path == kProxySingleHttpPort) { |
| 300 data = CreateServerPortValue(config.single_proxy); | 306 data = CreateServerPortValue(config.single_proxy); |
| 301 } else if (path == kProxyHttpUrl) { | 307 } else if (path == kProxyHttpUrl) { |
| 302 data = CreateServerHostValue(config.http_proxy); | 308 data = CreateServerHostValue(config.http_proxy); |
| 303 } else if (path == kProxyHttpsUrl) { | 309 } else if (path == kProxyHttpsUrl) { |
| 304 data = CreateServerHostValue(config.https_proxy); | 310 data = CreateServerHostValue(config.https_proxy); |
| 305 } else if (path == kProxyType) { | 311 } else if (path == kProxyType) { |
| 306 if (config.mode == UIProxyConfig::MODE_AUTO_DETECT || | 312 if (config.mode == UIProxyConfig::MODE_AUTO_DETECT || |
| 307 config.mode == UIProxyConfig::MODE_PAC_SCRIPT) { | 313 config.mode == UIProxyConfig::MODE_PAC_SCRIPT) { |
| 308 data = new base::Value(3); | 314 data = base::MakeUnique<base::Value>(3); |
| 309 } else if (config.mode == UIProxyConfig::MODE_SINGLE_PROXY || | 315 } else if (config.mode == UIProxyConfig::MODE_SINGLE_PROXY || |
| 310 config.mode == UIProxyConfig::MODE_PROXY_PER_SCHEME) { | 316 config.mode == UIProxyConfig::MODE_PROXY_PER_SCHEME) { |
| 311 data = new base::Value(2); | 317 data = base::MakeUnique<base::Value>(2); |
| 312 } else { | 318 } else { |
| 313 data = new base::Value(1); | 319 data = base::MakeUnique<base::Value>(1); |
| 314 } | 320 } |
| 315 switch (config.state) { | 321 switch (config.state) { |
| 316 case ProxyPrefs::CONFIG_POLICY: | 322 case ProxyPrefs::CONFIG_POLICY: |
| 317 controlled_by = "policy"; | 323 controlled_by = "policy"; |
| 318 break; | 324 break; |
| 319 case ProxyPrefs::CONFIG_EXTENSION: | 325 case ProxyPrefs::CONFIG_EXTENSION: |
| 320 controlled_by = "extension"; | 326 controlled_by = "extension"; |
| 321 break; | 327 break; |
| 322 case ProxyPrefs::CONFIG_OTHER_PRECEDE: | 328 case ProxyPrefs::CONFIG_OTHER_PRECEDE: |
| 323 controlled_by = "other"; | 329 controlled_by = "other"; |
| 324 break; | 330 break; |
| 325 default: | 331 default: |
| 326 if (!config.user_modifiable) | 332 if (!config.user_modifiable) |
| 327 controlled_by = "shared"; | 333 controlled_by = "shared"; |
| 328 break; | 334 break; |
| 329 } | 335 } |
| 330 } else if (path == kProxySingle) { | 336 } else if (path == kProxySingle) { |
| 331 data = new base::Value(config.mode == UIProxyConfig::MODE_SINGLE_PROXY); | 337 data = base::MakeUnique<base::Value>(config.mode == |
| 338 UIProxyConfig::MODE_SINGLE_PROXY); |
| 332 } else if (path == kProxyUsePacUrl) { | 339 } else if (path == kProxyUsePacUrl) { |
| 333 data = new base::Value(config.mode == UIProxyConfig::MODE_PAC_SCRIPT); | 340 data = base::MakeUnique<base::Value>(config.mode == |
| 341 UIProxyConfig::MODE_PAC_SCRIPT); |
| 334 } else if (path == kProxyFtpUrl) { | 342 } else if (path == kProxyFtpUrl) { |
| 335 data = CreateServerHostValue(config.ftp_proxy); | 343 data = CreateServerHostValue(config.ftp_proxy); |
| 336 } else if (path == kProxySocks) { | 344 } else if (path == kProxySocks) { |
| 337 data = CreateServerHostValue(config.socks_proxy); | 345 data = CreateServerHostValue(config.socks_proxy); |
| 338 } else if (path == kProxyHttpPort) { | 346 } else if (path == kProxyHttpPort) { |
| 339 data = CreateServerPortValue(config.http_proxy); | 347 data = CreateServerPortValue(config.http_proxy); |
| 340 } else if (path == kProxyHttpsPort) { | 348 } else if (path == kProxyHttpsPort) { |
| 341 data = CreateServerPortValue(config.https_proxy); | 349 data = CreateServerPortValue(config.https_proxy); |
| 342 } else if (path == kProxyFtpPort) { | 350 } else if (path == kProxyFtpPort) { |
| 343 data = CreateServerPortValue(config.ftp_proxy); | 351 data = CreateServerPortValue(config.ftp_proxy); |
| 344 } else if (path == kProxySocksPort) { | 352 } else if (path == kProxySocksPort) { |
| 345 data = CreateServerPortValue(config.socks_proxy); | 353 data = CreateServerPortValue(config.socks_proxy); |
| 346 } else if (path == kProxyIgnoreList) { | 354 } else if (path == kProxyIgnoreList) { |
| 347 base::ListValue* list = new base::ListValue(); | 355 auto list = base::MakeUnique<base::ListValue>(); |
| 348 const auto& bypass_rules = config.bypass_rules.rules(); | 356 const auto& bypass_rules = config.bypass_rules.rules(); |
| 349 for (const auto& rule : bypass_rules) | 357 for (const auto& rule : bypass_rules) |
| 350 list->AppendString(rule->ToString()); | 358 list->AppendString(rule->ToString()); |
| 351 data = list; | 359 data = std::move(list); |
| 352 } else { | 360 } else { |
| 353 *out_value = NULL; | 361 *out_value = NULL; |
| 354 return false; | 362 return false; |
| 355 } | 363 } |
| 356 | 364 |
| 357 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. | 365 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. |
| 358 base::DictionaryValue* dict = new base::DictionaryValue; | 366 base::DictionaryValue* dict = new base::DictionaryValue; |
| 359 if (!data) | 367 if (!data) |
| 360 data = new base::Value(""); | 368 data = base::MakeUnique<base::Value>(base::Value::Type::STRING); |
| 361 dict->Set("value", data); | 369 dict->Set("value", std::move(data)); |
| 362 if (path == kProxyType) { | 370 if (path == kProxyType) { |
| 363 if (!controlled_by.empty()) | 371 if (!controlled_by.empty()) |
| 364 dict->SetString("controlledBy", controlled_by); | 372 dict->SetString("controlledBy", controlled_by); |
| 365 dict->SetBoolean("disabled", !config.user_modifiable); | 373 dict->SetBoolean("disabled", !config.user_modifiable); |
| 366 } else { | 374 } else { |
| 367 dict->SetBoolean("disabled", false); | 375 dict->SetBoolean("disabled", false); |
| 368 } | 376 } |
| 369 *out_value = dict; | 377 *out_value = dict; |
| 370 return true; | 378 return true; |
| 371 } | 379 } |
| 372 | 380 |
| 373 } // namespace proxy_cros_settings_parser | 381 } // namespace proxy_cros_settings_parser |
| 374 | 382 |
| 375 } // namespace chromeos | 383 } // namespace chromeos |
| OLD | NEW |