Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / proxy_config / proxy_config_dictionary.cc
blob326dd137766812eace6a3a7a4a254a08a2d56f91
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"
12 namespace {
14 // Integer to specify the type of proxy settings.
15 // See ProxyPrefs for possible values and interactions with the other proxy
16 // preferences.
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";
31 } // namespace
33 ProxyConfigDictionary::ProxyConfigDictionary(const base::DictionaryValue* dict)
34 : dict_(dict->DeepCopy()) {
37 ProxyConfigDictionary::~ProxyConfigDictionary() {}
39 bool ProxyConfigDictionary::GetMode(ProxyPrefs::ProxyMode* out) const {
40 std::string mode_str;
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)) {
51 *out = false;
52 return true;
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 {
70 return *dict_;
73 // static
74 base::DictionaryValue* ProxyConfigDictionary::CreateDirect() {
75 return CreateDictionary(ProxyPrefs::MODE_DIRECT,
76 std::string(),
77 false,
78 std::string(),
79 std::string());
82 // static
83 base::DictionaryValue* ProxyConfigDictionary::CreateAutoDetect() {
84 return CreateDictionary(ProxyPrefs::MODE_AUTO_DETECT,
85 std::string(),
86 false,
87 std::string(),
88 std::string());
91 // static
92 base::DictionaryValue* ProxyConfigDictionary::CreatePacScript(
93 const std::string& pac_url,
94 bool pac_mandatory) {
95 return CreateDictionary(ProxyPrefs::MODE_PAC_SCRIPT,
96 pac_url,
97 pac_mandatory,
98 std::string(),
99 std::string());
102 // static
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,
108 std::string(),
109 false,
110 proxy_server,
111 bypass_list);
112 } else {
113 return CreateDirect();
117 // static
118 base::DictionaryValue* ProxyConfigDictionary::CreateSystem() {
119 return CreateDictionary(ProxyPrefs::MODE_SYSTEM,
120 std::string(),
121 false,
122 std::string(),
123 std::string());
126 // static
127 base::DictionaryValue* ProxyConfigDictionary::CreateDictionary(
128 ProxyPrefs::ProxyMode mode,
129 const std::string& pac_url,
130 bool pac_mandatory,
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);
143 return dict;
146 // static
147 void ProxyConfigDictionary::EncodeAndAppendProxyServer(
148 const std::string& url_scheme,
149 const net::ProxyServer& server,
150 std::string* spec) {
151 if (!server.is_valid())
152 return;
154 if (!spec->empty())
155 *spec += ';';
157 if (!url_scheme.empty()) {
158 *spec += url_scheme;
159 *spec += "=";
161 *spec += server.ToURI();