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.
5 #include "net/proxy/proxy_config_service_android.h"
7 #include <sys/system_properties.h>
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/compiler_specific.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/observer_list.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/string_tokenizer.h"
20 #include "base/string_util.h"
21 #include "googleurl/src/url_parse.h"
22 #include "jni/ProxyChangeListener_jni.h"
23 #include "net/base/host_port_pair.h"
24 #include "net/proxy/proxy_config.h"
26 using base::android::AttachCurrentThread
;
27 using base::android::ConvertUTF8ToJavaString
;
28 using base::android::ConvertJavaStringToUTF8
;
29 using base::android::CheckException
;
30 using base::android::ClearException
;
31 using base::android::ScopedJavaGlobalRef
;
37 typedef ProxyConfigServiceAndroid::GetPropertyCallback GetPropertyCallback
;
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
)
50 ProxyServer
ConstructProxyServer(ProxyServer::Scheme scheme
,
51 const std::string
& proxy_host
,
52 const std::string
& proxy_port
) {
53 DCHECK(!proxy_host
.empty());
55 if (proxy_port
.empty())
56 port_as_int
= ProxyServer::GetDefaultPortForScheme(scheme
);
57 else if (!ConvertStringToPort(proxy_port
, &port_as_int
))
59 DCHECK(port_as_int
> 0);
62 HostPortPair(proxy_host
, static_cast<uint16
>(port_as_int
)));
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
);
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
);
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
,
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())
104 StringTokenizer
tokenizer(non_proxy_hosts
, "|");
105 while (tokenizer
.GetNext()) {
106 std::string token
= tokenizer
.token();
108 TrimWhitespaceASCII(token
, TRIM_ALL
, &pattern
);
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);
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 // mostly equivalent Android implementation. There is one intentional
122 // difference: by default Chromium uses the HTTP port (80) for HTTPS
123 // connections via proxy. This default is identical on other platforms.
124 // On the opposite, Java spec suggests to use HTTPS port (443) by default (the
125 // default value of https.proxyPort).
126 rules
->type
= ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME
;
127 rules
->proxy_for_http
= LookupProxy("http", get_property
,
128 ProxyServer::SCHEME_HTTP
);
129 rules
->proxy_for_https
= LookupProxy("https", get_property
,
130 ProxyServer::SCHEME_HTTP
);
131 rules
->proxy_for_ftp
= LookupProxy("ftp", get_property
,
132 ProxyServer::SCHEME_HTTP
);
133 rules
->fallback_proxy
= LookupSocksProxy(get_property
);
134 rules
->bypass_rules
.Clear();
135 AddBypassRules("ftp", get_property
, &rules
->bypass_rules
);
136 AddBypassRules("http", get_property
, &rules
->bypass_rules
);
137 AddBypassRules("https", get_property
, &rules
->bypass_rules
);
138 return rules
->proxy_for_http
.is_valid() ||
139 rules
->proxy_for_https
.is_valid() ||
140 rules
->proxy_for_ftp
.is_valid() ||
141 rules
->fallback_proxy
.is_valid();
144 void GetLatestProxyConfigInternal(const GetPropertyCallback
& get_property
,
145 ProxyConfig
* config
) {
146 if (!GetProxyRules(get_property
, &config
->proxy_rules()))
147 *config
= ProxyConfig::CreateDirect();
150 std::string
GetJavaProperty(const std::string
& property
) {
151 // Use Java System.getProperty to get configuration information.
152 // TODO(pliard): Conversion to/from UTF8 ok here?
153 JNIEnv
* env
= AttachCurrentThread();
154 ScopedJavaLocalRef
<jstring
> str
= ConvertUTF8ToJavaString(env
, property
);
155 ScopedJavaLocalRef
<jstring
> result
=
156 Java_ProxyChangeListener_getProperty(env
, str
.obj());
157 return result
.is_null() ?
158 std::string() : ConvertJavaStringToUTF8(env
, result
.obj());
163 class ProxyConfigServiceAndroid::Delegate
164 : public base::RefCountedThreadSafe
<Delegate
> {
166 Delegate(base::SequencedTaskRunner
* network_task_runner
,
167 base::SequencedTaskRunner
* jni_task_runner
,
168 const GetPropertyCallback
& get_property_callback
)
169 : ALLOW_THIS_IN_INITIALIZER_LIST(jni_delegate_(this)),
170 network_task_runner_(network_task_runner
),
171 jni_task_runner_(jni_task_runner
),
172 get_property_callback_(get_property_callback
) {
176 DCHECK(OnJNIThread());
177 JNIEnv
* env
= AttachCurrentThread();
178 if (java_proxy_change_listener_
.is_null()) {
179 java_proxy_change_listener_
.Reset(
180 Java_ProxyChangeListener_create(
181 env
, base::android::GetApplicationContext()));
182 CHECK(!java_proxy_change_listener_
.is_null());
184 Java_ProxyChangeListener_start(
186 java_proxy_change_listener_
.obj(),
187 reinterpret_cast<jint
>(&jni_delegate_
));
190 void FetchInitialConfig() {
191 DCHECK(OnJNIThread());
192 ProxyConfig proxy_config
;
193 GetLatestProxyConfigInternal(get_property_callback_
, &proxy_config
);
194 network_task_runner_
->PostTask(
196 base::Bind(&Delegate::SetNewConfigOnNetworkThread
, this, proxy_config
));
201 ShutdownOnJNIThread();
203 jni_task_runner_
->PostTask(
205 base::Bind(&Delegate::ShutdownOnJNIThread
, this));
209 // Called only on the network thread.
210 void AddObserver(Observer
* observer
) {
211 DCHECK(OnNetworkThread());
212 observers_
.AddObserver(observer
);
215 void RemoveObserver(Observer
* observer
) {
216 DCHECK(OnNetworkThread());
217 observers_
.RemoveObserver(observer
);
220 ConfigAvailability
GetLatestProxyConfig(ProxyConfig
* config
) {
221 DCHECK(OnNetworkThread());
223 return ProxyConfigService::CONFIG_UNSET
;
224 *config
= proxy_config_
;
225 return ProxyConfigService::CONFIG_VALID
;
228 // Called on the JNI thread.
229 void ProxySettingsChanged() {
230 DCHECK(OnJNIThread());
231 ProxyConfig proxy_config
;
232 GetLatestProxyConfigInternal(get_property_callback_
, &proxy_config
);
233 network_task_runner_
->PostTask(
236 &Delegate::SetNewConfigOnNetworkThread
, this, proxy_config
));
240 friend class base::RefCountedThreadSafe
<Delegate
>;
242 class JNIDelegateImpl
: public ProxyConfigServiceAndroid::JNIDelegate
{
244 explicit JNIDelegateImpl(Delegate
* delegate
) : delegate_(delegate
) {}
246 // ProxyConfigServiceAndroid::JNIDelegate overrides.
247 virtual void ProxySettingsChanged(JNIEnv
*, jobject
) OVERRIDE
{
248 delegate_
->ProxySettingsChanged();
252 Delegate
* const delegate_
;
255 virtual ~Delegate() {}
257 void ShutdownOnJNIThread() {
258 if (java_proxy_change_listener_
.is_null())
260 JNIEnv
* env
= AttachCurrentThread();
261 Java_ProxyChangeListener_stop(env
, java_proxy_change_listener_
.obj());
264 // Called on the network thread.
265 void SetNewConfigOnNetworkThread(const ProxyConfig
& proxy_config
) {
266 DCHECK(OnNetworkThread());
267 proxy_config_
= proxy_config
;
268 FOR_EACH_OBSERVER(Observer
, observers_
,
269 OnProxyConfigChanged(proxy_config
,
270 ProxyConfigService::CONFIG_VALID
));
273 bool OnJNIThread() const {
274 return jni_task_runner_
->RunsTasksOnCurrentThread();
277 bool OnNetworkThread() const {
278 return network_task_runner_
->RunsTasksOnCurrentThread();
281 ScopedJavaGlobalRef
<jobject
> java_proxy_change_listener_
;
283 JNIDelegateImpl jni_delegate_
;
284 ObserverList
<Observer
> observers_
;
285 scoped_refptr
<base::SequencedTaskRunner
> network_task_runner_
;
286 scoped_refptr
<base::SequencedTaskRunner
> jni_task_runner_
;
287 GetPropertyCallback get_property_callback_
;
288 ProxyConfig proxy_config_
;
290 DISALLOW_COPY_AND_ASSIGN(Delegate
);
293 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
294 base::SequencedTaskRunner
* network_task_runner
,
295 base::SequencedTaskRunner
* jni_task_runner
)
296 : delegate_(new Delegate(
297 network_task_runner
, jni_task_runner
, base::Bind(&GetJavaProperty
))) {
298 delegate_
->SetupJNI();
299 delegate_
->FetchInitialConfig();
302 ProxyConfigServiceAndroid::~ProxyConfigServiceAndroid() {
303 delegate_
->Shutdown();
307 bool ProxyConfigServiceAndroid::Register(JNIEnv
* env
) {
308 return RegisterNativesImpl(env
);
311 void ProxyConfigServiceAndroid::AddObserver(Observer
* observer
) {
312 delegate_
->AddObserver(observer
);
315 void ProxyConfigServiceAndroid::RemoveObserver(Observer
* observer
) {
316 delegate_
->RemoveObserver(observer
);
319 ProxyConfigService::ConfigAvailability
320 ProxyConfigServiceAndroid::GetLatestProxyConfig(ProxyConfig
* config
) {
321 return delegate_
->GetLatestProxyConfig(config
);
324 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
325 base::SequencedTaskRunner
* network_task_runner
,
326 base::SequencedTaskRunner
* jni_task_runner
,
327 GetPropertyCallback get_property_callback
)
328 : delegate_(new Delegate(
329 network_task_runner
, jni_task_runner
, get_property_callback
)) {
330 delegate_
->SetupJNI();
331 delegate_
->FetchInitialConfig();
334 void ProxyConfigServiceAndroid::ProxySettingsChanged() {
335 delegate_
->ProxySettingsChanged();