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 "chrome/browser/prefs/proxy_config_dictionary.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/values.h"
13 // Integer to specify the type of proxy settings.
14 // See ProxyPrefs for possible values and interactions with the other proxy
16 const char kProxyMode
[] = "mode";
17 // String specifying the proxy server. For a specification of the expected
18 // syntax see net::ProxyConfig::ProxyRules::ParseFromString().
19 const char kProxyServer
[] = "server";
20 // URL to the proxy .pac file.
21 const char kProxyPacUrl
[] = "pac_url";
22 // Optional boolean flag indicating whether a valid PAC script is mandatory.
23 // If true, network traffic does not fall back to direct connections in case the
24 // PAC script is not available.
25 const char kProxyPacMandatory
[] = "pac_mandatory";
26 // String containing proxy bypass rules. For a specification of the
27 // expected syntax see net::ProxyBypassRules::ParseFromString().
28 const char kProxyBypassList
[] = "bypass_list";
32 ProxyConfigDictionary::ProxyConfigDictionary(const base::DictionaryValue
* dict
)
33 : dict_(dict
->DeepCopy()) {
36 ProxyConfigDictionary::~ProxyConfigDictionary() {}
38 bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode
* out
) const {
40 return dict_
->GetString(kProxyMode
, &mode_str
)
41 && StringToProxyMode(mode_str
, out
);
44 bool ProxyConfigDictionary::GetPacUrl(std::string
* out
) const {
45 return dict_
->GetString(kProxyPacUrl
, out
);
48 bool ProxyConfigDictionary::GetPacMandatory(bool* out
) const {
49 if (!dict_
->HasKey(kProxyPacMandatory
)) {
53 return dict_
->GetBoolean(kProxyPacMandatory
, out
);
56 bool ProxyConfigDictionary::GetProxyServer(std::string
* out
) const {
57 return dict_
->GetString(kProxyServer
, out
);
60 bool ProxyConfigDictionary::GetBypassList(std::string
* out
) const {
61 return dict_
->GetString(kProxyBypassList
, out
);
64 bool ProxyConfigDictionary::HasBypassList() const {
65 return dict_
->HasKey(kProxyBypassList
);
68 const base::DictionaryValue
& ProxyConfigDictionary::GetDictionary() const {
73 base::DictionaryValue
* ProxyConfigDictionary::CreateDirect() {
74 return CreateDictionary(ProxyPrefs::MODE_DIRECT
,
82 base::DictionaryValue
* ProxyConfigDictionary::CreateAutoDetect() {
83 return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT
,
91 base::DictionaryValue
* ProxyConfigDictionary::CreatePacScript(
92 const std::string
& pac_url
,
94 return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT
,
102 base::DictionaryValue
* ProxyConfigDictionary::CreateFixedServers(
103 const std::string
& proxy_server
,
104 const std::string
& bypass_list
) {
105 if (!proxy_server
.empty()) {
106 return CreateDictionary(ProxyPrefs::MODE_FIXED_SERVERS
,
112 return CreateDirect();
117 base::DictionaryValue
* ProxyConfigDictionary::CreateSystem() {
118 return CreateDictionary(ProxyPrefs::MODE_SYSTEM
,
126 base::DictionaryValue
* ProxyConfigDictionary::CreateDictionary(
127 ProxyPrefs::ProxyMode mode
,
128 const std::string
& pac_url
,
130 const std::string
& proxy_server
,
131 const std::string
& bypass_list
) {
132 base::DictionaryValue
* dict
= new base::DictionaryValue();
133 dict
->SetString(kProxyMode
, ProxyModeToString(mode
));
134 if (!pac_url
.empty()) {
135 dict
->SetString(kProxyPacUrl
, pac_url
);
136 dict
->SetBoolean(kProxyPacMandatory
, pac_mandatory
);
138 if (!proxy_server
.empty())
139 dict
->SetString(kProxyServer
, proxy_server
);
140 if (!bypass_list
.empty())
141 dict
->SetString(kProxyBypassList
, bypass_list
);