| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 *bad_message = true; | 73 *bad_message = true; |
| 74 return false; | 74 return false; |
| 75 } | 75 } |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool GetPacMandatoryFromExtensionPref(const DictionaryValue* proxy_config, | 79 bool GetPacMandatoryFromExtensionPref(const DictionaryValue* proxy_config, |
| 80 bool* out, | 80 bool* out, |
| 81 std::string* error, | 81 std::string* error, |
| 82 bool* bad_message){ | 82 bool* bad_message){ |
| 83 DictionaryValue* pac_dict = NULL; | 83 const DictionaryValue* pac_dict = NULL; |
| 84 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); | 84 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); |
| 85 if (!pac_dict) | 85 if (!pac_dict) |
| 86 return true; | 86 return true; |
| 87 | 87 |
| 88 bool mandatory_pac = false; | 88 bool mandatory_pac = false; |
| 89 if (pac_dict->HasKey(keys::kProxyConfigPacScriptMandatory) && | 89 if (pac_dict->HasKey(keys::kProxyConfigPacScriptMandatory) && |
| 90 !pac_dict->GetBoolean(keys::kProxyConfigPacScriptMandatory, | 90 !pac_dict->GetBoolean(keys::kProxyConfigPacScriptMandatory, |
| 91 &mandatory_pac)) { | 91 &mandatory_pac)) { |
| 92 LOG(ERROR) << "'pacScript.mandatory' could not be parsed."; | 92 LOG(ERROR) << "'pacScript.mandatory' could not be parsed."; |
| 93 *bad_message = true; | 93 *bad_message = true; |
| 94 return false; | 94 return false; |
| 95 } | 95 } |
| 96 *out = mandatory_pac; | 96 *out = mandatory_pac; |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool GetPacUrlFromExtensionPref(const DictionaryValue* proxy_config, | 100 bool GetPacUrlFromExtensionPref(const DictionaryValue* proxy_config, |
| 101 std::string* out, | 101 std::string* out, |
| 102 std::string* error, | 102 std::string* error, |
| 103 bool* bad_message) { | 103 bool* bad_message) { |
| 104 DictionaryValue* pac_dict = NULL; | 104 const DictionaryValue* pac_dict = NULL; |
| 105 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); | 105 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); |
| 106 if (!pac_dict) | 106 if (!pac_dict) |
| 107 return true; | 107 return true; |
| 108 | 108 |
| 109 // TODO(battre): Handle UTF-8 URLs (http://crbug.com/72692). | 109 // TODO(battre): Handle UTF-8 URLs (http://crbug.com/72692). |
| 110 string16 pac_url16; | 110 string16 pac_url16; |
| 111 if (pac_dict->HasKey(keys::kProxyConfigPacScriptUrl) && | 111 if (pac_dict->HasKey(keys::kProxyConfigPacScriptUrl) && |
| 112 !pac_dict->GetString(keys::kProxyConfigPacScriptUrl, &pac_url16)) { | 112 !pac_dict->GetString(keys::kProxyConfigPacScriptUrl, &pac_url16)) { |
| 113 LOG(ERROR) << "'pacScript.url' could not be parsed."; | 113 LOG(ERROR) << "'pacScript.url' could not be parsed."; |
| 114 *bad_message = true; | 114 *bad_message = true; |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 if (!IsStringASCII(pac_url16)) { | 117 if (!IsStringASCII(pac_url16)) { |
| 118 *error = "'pacScript.url' supports only ASCII URLs " | 118 *error = "'pacScript.url' supports only ASCII URLs " |
| 119 "(encode URLs in Punycode format)."; | 119 "(encode URLs in Punycode format)."; |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 *out = UTF16ToASCII(pac_url16); | 122 *out = UTF16ToASCII(pac_url16); |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool GetPacDataFromExtensionPref(const DictionaryValue* proxy_config, | 126 bool GetPacDataFromExtensionPref(const DictionaryValue* proxy_config, |
| 127 std::string* out, | 127 std::string* out, |
| 128 std::string* error, | 128 std::string* error, |
| 129 bool* bad_message) { | 129 bool* bad_message) { |
| 130 DictionaryValue* pac_dict = NULL; | 130 const DictionaryValue* pac_dict = NULL; |
| 131 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); | 131 proxy_config->GetDictionary(keys::kProxyConfigPacScript, &pac_dict); |
| 132 if (!pac_dict) | 132 if (!pac_dict) |
| 133 return true; | 133 return true; |
| 134 | 134 |
| 135 string16 pac_data16; | 135 string16 pac_data16; |
| 136 if (pac_dict->HasKey(keys::kProxyConfigPacScriptData) && | 136 if (pac_dict->HasKey(keys::kProxyConfigPacScriptData) && |
| 137 !pac_dict->GetString(keys::kProxyConfigPacScriptData, &pac_data16)) { | 137 !pac_dict->GetString(keys::kProxyConfigPacScriptData, &pac_data16)) { |
| 138 LOG(ERROR) << "'pacScript.data' could not be parsed."; | 138 LOG(ERROR) << "'pacScript.data' could not be parsed."; |
| 139 *bad_message = true; | 139 *bad_message = true; |
| 140 return false; | 140 return false; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 186 |
| 187 *out = net::ProxyServer(scheme, net::HostPortPair(host, port)); | 187 *out = net::ProxyServer(scheme, net::HostPortPair(host, port)); |
| 188 | 188 |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 | 191 |
| 192 bool GetProxyRulesStringFromExtensionPref(const DictionaryValue* proxy_config, | 192 bool GetProxyRulesStringFromExtensionPref(const DictionaryValue* proxy_config, |
| 193 std::string* out, | 193 std::string* out, |
| 194 std::string* error, | 194 std::string* error, |
| 195 bool* bad_message) { | 195 bool* bad_message) { |
| 196 DictionaryValue* proxy_rules = NULL; | 196 const DictionaryValue* proxy_rules = NULL; |
| 197 proxy_config->GetDictionary(keys::kProxyConfigRules, &proxy_rules); | 197 proxy_config->GetDictionary(keys::kProxyConfigRules, &proxy_rules); |
| 198 if (!proxy_rules) | 198 if (!proxy_rules) |
| 199 return true; | 199 return true; |
| 200 | 200 |
| 201 // Local data into which the parameters will be parsed. has_proxy describes | 201 // Local data into which the parameters will be parsed. has_proxy describes |
| 202 // whether a setting was found for the scheme; proxy_server holds the | 202 // whether a setting was found for the scheme; proxy_server holds the |
| 203 // respective ProxyServer objects containing those descriptions. | 203 // respective ProxyServer objects containing those descriptions. |
| 204 bool has_proxy[keys::SCHEME_MAX + 1]; | 204 bool has_proxy[keys::SCHEME_MAX + 1]; |
| 205 net::ProxyServer proxy_server[keys::SCHEME_MAX + 1]; | 205 net::ProxyServer proxy_server[keys::SCHEME_MAX + 1]; |
| 206 | 206 |
| 207 // Looking for all possible proxy types is inefficient if we have a | 207 // Looking for all possible proxy types is inefficient if we have a |
| 208 // singleProxy that will supersede per-URL proxies, but it's worth it to keep | 208 // singleProxy that will supersede per-URL proxies, but it's worth it to keep |
| 209 // the code simple and extensible. | 209 // the code simple and extensible. |
| 210 for (size_t i = 0; i <= keys::SCHEME_MAX; ++i) { | 210 for (size_t i = 0; i <= keys::SCHEME_MAX; ++i) { |
| 211 DictionaryValue* proxy_dict = NULL; | 211 const DictionaryValue* proxy_dict = NULL; |
| 212 has_proxy[i] = proxy_rules->GetDictionary(keys::field_name[i], | 212 has_proxy[i] = proxy_rules->GetDictionary(keys::field_name[i], |
| 213 &proxy_dict); | 213 &proxy_dict); |
| 214 if (has_proxy[i]) { | 214 if (has_proxy[i]) { |
| 215 net::ProxyServer::Scheme default_scheme = net::ProxyServer::SCHEME_HTTP; | 215 net::ProxyServer::Scheme default_scheme = net::ProxyServer::SCHEME_HTTP; |
| 216 if (!GetProxyServer(proxy_dict, default_scheme, | 216 if (!GetProxyServer(proxy_dict, default_scheme, |
| 217 &proxy_server[i], error, bad_message)) { | 217 &proxy_server[i], error, bad_message)) { |
| 218 // Don't set |error| here, as GetProxyServer takes care of that. | 218 // Don't set |error| here, as GetProxyServer takes care of that. |
| 219 return false; | 219 return false; |
| 220 } | 220 } |
| 221 } | 221 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 249 proxy_pref.append(keys::scheme_name[i]); | 249 proxy_pref.append(keys::scheme_name[i]); |
| 250 proxy_pref.append("="); | 250 proxy_pref.append("="); |
| 251 proxy_pref.append(proxy_server[i].ToURI()); | 251 proxy_pref.append(proxy_server[i].ToURI()); |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 | 254 |
| 255 *out = proxy_pref; | 255 *out = proxy_pref; |
| 256 return true; | 256 return true; |
| 257 } | 257 } |
| 258 | 258 |
| 259 bool JoinUrlList(ListValue* list, | 259 bool JoinUrlList(const ListValue* list, |
| 260 const std::string& joiner, | 260 const std::string& joiner, |
| 261 std::string* out, | 261 std::string* out, |
| 262 std::string* error, | 262 std::string* error, |
| 263 bool* bad_message) { | 263 bool* bad_message) { |
| 264 std::string result; | 264 std::string result; |
| 265 for (size_t i = 0; i < list->GetSize(); ++i) { | 265 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 266 if (!result.empty()) | 266 if (!result.empty()) |
| 267 result.append(joiner); | 267 result.append(joiner); |
| 268 | 268 |
| 269 // TODO(battre): handle UTF-8 (http://crbug.com/72692). | 269 // TODO(battre): handle UTF-8 (http://crbug.com/72692). |
| (...skipping 11 matching lines...) Expand all Loading... |
| 281 result.append(UTF16ToASCII(entry)); | 281 result.append(UTF16ToASCII(entry)); |
| 282 } | 282 } |
| 283 *out = result; | 283 *out = result; |
| 284 return true; | 284 return true; |
| 285 } | 285 } |
| 286 | 286 |
| 287 bool GetBypassListFromExtensionPref(const DictionaryValue* proxy_config, | 287 bool GetBypassListFromExtensionPref(const DictionaryValue* proxy_config, |
| 288 std::string *out, | 288 std::string *out, |
| 289 std::string* error, | 289 std::string* error, |
| 290 bool* bad_message) { | 290 bool* bad_message) { |
| 291 DictionaryValue* proxy_rules = NULL; | 291 const DictionaryValue* proxy_rules = NULL; |
| 292 proxy_config->GetDictionary(keys::kProxyConfigRules, &proxy_rules); | 292 proxy_config->GetDictionary(keys::kProxyConfigRules, &proxy_rules); |
| 293 if (!proxy_rules) | 293 if (!proxy_rules) |
| 294 return true; | 294 return true; |
| 295 | 295 |
| 296 if (!proxy_rules->HasKey(keys::kProxyConfigBypassList)) { | 296 if (!proxy_rules->HasKey(keys::kProxyConfigBypassList)) { |
| 297 *out = ""; | 297 *out = ""; |
| 298 return true; | 298 return true; |
| 299 } | 299 } |
| 300 ListValue* bypass_list = NULL; | 300 const ListValue* bypass_list = NULL; |
| 301 if (!proxy_rules->GetList(keys::kProxyConfigBypassList, &bypass_list)) { | 301 if (!proxy_rules->GetList(keys::kProxyConfigBypassList, &bypass_list)) { |
| 302 LOG(ERROR) << "'rules.bypassList' could not be parsed."; | 302 LOG(ERROR) << "'rules.bypassList' could not be parsed."; |
| 303 *bad_message = true; | 303 *bad_message = true; |
| 304 return false; | 304 return false; |
| 305 } | 305 } |
| 306 | 306 |
| 307 return JoinUrlList(bypass_list, ",", out, error, bad_message); | 307 return JoinUrlList(bypass_list, ",", out, error, bad_message); |
| 308 } | 308 } |
| 309 | 309 |
| 310 DictionaryValue* CreateProxyConfigDict(ProxyPrefs::ProxyMode mode_enum, | 310 DictionaryValue* CreateProxyConfigDict(ProxyPrefs::ProxyMode mode_enum, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 const std::string& delims) { | 482 const std::string& delims) { |
| 483 ListValue* out = new ListValue; | 483 ListValue* out = new ListValue; |
| 484 StringTokenizer entries(in, delims); | 484 StringTokenizer entries(in, delims); |
| 485 while (entries.GetNext()) | 485 while (entries.GetNext()) |
| 486 out->Append(Value::CreateStringValue(entries.token())); | 486 out->Append(Value::CreateStringValue(entries.token())); |
| 487 return out; | 487 return out; |
| 488 } | 488 } |
| 489 | 489 |
| 490 } // namespace proxy_api_helpers | 490 } // namespace proxy_api_helpers |
| 491 } // namespace extensions | 491 } // namespace extensions |
| OLD | NEW |