| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // 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 |
| 17 // itself. | 18 // itself. |
| 18 enum { | 19 enum { |
| 19 SCHEME_ALL = 0, | 20 SCHEME_ALL = 0, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 46 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); | 47 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); |
| 47 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, | 48 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, |
| 48 field_name_array_is_wrong_size); | 49 field_name_array_is_wrong_size); |
| 49 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, | 50 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, |
| 50 scheme_name_array_is_wrong_size); | 51 scheme_name_array_is_wrong_size); |
| 51 | 52 |
| 52 bool UseCustomProxySettingsFunction::RunImpl() { | 53 bool UseCustomProxySettingsFunction::RunImpl() { |
| 53 DictionaryValue* proxy_config; | 54 DictionaryValue* proxy_config; |
| 54 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &proxy_config)); | 55 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &proxy_config)); |
| 55 | 56 |
| 56 bool auto_detect = false; | 57 std::string proxy_mode; |
| 57 proxy_config->GetBoolean("autoDetect", &auto_detect); | 58 proxy_config->GetString("mode", &proxy_mode); |
| 58 | 59 |
| 59 DictionaryValue* pac_dict = NULL; | 60 DictionaryValue* pac_dict = NULL; |
| 60 proxy_config->GetDictionary("pacScript", &pac_dict); | 61 proxy_config->GetDictionary("pacScript", &pac_dict); |
| 61 | 62 |
| 62 DictionaryValue* proxy_rules = NULL; | 63 DictionaryValue* proxy_rules = NULL; |
| 63 proxy_config->GetDictionary("rules", &proxy_rules); | 64 proxy_config->GetDictionary("rules", &proxy_rules); |
| 64 | 65 |
| 65 return ApplyAutoDetect(auto_detect) && | 66 // TODO(battre,gfeher): Make sure all the preferences get always |
| 67 // overwritten. |
| 68 return ApplyMode(proxy_mode) && |
| 66 ApplyPacScript(pac_dict) && | 69 ApplyPacScript(pac_dict) && |
| 67 ApplyProxyRules(proxy_rules); | 70 ApplyProxyRules(proxy_rules); |
| 68 } | 71 } |
| 69 | 72 |
| 70 bool UseCustomProxySettingsFunction::GetProxyServer( | 73 bool UseCustomProxySettingsFunction::GetProxyServer( |
| 71 const DictionaryValue* dict, ProxyServer* proxy_server) { | 74 const DictionaryValue* dict, ProxyServer* proxy_server) { |
| 72 dict->GetString("scheme", &proxy_server->scheme); | 75 dict->GetString("scheme", &proxy_server->scheme); |
| 73 EXTENSION_FUNCTION_VALIDATE(dict->GetString("host", &proxy_server->host)); | 76 EXTENSION_FUNCTION_VALIDATE(dict->GetString("host", &proxy_server->host)); |
| 74 dict->GetInteger("port", &proxy_server->port); | 77 dict->GetInteger("port", &proxy_server->port); |
| 75 return true; | 78 return true; |
| 76 } | 79 } |
| 77 | 80 |
| 78 bool UseCustomProxySettingsFunction::ApplyAutoDetect(bool auto_detect) { | 81 bool UseCustomProxySettingsFunction::ApplyMode(const std::string& mode) { |
| 79 // We take control of the auto-detect preference even if none was specified, | 82 // We take control of the mode preference even if none was specified, so that |
| 80 // so that all proxy preferences are controlled by the same extension (if not | 83 // all proxy preferences are controlled by the same extension (if not by a |
| 81 // by a higher-priority source). | 84 // higher-priority source). |
| 82 SendNotification(prefs::kProxyAutoDetect, | 85 bool result = true; |
| 83 Value::CreateBooleanValue(auto_detect)); | 86 ProxyPrefs::ProxyMode mode_enum; |
| 84 return true; | 87 if (!ProxyPrefs::StringToProxyMode(mode, &mode_enum)) { |
| 88 mode_enum = ProxyPrefs::MODE_SYSTEM; |
| 89 LOG(WARNING) << "Invalid mode for proxy settings: " << mode; |
| 90 result = false; |
| 91 } |
| 92 ApplyPreference(prefs::kProxyMode, Value::CreateIntegerValue(mode_enum)); |
| 93 return result; |
| 85 } | 94 } |
| 86 | 95 |
| 87 bool UseCustomProxySettingsFunction::ApplyPacScript(DictionaryValue* pac_dict) { | 96 bool UseCustomProxySettingsFunction::ApplyPacScript(DictionaryValue* pac_dict) { |
| 88 std::string pac_url; | 97 std::string pac_url; |
| 89 if (pac_dict) | 98 if (pac_dict) |
| 90 pac_dict->GetString("url", &pac_url); | 99 pac_dict->GetString("url", &pac_url); |
| 91 | 100 |
| 92 // We take control of the PAC preference even if none was specified, so that | 101 // We take control of the PAC preference even if none was specified, so that |
| 93 // all proxy preferences are controlled by the same extension (if not by a | 102 // all proxy preferences are controlled by the same extension (if not by a |
| 94 // higher-priority source). | 103 // higher-priority source). |
| 95 SendNotification(prefs::kProxyPacUrl, Value::CreateStringValue(pac_url)); | 104 ApplyPreference(prefs::kProxyPacUrl, Value::CreateStringValue(pac_url)); |
| 96 return true; | 105 return true; |
| 97 } | 106 } |
| 98 | 107 |
| 99 bool UseCustomProxySettingsFunction::ApplyProxyRules( | 108 bool UseCustomProxySettingsFunction::ApplyProxyRules( |
| 100 DictionaryValue* proxy_rules) { | 109 DictionaryValue* proxy_rules) { |
| 101 if (!proxy_rules) | 110 if (!proxy_rules) { |
| 111 ApplyPreference(prefs::kProxyServer, Value::CreateStringValue("")); |
| 102 return true; | 112 return true; |
| 113 } |
| 103 | 114 |
| 104 // Local data into which the parameters will be parsed. has_proxy describes | 115 // Local data into which the parameters will be parsed. has_proxy describes |
| 105 // whether a setting was found for the scheme; proxy_dict holds the | 116 // whether a setting was found for the scheme; proxy_dict holds the |
| 106 // DictionaryValues which in turn contain proxy server descriptions, and | 117 // DictionaryValues which in turn contain proxy server descriptions, and |
| 107 // proxy_server holds ProxyServer structs containing those descriptions. | 118 // proxy_server holds ProxyServer structs containing those descriptions. |
| 108 bool has_proxy[SCHEME_MAX + 1]; | 119 bool has_proxy[SCHEME_MAX + 1]; |
| 109 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; | 120 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; |
| 110 ProxyServer proxy_server[SCHEME_MAX + 1]; | 121 ProxyServer proxy_server[SCHEME_MAX + 1]; |
| 111 | 122 |
| 112 // Looking for all possible proxy types is inefficient if we have a | 123 // 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... |
| 146 proxy_pref.append(proxy_server[i].scheme); | 157 proxy_pref.append(proxy_server[i].scheme); |
| 147 proxy_pref.append("://"); | 158 proxy_pref.append("://"); |
| 148 proxy_pref.append(proxy_server[i].host); | 159 proxy_pref.append(proxy_server[i].host); |
| 149 if (proxy_server[i].port != ProxyServer::INVALID_PORT) { | 160 if (proxy_server[i].port != ProxyServer::INVALID_PORT) { |
| 150 proxy_pref.append(":"); | 161 proxy_pref.append(":"); |
| 151 proxy_pref.append(base::StringPrintf("%d", proxy_server[i].port)); | 162 proxy_pref.append(base::StringPrintf("%d", proxy_server[i].port)); |
| 152 } | 163 } |
| 153 } | 164 } |
| 154 } | 165 } |
| 155 | 166 |
| 156 SendNotification(prefs::kProxyServer, Value::CreateStringValue(proxy_pref)); | 167 ApplyPreference(prefs::kProxyServer, Value::CreateStringValue(proxy_pref)); |
| 157 return true; | 168 return true; |
| 158 } | 169 } |
| 159 | 170 |
| 160 void UseCustomProxySettingsFunction::SendNotification(const char* pref_path, | 171 void UseCustomProxySettingsFunction::ApplyPreference(const char* pref_path, |
| 161 Value* pref_value) { | 172 Value* pref_value) { |
| 162 profile()->GetExtensionService()->extension_prefs() | 173 profile()->GetExtensionService()->extension_prefs() |
| 163 ->SetExtensionControlledPref(extension_id(), pref_path, pref_value); | 174 ->SetExtensionControlledPref(extension_id(), pref_path, pref_value); |
| 164 } | 175 } |
| OLD | NEW |