blob: e2c6ce30f8e6ea03aa4d2aa1f00832dca1649b8c [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2011 The Chromium Authors
[email protected]c2f23d012011-02-09 14:52:172// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
stevenjbb237e2ae2015-07-02 22:02:115#include "components/proxy_config/proxy_config_dictionary.h"
[email protected]c2f23d012011-02-09 14:52:176
Gyuyoung Kim8b084c02018-01-23 13:34:377#include <memory>
vabr6f6e2662017-03-31 08:08:338#include <utility>
9
Hans Wennborgdf87046c2020-04-28 11:06:2410#include "base/check.h"
Eric Orth5ccc3f02021-09-23 00:01:5711#include "net/base/proxy_server.h"
12#include "net/base/proxy_string_util.h"
Lily Houghton582d4622018-01-22 22:43:4013#include "net/proxy_resolution/proxy_config.h"
[email protected]c2f23d012011-02-09 14:52:1714
15namespace {
16
17// Integer to specify the type of proxy settings.
18// See ProxyPrefs for possible values and interactions with the other proxy
19// preferences.
20const char kProxyMode[] = "mode";
21// String specifying the proxy server. For a specification of the expected
22// syntax see net::ProxyConfig::ProxyRules::ParseFromString().
23const char kProxyServer[] = "server";
24// URL to the proxy .pac file.
25const char kProxyPacUrl[] = "pac_url";
[email protected]e0470bd22011-05-03 12:44:1926// Optional boolean flag indicating whether a valid PAC script is mandatory.
27// If true, network traffic does not fall back to direct connections in case the
28// PAC script is not available.
29const char kProxyPacMandatory[] = "pac_mandatory";
[email protected]c2f23d012011-02-09 14:52:1730// String containing proxy bypass rules. For a specification of the
31// expected syntax see net::ProxyBypassRules::ParseFromString().
32const char kProxyBypassList[] = "bypass_list";
33
34} // namespace
35
Andrey Davydov21c16e22022-09-29 16:19:4536ProxyConfigDictionary::ProxyConfigDictionary(base::Value::Dict dict)
37 : dict_(std::move(dict)) {}
[email protected]c2f23d012011-02-09 14:52:1738
Andreea Costinasdd0ee9e2021-09-13 15:17:4539ProxyConfigDictionary::ProxyConfigDictionary(ProxyConfigDictionary&& other) {
40 dict_ = std::move(other.dict_);
41}
42
43ProxyConfigDictionary::~ProxyConfigDictionary() = default;
[email protected]b6ce91b2011-02-10 21:30:5044
[email protected]c2f23d012011-02-09 14:52:1745bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode* out) const {
Andrey Davydov21c16e22022-09-29 16:19:4546 const base::Value* mode_value = dict_.Find(kProxyMode);
Steven Bennetts3e470ac2018-07-27 18:29:2747 if (!mode_value || !mode_value->is_string())
48 return false;
49 std::string mode_str = mode_value->GetString();
50 return StringToProxyMode(mode_str, out);
[email protected]c2f23d012011-02-09 14:52:1751}
52
53bool ProxyConfigDictionary::GetPacUrl(std::string* out) const {
Steven Bennetts3e470ac2018-07-27 18:29:2754 return GetString(kProxyPacUrl, out);
[email protected]c2f23d012011-02-09 14:52:1755}
56
[email protected]e0470bd22011-05-03 12:44:1957bool ProxyConfigDictionary::GetPacMandatory(bool* out) const {
Andrey Davydov21c16e22022-09-29 16:19:4558 const base::Value* value = dict_.Find(kProxyPacMandatory);
Steven Bennetts3e470ac2018-07-27 18:29:2759 if (!value || !value->is_bool()) {
[email protected]e0470bd22011-05-03 12:44:1960 *out = false;
Steven Bennetts3e470ac2018-07-27 18:29:2761 return false;
[email protected]e0470bd22011-05-03 12:44:1962 }
Steven Bennetts3e470ac2018-07-27 18:29:2763 *out = value->GetBool();
64 return true;
[email protected]e0470bd22011-05-03 12:44:1965}
66
[email protected]c2f23d012011-02-09 14:52:1767bool ProxyConfigDictionary::GetProxyServer(std::string* out) const {
Steven Bennetts3e470ac2018-07-27 18:29:2768 return GetString(kProxyServer, out);
[email protected]c2f23d012011-02-09 14:52:1769}
70
71bool ProxyConfigDictionary::GetBypassList(std::string* out) const {
Steven Bennetts3e470ac2018-07-27 18:29:2772 return GetString(kProxyBypassList, out);
[email protected]c2f23d012011-02-09 14:52:1773}
74
[email protected]ce50d072011-02-14 12:14:2375bool ProxyConfigDictionary::HasBypassList() const {
Andrey Davydov21c16e22022-09-29 16:19:4576 return dict_.Find(kProxyBypassList);
[email protected]ce50d072011-02-14 12:14:2377}
78
Andrey Davydov21c16e22022-09-29 16:19:4579const base::Value::Dict& ProxyConfigDictionary::GetDictionary() const {
Steven Bennetts3e470ac2018-07-27 18:29:2780 return dict_;
[email protected]9da3e452013-06-10 15:09:5881}
82
[email protected]c2f23d012011-02-09 14:52:1783// static
Andrey Davydov21c16e22022-09-29 16:19:4584base::Value::Dict ProxyConfigDictionary::CreateDirect() {
Steven Bennetts3e470ac2018-07-27 18:29:2785 return CreateDictionary(ProxyPrefs::MODE_DIRECT, std::string(), false,
86 std::string(), std::string());
[email protected]c2f23d012011-02-09 14:52:1787}
88
89// static
Andrey Davydov21c16e22022-09-29 16:19:4590base::Value::Dict ProxyConfigDictionary::CreateAutoDetect() {
Steven Bennetts3e470ac2018-07-27 18:29:2791 return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT, std::string(), false,
92 std::string(), std::string());
[email protected]c2f23d012011-02-09 14:52:1793}
94
95// static
Andrey Davydov21c16e22022-09-29 16:19:4596base::Value::Dict ProxyConfigDictionary::CreatePacScript(
97 const std::string& pac_url,
98 bool pac_mandatory) {
Steven Bennetts3e470ac2018-07-27 18:29:2799 return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT, pac_url, pac_mandatory,
100 std::string(), std::string());
[email protected]c2f23d012011-02-09 14:52:17101}
102
103// static
Andrey Davydov21c16e22022-09-29 16:19:45104base::Value::Dict ProxyConfigDictionary::CreateFixedServers(
Steven Bennetts3e470ac2018-07-27 18:29:27105 const std::string& proxy_server,
106 const std::string& bypass_list) {
[email protected]b3633612011-02-09 15:06:39107 if (!proxy_server.empty()) {
Steven Bennetts3e470ac2018-07-27 18:29:27108 return CreateDictionary(ProxyPrefs::MODE_FIXED_SERVERS, std::string(),
109 false, proxy_server, bypass_list);
[email protected]b3633612011-02-09 15:06:39110 } else {
111 return CreateDirect();
112 }
[email protected]c2f23d012011-02-09 14:52:17113}
114
115// static
Andrey Davydov21c16e22022-09-29 16:19:45116base::Value::Dict ProxyConfigDictionary::CreateSystem() {
Steven Bennetts3e470ac2018-07-27 18:29:27117 return CreateDictionary(ProxyPrefs::MODE_SYSTEM, std::string(), false,
118 std::string(), std::string());
[email protected]c2f23d012011-02-09 14:52:17119}
120
121// static
Andrey Davydov21c16e22022-09-29 16:19:45122base::Value::Dict ProxyConfigDictionary::CreateDictionary(
[email protected]c2f23d012011-02-09 14:52:17123 ProxyPrefs::ProxyMode mode,
124 const std::string& pac_url,
[email protected]e0470bd22011-05-03 12:44:19125 bool pac_mandatory,
[email protected]c2f23d012011-02-09 14:52:17126 const std::string& proxy_server,
127 const std::string& bypass_list) {
Andrey Davydov21c16e22022-09-29 16:19:45128 base::Value::Dict dict;
129 dict.Set(kProxyMode, base::Value(ProxyModeToString(mode)));
[email protected]e0470bd22011-05-03 12:44:19130 if (!pac_url.empty()) {
Andrey Davydov21c16e22022-09-29 16:19:45131 dict.Set(kProxyPacUrl, base::Value(pac_url));
132 dict.Set(kProxyPacMandatory, base::Value(pac_mandatory));
[email protected]e0470bd22011-05-03 12:44:19133 }
[email protected]c2f23d012011-02-09 14:52:17134 if (!proxy_server.empty())
Andrey Davydov21c16e22022-09-29 16:19:45135 dict.Set(kProxyServer, base::Value(proxy_server));
[email protected]c2f23d012011-02-09 14:52:17136 if (!bypass_list.empty())
Andrey Davydov21c16e22022-09-29 16:19:45137 dict.Set(kProxyBypassList, base::Value(bypass_list));
[email protected]c2f23d012011-02-09 14:52:17138 return dict;
139}
stevenjb83368882015-07-10 20:51:45140
141// static
142void ProxyConfigDictionary::EncodeAndAppendProxyServer(
143 const std::string& url_scheme,
144 const net::ProxyServer& server,
145 std::string* spec) {
146 if (!server.is_valid())
147 return;
148
149 if (!spec->empty())
150 *spec += ';';
151
152 if (!url_scheme.empty()) {
153 *spec += url_scheme;
154 *spec += "=";
155 }
Eric Orth5ccc3f02021-09-23 00:01:57156 *spec += net::ProxyServerToProxyUri(server);
stevenjb83368882015-07-10 20:51:45157}
Steven Bennetts3e470ac2018-07-27 18:29:27158
159bool ProxyConfigDictionary::GetString(const char* key, std::string* out) const {
Andrey Davydov21c16e22022-09-29 16:19:45160 const base::Value* value = dict_.Find(key);
Steven Bennetts3e470ac2018-07-27 18:29:27161 if (!value || !value->is_string()) {
162 *out = "";
163 return false;
164 }
165 *out = value->GetString();
166 return true;
167}