proxy_config_dictionary.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "components/proxy_config/proxy_config_dictionary.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/check.h"
  8. #include "net/base/proxy_server.h"
  9. #include "net/base/proxy_string_util.h"
  10. #include "net/proxy_resolution/proxy_config.h"
  11. namespace {
  12. // Integer to specify the type of proxy settings.
  13. // See ProxyPrefs for possible values and interactions with the other proxy
  14. // preferences.
  15. const char kProxyMode[] = "mode";
  16. // String specifying the proxy server. For a specification of the expected
  17. // syntax see net::ProxyConfig::ProxyRules::ParseFromString().
  18. const char kProxyServer[] = "server";
  19. // URL to the proxy .pac file.
  20. const char kProxyPacUrl[] = "pac_url";
  21. // Optional boolean flag indicating whether a valid PAC script is mandatory.
  22. // If true, network traffic does not fall back to direct connections in case the
  23. // PAC script is not available.
  24. const char kProxyPacMandatory[] = "pac_mandatory";
  25. // String containing proxy bypass rules. For a specification of the
  26. // expected syntax see net::ProxyBypassRules::ParseFromString().
  27. const char kProxyBypassList[] = "bypass_list";
  28. } // namespace
  29. ProxyConfigDictionary::ProxyConfigDictionary(base::Value dict)
  30. : dict_(std::move(dict)) {
  31. DCHECK(dict_.is_dict());
  32. }
  33. ProxyConfigDictionary::ProxyConfigDictionary(ProxyConfigDictionary&& other) {
  34. dict_ = std::move(other.dict_);
  35. }
  36. ProxyConfigDictionary::~ProxyConfigDictionary() = default;
  37. bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode* out) const {
  38. const base::Value* mode_value = dict_.FindKey(kProxyMode);
  39. if (!mode_value || !mode_value->is_string())
  40. return false;
  41. std::string mode_str = mode_value->GetString();
  42. return StringToProxyMode(mode_str, out);
  43. }
  44. bool ProxyConfigDictionary::GetPacUrl(std::string* out) const {
  45. return GetString(kProxyPacUrl, out);
  46. }
  47. bool ProxyConfigDictionary::GetPacMandatory(bool* out) const {
  48. const base::Value* value = dict_.FindKey(kProxyPacMandatory);
  49. if (!value || !value->is_bool()) {
  50. *out = false;
  51. return false;
  52. }
  53. *out = value->GetBool();
  54. return true;
  55. }
  56. bool ProxyConfigDictionary::GetProxyServer(std::string* out) const {
  57. return GetString(kProxyServer, out);
  58. }
  59. bool ProxyConfigDictionary::GetBypassList(std::string* out) const {
  60. return GetString(kProxyBypassList, out);
  61. }
  62. bool ProxyConfigDictionary::HasBypassList() const {
  63. return dict_.FindKey(kProxyBypassList);
  64. }
  65. const base::Value& ProxyConfigDictionary::GetDictionary() const {
  66. return dict_;
  67. }
  68. // static
  69. base::Value ProxyConfigDictionary::CreateDirect() {
  70. return CreateDictionary(ProxyPrefs::MODE_DIRECT, std::string(), false,
  71. std::string(), std::string());
  72. }
  73. // static
  74. base::Value ProxyConfigDictionary::CreateAutoDetect() {
  75. return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT, std::string(), false,
  76. std::string(), std::string());
  77. }
  78. // static
  79. base::Value ProxyConfigDictionary::CreatePacScript(const std::string& pac_url,
  80. bool pac_mandatory) {
  81. return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT, pac_url, pac_mandatory,
  82. std::string(), std::string());
  83. }
  84. // static
  85. base::Value ProxyConfigDictionary::CreateFixedServers(
  86. const std::string& proxy_server,
  87. const std::string& bypass_list) {
  88. if (!proxy_server.empty()) {
  89. return CreateDictionary(ProxyPrefs::MODE_FIXED_SERVERS, std::string(),
  90. false, proxy_server, bypass_list);
  91. } else {
  92. return CreateDirect();
  93. }
  94. }
  95. // static
  96. base::Value ProxyConfigDictionary::CreateSystem() {
  97. return CreateDictionary(ProxyPrefs::MODE_SYSTEM, std::string(), false,
  98. std::string(), std::string());
  99. }
  100. // static
  101. base::Value ProxyConfigDictionary::CreateDictionary(
  102. ProxyPrefs::ProxyMode mode,
  103. const std::string& pac_url,
  104. bool pac_mandatory,
  105. const std::string& proxy_server,
  106. const std::string& bypass_list) {
  107. base::Value dict(base::Value::Type::DICTIONARY);
  108. dict.SetKey(kProxyMode, base::Value(ProxyModeToString(mode)));
  109. if (!pac_url.empty()) {
  110. dict.SetKey(kProxyPacUrl, base::Value(pac_url));
  111. dict.SetKey(kProxyPacMandatory, base::Value(pac_mandatory));
  112. }
  113. if (!proxy_server.empty())
  114. dict.SetKey(kProxyServer, base::Value(proxy_server));
  115. if (!bypass_list.empty())
  116. dict.SetKey(kProxyBypassList, base::Value(bypass_list));
  117. return dict;
  118. }
  119. // static
  120. void ProxyConfigDictionary::EncodeAndAppendProxyServer(
  121. const std::string& url_scheme,
  122. const net::ProxyServer& server,
  123. std::string* spec) {
  124. if (!server.is_valid())
  125. return;
  126. if (!spec->empty())
  127. *spec += ';';
  128. if (!url_scheme.empty()) {
  129. *spec += url_scheme;
  130. *spec += "=";
  131. }
  132. *spec += net::ProxyServerToProxyUri(server);
  133. }
  134. bool ProxyConfigDictionary::GetString(const char* key, std::string* out) const {
  135. const base::Value* value = dict_.FindKey(key);
  136. if (!value || !value->is_string()) {
  137. *out = "";
  138. return false;
  139. }
  140. *out = value->GetString();
  141. return true;
  142. }