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" | |
9 #include "base/values.h" | 8 #include "base/values.h" |
10 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 9 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
11 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 #include "net/base/host_port_pair.h" | |
14 #include "net/proxy/proxy_server.h" | |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // The scheme for which to use a manually specified proxy, not of the proxy URI | 18 // The scheme for which to use a manually specified proxy, not of the proxy URI |
18 // itself. | 19 // itself. |
19 enum { | 20 enum { |
20 SCHEME_ALL = 0, | 21 SCHEME_ALL = 0, |
21 SCHEME_HTTP, | 22 SCHEME_HTTP, |
22 SCHEME_HTTPS, | 23 SCHEME_HTTPS, |
23 SCHEME_FTP, | 24 SCHEME_FTP, |
(...skipping 11 matching lines...) Expand all Loading... | |
35 | 36 |
36 // The names of the schemes to be used to build the preference value string | 37 // The names of the schemes to be used to build the preference value string |
37 // for manual proxy settings. These must be kept in sync with the SCHEME_* | 38 // for manual proxy settings. These must be kept in sync with the SCHEME_* |
38 // constants. | 39 // constants. |
39 const char* scheme_name[] = { "*error*", | 40 const char* scheme_name[] = { "*error*", |
40 "http", | 41 "http", |
41 "https", | 42 "https", |
42 "ftp", | 43 "ftp", |
43 "socks" }; | 44 "socks" }; |
44 | 45 |
46 // Note that there are subtle differences in the meaning of schemes here. | |
47 // SCHEME_HTTP in the enumeration on top above means "proxy server for all HTTP | |
48 // requests", whereas net::ProxyServer::SCHEME_HTTP means "proxy those | |
49 // through an HTTP proxy". | |
50 // Must be kept in sync with SCHEME_* constants. | |
51 net::ProxyServer::Scheme default_schemes[] = { net::ProxyServer::SCHEME_HTTP, | |
52 net::ProxyServer::SCHEME_HTTP, | |
53 net::ProxyServer::SCHEME_HTTPS, | |
eroman
2011/02/09 05:12:46
This should be SCHEME_HTTP.
SCHEME_HTTPS is some
battre
2011/02/09 10:04:38
Done.
| |
54 net::ProxyServer::SCHEME_HTTP, | |
55 net::ProxyServer::SCHEME_SOCKS5 | |
56 }; | |
57 | |
45 } // namespace | 58 } // namespace |
46 | 59 |
47 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); | 60 COMPILE_ASSERT(SCHEME_MAX == SCHEME_SOCKS, SCHEME_MAX_must_equal_SCHEME_SOCKS); |
48 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, | 61 COMPILE_ASSERT(arraysize(field_name) == SCHEME_MAX + 1, |
49 field_name_array_is_wrong_size); | 62 field_name_array_is_wrong_size); |
50 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, | 63 COMPILE_ASSERT(arraysize(scheme_name) == SCHEME_MAX + 1, |
51 scheme_name_array_is_wrong_size); | 64 scheme_name_array_is_wrong_size); |
65 COMPILE_ASSERT(arraysize(default_schemes) == SCHEME_MAX + 1, | |
eroman
2011/02/09 05:12:46
To be honest, I think it might be simpler to simpl
battre
2011/02/09 10:04:38
Done.
| |
66 default_schemes_array_is_wrong_size); | |
52 COMPILE_ASSERT(SCHEME_ALL == 0, singleProxy_must_be_first_option); | 67 COMPILE_ASSERT(SCHEME_ALL == 0, singleProxy_must_be_first_option); |
53 | 68 |
69 // Converts a proxy server description |dict| as passed by the API caller | |
70 // (e.g. for the http proxy in the rules element) and converts it to a | |
71 // ProxyServer. Returns true if successful. | |
72 static bool GetProxyServer( | |
73 const DictionaryValue* dict, | |
eroman
2011/02/09 05:12:46
nit: rather than making these two functions "stati
battre
2011/02/09 10:04:38
Done.
| |
74 net::ProxyServer::Scheme default_scheme, | |
75 net::ProxyServer* proxy_server) { | |
76 std::string scheme_string; // optional. | |
77 dict->GetString("scheme", &scheme_string); | |
78 | |
79 net::ProxyServer::Scheme scheme = | |
80 net::ProxyServer::GetSchemeFromURI(scheme_string); | |
81 if (scheme == net::ProxyServer::SCHEME_INVALID) | |
82 scheme = default_scheme; | |
83 | |
84 std::string host; | |
85 if (!dict->GetString("host", &host)) | |
86 return false; | |
87 | |
88 int port; // optional. | |
89 if (!dict->GetInteger("port", &port)) | |
90 port = net::ProxyServer::GetDefaultPortForScheme(scheme); | |
91 | |
92 *proxy_server = net::ProxyServer(scheme, net::HostPortPair(host, port)); | |
93 | |
94 return true; | |
95 } | |
96 | |
97 // Converts a proxy "rules" element passed by the API caller into a proxy | |
98 // configuration string that can be used by the proxy subsystem (see | |
99 // proxy_config.h). Returns true if successful. | |
100 static bool GetProxyRules( | |
101 DictionaryValue* proxy_rules, | |
eroman
2011/02/09 05:12:46
nit: can this fit all on one line?
battre
2011/02/09 10:04:38
Done.
| |
102 std::string* out) { | |
103 if (!proxy_rules) | |
104 return false; | |
105 | |
106 // Local data into which the parameters will be parsed. has_proxy describes | |
107 // whether a setting was found for the scheme; proxy_dict holds the | |
108 // DictionaryValues which in turn contain proxy server descriptions, and | |
109 // proxy_server holds ProxyServer structs containing those descriptions. | |
110 bool has_proxy[SCHEME_MAX + 1]; | |
111 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; | |
112 net::ProxyServer proxy_server[SCHEME_MAX + 1]; | |
113 | |
114 // Looking for all possible proxy types is inefficient if we have a | |
115 // singleProxy that will supersede per-URL proxies, but it's worth it to keep | |
116 // the code simple and extensible. | |
117 for (size_t i = 0; i <= SCHEME_MAX; ++i) { | |
118 has_proxy[i] = proxy_rules->GetDictionary(field_name[i], &proxy_dict[i]); | |
119 if (has_proxy[i]) { | |
120 if (!GetProxyServer(proxy_dict[i], default_schemes[i], &proxy_server[i])) | |
121 return false; | |
122 } | |
123 } | |
124 | |
125 // Handle case that only singleProxy is specified. | |
126 if (has_proxy[SCHEME_ALL]) { | |
127 for (size_t i = 1; i <= SCHEME_MAX; ++i) { | |
128 if (has_proxy[i]) { | |
129 LOG(ERROR) << "Proxy rule for " << field_name[SCHEME_ALL] << " and " | |
130 << field_name[i] << " cannot be set at the same time."; | |
131 return false; | |
132 } | |
133 } | |
134 *out = proxy_server[SCHEME_ALL].ToURI(); | |
135 return true; | |
136 } | |
137 | |
138 // Handle case the anything but singleProxy is specified. | |
eroman
2011/02/09 05:12:46
nit: typo in wording.
battre
2011/02/09 10:04:38
Done.
| |
139 | |
140 // Build the proxy preference string. | |
141 std::string proxy_pref; | |
142 for (size_t i = 1; i <= SCHEME_MAX; ++i) { | |
143 if (has_proxy[i]) { | |
144 // http=foopy:4010;ftp=socks://foopy2:80 | |
145 if (!proxy_pref.empty()) | |
146 proxy_pref.append(";"); | |
147 proxy_pref.append(scheme_name[i]); | |
148 proxy_pref.append("="); | |
149 proxy_pref.append(proxy_server[i].ToURI()); | |
150 } | |
151 } | |
152 | |
153 *out = proxy_pref; | |
154 return true; | |
155 } | |
156 | |
54 void ProxySettingsFunction::ApplyPreference(const char* pref_path, | 157 void ProxySettingsFunction::ApplyPreference(const char* pref_path, |
55 Value* pref_value, | 158 Value* pref_value, |
56 bool incognito) { | 159 bool incognito) { |
57 profile()->GetExtensionService()->extension_prefs()-> | 160 profile()->GetExtensionService()->extension_prefs()-> |
58 SetExtensionControlledPref(extension_id(), pref_path, incognito, | 161 SetExtensionControlledPref(extension_id(), pref_path, incognito, |
59 pref_value); | 162 pref_value); |
60 } | 163 } |
61 | 164 |
62 void ProxySettingsFunction::RemovePreference(const char* pref_path, | 165 void ProxySettingsFunction::RemovePreference(const char* pref_path, |
63 bool incognito) { | 166 bool incognito) { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 case ProxyPrefs::kModeCount: | 240 case ProxyPrefs::kModeCount: |
138 NOTREACHED(); | 241 NOTREACHED(); |
139 } | 242 } |
140 if (!result_proxy_config) | 243 if (!result_proxy_config) |
141 return false; | 244 return false; |
142 | 245 |
143 ApplyPreference(prefs::kProxy, result_proxy_config, incognito); | 246 ApplyPreference(prefs::kProxy, result_proxy_config, incognito); |
144 return true; | 247 return true; |
145 } | 248 } |
146 | 249 |
147 bool UseCustomProxySettingsFunction::GetProxyServer( | |
148 const DictionaryValue* dict, ProxyServer* proxy_server) { | |
149 dict->GetString("scheme", &proxy_server->scheme); | |
150 EXTENSION_FUNCTION_VALIDATE(dict->GetString("host", &proxy_server->host)); | |
151 dict->GetInteger("port", &proxy_server->port); | |
152 return true; | |
153 } | |
154 | |
155 bool UseCustomProxySettingsFunction::GetProxyRules( | |
156 DictionaryValue* proxy_rules, | |
157 std::string* out) { | |
158 if (!proxy_rules) | |
159 return false; | |
160 | |
161 // Local data into which the parameters will be parsed. has_proxy describes | |
162 // whether a setting was found for the scheme; proxy_dict holds the | |
163 // DictionaryValues which in turn contain proxy server descriptions, and | |
164 // proxy_server holds ProxyServer structs containing those descriptions. | |
165 bool has_proxy[SCHEME_MAX + 1]; | |
166 DictionaryValue* proxy_dict[SCHEME_MAX + 1]; | |
167 ProxyServer proxy_server[SCHEME_MAX + 1]; | |
168 | |
169 // Looking for all possible proxy types is inefficient if we have a | |
170 // singleProxy that will supersede per-URL proxies, but it's worth it to keep | |
171 // the code simple and extensible. | |
172 for (size_t i = 0; i <= SCHEME_MAX; ++i) { | |
173 has_proxy[i] = proxy_rules->GetDictionary(field_name[i], &proxy_dict[i]); | |
174 if (has_proxy[i]) { | |
175 if (!GetProxyServer(proxy_dict[i], &proxy_server[i])) | |
176 return false; | |
177 } | |
178 } | |
179 | |
180 // Handle case that only singleProxy is specified. | |
181 if (has_proxy[SCHEME_ALL]) { | |
182 for (size_t i = 1; i <= SCHEME_MAX; ++i) { | |
183 if (has_proxy[i]) { | |
184 LOG(ERROR) << "Proxy rule for " << field_name[SCHEME_ALL] << " and " | |
185 << field_name[i] << " cannot be set at the same time."; | |
186 return false; | |
187 } | |
188 } | |
189 if (!proxy_server[SCHEME_ALL].scheme.empty()) | |
190 LOG(WARNING) << "Ignoring scheme attribute from proxy server."; | |
191 // Build the proxy preference string. | |
192 std::string proxy_pref; | |
193 proxy_pref.append(proxy_server[SCHEME_ALL].host); | |
194 if (proxy_server[SCHEME_ALL].port != ProxyServer::INVALID_PORT) { | |
195 proxy_pref.append(":"); | |
196 proxy_pref.append(base::StringPrintf("%d", | |
197 proxy_server[SCHEME_ALL].port)); | |
198 } | |
199 *out = proxy_pref; | |
200 return true; | |
201 } | |
202 | |
203 // Handle case the anything but singleProxy is specified. | |
204 | |
205 // Build the proxy preference string. | |
206 std::string proxy_pref; | |
207 for (size_t i = 1; i <= SCHEME_MAX; ++i) { | |
208 if (has_proxy[i]) { | |
209 // http=foopy:4010;ftp=socks://foopy2:80 | |
210 if (!proxy_pref.empty()) | |
211 proxy_pref.append(";"); | |
212 proxy_pref.append(scheme_name[i]); | |
213 proxy_pref.append("="); | |
214 proxy_pref.append(proxy_server[i].scheme); | |
215 proxy_pref.append("://"); | |
216 proxy_pref.append(proxy_server[i].host); | |
217 if (proxy_server[i].port != ProxyServer::INVALID_PORT) { | |
218 proxy_pref.append(":"); | |
219 proxy_pref.append(base::StringPrintf("%d", proxy_server[i].port)); | |
220 } | |
221 } | |
222 } | |
223 | |
224 *out = proxy_pref; | |
225 return true; | |
226 } | |
227 | |
228 bool RemoveCustomProxySettingsFunction::RunImpl() { | 250 bool RemoveCustomProxySettingsFunction::RunImpl() { |
229 bool incognito = false; | 251 bool incognito = false; |
230 args_->GetBoolean(0, &incognito); | 252 if (HasOptionalArgument(0)) { |
253 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &incognito)); | |
254 } | |
231 | 255 |
232 RemovePreference(prefs::kProxy, incognito); | 256 RemovePreference(prefs::kProxy, incognito); |
233 return true; | 257 return true; |
234 } | 258 } |
OLD | NEW |