OLD | NEW |
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 Loading... |
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(const std::string& host, int port, |
| 164 ProxyConfig* config) { |
| 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 Loading... |
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(host, port, &proxy_config); |
| 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 virtual void ProxySettingsChangedTo(JNIEnv* env, jobject jself, |
| 271 jstring jhost, jint jport) OVERRIDE { |
| 272 std::string host = ConvertJavaStringToUTF8(env, jhost); |
| 273 delegate_->ProxySettingsChangedTo(host, jport); |
| 274 } |
| 275 |
| 276 virtual void ProxySettingsChanged(JNIEnv* env, jobject self) OVERRIDE { |
249 delegate_->ProxySettingsChanged(); | 277 delegate_->ProxySettingsChanged(); |
250 } | 278 } |
251 | 279 |
252 private: | 280 private: |
253 Delegate* const delegate_; | 281 Delegate* const delegate_; |
254 }; | 282 }; |
255 | 283 |
256 virtual ~Delegate() {} | 284 virtual ~Delegate() {} |
257 | 285 |
258 void ShutdownOnJNIThread() { | 286 void ShutdownOnJNIThread() { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 network_task_runner, jni_task_runner, get_property_callback)) { | 358 network_task_runner, jni_task_runner, get_property_callback)) { |
331 delegate_->SetupJNI(); | 359 delegate_->SetupJNI(); |
332 delegate_->FetchInitialConfig(); | 360 delegate_->FetchInitialConfig(); |
333 } | 361 } |
334 | 362 |
335 void ProxyConfigServiceAndroid::ProxySettingsChanged() { | 363 void ProxyConfigServiceAndroid::ProxySettingsChanged() { |
336 delegate_->ProxySettingsChanged(); | 364 delegate_->ProxySettingsChanged(); |
337 } | 365 } |
338 | 366 |
339 } // namespace net | 367 } // namespace net |
OLD | NEW |