Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: net/proxy/proxy_config_service_android.cc

Issue 10206014: Upstream Android proxy config service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comment Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/proxy/proxy_config_service_android.h"
6
7 #include <sys/system_properties.h>
8
9 #include "base/android/jni_string.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/observer_list.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/string_tokenizer.h"
19 #include "base/string_util.h"
20 #include "googleurl/src/url_parse.h"
21 #include "jni/proxy_change_listener_jni.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/proxy/proxy_config.h"
24
25 using base::android::AttachCurrentThread;
26 using base::android::ConvertUTF8ToJavaString;
27 using base::android::ConvertJavaStringToUTF8;
28 using base::android::CheckException;
29 using base::android::ClearException;
30 using base::android::GetMethodID;
31 using base::android::ScopedJavaGlobalRef;
32
33 namespace net {
34
35 namespace {
36
37 typedef ProxyConfigServiceAndroid::GetPropertyCallback GetPropertyCallback;
38
39 // Returns whether the provided string was successfully converted to a port.
40 bool ConvertStringToPort(const std::string& port, int* output) {
41 url_parse::Component component(0, port.size());
42 int result = url_parse::ParsePort(port.c_str(), component);
43 if (result == url_parse::PORT_INVALID ||
44 result == url_parse::PORT_UNSPECIFIED)
45 return false;
46 *output = result;
47 return true;
48 }
49
50 ProxyServer ConstructProxyServer(ProxyServer::Scheme scheme,
51 const std::string& proxy_host,
52 const std::string& proxy_port) {
53 DCHECK(!proxy_host.empty());
54 int port_as_int = 0;
55 if (proxy_port.empty())
56 port_as_int = ProxyServer::GetDefaultPortForScheme(scheme);
57 else if (!ConvertStringToPort(proxy_port, &port_as_int))
58 return ProxyServer();
59 DCHECK(port_as_int > 0);
60 return ProxyServer(
61 scheme,
62 HostPortPair(proxy_host, static_cast<uint16>(port_as_int)));
63 }
64
65 ProxyServer LookupProxy(const std::string& prefix,
66 const GetPropertyCallback& get_property,
67 ProxyServer::Scheme scheme) {
68 DCHECK(!prefix.empty());
69 std::string proxy_host = get_property.Run(prefix + ".proxyHost");
70 if (!proxy_host.empty()) {
71 std::string proxy_port = get_property.Run(prefix + ".proxyPort");
72 return ConstructProxyServer(scheme, proxy_host, proxy_port);
73 }
74 // Fall back to default proxy, if any.
75 proxy_host = get_property.Run("proxyHost");
76 if (!proxy_host.empty()) {
77 std::string proxy_port = get_property.Run("proxyPort");
78 return ConstructProxyServer(scheme, proxy_host, proxy_port);
79 }
80 return ProxyServer();
81 }
82
83 ProxyServer LookupSocksProxy(const GetPropertyCallback& get_property) {
84 std::string proxy_host = get_property.Run("socksProxyHost");
85 if (!proxy_host.empty()) {
86 std::string proxy_port = get_property.Run("socksProxyPort");
87 return ConstructProxyServer(ProxyServer::SCHEME_SOCKS5, proxy_host,
88 proxy_port);
89 }
90 return ProxyServer();
91 }
92
93 void AddBypassRules(const std::string& scheme,
94 const GetPropertyCallback& get_property,
95 ProxyBypassRules* bypass_rules) {
96 // The format of a hostname pattern is a list of hostnames that are separated
97 // by | and that use * as a wildcard. For example, setting the
98 // http.nonProxyHosts property to *.android.com|*.kernel.org will cause
99 // requests to http://developer.android.com to be made without a proxy.
100 std::string non_proxy_hosts =
101 get_property.Run(scheme + ".nonProxyHosts");
102 if (non_proxy_hosts.empty())
103 return;
104 StringTokenizer tokenizer(non_proxy_hosts, "|");
105 while (tokenizer.GetNext()) {
106 std::string token = tokenizer.token();
107 std::string pattern;
108 TrimWhitespaceASCII(token, TRIM_ALL, &pattern);
109 if (pattern.empty())
110 continue;
111 // '?' is not one of the specified pattern characters above.
112 DCHECK_EQ(std::string::npos, pattern.find('?'));
113 bypass_rules->AddRuleForHostname(scheme, pattern, -1);
114 }
115 }
116
117 // Returns true if a valid proxy was found.
118 bool GetProxyRules(const GetPropertyCallback& get_property,
119 ProxyConfig::ProxyRules* rules) {
120 // See libcore/luni/src/main/java/java/net/ProxySelectorImpl.java for the
121 // equivalent Android implementation.
122 rules->type = ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
123 rules->proxy_for_http = LookupProxy("http", get_property,
124 ProxyServer::SCHEME_HTTP);
125 rules->proxy_for_https = LookupProxy("https", get_property,
126 ProxyServer::SCHEME_HTTPS);
127 rules->proxy_for_ftp = LookupProxy("ftp", get_property,
128 ProxyServer::SCHEME_HTTP);
129 rules->fallback_proxy = LookupSocksProxy(get_property);
130 rules->bypass_rules.Clear();
131 AddBypassRules("ftp", get_property, &rules->bypass_rules);
132 AddBypassRules("http", get_property, &rules->bypass_rules);
133 AddBypassRules("https", get_property, &rules->bypass_rules);
134 return rules->proxy_for_http.is_valid() ||
135 rules->proxy_for_https.is_valid() ||
136 rules->proxy_for_ftp.is_valid() ||
137 rules->fallback_proxy.is_valid();
138 };
139
140 void GetLatestProxyConfigInternal(const GetPropertyCallback& get_property,
141 ProxyConfig* config) {
142 if (!GetProxyRules(get_property, &config->proxy_rules()))
143 *config = ProxyConfig::CreateDirect();
144 }
145
146 std::string GetJavaProperty(const std::string& property) {
147 // Use Java System.getProperty to get configuration information.
148 // TODO(pliard): Conversion to/from UTF8 ok here?
149 JNIEnv* env = AttachCurrentThread();
150 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property);
151 ScopedJavaLocalRef<jstring> result =
152 Java_ProxyChangeListener_getProperty(env, str.obj());
153 return result.is_null() ?
154 std::string() : ConvertJavaStringToUTF8(env, result.obj());
155 }
156
157 } // namespace
158
159 class ProxyConfigServiceAndroid::Delegate
160 : public base::RefCountedThreadSafe<Delegate> {
161 public:
162 Delegate(base::SingleThreadTaskRunner* network_task_runner,
163 base::SingleThreadTaskRunner* jni_task_runner,
164 const GetPropertyCallback& get_property_callback)
165 : network_task_runner_(network_task_runner),
166 jni_task_runner_(jni_task_runner),
167 get_property_callback_(get_property_callback) {}
168
169 void SetupJNI(const ProxyConfigServiceAndroid* service) {
170 if (OnJNIThread())
171 SetupOnJNIThread(service);
172 else
173 jni_task_runner_->PostTask(
174 FROM_HERE,
175 base::Bind(&Delegate::SetupOnJNIThread, this, service));
Ryan Sleevi 2012/05/25 17:42:14 nit: uses curly braces around these ifs because th
Philippe 2012/05/29 10:05:30 Done.
176 }
177
178 void FetchInitialConfig() {
179 if (OnJNIThread())
180 FetchInitialConfigOnJNIThread();
181 else
182 jni_task_runner_->PostTask(
183 FROM_HERE,
184 base::Bind(&Delegate::FetchInitialConfigOnJNIThread, this));
185 }
186
187 // Called only on the network thread.
188 void AddObserver(Observer* observer) {
189 DCHECK(OnNetworkThread());
190 observers_.AddObserver(observer);
191 }
192
193 void RemoveObserver(Observer* observer) {
194 DCHECK(OnNetworkThread());
195 observers_.RemoveObserver(observer);
196 }
197
198 ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) {
199 DCHECK(OnNetworkThread());
200 if (!config)
201 return ProxyConfigService::CONFIG_UNSET;
202 *config = proxy_config_;
203 return ProxyConfigService::CONFIG_VALID;
204 }
205
206 // Called on the JNI thread.
207 void ProxySettingsChanged() {
208 DCHECK(OnJNIThread());
209 ProxyConfig proxy_config;
210 GetLatestProxyConfigInternal(get_property_callback_, &proxy_config);
211 network_task_runner_->PostTask(
212 FROM_HERE,
213 base::Bind(
214 &Delegate::SetNewConfigAndNotifyObservers, this, proxy_config));
215 }
216
217 private:
218 friend class base::RefCountedThreadSafe<Delegate>;
219
220 virtual ~Delegate() {
221 Shutdown();
222 }
223
224 void SetupOnJNIThread(const ProxyConfigServiceAndroid* service) {
225 JNIEnv* env = AttachCurrentThread();
226 if (java_proxy_change_listener_.is_null()) {
227 java_proxy_change_listener_.Reset(
228 Java_ProxyChangeListener_create(
229 env, base::android::GetApplicationContext()));
230 CHECK(!java_proxy_change_listener_.is_null());
231 }
232 Java_ProxyChangeListener_start(
233 env,
234 java_proxy_change_listener_.obj(),
235 reinterpret_cast<jint>(service));
236 }
237
238 void FetchInitialConfigOnJNIThread() {
239 DCHECK(OnJNIThread());
240 ProxyConfig proxy_config;
241 GetLatestProxyConfigInternal(get_property_callback_, &proxy_config);
242 SetNewConfigAndNotifyObservers(proxy_config);
243 }
244
245 static void ShutdownOnJNIThread(
246 const ScopedJavaGlobalRef<jobject>& java_proxy_change_listener) {
247 if (java_proxy_change_listener.is_null())
248 return;
249 JNIEnv* env = AttachCurrentThread();
250 Java_ProxyChangeListener_stop(env, java_proxy_change_listener.obj());
251 }
252
253 void Shutdown() {
254 if (OnJNIThread())
255 ShutdownOnJNIThread(java_proxy_change_listener_);
256 else
257 jni_task_runner_->PostTask(
258 FROM_HERE,
259 base::Bind(&Delegate::ShutdownOnJNIThread,
260 java_proxy_change_listener_));
Ryan Sleevi 2012/05/25 17:42:14 braces
Philippe 2012/05/29 10:05:30 Done.
261 }
262
263 // Called on the network thread.
264 void SetNewConfigAndNotifyObservers(const ProxyConfig& proxy_config) {
265 DCHECK(OnNetworkThread());
266 proxy_config_ = proxy_config;
267 FOR_EACH_OBSERVER(Observer, observers_,
268 OnProxyConfigChanged(proxy_config,
269 ProxyConfigService::CONFIG_VALID));
270 }
271
272 bool OnJNIThread() const {
273 return jni_task_runner_->BelongsToCurrentThread();
274 }
275
276 bool OnNetworkThread() const {
277 return network_task_runner_->BelongsToCurrentThread();
278 }
279
280 ScopedJavaGlobalRef<jobject> java_proxy_change_listener_;
281
282 ObserverList<Observer> observers_;
283 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
284 scoped_refptr<base::SingleThreadTaskRunner> jni_task_runner_;
285 GetPropertyCallback get_property_callback_;
286 ProxyConfig proxy_config_;
287
288 DISALLOW_COPY_AND_ASSIGN(Delegate);
289 };
290
291 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
292 base::SingleThreadTaskRunner* network_task_runner,
293 base::SingleThreadTaskRunner* jni_task_runner)
294 : delegate_(new Delegate(
295 network_task_runner, jni_task_runner, base::Bind(&GetJavaProperty))) {
296 delegate_->SetupJNI(this);
297 delegate_->FetchInitialConfig();
298 }
299
300 ProxyConfigServiceAndroid::~ProxyConfigServiceAndroid() {}
301
302 // static
303 bool ProxyConfigServiceAndroid::Register(JNIEnv* env) {
304 return RegisterNativesImpl(env);
305 }
306
307 void ProxyConfigServiceAndroid::AddObserver(Observer* observer) {
308 delegate_->AddObserver(observer);
309 }
310
311 void ProxyConfigServiceAndroid::RemoveObserver(Observer* observer) {
312 delegate_->RemoveObserver(observer);
313 }
314
315 ProxyConfigService::ConfigAvailability
316 ProxyConfigServiceAndroid::GetLatestProxyConfig(ProxyConfig* config) {
317 return delegate_->GetLatestProxyConfig(config);
318 }
319
320 void ProxyConfigServiceAndroid::ProxySettingsChanged(JNIEnv*, jobject) {
321 delegate_->ProxySettingsChanged();
322 }
323
324 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
325 base::SingleThreadTaskRunner* network_task_runner,
326 base::SingleThreadTaskRunner* jni_task_runner,
327 GetPropertyCallback get_property_callback)
328 : delegate_(new Delegate(
329 network_task_runner, jni_task_runner, get_property_callback)) {
330 delegate_->FetchInitialConfig();
331 }
332
333 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698