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.
5 #include "components/proxy_config/proxy_config_dictionary.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10 #include "net/proxy/proxy_config.h"
14 // Integer to specify the type of proxy settings.
15 // See ProxyPrefs for possible values and interactions with the other proxy
17 const char kProxyMode
[] = "mode";
18 // String specifying the proxy server. For a specification of the expected
19 // syntax see net::ProxyConfig::ProxyRules::ParseFromString().
20 const char kProxyServer
[] = "server";
21 // URL to the proxy .pac file.
22 const char kProxyPacUrl
[] = "pac_url";
23 // Optional boolean flag indicating whether a valid PAC script is mandatory.
24 // If true, network traffic does not fall back to direct connections in case the
25 // PAC script is not available.
26 const char kProxyPacMandatory
[] = "pac_mandatory";
27 // String containing proxy bypass rules. For a specification of the
28 // expected syntax see net::ProxyBypassRules::ParseFromString().
29 const char kProxyBypassList
[] = "bypass_list";
33 ProxyConfigDictionary::ProxyConfigDictionary(const base::DictionaryValue
* dict
)
34 : dict_(dict
->DeepCopy()) {
37 ProxyConfigDictionary::~ProxyConfigDictionary() {}
39 bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode
* out
) const {
41 return dict_
->GetString(kProxyMode
, &mode_str
)
42 && StringToProxyMode(mode_str
, out
);
45 bool ProxyConfigDictionary::GetPacUrl(std::string
* out
) const {
46 return dict_
->GetString(kProxyPacUrl
, out
);
49 bool ProxyConfigDictionary::GetPacMandatory(bool* out
) const {
50 if (!dict_
->HasKey(kProxyPacMandatory
)) {
54 return dict_
->GetBoolean(kProxyPacMandatory
, out
);
57 bool ProxyConfigDictionary::GetProxyServer(std::string
* out
) const {
58 return dict_
->GetString(kProxyServer
, out
);
61 bool ProxyConfigDictionary::GetBypassList(std::string
* out
) const {
62 return dict_
->GetString(kProxyBypassList
, out
);
65 bool ProxyConfigDictionary::HasBypassList() const {
66 return dict_
->HasKey(kProxyBypassList
);
69 const base::DictionaryValue
& ProxyConfigDictionary::GetDictionary() const {
74 base::DictionaryValue
* ProxyConfigDictionary::CreateDirect() {
75 return CreateDictionary(ProxyPrefs::MODE_DIRECT
,
83 base::DictionaryValue
* ProxyConfigDictionary::CreateAutoDetect() {
84 return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT
,
92 base::DictionaryValue
* ProxyConfigDictionary::CreatePacScript(
93 const std::string
& pac_url
,
95 return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT
,
103 base::DictionaryValue
* ProxyConfigDictionary::CreateFixedServers(
104 const std::string
& proxy_server
,
105 const std::string
& bypass_list
) {
106 if (!proxy_server
.empty()) {
107 return CreateDictionary(ProxyPrefs::MODE_FIXED_SERVERS
,
113 return CreateDirect();
118 base::DictionaryValue
* ProxyConfigDictionary::CreateSystem() {
119 return CreateDictionary(ProxyPrefs::MODE_SYSTEM
,
127 base::DictionaryValue
* ProxyConfigDictionary::CreateDictionary(
128 ProxyPrefs::ProxyMode mode
,
129 const std::string
& pac_url
,
131 const std::string
& proxy_server
,
132 const std::string
& bypass_list
) {
133 base::DictionaryValue
* dict
= new base::DictionaryValue();
134 dict
->SetString(kProxyMode
, ProxyModeToString(mode
));
135 if (!pac_url
.empty()) {
136 dict
->SetString(kProxyPacUrl
, pac_url
);
137 dict
->SetBoolean(kProxyPacMandatory
, pac_mandatory
);
139 if (!proxy_server
.empty())
140 dict
->SetString(kProxyServer
, proxy_server
);
141 if (!bypass_list
.empty())
142 dict
->SetString(kProxyBypassList
, bypass_list
);
147 void ProxyConfigDictionary::EncodeAndAppendProxyServer(
148 const std::string
& url_scheme
,
149 const net::ProxyServer
& server
,
151 if (!server
.is_valid())
157 if (!url_scheme
.empty()) {
161 *spec
+= server
.ToURI();