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