OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_proxy_api.h" | 5 #include "chrome/browser/extensions/extension_proxy_api.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/string_tokenizer.h" | |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 10 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 #include "net/proxy/proxy_config.h" | 14 #include "net/proxy/proxy_config.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // The scheme for which to use a manually specified proxy, not of the proxy URI | 18 // The scheme for which to use a manually specified proxy, not of the proxy URI |
(...skipping 24 matching lines...) Expand all Loading... | |
42 "ftp", | 43 "ftp", |
43 "socks" }; | 44 "socks" }; |
44 | 45 |
45 // String literals in dictionaries used to communicate with extension. | 46 // String literals in dictionaries used to communicate with extension. |
46 const char kProxyCfgMode[] = "mode"; | 47 const char kProxyCfgMode[] = "mode"; |
47 const char kProxyCfgPacScript[] = "pacScript"; | 48 const char kProxyCfgPacScript[] = "pacScript"; |
48 const char kProxyCfgPacScriptUrl[] = "url"; | 49 const char kProxyCfgPacScriptUrl[] = "url"; |
49 const char kProxyCfgRules[] = "rules"; | 50 const char kProxyCfgRules[] = "rules"; |
50 const char kProxyCfgRuleHost[] = "host"; | 51 const char kProxyCfgRuleHost[] = "host"; |
51 const char kProxyCfgRulePort[] = "port"; | 52 const char kProxyCfgRulePort[] = "port"; |
53 const char kProxyCfgBypassList[] = "bypassList"; | |
52 const char kProxyCfgScheme[] = "scheme"; | 54 const char kProxyCfgScheme[] = "scheme"; |
53 | 55 |
54 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); | 56 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); |
55 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, | 57 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, |
56 field_name_array_is_wrong_size); | 58 field_name_array_is_wrong_size); |
57 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, | 59 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, |
58 scheme_name_array_is_wrong_size); | 60 scheme_name_array_is_wrong_size); |
59 COMPILE_ASSERT(SCHEME_ALL == 0, singleProxy_must_be_first_option); | 61 COMPILE_ASSERT(SCHEME_ALL == 0, singleProxy_must_be_first_option); |
60 | 62 |
63 bool TokenizeToStringList( | |
64 const std::string& in, const std::string& delims, ListValue** out) { | |
65 scoped_ptr<ListValue> result(new ListValue); | |
66 StringTokenizer entries(in, delims); | |
67 while (entries.GetNext()) { | |
68 result->Append(Value::CreateStringValue(entries.token())); | |
69 } | |
70 *out = result.release(); | |
71 return true; | |
72 } | |
73 | |
74 bool JoinStringList( | |
75 ListValue* list, const std::string& joiner, std::string* out) { | |
76 std::string result; | |
77 for (size_t i = 0; i < list->GetSize(); ++i) { | |
78 if (!result.empty()) | |
79 result.append(joiner); | |
80 std::string entry; | |
81 if (!list->GetString(i, &entry)) | |
82 return false; | |
83 result.append(entry); | |
84 } | |
85 *out = result; | |
86 return true; | |
87 } | |
88 | |
61 // Converts a proxy server description |dict| as passed by the API caller | 89 // Converts a proxy server description |dict| as passed by the API caller |
62 // (e.g. for the http proxy in the rules element) and converts it to a | 90 // (e.g. for the http proxy in the rules element) and converts it to a |
63 // ProxyServer. Returns true if successful. | 91 // ProxyServer. Returns true if successful. |
64 bool GetProxyServer(const DictionaryValue* dict, | 92 bool GetProxyServer(const DictionaryValue* dict, |
65 net::ProxyServer::Scheme default_scheme, | 93 net::ProxyServer::Scheme default_scheme, |
66 net::ProxyServer* proxy_server) { | 94 net::ProxyServer* proxy_server) { |
67 std::string scheme_string; // optional. | 95 std::string scheme_string; // optional. |
68 dict->GetString(kProxyCfgScheme, &scheme_string); | 96 dict->GetString(kProxyCfgScheme, &scheme_string); |
69 | 97 |
70 net::ProxyServer::Scheme scheme = | 98 net::ProxyServer::Scheme scheme = |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 proxy_pref.append(scheme_name[i]); | 167 proxy_pref.append(scheme_name[i]); |
140 proxy_pref.append("="); | 168 proxy_pref.append("="); |
141 proxy_pref.append(proxy_server[i].ToURI()); | 169 proxy_pref.append(proxy_server[i].ToURI()); |
142 } | 170 } |
143 } | 171 } |
144 | 172 |
145 *out = proxy_pref; | 173 *out = proxy_pref; |
146 return true; | 174 return true; |
147 } | 175 } |
148 | 176 |
177 // Retrieves the "bypassList" from a proxy "rules" element passed by the API | |
178 // caller in a proxy configuration and returns it as a joined string. | |
eroman
2011/02/11 04:52:28
The wording of this comment doesn't seem right.
battre
2011/02/11 15:06:03
Done.
| |
179 // Returns true if successful (i.e. string could be delivered or no "bypassList" | |
180 // exists in the |proxy_rules|). | |
181 bool GetBypassList(DictionaryValue* proxy_rules, std::string* out) { | |
182 if (!proxy_rules) | |
183 return false; | |
184 | |
185 ListValue* bypass_list; | |
186 if (!proxy_rules->HasKey(kProxyCfgBypassList)) { | |
187 *out = ""; | |
188 return true; | |
189 } | |
190 if (!proxy_rules->GetList(kProxyCfgBypassList, &bypass_list)) | |
191 return false; | |
192 | |
193 return JoinStringList(bypass_list, ",", out); | |
194 } | |
195 | |
149 } // namespace | 196 } // namespace |
150 | 197 |
151 void ProxySettingsFunction::ApplyPreference(const char* pref_path, | 198 void ProxySettingsFunction::ApplyPreference(const char* pref_path, |
152 Value* pref_value, | 199 Value* pref_value, |
153 bool incognito) { | 200 bool incognito) { |
154 Profile* use_profile = profile(); | 201 Profile* use_profile = profile(); |
155 if (use_profile->IsOffTheRecord()) | 202 if (use_profile->IsOffTheRecord()) |
156 use_profile = use_profile->GetOriginalProfile(); | 203 use_profile = use_profile->GetOriginalProfile(); |
157 | 204 |
158 use_profile->GetExtensionService()->extension_prefs()-> | 205 use_profile->GetExtensionService()->extension_prefs()-> |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 } | 245 } |
199 | 246 |
200 DictionaryValue* proxy_rules = NULL; | 247 DictionaryValue* proxy_rules = NULL; |
201 proxy_config->GetDictionary(kProxyCfgRules, &proxy_rules); | 248 proxy_config->GetDictionary(kProxyCfgRules, &proxy_rules); |
202 std::string proxy_rules_string; | 249 std::string proxy_rules_string; |
203 if (proxy_rules && !GetProxyRules(proxy_rules, &proxy_rules_string)) { | 250 if (proxy_rules && !GetProxyRules(proxy_rules, &proxy_rules_string)) { |
204 LOG(ERROR) << "Invalid 'rules' specified. " | 251 LOG(ERROR) << "Invalid 'rules' specified. " |
205 << "Setting custom proxy settings failed."; | 252 << "Setting custom proxy settings failed."; |
206 return false; | 253 return false; |
207 } | 254 } |
255 std::string bypass_list; | |
256 if (proxy_rules && !GetBypassList(proxy_rules, &bypass_list)) { | |
eroman
2011/02/11 04:52:28
There are string encoding problems in this file.
battre
2011/02/11 15:06:03
I have replaced all GetString calls with GetString
| |
257 LOG(ERROR) << "Invalid 'bypassList' specified. " | |
258 << "Setting custom proxy settings failed."; | |
259 return false; | |
260 } | |
208 | 261 |
209 // not supported, yet. | |
210 std::string bypass_list; | |
211 | |
212 DictionaryValue* result_proxy_config = NULL; | 262 DictionaryValue* result_proxy_config = NULL; |
213 switch (mode_enum) { | 263 switch (mode_enum) { |
214 case ProxyPrefs::MODE_DIRECT: | 264 case ProxyPrefs::MODE_DIRECT: |
215 result_proxy_config = ProxyConfigDictionary::CreateDirect(); | 265 result_proxy_config = ProxyConfigDictionary::CreateDirect(); |
216 break; | 266 break; |
217 case ProxyPrefs::MODE_AUTO_DETECT: | 267 case ProxyPrefs::MODE_AUTO_DETECT: |
218 result_proxy_config = ProxyConfigDictionary::CreateAutoDetect(); | 268 result_proxy_config = ProxyConfigDictionary::CreateAutoDetect(); |
219 break; | 269 break; |
220 case ProxyPrefs::MODE_PAC_SCRIPT: { | 270 case ProxyPrefs::MODE_PAC_SCRIPT: { |
221 if (!pac_dict) { | 271 if (!pac_dict) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 if (!dict.GetPacUrl(&pac_url)) { | 358 if (!dict.GetPacUrl(&pac_url)) { |
309 LOG(ERROR) << "Missing pac url"; | 359 LOG(ERROR) << "Missing pac url"; |
310 return false; | 360 return false; |
311 } | 361 } |
312 DictionaryValue* pac_dict = new DictionaryValue; | 362 DictionaryValue* pac_dict = new DictionaryValue; |
313 pac_dict->SetString(kProxyCfgPacScriptUrl, pac_url); | 363 pac_dict->SetString(kProxyCfgPacScriptUrl, pac_url); |
314 api_proxy_config->Set(kProxyCfgPacScript, pac_dict); | 364 api_proxy_config->Set(kProxyCfgPacScript, pac_dict); |
315 break; | 365 break; |
316 } | 366 } |
317 case ProxyPrefs::MODE_FIXED_SERVERS: { | 367 case ProxyPrefs::MODE_FIXED_SERVERS: { |
318 // TODO(battre): Handle bypass list. | 368 DictionaryValue* rules_dict = new DictionaryValue; |
369 | |
319 std::string proxy_servers; | 370 std::string proxy_servers; |
320 if (!dict.GetProxyServer(&proxy_servers)) { | 371 if (!dict.GetProxyServer(&proxy_servers)) { |
321 LOG(ERROR) << "Missing proxy servers"; | 372 LOG(ERROR) << "Missing proxy servers in configuration"; |
322 return false; | 373 return false; |
323 } | 374 } |
324 DictionaryValue* rules_dict = new DictionaryValue; | |
325 if (!ParseRules(proxy_servers, rules_dict)) { | 375 if (!ParseRules(proxy_servers, rules_dict)) { |
326 LOG(ERROR) << "Could not parse proxy rules"; | 376 LOG(ERROR) << "Could not parse proxy rules"; |
327 return false; | 377 return false; |
328 } | 378 } |
379 | |
380 bool hasBypassList = dict.HasBypassList(); | |
381 if (hasBypassList) { | |
382 std::string bypass_list_string; | |
383 if (!dict.GetBypassList(&bypass_list_string)) { | |
384 LOG(ERROR) << "Invalid bypassList in configuration"; | |
385 return false; | |
eroman
2011/02/11 04:52:28
See comment below.
battre
2011/02/11 15:06:03
Done.
| |
386 } | |
387 ListValue* bypass_list = NULL; | |
388 if (TokenizeToStringList(bypass_list_string, ",;", &bypass_list)) { | |
389 rules_dict->Set(kProxyCfgBypassList, bypass_list); | |
390 } else { | |
391 LOG(ERROR) << "Error parsing bypassList " << bypass_list_string; | |
392 return false; | |
eroman
2011/02/11 04:52:28
Isn't this going to leak |rules_dict|?
I suggest
battre
2011/02/11 15:06:03
Done.
| |
393 } | |
394 } | |
329 api_proxy_config->Set(kProxyCfgRules, rules_dict); | 395 api_proxy_config->Set(kProxyCfgRules, rules_dict); |
330 break; | 396 break; |
331 } | 397 } |
332 case ProxyPrefs::kModeCount: | 398 case ProxyPrefs::kModeCount: |
333 NOTREACHED(); | 399 NOTREACHED(); |
334 } | 400 } |
335 return true; | 401 return true; |
336 } | 402 } |
337 | 403 |
338 bool GetCurrentProxySettingsFunction::ParseRules(const std::string& rules, | 404 bool GetCurrentProxySettingsFunction::ParseRules(const std::string& rules, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
389 break; | 455 break; |
390 case net::ProxyServer::SCHEME_DIRECT: | 456 case net::ProxyServer::SCHEME_DIRECT: |
391 case net::ProxyServer::SCHEME_INVALID: | 457 case net::ProxyServer::SCHEME_INVALID: |
392 NOTREACHED(); | 458 NOTREACHED(); |
393 return out; | 459 return out; |
394 } | 460 } |
395 out->SetString(kProxyCfgRuleHost, proxy.host_port_pair().host()); | 461 out->SetString(kProxyCfgRuleHost, proxy.host_port_pair().host()); |
396 out->SetInteger(kProxyCfgRulePort, proxy.host_port_pair().port()); | 462 out->SetInteger(kProxyCfgRulePort, proxy.host_port_pair().port()); |
397 return out; | 463 return out; |
398 } | 464 } |
OLD | NEW |