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

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

Issue 26763005: android: Use new proxy from PROXY_CHANGE intent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "net/proxy/proxy_config_service_android.h" 5 #include "net/proxy/proxy_config_service_android.h"
6 6
7 #include <sys/system_properties.h> 7 #include <sys/system_properties.h>
8 8
9 #include "base/android/jni_string.h" 9 #include "base/android/jni_string.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/observer_list.h" 17 #include "base/observer_list.h"
18 #include "base/sequenced_task_runner.h" 18 #include "base/sequenced_task_runner.h"
19 #include "base/strings/string_tokenizer.h" 19 #include "base/strings/string_tokenizer.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
21 #include "jni/ProxyChangeListener_jni.h" 22 #include "jni/ProxyChangeListener_jni.h"
22 #include "net/base/host_port_pair.h" 23 #include "net/base/host_port_pair.h"
23 #include "net/proxy/proxy_config.h" 24 #include "net/proxy/proxy_config.h"
24 #include "url/url_parse.h" 25 #include "url/url_parse.h"
25 26
26 using base::android::AttachCurrentThread; 27 using base::android::AttachCurrentThread;
27 using base::android::ConvertUTF8ToJavaString; 28 using base::android::ConvertUTF8ToJavaString;
28 using base::android::ConvertJavaStringToUTF8; 29 using base::android::ConvertJavaStringToUTF8;
29 using base::android::CheckException; 30 using base::android::CheckException;
30 using base::android::ClearException; 31 using base::android::ClearException;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // Use Java System.getProperty to get configuration information. 153 // Use Java System.getProperty to get configuration information.
153 // TODO(pliard): Conversion to/from UTF8 ok here? 154 // TODO(pliard): Conversion to/from UTF8 ok here?
154 JNIEnv* env = AttachCurrentThread(); 155 JNIEnv* env = AttachCurrentThread();
155 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property); 156 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property);
156 ScopedJavaLocalRef<jstring> result = 157 ScopedJavaLocalRef<jstring> result =
157 Java_ProxyChangeListener_getProperty(env, str.obj()); 158 Java_ProxyChangeListener_getProperty(env, str.obj());
158 return result.is_null() ? 159 return result.is_null() ?
159 std::string() : ConvertJavaStringToUTF8(env, result.obj()); 160 std::string() : ConvertJavaStringToUTF8(env, result.obj());
160 } 161 }
161 162
163 void CreateStaticProxyConfig(ProxyConfig* config, const std::string& host,
Philippe 2013/10/15 14:33:21 Nit: |config| should go last since it's an output
Elly Fong-Jones 2013/10/15 16:37:15 Done.
164 int port) {
165 if (port != 0) {
166 std::string rules = base::StringPrintf("%s:%d", host.c_str(), port);
167 config->proxy_rules().ParseFromString(rules);
168 } else {
169 *config = ProxyConfig::CreateDirect();
170 }
171 }
172
162 } // namespace 173 } // namespace
163 174
164 class ProxyConfigServiceAndroid::Delegate 175 class ProxyConfigServiceAndroid::Delegate
165 : public base::RefCountedThreadSafe<Delegate> { 176 : public base::RefCountedThreadSafe<Delegate> {
166 public: 177 public:
167 Delegate(base::SequencedTaskRunner* network_task_runner, 178 Delegate(base::SequencedTaskRunner* network_task_runner,
168 base::SequencedTaskRunner* jni_task_runner, 179 base::SequencedTaskRunner* jni_task_runner,
169 const GetPropertyCallback& get_property_callback) 180 const GetPropertyCallback& get_property_callback)
170 : jni_delegate_(this), 181 : jni_delegate_(this),
171 network_task_runner_(network_task_runner), 182 network_task_runner_(network_task_runner),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 void ProxySettingsChanged() { 241 void ProxySettingsChanged() {
231 DCHECK(OnJNIThread()); 242 DCHECK(OnJNIThread());
232 ProxyConfig proxy_config; 243 ProxyConfig proxy_config;
233 GetLatestProxyConfigInternal(get_property_callback_, &proxy_config); 244 GetLatestProxyConfigInternal(get_property_callback_, &proxy_config);
234 network_task_runner_->PostTask( 245 network_task_runner_->PostTask(
235 FROM_HERE, 246 FROM_HERE,
236 base::Bind( 247 base::Bind(
237 &Delegate::SetNewConfigOnNetworkThread, this, proxy_config)); 248 &Delegate::SetNewConfigOnNetworkThread, this, proxy_config));
238 } 249 }
239 250
251 // Called on the JNI thread.
252 void ProxySettingsChangedTo(const std::string& host, int port) {
253 DCHECK(OnJNIThread());
254 ProxyConfig proxy_config;
255 CreateStaticProxyConfig(&proxy_config, host, port);
256 network_task_runner_->PostTask(
257 FROM_HERE,
258 base::Bind(
259 &Delegate::SetNewConfigOnNetworkThread, this, proxy_config));
260 }
261
240 private: 262 private:
241 friend class base::RefCountedThreadSafe<Delegate>; 263 friend class base::RefCountedThreadSafe<Delegate>;
242 264
243 class JNIDelegateImpl : public ProxyConfigServiceAndroid::JNIDelegate { 265 class JNIDelegateImpl : public ProxyConfigServiceAndroid::JNIDelegate {
244 public: 266 public:
245 explicit JNIDelegateImpl(Delegate* delegate) : delegate_(delegate) {} 267 explicit JNIDelegateImpl(Delegate* delegate) : delegate_(delegate) {}
246 268
247 // ProxyConfigServiceAndroid::JNIDelegate overrides. 269 // ProxyConfigServiceAndroid::JNIDelegate overrides.
248 virtual void ProxySettingsChanged(JNIEnv*, jobject) OVERRIDE { 270 // Pull the host and port out of the supplied ProxyConfig object and pass
249 delegate_->ProxySettingsChanged(); 271 // them on to the delegate.
272 virtual void ProxySettingsChanged(JNIEnv* env, jobject jself,
Philippe 2013/10/15 14:33:21 See my comment in the Java file (TL;DR: please dir
Elly Fong-Jones 2013/10/15 16:37:15 Done.
273 jobject jcfg) OVERRIDE {
274 jclass cls = env->GetObjectClass(jcfg);
275
276 jfieldID hostfield = env->GetFieldID(cls, "host_", "Ljava/lang/String;");
277 jstring hoststr = (jstring)(env->GetObjectField(jcfg, hostfield));
278 jsize len = env->GetStringLength(hoststr);
279 const char *chars = env->GetStringUTFChars(hoststr, NULL);
280 std::string host(chars, len);
281 env->ReleaseStringUTFChars(hoststr, chars);
282
283 jfieldID portfield = env->GetFieldID(cls, "port_", "I");
284 jint port = env->GetIntField(jcfg, portfield);
285
286 delegate_->ProxySettingsChangedTo(host, port);
250 } 287 }
251 288
252 private: 289 private:
253 Delegate* const delegate_; 290 Delegate* const delegate_;
254 }; 291 };
255 292
256 virtual ~Delegate() {} 293 virtual ~Delegate() {}
257 294
258 void ShutdownOnJNIThread() { 295 void ShutdownOnJNIThread() {
259 if (java_proxy_change_listener_.is_null()) 296 if (java_proxy_change_listener_.is_null())
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 network_task_runner, jni_task_runner, get_property_callback)) { 367 network_task_runner, jni_task_runner, get_property_callback)) {
331 delegate_->SetupJNI(); 368 delegate_->SetupJNI();
332 delegate_->FetchInitialConfig(); 369 delegate_->FetchInitialConfig();
333 } 370 }
334 371
335 void ProxyConfigServiceAndroid::ProxySettingsChanged() { 372 void ProxyConfigServiceAndroid::ProxySettingsChanged() {
336 delegate_->ProxySettingsChanged(); 373 delegate_->ProxySettingsChanged();
337 } 374 }
338 375
339 } // namespace net 376 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698