| 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 // Implementation of helper functions for the Chrome Extensions Proxy Settings | 5 // Implementation of helper functions for the Chrome Extensions Proxy Settings |
| 6 // API. | 6 // API. |
| 7 // | 7 // |
| 8 // Throughout this code, we report errors to the user by setting an |error| | 8 // Throughout this code, we report errors to the user by setting an |error| |
| 9 // parameter, if and only if these errors can be cause by invalid input | 9 // parameter, if and only if these errors can be cause by invalid input |
| 10 // from the extension and we cannot expect that the extensions API has | 10 // from the extension and we cannot expect that the extensions API has |
| 11 // caught this error before. In all other cases we are dealing with internal | 11 // caught this error before. In all other cases we are dealing with internal |
| 12 // errors and log to LOG(ERROR). | 12 // errors and log to LOG(ERROR). |
| 13 | 13 |
| 14 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h" | 14 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h" |
| 15 | 15 |
| 16 #include <stddef.h> | 16 #include <stddef.h> |
| 17 | 17 |
| 18 #include <utility> |
| 19 |
| 18 #include "base/base64.h" | 20 #include "base/base64.h" |
| 21 #include "base/memory/ptr_util.h" |
| 19 #include "base/strings/string_tokenizer.h" | 22 #include "base/strings/string_tokenizer.h" |
| 20 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/values.h" | 25 #include "base/values.h" |
| 23 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h" | 26 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h" |
| 24 #include "components/proxy_config/proxy_config_dictionary.h" | 27 #include "components/proxy_config/proxy_config_dictionary.h" |
| 25 #include "extensions/common/error_utils.h" | 28 #include "extensions/common/error_utils.h" |
| 26 #include "net/base/data_url.h" | 29 #include "net/base/data_url.h" |
| 27 #include "net/proxy/proxy_config.h" | 30 #include "net/proxy/proxy_config.h" |
| 28 | 31 |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 355 } |
| 353 case ProxyPrefs::MODE_SYSTEM: | 356 case ProxyPrefs::MODE_SYSTEM: |
| 354 result_proxy_config = ProxyConfigDictionary::CreateSystem(); | 357 result_proxy_config = ProxyConfigDictionary::CreateSystem(); |
| 355 break; | 358 break; |
| 356 case ProxyPrefs::kModeCount: | 359 case ProxyPrefs::kModeCount: |
| 357 NOTREACHED(); | 360 NOTREACHED(); |
| 358 } | 361 } |
| 359 return result_proxy_config; | 362 return result_proxy_config; |
| 360 } | 363 } |
| 361 | 364 |
| 362 base::DictionaryValue* CreateProxyRulesDict( | 365 std::unique_ptr<base::DictionaryValue> CreateProxyRulesDict( |
| 363 const ProxyConfigDictionary& proxy_config) { | 366 const ProxyConfigDictionary& proxy_config) { |
| 364 ProxyPrefs::ProxyMode mode; | 367 ProxyPrefs::ProxyMode mode; |
| 365 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_FIXED_SERVERS); | 368 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_FIXED_SERVERS); |
| 366 | 369 |
| 367 std::unique_ptr<base::DictionaryValue> extension_proxy_rules( | 370 auto extension_proxy_rules = base::MakeUnique<base::DictionaryValue>(); |
| 368 new base::DictionaryValue); | |
| 369 | 371 |
| 370 std::string proxy_servers; | 372 std::string proxy_servers; |
| 371 if (!proxy_config.GetProxyServer(&proxy_servers)) { | 373 if (!proxy_config.GetProxyServer(&proxy_servers)) { |
| 372 LOG(ERROR) << "Missing proxy servers in configuration."; | 374 LOG(ERROR) << "Missing proxy servers in configuration."; |
| 373 return NULL; | 375 return NULL; |
| 374 } | 376 } |
| 375 | 377 |
| 376 net::ProxyConfig::ProxyRules rules; | 378 net::ProxyConfig::ProxyRules rules; |
| 377 rules.ParseFromString(proxy_servers); | 379 rules.ParseFromString(proxy_servers); |
| 378 | 380 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // representing this scheme in the code above. | 416 // representing this scheme in the code above. |
| 415 static_assert(keys::SCHEME_MAX == 4, | 417 static_assert(keys::SCHEME_MAX == 4, |
| 416 "rules need to be updated along with schemes"); | 418 "rules need to be updated along with schemes"); |
| 417 | 419 |
| 418 if (proxy_config.HasBypassList()) { | 420 if (proxy_config.HasBypassList()) { |
| 419 std::string bypass_list_string; | 421 std::string bypass_list_string; |
| 420 if (!proxy_config.GetBypassList(&bypass_list_string)) { | 422 if (!proxy_config.GetBypassList(&bypass_list_string)) { |
| 421 LOG(ERROR) << "Invalid bypassList in configuration."; | 423 LOG(ERROR) << "Invalid bypassList in configuration."; |
| 422 return NULL; | 424 return NULL; |
| 423 } | 425 } |
| 424 base::ListValue* bypass_list = | 426 std::unique_ptr<base::ListValue> bypass_list = |
| 425 TokenizeToStringList(bypass_list_string, ",;"); | 427 TokenizeToStringList(bypass_list_string, ",;"); |
| 426 extension_proxy_rules->Set(keys::kProxyConfigBypassList, bypass_list); | 428 extension_proxy_rules->Set(keys::kProxyConfigBypassList, |
| 429 std::move(bypass_list)); |
| 427 } | 430 } |
| 428 | 431 |
| 429 return extension_proxy_rules.release(); | 432 return extension_proxy_rules; |
| 430 } | 433 } |
| 431 | 434 |
| 432 base::DictionaryValue* CreateProxyServerDict(const net::ProxyServer& proxy) { | 435 std::unique_ptr<base::DictionaryValue> CreateProxyServerDict( |
| 433 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 436 const net::ProxyServer& proxy) { |
| 437 auto out = base::MakeUnique<base::DictionaryValue>(); |
| 434 switch (proxy.scheme()) { | 438 switch (proxy.scheme()) { |
| 435 case net::ProxyServer::SCHEME_HTTP: | 439 case net::ProxyServer::SCHEME_HTTP: |
| 436 out->SetString(keys::kProxyConfigRuleScheme, "http"); | 440 out->SetString(keys::kProxyConfigRuleScheme, "http"); |
| 437 break; | 441 break; |
| 438 case net::ProxyServer::SCHEME_HTTPS: | 442 case net::ProxyServer::SCHEME_HTTPS: |
| 439 out->SetString(keys::kProxyConfigRuleScheme, "https"); | 443 out->SetString(keys::kProxyConfigRuleScheme, "https"); |
| 440 break; | 444 break; |
| 441 case net::ProxyServer::SCHEME_QUIC: | 445 case net::ProxyServer::SCHEME_QUIC: |
| 442 out->SetString(keys::kProxyConfigRuleScheme, "quic"); | 446 out->SetString(keys::kProxyConfigRuleScheme, "quic"); |
| 443 break; | 447 break; |
| 444 case net::ProxyServer::SCHEME_SOCKS4: | 448 case net::ProxyServer::SCHEME_SOCKS4: |
| 445 out->SetString(keys::kProxyConfigRuleScheme, "socks4"); | 449 out->SetString(keys::kProxyConfigRuleScheme, "socks4"); |
| 446 break; | 450 break; |
| 447 case net::ProxyServer::SCHEME_SOCKS5: | 451 case net::ProxyServer::SCHEME_SOCKS5: |
| 448 out->SetString(keys::kProxyConfigRuleScheme, "socks5"); | 452 out->SetString(keys::kProxyConfigRuleScheme, "socks5"); |
| 449 break; | 453 break; |
| 450 case net::ProxyServer::SCHEME_DIRECT: | 454 case net::ProxyServer::SCHEME_DIRECT: |
| 451 case net::ProxyServer::SCHEME_INVALID: | 455 case net::ProxyServer::SCHEME_INVALID: |
| 452 NOTREACHED(); | 456 NOTREACHED(); |
| 453 return NULL; | 457 return NULL; |
| 454 } | 458 } |
| 455 out->SetString(keys::kProxyConfigRuleHost, proxy.host_port_pair().host()); | 459 out->SetString(keys::kProxyConfigRuleHost, proxy.host_port_pair().host()); |
| 456 out->SetInteger(keys::kProxyConfigRulePort, proxy.host_port_pair().port()); | 460 out->SetInteger(keys::kProxyConfigRulePort, proxy.host_port_pair().port()); |
| 457 return out.release(); | 461 return out; |
| 458 } | 462 } |
| 459 | 463 |
| 460 base::DictionaryValue* CreatePacScriptDict( | 464 std::unique_ptr<base::DictionaryValue> CreatePacScriptDict( |
| 461 const ProxyConfigDictionary& proxy_config) { | 465 const ProxyConfigDictionary& proxy_config) { |
| 462 ProxyPrefs::ProxyMode mode; | 466 ProxyPrefs::ProxyMode mode; |
| 463 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_PAC_SCRIPT); | 467 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_PAC_SCRIPT); |
| 464 | 468 |
| 465 std::unique_ptr<base::DictionaryValue> pac_script_dict( | 469 auto pac_script_dict = base::MakeUnique<base::DictionaryValue>(); |
| 466 new base::DictionaryValue); | |
| 467 std::string pac_url; | 470 std::string pac_url; |
| 468 if (!proxy_config.GetPacUrl(&pac_url)) { | 471 if (!proxy_config.GetPacUrl(&pac_url)) { |
| 469 LOG(ERROR) << "Invalid proxy configuration. Missing PAC URL."; | 472 LOG(ERROR) << "Invalid proxy configuration. Missing PAC URL."; |
| 470 return NULL; | 473 return NULL; |
| 471 } | 474 } |
| 472 bool pac_mandatory = false; | 475 bool pac_mandatory = false; |
| 473 if (!proxy_config.GetPacMandatory(&pac_mandatory)) { | 476 if (!proxy_config.GetPacMandatory(&pac_mandatory)) { |
| 474 LOG(ERROR) << "Invalid proxy configuration. Missing PAC mandatory field."; | 477 LOG(ERROR) << "Invalid proxy configuration. Missing PAC mandatory field."; |
| 475 return NULL; | 478 return NULL; |
| 476 } | 479 } |
| 477 | 480 |
| 478 if (base::StartsWith(pac_url, "data", base::CompareCase::SENSITIVE)) { | 481 if (base::StartsWith(pac_url, "data", base::CompareCase::SENSITIVE)) { |
| 479 std::string pac_data; | 482 std::string pac_data; |
| 480 if (!CreatePACScriptFromDataURL(pac_url, &pac_data)) { | 483 if (!CreatePACScriptFromDataURL(pac_url, &pac_data)) { |
| 481 LOG(ERROR) << "Cannot decode base64-encoded PAC data URL: " << pac_url; | 484 LOG(ERROR) << "Cannot decode base64-encoded PAC data URL: " << pac_url; |
| 482 return NULL; | 485 return NULL; |
| 483 } | 486 } |
| 484 pac_script_dict->SetString(keys::kProxyConfigPacScriptData, pac_data); | 487 pac_script_dict->SetString(keys::kProxyConfigPacScriptData, pac_data); |
| 485 } else { | 488 } else { |
| 486 pac_script_dict->SetString(keys::kProxyConfigPacScriptUrl, pac_url); | 489 pac_script_dict->SetString(keys::kProxyConfigPacScriptUrl, pac_url); |
| 487 } | 490 } |
| 488 pac_script_dict->SetBoolean(keys::kProxyConfigPacScriptMandatory, | 491 pac_script_dict->SetBoolean(keys::kProxyConfigPacScriptMandatory, |
| 489 pac_mandatory); | 492 pac_mandatory); |
| 490 return pac_script_dict.release(); | 493 return pac_script_dict; |
| 491 } | 494 } |
| 492 | 495 |
| 493 base::ListValue* TokenizeToStringList(const std::string& in, | 496 std::unique_ptr<base::ListValue> TokenizeToStringList( |
| 494 const std::string& delims) { | 497 const std::string& in, |
| 495 base::ListValue* out = new base::ListValue; | 498 const std::string& delims) { |
| 499 auto out = base::MakeUnique<base::ListValue>(); |
| 496 base::StringTokenizer entries(in, delims); | 500 base::StringTokenizer entries(in, delims); |
| 497 while (entries.GetNext()) | 501 while (entries.GetNext()) |
| 498 out->AppendString(entries.token()); | 502 out->AppendString(entries.token()); |
| 499 return out; | 503 return out; |
| 500 } | 504 } |
| 501 | 505 |
| 502 } // namespace proxy_api_helpers | 506 } // namespace proxy_api_helpers |
| 503 } // namespace extensions | 507 } // namespace extensions |
| OLD | NEW |