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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
282 bool GetProxyPrefValue(const std::string& network_guid, | 285 bool GetProxyPrefValue(const std::string& network_guid, |
283 const std::string& path, | 286 const std::string& path, |
284 UIProxyConfigService* config_service, | 287 UIProxyConfigService* config_service, |
285 base::Value** out_value) { | 288 base::Value** out_value) { |
Daniel Erat
2017/03/23 14:21:30
nit: add a TODO to make this be a std::unique_ptr<
vabr (Chromium)
2017/03/23 22:43:50
Good point!
Done.
| |
286 std::string controlled_by; | 289 std::string controlled_by; |
287 base::Value* data = NULL; | 290 std::unique_ptr<base::Value> data; |
288 UIProxyConfig config; | 291 UIProxyConfig config; |
289 config_service->GetProxyConfig(network_guid, &config); | 292 config_service->GetProxyConfig(network_guid, &config); |
290 | 293 |
291 if (path == kProxyPacUrl) { | 294 if (path == kProxyPacUrl) { |
292 // Only show pacurl for pac-script mode. | 295 // Only show pacurl for pac-script mode. |
293 if (config.mode == UIProxyConfig::MODE_PAC_SCRIPT && | 296 if (config.mode == UIProxyConfig::MODE_PAC_SCRIPT && |
294 config.automatic_proxy.pac_url.is_valid()) { | 297 config.automatic_proxy.pac_url.is_valid()) { |
295 data = new base::Value(config.automatic_proxy.pac_url.spec()); | 298 data = |
299 base::MakeUnique<base::Value>(config.automatic_proxy.pac_url.spec()); | |
296 } | 300 } |
297 } else if (path == kProxySingleHttp) { | 301 } else if (path == kProxySingleHttp) { |
298 data = CreateServerHostValue(config.single_proxy); | 302 data = CreateServerHostValue(config.single_proxy); |
299 } else if (path == kProxySingleHttpPort) { | 303 } else if (path == kProxySingleHttpPort) { |
300 data = CreateServerPortValue(config.single_proxy); | 304 data = CreateServerPortValue(config.single_proxy); |
301 } else if (path == kProxyHttpUrl) { | 305 } else if (path == kProxyHttpUrl) { |
302 data = CreateServerHostValue(config.http_proxy); | 306 data = CreateServerHostValue(config.http_proxy); |
303 } else if (path == kProxyHttpsUrl) { | 307 } else if (path == kProxyHttpsUrl) { |
304 data = CreateServerHostValue(config.https_proxy); | 308 data = CreateServerHostValue(config.https_proxy); |
305 } else if (path == kProxyType) { | 309 } else if (path == kProxyType) { |
306 if (config.mode == UIProxyConfig::MODE_AUTO_DETECT || | 310 if (config.mode == UIProxyConfig::MODE_AUTO_DETECT || |
307 config.mode == UIProxyConfig::MODE_PAC_SCRIPT) { | 311 config.mode == UIProxyConfig::MODE_PAC_SCRIPT) { |
308 data = new base::Value(3); | 312 data = base::MakeUnique<base::Value>(3); |
309 } else if (config.mode == UIProxyConfig::MODE_SINGLE_PROXY || | 313 } else if (config.mode == UIProxyConfig::MODE_SINGLE_PROXY || |
310 config.mode == UIProxyConfig::MODE_PROXY_PER_SCHEME) { | 314 config.mode == UIProxyConfig::MODE_PROXY_PER_SCHEME) { |
311 data = new base::Value(2); | 315 data = base::MakeUnique<base::Value>(2); |
312 } else { | 316 } else { |
313 data = new base::Value(1); | 317 data = base::MakeUnique<base::Value>(1); |
314 } | 318 } |
315 switch (config.state) { | 319 switch (config.state) { |
316 case ProxyPrefs::CONFIG_POLICY: | 320 case ProxyPrefs::CONFIG_POLICY: |
317 controlled_by = "policy"; | 321 controlled_by = "policy"; |
318 break; | 322 break; |
319 case ProxyPrefs::CONFIG_EXTENSION: | 323 case ProxyPrefs::CONFIG_EXTENSION: |
320 controlled_by = "extension"; | 324 controlled_by = "extension"; |
321 break; | 325 break; |
322 case ProxyPrefs::CONFIG_OTHER_PRECEDE: | 326 case ProxyPrefs::CONFIG_OTHER_PRECEDE: |
323 controlled_by = "other"; | 327 controlled_by = "other"; |
324 break; | 328 break; |
325 default: | 329 default: |
326 if (!config.user_modifiable) | 330 if (!config.user_modifiable) |
327 controlled_by = "shared"; | 331 controlled_by = "shared"; |
328 break; | 332 break; |
329 } | 333 } |
330 } else if (path == kProxySingle) { | 334 } else if (path == kProxySingle) { |
331 data = new base::Value(config.mode == UIProxyConfig::MODE_SINGLE_PROXY); | 335 data = base::MakeUnique<base::Value>(config.mode == |
336 UIProxyConfig::MODE_SINGLE_PROXY); | |
332 } else if (path == kProxyUsePacUrl) { | 337 } else if (path == kProxyUsePacUrl) { |
333 data = new base::Value(config.mode == UIProxyConfig::MODE_PAC_SCRIPT); | 338 data = base::MakeUnique<base::Value>(config.mode == |
339 UIProxyConfig::MODE_PAC_SCRIPT); | |
334 } else if (path == kProxyFtpUrl) { | 340 } else if (path == kProxyFtpUrl) { |
335 data = CreateServerHostValue(config.ftp_proxy); | 341 data = CreateServerHostValue(config.ftp_proxy); |
336 } else if (path == kProxySocks) { | 342 } else if (path == kProxySocks) { |
337 data = CreateServerHostValue(config.socks_proxy); | 343 data = CreateServerHostValue(config.socks_proxy); |
338 } else if (path == kProxyHttpPort) { | 344 } else if (path == kProxyHttpPort) { |
339 data = CreateServerPortValue(config.http_proxy); | 345 data = CreateServerPortValue(config.http_proxy); |
340 } else if (path == kProxyHttpsPort) { | 346 } else if (path == kProxyHttpsPort) { |
341 data = CreateServerPortValue(config.https_proxy); | 347 data = CreateServerPortValue(config.https_proxy); |
342 } else if (path == kProxyFtpPort) { | 348 } else if (path == kProxyFtpPort) { |
343 data = CreateServerPortValue(config.ftp_proxy); | 349 data = CreateServerPortValue(config.ftp_proxy); |
344 } else if (path == kProxySocksPort) { | 350 } else if (path == kProxySocksPort) { |
345 data = CreateServerPortValue(config.socks_proxy); | 351 data = CreateServerPortValue(config.socks_proxy); |
346 } else if (path == kProxyIgnoreList) { | 352 } else if (path == kProxyIgnoreList) { |
347 base::ListValue* list = new base::ListValue(); | 353 auto list = base::MakeUnique<base::ListValue>(); |
348 const auto& bypass_rules = config.bypass_rules.rules(); | 354 const auto& bypass_rules = config.bypass_rules.rules(); |
349 for (const auto& rule : bypass_rules) | 355 for (const auto& rule : bypass_rules) |
350 list->AppendString(rule->ToString()); | 356 list->AppendString(rule->ToString()); |
351 data = list; | 357 data = std::move(list); |
352 } else { | 358 } else { |
353 *out_value = NULL; | 359 *out_value = NULL; |
354 return false; | 360 return false; |
355 } | 361 } |
356 | 362 |
357 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. | 363 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. |
358 base::DictionaryValue* dict = new base::DictionaryValue; | 364 base::DictionaryValue* dict = new base::DictionaryValue; |
359 if (!data) | 365 if (!data) |
360 data = new base::Value(""); | 366 data = base::MakeUnique<base::Value>(base::Value::Type::STRING); |
361 dict->Set("value", data); | 367 dict->Set("value", std::move(data)); |
362 if (path == kProxyType) { | 368 if (path == kProxyType) { |
363 if (!controlled_by.empty()) | 369 if (!controlled_by.empty()) |
364 dict->SetString("controlledBy", controlled_by); | 370 dict->SetString("controlledBy", controlled_by); |
365 dict->SetBoolean("disabled", !config.user_modifiable); | 371 dict->SetBoolean("disabled", !config.user_modifiable); |
366 } else { | 372 } else { |
367 dict->SetBoolean("disabled", false); | 373 dict->SetBoolean("disabled", false); |
368 } | 374 } |
369 *out_value = dict; | 375 *out_value = dict; |
370 return true; | 376 return true; |
371 } | 377 } |
372 | 378 |
373 } // namespace proxy_cros_settings_parser | 379 } // namespace proxy_cros_settings_parser |
374 | 380 |
375 } // namespace chromeos | 381 } // namespace chromeos |
OLD | NEW |