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

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: Address Ryan and Yaron's comments 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 "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/string_number_conversions.h"
15 #include "base/string_tokenizer.h"
16 #include "base/string_util.h"
17 #include "jni/proxy_change_listener_jni.h"
Ryan Sleevi 2012/05/14 18:00:25 Is this absolute to the source tree? Or is this re
Philippe 2012/05/15 16:12:48 Indeed, it is "<(SHARED_INTERMEDIATE_DIR)/net/jni/
18 #include "net/base/host_port_pair.h"
19 #include "net/proxy/proxy_config.h"
20
21 #include <sys/system_properties.h>
Ryan Sleevi 2012/05/14 18:00:25 C headers before C++ & Project headers ( http://go
Philippe 2012/05/15 16:12:48 Done.
22
23 using base::android::AttachCurrentThread;
24 using base::android::ConvertUTF8ToJavaString;
25 using base::android::ConvertJavaStringToUTF8;
26 using base::android::CheckException;
27 using base::android::ClearException;
28 using base::android::GetMethodID;
29
30 namespace net {
31
32 namespace {
33
34 ProxyServer ConstructProxyServer(ProxyServer::Scheme scheme,
35 const std::string& proxy_host,
36 const std::string& proxy_port) {
37 DCHECK(!proxy_host.empty());
38 if (proxy_port.empty())
39 return ProxyServer::FromURI(proxy_host, scheme);
Ryan Sleevi 2012/05/14 18:00:25 I'm still not convinced this is handling IPv6 prop
Philippe 2012/05/15 16:12:48 I fixed the implementation to use HostPortPair in
40 uint port_as_uint = 0;
41 base::StringToUint(proxy_port, &port_as_uint);
Ryan Sleevi 2012/05/14 18:00:25 BUG? Using a GURL seems more appropriate here for
Philippe 2012/05/15 16:12:48 Done.
42 return ProxyServer::FromURI(
43 HostPortPair(proxy_host, port_as_uint).ToString(), scheme);
44 }
45
46 ProxyServer LookupProxy(
47 const std::string& prefix,
48 ProxyConfigServiceAndroid::Delegate* delegate,
49 ProxyServer::Scheme scheme) {
50 DCHECK(!prefix.empty());
51 std::string proxy_host = delegate->GetProperty(prefix + ".proxyHost");
52 if (!proxy_host.empty()) {
53 std::string proxy_port = delegate->GetProperty(prefix + ".proxyPort");
54 return ConstructProxyServer(scheme, proxy_host, proxy_port);
55 }
56 // Fall back to default proxy, if any.
57 proxy_host = delegate->GetProperty("proxyHost");
58 if (!proxy_host.empty()) {
59 std::string proxy_port = delegate->GetProperty("proxyPort");
60 return ConstructProxyServer(scheme, proxy_host, proxy_port);
61 }
62 return ProxyServer();
63 }
64
65 ProxyServer LookupSocksProxy(
66 ProxyConfigServiceAndroid::Delegate* delegate) {
67 std::string proxy_host = delegate->GetProperty("socksProxyHost");
68 if (!proxy_host.empty()) {
69 std::string proxy_port = delegate->GetProperty("socksProxyPort");
70 return ConstructProxyServer(ProxyServer::SCHEME_SOCKS5, proxy_host,
71 proxy_port);
72 }
73 return ProxyServer();
74 }
75
76 void AddBypassRules(
77 const std::string& scheme,
78 ProxyConfigServiceAndroid::Delegate* delegate,
79 ProxyBypassRules* bypass_rules) {
80 // The format of a hostname pattern is a list of hostnames that are separated
81 // by | and that use * as a wildcard. For example, setting the
82 // http.nonProxyHosts property to *.android.com|*.kernel.org will cause
83 // requests to http://developer.android.com to be made without a proxy.
84 std::string non_proxy_hosts =
85 delegate->GetProperty(scheme + ".nonProxyHosts");
86 if (non_proxy_hosts.empty())
87 return;
88 StringTokenizer tokenizer(non_proxy_hosts, "|");
89 while (tokenizer.GetNext()) {
90 std::string token = tokenizer.token();
91 std::string pattern;
92 TrimWhitespaceASCII(token, TRIM_ALL, &pattern);
93 if (pattern.empty())
94 continue;
95 // '?' is not one of the specified pattern characters above.
96 DCHECK_EQ(std::string::npos, pattern.find('?'));
97 bypass_rules->AddRuleForHostname(scheme, pattern, -1);
98 }
99 }
100
101 // returns true if a valid proxy was found.
102 bool GetProxyRules(
103 ProxyConfigServiceAndroid::Delegate* delegate,
104 ProxyConfig::ProxyRules* rules) {
105 // See libcore/luni/src/main/java/java/net/ProxySelectorImpl.java for the
106 // semantics we're trying to match.
Ryan Sleevi 2012/05/14 18:00:25 ocd nit: It's been raised by reviewers of my code
Philippe 2012/05/15 16:12:48 Done.
107 rules->type = ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
108 rules->proxy_for_http = LookupProxy("http", delegate,
109 ProxyServer::SCHEME_HTTP);
110 rules->proxy_for_https = LookupProxy("https", delegate,
111 ProxyServer::SCHEME_HTTPS);
112 rules->proxy_for_ftp = LookupProxy("ftp", delegate, ProxyServer::SCHEME_HTTP);
113 rules->fallback_proxy = LookupSocksProxy(delegate);
114 rules->bypass_rules.Clear();
115 AddBypassRules("ftp", delegate, &rules->bypass_rules);
116 AddBypassRules("http", delegate, &rules->bypass_rules);
117 AddBypassRules("https", delegate, &rules->bypass_rules);
118 return rules->proxy_for_http.is_valid() ||
119 rules->proxy_for_https.is_valid() ||
120 rules->proxy_for_ftp.is_valid() ||
121 rules->fallback_proxy.is_valid();
122 };
123
124 void GetLatestProxyConfigInternal(
125 ProxyConfigServiceAndroid::Delegate* delegate,
126 ProxyConfig* config) {
127 if (!GetProxyRules(delegate, &config->proxy_rules()))
128 *config = ProxyConfig::CreateDirect();
129 }
130
131 // This class implements the link to Android's proxy broadcast mechanism.
132 class DelegateImpl : public ProxyConfigServiceAndroid::Delegate {
133 public:
134 DelegateImpl() : service_(NULL) {}
135
136 virtual void Start(ProxyConfigServiceAndroid* service) OVERRIDE {
137 DCHECK(!service_);
138 JNIEnv* env = AttachCurrentThread();
139 service_ = service;
140 if (java_proxy_change_listener_android_.is_null()) {
141 java_proxy_change_listener_android_.Reset(
142 Java_ProxyChangeListener_create(
143 env, base::android::GetApplicationContext()));
144 CHECK(!java_proxy_change_listener_android_.is_null());
145 }
146 Java_ProxyChangeListener_start(
147 env,
148 java_proxy_change_listener_android_.obj(),
149 reinterpret_cast<jint>(service));
150 }
151
152 virtual void Stop() OVERRIDE {
153 // The java object will be NULL if Stop() was already called (e.g. after a
154 // Start()), or if Start() was never called. Stop() is called from the
155 // destructor of ProxyConfigServiceAndroid.
156 if (java_proxy_change_listener_android_.is_null())
157 return;
158 JNIEnv* env = AttachCurrentThread();
159 Java_ProxyChangeListener_stop(env,
160 java_proxy_change_listener_android_.obj());
161 }
162
163 virtual std::string GetProperty(const std::string& property) OVERRIDE {
164 // Use Java System.getProperty to get configuration information.
165 // Question: Conversion to/from UTF8 ok here?
Ryan Sleevi 2012/05/14 18:00:25 Should there be a TODO and a bug followed for foll
Philippe 2012/05/15 16:12:48 I made it a TODO and will file a bug.
joth 2012/06/21 01:12:43 Was there a bug? AFAICT UTF8 should be fine: the p
Philippe 2012/06/21 11:32:55 No, it seems that I forgot to create the bug :/ I'
166 JNIEnv* env = AttachCurrentThread();
167 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property);
168 ScopedJavaLocalRef<jstring> result =
169 Java_ProxyChangeListener_getProperty(env, str.obj());
170 return result.is_null() ? "" : ConvertJavaStringToUTF8(env, result.obj());
Ryan Sleevi 2012/05/14 18:00:25 ocd style: std::string() rather than ""
Philippe 2012/05/15 16:12:48 Done.
171 }
172
173 private:
174 ProxyConfigServiceAndroid* service_;
Ryan Sleevi 2012/05/14 18:00:25 Did I miss where |service_| is actually used here?
Philippe 2012/05/15 16:12:48 No you didn't :) It was used before when we had No
175 base::android::ScopedJavaGlobalRef<jobject>
176 java_proxy_change_listener_android_;
177 };
178
179 } // namespace
180
181 struct ProxyConfigServiceAndroid::SharedState
Ryan Sleevi 2012/05/14 18:00:25 style: Classes that are RefCountedThreadSafe<> sho
Philippe 2012/05/15 16:12:48 Done.
182 : public base::RefCountedThreadSafe<SharedState> {
183 SharedState(Delegate* delegate) : delegate_(delegate) {
Ryan Sleevi 2012/05/14 18:00:25 style: explicit SharedState(...) {
Philippe 2012/05/15 16:12:48 Done.
184 DCHECK(delegate);
185 }
186
187 // Non-reassignable pointer thus safely readable by any thread.
188 const scoped_ptr<Delegate> delegate_;
189 // Note that this is only accessed from the observer thread.
190 ObserverList<Observer> observers_;
191 };
192
193 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
194 scoped_refptr<base::SingleThreadTaskRunner> observer_runner)
195 : shared_state_(new SharedState(new DelegateImpl)),
196 observer_runner_(observer_runner) {
197 DCHECK(observer_runner_.get());
198 }
199
200 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid(
201 scoped_refptr<base::SingleThreadTaskRunner> observer_runner,
202 ProxyConfigServiceAndroid::Delegate* delegate)
203 : shared_state_(new SharedState(delegate)),
204 observer_runner_(observer_runner) {
205 DCHECK(observer_runner_.get());
206 }
Ryan Sleevi 2012/05/14 18:00:25 style: Make sure that the order matches the header
Philippe 2012/05/15 16:12:48 Done.
207
208 ProxyConfigServiceAndroid::~ProxyConfigServiceAndroid() {
209 shared_state_->delegate_->Stop();
210 }
211
212 // static
213 bool ProxyConfigServiceAndroid::Register(JNIEnv* env) {
214 return RegisterNativesImpl(env);
215 }
216
217 void ProxyConfigServiceAndroid::AddObserver(Observer* observer) {
218 DCHECK(OnObserverThread());
219 if (shared_state_->observers_.size() == 0)
220 shared_state_->delegate_->Start(this);
221 // Note that any callbacks from the delegate will result in a scheduled task
222 // on this same thread, so there is no race between delegate_->Start() and
223 // AddObserver().
224 shared_state_->observers_.AddObserver(observer);
225 }
226
227 void ProxyConfigServiceAndroid::RemoveObserver(Observer* observer) {
228 DCHECK(OnObserverThread());
229 shared_state_->observers_.RemoveObserver(observer);
230 if (shared_state_->observers_.size() == 0) {
231 shared_state_->delegate_->Stop();
Ryan Sleevi 2012/05/14 18:00:25 style nit: Remove the braces around one-line ifs f
Philippe 2012/05/15 16:12:48 Done.
232 }
233 }
234
235 ProxyConfigService::ConfigAvailability
236 ProxyConfigServiceAndroid::GetLatestProxyConfig(ProxyConfig* config) {
237 if (!config)
238 return ProxyConfigService::CONFIG_UNSET;
239 GetLatestProxyConfigInternal(shared_state_->delegate_.get(), config);
240 return ProxyConfigService::CONFIG_VALID;
241 }
242
243 void ProxyConfigServiceAndroid::ProxySettingsChanged() {
244 observer_runner_->PostTask(
245 FROM_HERE, base::Bind(&ProxySettingsChangedCallback, shared_state_));
246 }
247
248 // static
249 void ProxyConfigServiceAndroid::ProxySettingsChangedCallback(
250 scoped_refptr<SharedState> callback_state) {
251 // Allow another task to be scheduled (before running observers).
252 ProxyConfig config;
253 GetLatestProxyConfigInternal(callback_state->delegate_.get(), &config);
254 FOR_EACH_OBSERVER(Observer, callback_state->observers_,
255 OnProxyConfigChanged(config,
256 ProxyConfigService::CONFIG_VALID));
257 }
258
259 bool ProxyConfigServiceAndroid::OnObserverThread() const {
260 return observer_runner_->BelongsToCurrentThread();
261 }
262
263 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698