| 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 // Implementation of helper functions for the Chrome Extensions Proxy Settings | 5 // Implementation of helper functions for the Chrome Extensions Proxy Settings |
| 6 // API. | 6 // API. |
| 7 // | 7 // |
| 8 // Throughout this code, we report errors to the user by setting an |error| | 8 // Throughout this code, we report errors to the user by setting an |error| |
| 9 // parameter, if and only if these errors can be cause by invalid input | 9 // parameter, if and only if these errors can be cause by invalid input |
| 10 // from the extension and we cannot expect that the extensions API has | 10 // from the extension and we cannot expect that the extensions API has |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // We can safely assume that this is ASCII due to the allowed enumeration | 65 // We can safely assume that this is ASCII due to the allowed enumeration |
| 66 // values specified in extension_api.json. | 66 // values specified in extension_api.json. |
| 67 proxy_config->GetStringASCII(keys::kProxyConfigMode, &proxy_mode); | 67 proxy_config->GetStringASCII(keys::kProxyConfigMode, &proxy_mode); |
| 68 if (!ProxyPrefs::StringToProxyMode(proxy_mode, out)) { | 68 if (!ProxyPrefs::StringToProxyMode(proxy_mode, out)) { |
| 69 LOG(ERROR) << "Invalid mode for proxy settings: " << proxy_mode; | 69 LOG(ERROR) << "Invalid mode for proxy settings: " << proxy_mode; |
| 70 return false; | 70 return false; |
| 71 } | 71 } |
| 72 return true; | 72 return true; |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool GetPacMandatoryFromExtensionPref(const DictionaryValue* proxy_config, |
| 76 bool* out, |
| 77 std::string* error){ |
| 78 DictionaryValue* pac_dict = NULL; |
| 79 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); |
| 80 if (!pac_dict) |
| 81 return true; |
| 82 |
| 83 bool mandatory_pac = false; |
| 84 if (pac_dict->HasKey(keys::kProxyConfigPacScriptMandatory) && |
| 85 !pac_dict->GetBoolean(keys::kProxyConfigPacScriptMandatory, |
| 86 &mandatory_pac)) { |
| 87 LOG(ERROR) << "'pacScript.mandatory' could not be parsed."; |
| 88 return false; |
| 89 } |
| 90 *out = mandatory_pac; |
| 91 return true; |
| 92 } |
| 93 |
| 75 bool GetPacUrlFromExtensionPref(const DictionaryValue* proxy_config, | 94 bool GetPacUrlFromExtensionPref(const DictionaryValue* proxy_config, |
| 76 std::string* out, | 95 std::string* out, |
| 77 std::string* error) { | 96 std::string* error) { |
| 78 DictionaryValue* pac_dict = NULL; | 97 DictionaryValue* pac_dict = NULL; |
| 79 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); | 98 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); |
| 80 if (!pac_dict) | 99 if (!pac_dict) |
| 81 return true; | 100 return true; |
| 82 | 101 |
| 83 // TODO(battre): Handle UTF-8 URLs (http://crbug.com/72692). | 102 // TODO(battre): Handle UTF-8 URLs (http://crbug.com/72692). |
| 84 string16 pac_url16; | 103 string16 pac_url16; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 ListValue* bypass_list = NULL; | 284 ListValue* bypass_list = NULL; |
| 266 if (!proxy_rules->GetList(keys::kProxyConfigBypassList, &bypass_list)) { | 285 if (!proxy_rules->GetList(keys::kProxyConfigBypassList, &bypass_list)) { |
| 267 LOG(ERROR) << "'rules.bypassList' not be parsed."; | 286 LOG(ERROR) << "'rules.bypassList' not be parsed."; |
| 268 return false; | 287 return false; |
| 269 } | 288 } |
| 270 | 289 |
| 271 return JoinUrlList(bypass_list, ",", out, error); | 290 return JoinUrlList(bypass_list, ",", out, error); |
| 272 } | 291 } |
| 273 | 292 |
| 274 DictionaryValue* CreateProxyConfigDict(ProxyPrefs::ProxyMode mode_enum, | 293 DictionaryValue* CreateProxyConfigDict(ProxyPrefs::ProxyMode mode_enum, |
| 294 bool pac_mandatory, |
| 275 const std::string& pac_url, | 295 const std::string& pac_url, |
| 276 const std::string& pac_data, | 296 const std::string& pac_data, |
| 277 const std::string& proxy_rules_string, | 297 const std::string& proxy_rules_string, |
| 278 const std::string& bypass_list, | 298 const std::string& bypass_list, |
| 279 std::string* error) { | 299 std::string* error) { |
| 280 DictionaryValue* result_proxy_config = NULL; | 300 DictionaryValue* result_proxy_config = NULL; |
| 281 switch (mode_enum) { | 301 switch (mode_enum) { |
| 282 case ProxyPrefs::MODE_DIRECT: | 302 case ProxyPrefs::MODE_DIRECT: |
| 283 result_proxy_config = ProxyConfigDictionary::CreateDirect(); | 303 result_proxy_config = ProxyConfigDictionary::CreateDirect(); |
| 284 break; | 304 break; |
| 285 case ProxyPrefs::MODE_AUTO_DETECT: | 305 case ProxyPrefs::MODE_AUTO_DETECT: |
| 286 result_proxy_config = ProxyConfigDictionary::CreateAutoDetect(); | 306 result_proxy_config = ProxyConfigDictionary::CreateAutoDetect(); |
| 287 break; | 307 break; |
| 288 case ProxyPrefs::MODE_PAC_SCRIPT: { | 308 case ProxyPrefs::MODE_PAC_SCRIPT: { |
| 289 std::string url; | 309 std::string url; |
| 290 if (!pac_url.empty()) { | 310 if (!pac_url.empty()) { |
| 291 url = pac_url; | 311 url = pac_url; |
| 292 } else if (!pac_data.empty()) { | 312 } else if (!pac_data.empty()) { |
| 293 if (!CreateDataURLFromPACScript(pac_data, &url)) { | 313 if (!CreateDataURLFromPACScript(pac_data, &url)) { |
| 294 *error = "Internal error, at base64 encoding of 'pacScript.data'."; | 314 *error = "Internal error, at base64 encoding of 'pacScript.data'."; |
| 295 return NULL; | 315 return NULL; |
| 296 } | 316 } |
| 297 } else { | 317 } else { |
| 298 *error = "Proxy mode 'pac_script' requires a 'pacScript' field with " | 318 *error = "Proxy mode 'pac_script' requires a 'pacScript' field with " |
| 299 "either a 'url' field or a 'data' field."; | 319 "either a 'url' field or a 'data' field."; |
| 300 return NULL; | 320 return NULL; |
| 301 } | 321 } |
| 302 result_proxy_config = ProxyConfigDictionary::CreatePacScript(url); | 322 result_proxy_config = |
| 323 ProxyConfigDictionary::CreatePacScript(url, pac_mandatory); |
| 303 break; | 324 break; |
| 304 } | 325 } |
| 305 case ProxyPrefs::MODE_FIXED_SERVERS: { | 326 case ProxyPrefs::MODE_FIXED_SERVERS: { |
| 306 if (proxy_rules_string.empty()) { | 327 if (proxy_rules_string.empty()) { |
| 307 *error = "Proxy mode 'fixed_servers' requires a 'rules' field."; | 328 *error = "Proxy mode 'fixed_servers' requires a 'rules' field."; |
| 308 return NULL; | 329 return NULL; |
| 309 } | 330 } |
| 310 result_proxy_config = ProxyConfigDictionary::CreateFixedServers( | 331 result_proxy_config = ProxyConfigDictionary::CreateFixedServers( |
| 311 proxy_rules_string, bypass_list); | 332 proxy_rules_string, bypass_list); |
| 312 break; | 333 break; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 const ProxyConfigDictionary& proxy_config) { | 433 const ProxyConfigDictionary& proxy_config) { |
| 413 ProxyPrefs::ProxyMode mode; | 434 ProxyPrefs::ProxyMode mode; |
| 414 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_PAC_SCRIPT); | 435 CHECK(proxy_config.GetMode(&mode) && mode == ProxyPrefs::MODE_PAC_SCRIPT); |
| 415 | 436 |
| 416 scoped_ptr<DictionaryValue> pac_script_dict(new DictionaryValue); | 437 scoped_ptr<DictionaryValue> pac_script_dict(new DictionaryValue); |
| 417 std::string pac_url; | 438 std::string pac_url; |
| 418 if (!proxy_config.GetPacUrl(&pac_url)) { | 439 if (!proxy_config.GetPacUrl(&pac_url)) { |
| 419 LOG(ERROR) << "Invalid proxy configuration. Missing PAC URL."; | 440 LOG(ERROR) << "Invalid proxy configuration. Missing PAC URL."; |
| 420 return NULL; | 441 return NULL; |
| 421 } | 442 } |
| 443 bool pac_mandatory = false; |
| 444 if (!proxy_config.GetPacMandatory(&pac_mandatory)) { |
| 445 LOG(ERROR) << "Invalid proxy configuration. Missing PAC mandatory field."; |
| 446 return NULL; |
| 447 } |
| 422 | 448 |
| 423 if (pac_url.find("data") == 0) { | 449 if (pac_url.find("data") == 0) { |
| 424 std::string pac_data; | 450 std::string pac_data; |
| 425 if (!CreatePACScriptFromDataURL(pac_url, &pac_data)) { | 451 if (!CreatePACScriptFromDataURL(pac_url, &pac_data)) { |
| 426 LOG(ERROR) << "Cannot decode base64-encoded PAC data URL."; | 452 LOG(ERROR) << "Cannot decode base64-encoded PAC data URL."; |
| 427 return NULL; | 453 return NULL; |
| 428 } | 454 } |
| 429 pac_script_dict->SetString(keys::kProxyConfigPacScriptData, pac_data); | 455 pac_script_dict->SetString(keys::kProxyConfigPacScriptData, pac_data); |
| 430 } else { | 456 } else { |
| 431 pac_script_dict->SetString(keys::kProxyConfigPacScriptUrl, pac_url); | 457 pac_script_dict->SetString(keys::kProxyConfigPacScriptUrl, pac_url); |
| 432 } | 458 } |
| 459 pac_script_dict->SetBoolean(keys::kProxyConfigPacScriptMandatory, |
| 460 pac_mandatory); |
| 433 return pac_script_dict.release(); | 461 return pac_script_dict.release(); |
| 434 } | 462 } |
| 435 | 463 |
| 436 ListValue* TokenizeToStringList(const std::string& in, | 464 ListValue* TokenizeToStringList(const std::string& in, |
| 437 const std::string& delims) { | 465 const std::string& delims) { |
| 438 ListValue* out = new ListValue; | 466 ListValue* out = new ListValue; |
| 439 StringTokenizer entries(in, delims); | 467 StringTokenizer entries(in, delims); |
| 440 while (entries.GetNext()) | 468 while (entries.GetNext()) |
| 441 out->Append(Value::CreateStringValue(entries.token())); | 469 out->Append(Value::CreateStringValue(entries.token())); |
| 442 return out; | 470 return out; |
| 443 } | 471 } |
| 444 | 472 |
| 445 } // namespace extension_proxy_api_helpers | 473 } // namespace extension_proxy_api_helpers |
| OLD | NEW |