Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_android.h" | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/android/scoped_java_ref.h" | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/bind.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/location.h" | |
| 16 #include "base/logging.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/observer_list.h" | |
| 19 #include "base/single_thread_task_runner.h" | |
| 20 #include "base/string_tokenizer.h" | |
| 21 #include "base/string_util.h" | |
| 22 #include "googleurl/src/url_parse.h" | |
| 23 #include "jni/proxy_change_listener_jni.h" | |
| 24 #include "net/base/host_port_pair.h" | |
| 25 #include "net/proxy/proxy_config.h" | |
| 26 | |
| 27 using base::android::AttachCurrentThread; | |
| 28 using base::android::ConvertUTF8ToJavaString; | |
| 29 using base::android::ConvertJavaStringToUTF8; | |
| 30 using base::android::CheckException; | |
| 31 using base::android::ClearException; | |
| 32 using base::android::GetMethodID; | |
| 33 | |
| 34 namespace net { | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 // Returns whether the provided string was successfully converted to a port. | |
| 39 bool ConvertStringToPort(const std::string& port, int* output) { | |
| 40 url_parse::Component component(0, port.size()); | |
| 41 int result = url_parse::ParsePort(port.c_str(), component); | |
| 42 if (result == url_parse::PORT_INVALID || | |
| 43 result == url_parse::PORT_UNSPECIFIED) | |
| 44 return false; | |
| 45 *output = result; | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 ProxyServer ConstructProxyServer(ProxyServer::Scheme scheme, | |
| 50 const std::string& proxy_host, | |
| 51 const std::string& proxy_port) { | |
| 52 DCHECK(!proxy_host.empty()); | |
| 53 int port_as_int = 0; | |
| 54 if (proxy_port.empty()) | |
| 55 port_as_int = ProxyServer::GetDefaultPortForScheme(scheme); | |
| 56 else if (!ConvertStringToPort(proxy_port, &port_as_int)) | |
| 57 return ProxyServer(); | |
| 58 DCHECK(port_as_int > 0); | |
| 59 return ProxyServer::FromURI( | |
|
Ryan Sleevi
2012/05/19 01:26:31
use the constructor
return ProxyServer(scheme, Ho
Philippe
2012/05/22 15:23:05
Done.
| |
| 60 HostPortPair(proxy_host, static_cast<uint>(port_as_int)).ToString(), | |
|
Ryan Sleevi
2012/05/19 01:26:31
uint16
Philippe
2012/05/22 15:23:05
Done.
| |
| 61 scheme); | |
| 62 } | |
| 63 | |
| 64 ProxyServer LookupProxy( | |
| 65 const std::string& prefix, | |
| 66 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 67 ProxyServer::Scheme scheme) { | |
| 68 DCHECK(!prefix.empty()); | |
| 69 std::string proxy_host = delegate->GetProperty(prefix + ".proxyHost"); | |
| 70 if (!proxy_host.empty()) { | |
| 71 std::string proxy_port = delegate->GetProperty(prefix + ".proxyPort"); | |
| 72 return ConstructProxyServer(scheme, proxy_host, proxy_port); | |
| 73 } | |
| 74 // Fall back to default proxy, if any. | |
| 75 proxy_host = delegate->GetProperty("proxyHost"); | |
| 76 if (!proxy_host.empty()) { | |
| 77 std::string proxy_port = delegate->GetProperty("proxyPort"); | |
| 78 return ConstructProxyServer(scheme, proxy_host, proxy_port); | |
| 79 } | |
| 80 return ProxyServer(); | |
| 81 } | |
| 82 | |
| 83 ProxyServer LookupSocksProxy( | |
| 84 ProxyConfigServiceAndroid::Delegate* delegate) { | |
| 85 std::string proxy_host = delegate->GetProperty("socksProxyHost"); | |
| 86 if (!proxy_host.empty()) { | |
| 87 std::string proxy_port = delegate->GetProperty("socksProxyPort"); | |
| 88 return ConstructProxyServer(ProxyServer::SCHEME_SOCKS5, proxy_host, | |
| 89 proxy_port); | |
| 90 } | |
| 91 return ProxyServer(); | |
| 92 } | |
| 93 | |
| 94 void AddBypassRules( | |
| 95 const std::string& scheme, | |
| 96 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 97 ProxyBypassRules* bypass_rules) { | |
| 98 // The format of a hostname pattern is a list of hostnames that are separated | |
| 99 // by | and that use * as a wildcard. For example, setting the | |
| 100 // http.nonProxyHosts property to *.android.com|*.kernel.org will cause | |
| 101 // requests to http://developer.android.com to be made without a proxy. | |
| 102 std::string non_proxy_hosts = | |
| 103 delegate->GetProperty(scheme + ".nonProxyHosts"); | |
| 104 if (non_proxy_hosts.empty()) | |
| 105 return; | |
| 106 StringTokenizer tokenizer(non_proxy_hosts, "|"); | |
| 107 while (tokenizer.GetNext()) { | |
| 108 std::string token = tokenizer.token(); | |
| 109 std::string pattern; | |
| 110 TrimWhitespaceASCII(token, TRIM_ALL, &pattern); | |
| 111 if (pattern.empty()) | |
| 112 continue; | |
| 113 // '?' is not one of the specified pattern characters above. | |
| 114 DCHECK_EQ(std::string::npos, pattern.find('?')); | |
| 115 bypass_rules->AddRuleForHostname(scheme, pattern, -1); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // returns true if a valid proxy was found. | |
| 120 bool GetProxyRules( | |
| 121 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 122 ProxyConfig::ProxyRules* rules) { | |
| 123 // See libcore/luni/src/main/java/java/net/ProxySelectorImpl.java for the | |
| 124 // equivalent Android implementation. | |
| 125 rules->type = ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | |
| 126 rules->proxy_for_http = LookupProxy("http", delegate, | |
| 127 ProxyServer::SCHEME_HTTP); | |
| 128 rules->proxy_for_https = LookupProxy("https", delegate, | |
| 129 ProxyServer::SCHEME_HTTPS); | |
| 130 rules->proxy_for_ftp = LookupProxy("ftp", delegate, ProxyServer::SCHEME_HTTP); | |
| 131 rules->fallback_proxy = LookupSocksProxy(delegate); | |
| 132 rules->bypass_rules.Clear(); | |
| 133 AddBypassRules("ftp", delegate, &rules->bypass_rules); | |
| 134 AddBypassRules("http", delegate, &rules->bypass_rules); | |
| 135 AddBypassRules("https", delegate, &rules->bypass_rules); | |
| 136 return rules->proxy_for_http.is_valid() || | |
| 137 rules->proxy_for_https.is_valid() || | |
| 138 rules->proxy_for_ftp.is_valid() || | |
| 139 rules->fallback_proxy.is_valid(); | |
| 140 }; | |
| 141 | |
| 142 void GetLatestProxyConfigInternal( | |
| 143 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 144 ProxyConfig* config) { | |
| 145 if (!GetProxyRules(delegate, &config->proxy_rules())) | |
| 146 *config = ProxyConfig::CreateDirect(); | |
| 147 } | |
| 148 | |
| 149 // This class implements the link to Android's proxy broadcast mechanism. | |
| 150 class DelegateImpl : public ProxyConfigServiceAndroid::Delegate { | |
| 151 public: | |
| 152 virtual void Start(ProxyConfigServiceAndroid* service) OVERRIDE { | |
| 153 JNIEnv* env = AttachCurrentThread(); | |
| 154 if (java_proxy_change_listener_android_.is_null()) { | |
| 155 java_proxy_change_listener_android_.Reset( | |
| 156 Java_ProxyChangeListener_create( | |
| 157 env, base::android::GetApplicationContext())); | |
| 158 CHECK(!java_proxy_change_listener_android_.is_null()); | |
| 159 } | |
| 160 Java_ProxyChangeListener_start( | |
| 161 env, | |
| 162 java_proxy_change_listener_android_.obj(), | |
| 163 reinterpret_cast<jint>(service)); | |
| 164 } | |
| 165 | |
| 166 virtual void Stop() OVERRIDE { | |
| 167 // The java object will be NULL if Stop() was already called (e.g. after a | |
| 168 // Start()), or if Start() was never called. Stop() is called from the | |
| 169 // destructor of ProxyConfigServiceAndroid. | |
| 170 if (java_proxy_change_listener_android_.is_null()) | |
| 171 return; | |
| 172 JNIEnv* env = AttachCurrentThread(); | |
| 173 Java_ProxyChangeListener_stop(env, | |
| 174 java_proxy_change_listener_android_.obj()); | |
| 175 } | |
| 176 | |
| 177 virtual std::string GetProperty(const std::string& property) OVERRIDE { | |
| 178 // Use Java System.getProperty to get configuration information. | |
| 179 // TODO(pliard): Conversion to/from UTF8 ok here? | |
| 180 JNIEnv* env = AttachCurrentThread(); | |
| 181 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property); | |
| 182 ScopedJavaLocalRef<jstring> result = | |
| 183 Java_ProxyChangeListener_getProperty(env, str.obj()); | |
| 184 return result.is_null() | |
| 185 ? std::string() | |
|
Ryan Sleevi
2012/05/19 01:26:31
the ? should be on the previous line
See http://w
Philippe
2012/05/22 15:23:05
Done.
| |
| 186 : ConvertJavaStringToUTF8(env, result.obj()); | |
| 187 } | |
| 188 | |
| 189 private: | |
| 190 base::android::ScopedJavaGlobalRef<jobject> | |
| 191 java_proxy_change_listener_android_; | |
| 192 }; | |
| 193 | |
| 194 } // namespace | |
| 195 | |
| 196 class ProxyConfigServiceAndroid::SharedState | |
| 197 : public base::RefCountedThreadSafe<SharedState> { | |
| 198 public: | |
| 199 explicit SharedState(Delegate* delegate) : delegate_(delegate) { | |
| 200 DCHECK(delegate); | |
| 201 } | |
| 202 | |
| 203 // Non-reassignable pointer thus safely readable by any thread. | |
| 204 const scoped_ptr<Delegate> delegate_; | |
| 205 // Note that this is only accessed from the observer thread. | |
| 206 ObserverList<Observer> observers_; | |
| 207 | |
| 208 private: | |
| 209 friend class base::RefCountedThreadSafe<SharedState>; | |
| 210 ~SharedState() {} | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(SharedState); | |
| 213 }; | |
| 214 | |
| 215 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid( | |
| 216 base::SingleThreadTaskRunner* observer_runner) | |
| 217 : shared_state_(new SharedState(new DelegateImpl())), | |
| 218 observer_runner_(observer_runner) { | |
| 219 DCHECK(observer_runner_.get()); | |
| 220 } | |
| 221 | |
| 222 ProxyConfigServiceAndroid::~ProxyConfigServiceAndroid() { | |
| 223 shared_state_->delegate_->Stop(); | |
| 224 } | |
| 225 | |
| 226 // static | |
| 227 bool ProxyConfigServiceAndroid::Register(JNIEnv* env) { | |
| 228 return RegisterNativesImpl(env); | |
| 229 } | |
| 230 | |
| 231 void ProxyConfigServiceAndroid::AddObserver(Observer* observer) { | |
| 232 DCHECK(OnObserverThread()); | |
| 233 if (shared_state_->observers_.size() == 0) | |
| 234 shared_state_->delegate_->Start(this); | |
| 235 // Note that any callbacks from the delegate will result in a scheduled task | |
| 236 // on this same thread, so there is no race between delegate_->Start() and | |
| 237 // AddObserver(). | |
| 238 shared_state_->observers_.AddObserver(observer); | |
| 239 } | |
| 240 | |
| 241 void ProxyConfigServiceAndroid::RemoveObserver(Observer* observer) { | |
| 242 DCHECK(OnObserverThread()); | |
| 243 shared_state_->observers_.RemoveObserver(observer); | |
| 244 if (shared_state_->observers_.size() == 0) | |
| 245 shared_state_->delegate_->Stop(); | |
| 246 } | |
| 247 | |
| 248 ProxyConfigService::ConfigAvailability | |
| 249 ProxyConfigServiceAndroid::GetLatestProxyConfig(ProxyConfig* config) { | |
| 250 if (!config) | |
| 251 return ProxyConfigService::CONFIG_UNSET; | |
| 252 GetLatestProxyConfigInternal(shared_state_->delegate_.get(), config); | |
| 253 return ProxyConfigService::CONFIG_VALID; | |
| 254 } | |
| 255 | |
| 256 void ProxyConfigServiceAndroid::ProxySettingsChangedInternal() { | |
| 257 observer_runner_->PostTask( | |
| 258 FROM_HERE, base::Bind(&ProxySettingsChangedCallback, shared_state_)); | |
| 259 } | |
| 260 | |
| 261 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid( | |
| 262 base::SingleThreadTaskRunner* observer_runner, | |
| 263 ProxyConfigServiceAndroid::Delegate* delegate) | |
| 264 : shared_state_(new SharedState(delegate)), | |
| 265 observer_runner_(observer_runner) { | |
| 266 DCHECK(observer_runner_.get()); | |
| 267 } | |
| 268 | |
| 269 // static | |
| 270 void ProxyConfigServiceAndroid::ProxySettingsChangedCallback( | |
| 271 const scoped_refptr<SharedState>& callback_state) { | |
| 272 // Allow another task to be scheduled (before running observers). | |
| 273 ProxyConfig config; | |
| 274 GetLatestProxyConfigInternal(callback_state->delegate_.get(), &config); | |
| 275 FOR_EACH_OBSERVER(Observer, callback_state->observers_, | |
| 276 OnProxyConfigChanged(config, | |
| 277 ProxyConfigService::CONFIG_VALID)); | |
|
Ryan Sleevi
2012/05/19 01:26:31
I don't believe the threading is correct here.
Th
Philippe
2012/05/22 15:23:05
Indeed.
Now I fetch the latest proxy config in Pr
| |
| 278 } | |
| 279 | |
| 280 bool ProxyConfigServiceAndroid::OnObserverThread() const { | |
| 281 return observer_runner_->BelongsToCurrentThread(); | |
| 282 } | |
| 283 | |
| 284 } // namespace net | |
| OLD | NEW |