| 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/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/prefs/proxy_prefs.h" | 10 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // The scheme for which to use a manually specified proxy, not of the proxy URI | 17 // The scheme for which to use a manually specified proxy, not of the proxy URI |
| 18 // itself. | 18 // itself. |
| 19 enum { | 19 enum { |
| 20 SCHEME_ALL = 0, | 20 SCHEME_ALL = 0, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 DictionaryValue* proxy_config; | 68 DictionaryValue* proxy_config; |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &proxy_config)); | 69 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &proxy_config)); |
| 70 | 70 |
| 71 bool incognito = false; // Optional argument, defaults to false. | 71 bool incognito = false; // Optional argument, defaults to false. |
| 72 if (HasOptionalArgument(1)) { | 72 if (HasOptionalArgument(1)) { |
| 73 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &incognito)); | 73 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &incognito)); |
| 74 } | 74 } |
| 75 | 75 |
| 76 std::string proxy_mode; | 76 std::string proxy_mode; |
| 77 proxy_config->GetString("mode", &proxy_mode); | 77 proxy_config->GetString("mode", &proxy_mode); |
| 78 ProxyPrefs::ProxyMode mode_enum; |
| 79 if (!ProxyPrefs::StringToProxyMode(proxy_mode, &mode_enum)) { |
| 80 LOG(ERROR) << "Invalid mode for proxy settings: " << proxy_mode << ". " |
| 81 << "Setting custom proxy settings failed."; |
| 82 return false; |
| 83 } |
| 78 | 84 |
| 79 DictionaryValue* pac_dict = NULL; | 85 DictionaryValue* pac_dict = NULL; |
| 80 proxy_config->GetDictionary("pacScript", &pac_dict); | 86 proxy_config->GetDictionary("pacScript", &pac_dict); |
| 87 std::string pac_url; |
| 88 if (pac_dict && !pac_dict->GetString("url", &pac_url)) { |
| 89 LOG(ERROR) << "'pacScript' requires a 'url' field. " |
| 90 << "Setting custom proxy settings failed."; |
| 91 return false; |
| 92 } |
| 81 | 93 |
| 82 DictionaryValue* proxy_rules = NULL; | 94 DictionaryValue* proxy_rules = NULL; |
| 83 proxy_config->GetDictionary("rules", &proxy_rules); | 95 proxy_config->GetDictionary("rules", &proxy_rules); |
| 96 std::string proxy_rules_string; |
| 97 if (proxy_rules && !GetProxyRules(proxy_rules, &proxy_rules_string)) { |
| 98 LOG(ERROR) << "Invalid 'rules' specified. " |
| 99 << "Setting custom proxy settings failed."; |
| 100 return false; |
| 101 } |
| 84 | 102 |
| 85 // TODO(battre,gfeher): Make sure all the preferences get always | 103 // not supported, yet. |
| 86 // overwritten. | 104 std::string bypass_list; |
| 87 return ApplyMode(proxy_mode, incognito) && | 105 |
| 88 ApplyPacScript(pac_dict, incognito) && | 106 DictionaryValue* result_proxy_config = NULL; |
| 89 ApplyProxyRules(proxy_rules, incognito); | 107 switch (mode_enum) { |
| 108 case ProxyPrefs::MODE_DIRECT: |
| 109 result_proxy_config = ProxyConfigDictionary::CreateDirect(); |
| 110 break; |
| 111 case ProxyPrefs::MODE_AUTO_DETECT: |
| 112 result_proxy_config = ProxyConfigDictionary::CreateAutoDetect(); |
| 113 break; |
| 114 case ProxyPrefs::MODE_PAC_SCRIPT: { |
| 115 if (!pac_dict) { |
| 116 LOG(ERROR) << "Proxy mode 'pac_script' requires a 'pacScript' field. " |
| 117 << "Setting custom proxy settings failed."; |
| 118 return false; |
| 119 } |
| 120 result_proxy_config = ProxyConfigDictionary::CreatePacScript(pac_url); |
| 121 break; |
| 122 } |
| 123 case ProxyPrefs::MODE_FIXED_SERVERS: { |
| 124 if (!proxy_rules) { |
| 125 LOG(ERROR) << "Proxy mode 'fixed_servers' requires a 'rules' field. " |
| 126 << "Setting custom proxy settings failed."; |
| 127 return false; |
| 128 } |
| 129 result_proxy_config = ProxyConfigDictionary::CreateFixedServers( |
| 130 proxy_rules_string, bypass_list); |
| 131 break; |
| 132 } |
| 133 case ProxyPrefs::MODE_SYSTEM: |
| 134 result_proxy_config = ProxyConfigDictionary::CreateSystem(); |
| 135 break; |
| 136 case ProxyPrefs::kModeCount: |
| 137 NOTREACHED(); |
| 138 } |
| 139 if (!result_proxy_config) |
| 140 return false; |
| 141 |
| 142 ApplyPreference(prefs::kProxy, result_proxy_config, incognito); |
| 143 return true; |
| 90 } | 144 } |
| 91 | 145 |
| 92 bool UseCustomProxySettingsFunction::GetProxyServer( | 146 bool UseCustomProxySettingsFunction::GetProxyServer( |
| 93 const DictionaryValue* dict, ProxyServer* proxy_server) { | 147 const DictionaryValue* dict, ProxyServer* proxy_server) { |
| 94 dict->GetString("scheme", &proxy_server->scheme); | 148 dict->GetString("scheme", &proxy_server->scheme); |
| 95 EXTENSION_FUNCTION_VALIDATE(dict->GetString("host", &proxy_server->host)); | 149 EXTENSION_FUNCTION_VALIDATE(dict->GetString("host", &proxy_server->host)); |
| 96 dict->GetInteger("port", &proxy_server->port); | 150 dict->GetInteger("port", &proxy_server->port); |
| 97 return true; | 151 return true; |
| 98 } | 152 } |
| 99 | 153 |
| 100 bool UseCustomProxySettingsFunction::ApplyMode(const std::string& mode, | 154 bool UseCustomProxySettingsFunction::GetProxyRules( |
| 101 bool incognito) { | |
| 102 // We take control of the mode preference even if none was specified, so that | |
| 103 // all proxy preferences are controlled by the same extension (if not by a | |
| 104 // higher-priority source). | |
| 105 bool result = true; | |
| 106 ProxyPrefs::ProxyMode mode_enum; | |
| 107 if (!ProxyPrefs::StringToProxyMode(mode, &mode_enum)) { | |
| 108 mode_enum = ProxyPrefs::MODE_SYSTEM; | |
| 109 LOG(WARNING) << "Invalid mode for proxy settings: " << mode; | |
| 110 result = false; | |
| 111 } | |
| 112 ApplyPreference( | |
| 113 prefs::kProxyMode, Value::CreateIntegerValue(mode_enum), incognito); | |
| 114 return result; | |
| 115 } | |
| 116 | |
| 117 bool UseCustomProxySettingsFunction::ApplyPacScript(DictionaryValue* pac_dict, | |
| 118 bool incognito) { | |
| 119 std::string pac_url; | |
| 120 if (pac_dict) | |
| 121 pac_dict->GetString("url", &pac_url); | |
| 122 | |
| 123 // We take control of the PAC preference even if none was specified, so that | |
| 124 // all proxy preferences are controlled by the same extension (if not by a | |
| 125 // higher-priority source). | |
| 126 ApplyPreference( | |
| 127 prefs::kProxyPacUrl, Value::CreateStringValue(pac_url), incognito); | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 bool UseCustomProxySettingsFunction::ApplyProxyRules( | |
| 132 DictionaryValue* proxy_rules, | 155 DictionaryValue* proxy_rules, |
| 133 bool incognito) { | 156 std::string* out) { |
| 134 if (!proxy_rules) { | 157 if (!proxy_rules) |
| 135 ApplyPreference( | 158 return false; |
| 136 prefs::kProxyServer, Value::CreateStringValue(""), incognito); | |
| 137 return true; | |
| 138 } | |
| 139 | 159 |
| 140 // Local data into which the parameters will be parsed. has_proxy describes | 160 // Local data into which the parameters will be parsed. has_proxy describes |
| 141 // whether a setting was found for the scheme; proxy_dict holds the | 161 // whether a setting was found for the scheme; proxy_dict holds the |
| 142 // DictionaryValues which in turn contain proxy server descriptions, and | 162 // DictionaryValues which in turn contain proxy server descriptions, and |
| 143 // proxy_server holds ProxyServer structs containing those descriptions. | 163 // proxy_server holds ProxyServer structs containing those descriptions. |
| 144 bool has_proxy[SCHEME_MAX + 1]; | 164 bool has_proxy[SCHEME_MAX + 1]; |
| 145 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; | 165 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; |
| 146 ProxyServer proxy_server[SCHEME_MAX + 1]; | 166 ProxyServer proxy_server[SCHEME_MAX + 1]; |
| 147 | 167 |
| 148 // Looking for all possible proxy types is inefficient if we have a | 168 // Looking for all possible proxy types is inefficient if we have a |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 proxy_pref.append(proxy_server[i].scheme); | 202 proxy_pref.append(proxy_server[i].scheme); |
| 183 proxy_pref.append("://"); | 203 proxy_pref.append("://"); |
| 184 proxy_pref.append(proxy_server[i].host); | 204 proxy_pref.append(proxy_server[i].host); |
| 185 if (proxy_server[i].port != ProxyServer::INVALID_PORT) { | 205 if (proxy_server[i].port != ProxyServer::INVALID_PORT) { |
| 186 proxy_pref.append(":"); | 206 proxy_pref.append(":"); |
| 187 proxy_pref.append(base::StringPrintf("%d", proxy_server[i].port)); | 207 proxy_pref.append(base::StringPrintf("%d", proxy_server[i].port)); |
| 188 } | 208 } |
| 189 } | 209 } |
| 190 } | 210 } |
| 191 | 211 |
| 192 ApplyPreference( | 212 *out = proxy_pref; |
| 193 prefs::kProxyServer, Value::CreateStringValue(proxy_pref), incognito); | |
| 194 return true; | 213 return true; |
| 195 } | 214 } |
| 196 | 215 |
| 197 bool RemoveCustomProxySettingsFunction::RunImpl() { | 216 bool RemoveCustomProxySettingsFunction::RunImpl() { |
| 198 bool incognito = false; | 217 bool incognito = false; |
| 199 args_->GetBoolean(0, &incognito); | 218 args_->GetBoolean(0, &incognito); |
| 200 | 219 |
| 201 RemovePreference(prefs::kProxyMode, incognito); | 220 RemovePreference(prefs::kProxy, incognito); |
| 202 RemovePreference(prefs::kProxyPacUrl, incognito); | |
| 203 RemovePreference(prefs::kProxyServer, incognito); | |
| 204 return true; | 221 return true; |
| 205 } | 222 } |
| OLD | NEW |