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