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 "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/message_loop_proxy.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/string_tokenizer.h" | |
| 15 #include "base/string_util.h" | |
| 16 #include "jni/proxy_change_listener_jni.h" | |
| 17 #include "net/proxy/proxy_config.h" | |
| 18 | |
| 19 #include <sys/system_properties.h> | |
| 20 | |
| 21 using base::android::AttachCurrentThread; | |
| 22 using base::android::ConvertUTF8ToJavaString; | |
| 23 using base::android::ConvertJavaStringToUTF8; | |
| 24 using base::android::CheckException; | |
| 25 using base::android::ClearException; | |
| 26 using base::android::GetMethodID; | |
| 27 | |
| 28 namespace net { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 ProxyServer ConstructProxyServer(ProxyServer::Scheme scheme, | |
| 33 const std::string& proxy_host, | |
| 34 const std::string& proxy_port) { | |
| 35 DCHECK(!proxy_host.empty()); | |
| 36 if (proxy_port.empty()) | |
| 37 return ProxyServer::FromURI(proxy_host, scheme); | |
| 38 return ProxyServer::FromURI(proxy_host + ":" + proxy_port, scheme); | |
|
Ryan Sleevi
2012/05/07 17:12:20
What happens if |proxy_host| is an IPv6 host (a:b)
Philippe
2012/05/09 11:48:40
Done.
| |
| 39 } | |
| 40 | |
| 41 ProxyServer LookupProxy( | |
| 42 const std::string& prefix, | |
|
Ryan Sleevi
2012/05/07 17:12:20
Is it valid for prefix to be .empty()?
DCHECK for
Philippe
2012/05/09 11:48:40
Done.
| |
| 43 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 44 ProxyServer::Scheme scheme) { | |
| 45 std::string proxy_host = delegate->GetProperty(prefix + ".proxyHost"); | |
| 46 if (!proxy_host.empty()) { | |
| 47 const std::string proxy_port = delegate->GetProperty(prefix + ".proxyPort"); | |
|
Ryan Sleevi
2012/05/07 17:12:20
I don't believe I've seen in Chromium code the opt
Philippe
2012/05/09 11:48:40
It's mainly for local documentation sake as you sa
| |
| 48 return ConstructProxyServer(scheme, proxy_host, proxy_port); | |
| 49 } | |
| 50 // Fall back to default proxy, if any. | |
| 51 proxy_host = delegate->GetProperty("proxyHost"); | |
| 52 if (!proxy_host.empty()) { | |
| 53 const std::string proxy_port = delegate->GetProperty("proxyPort"); | |
|
Ryan Sleevi
2012/05/07 17:12:20
You check !proxy_host.empty(), but never check pro
John Knottenbelt
2012/05/08 11:43:12
ConstructProxyServer checks for empty port to avoi
| |
| 54 return ConstructProxyServer(scheme, proxy_host, proxy_port); | |
| 55 } | |
| 56 return ProxyServer(); | |
| 57 } | |
| 58 | |
| 59 ProxyServer LookupSocksProxy( | |
| 60 ProxyConfigServiceAndroid::Delegate* delegate) { | |
| 61 const std::string proxy_host = delegate->GetProperty("socksProxyHost"); | |
| 62 if (!proxy_host.empty()) { | |
| 63 const std::string proxy_port = delegate->GetProperty("socksProxyPort"); | |
| 64 return ConstructProxyServer(ProxyServer::SCHEME_SOCKS5, proxy_host, | |
| 65 proxy_port); | |
| 66 } | |
| 67 return ProxyServer(); | |
| 68 } | |
| 69 | |
| 70 void AddBypassRules( | |
| 71 const std::string& scheme, | |
| 72 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 73 ProxyBypassRules* bypass_rules) { | |
| 74 // The format of a hostname pattern is a list of hostnames that are separated | |
| 75 // by | and that use * as a wildcard. For example, setting the | |
| 76 // http.nonProxyHosts property to *.android.com|*.kernel.org will cause | |
| 77 // requests to http://developer.android.com to be made without a proxy. | |
| 78 const std::string non_proxy_hosts = | |
| 79 delegate->GetProperty(scheme + ".nonProxyHosts"); | |
| 80 if (non_proxy_hosts.empty()) | |
| 81 return; | |
| 82 StringTokenizer tokenizer(non_proxy_hosts, "|"); | |
| 83 while (tokenizer.GetNext()) { | |
| 84 const std::string token = tokenizer.token(); | |
| 85 std::string pattern; | |
| 86 TrimWhitespaceASCII(token, TRIM_ALL, &pattern); | |
| 87 if (pattern.empty()) | |
| 88 continue; | |
| 89 // '?' is not one of the specified pattern characters above. | |
| 90 DCHECK_EQ(std::string::npos, pattern.find('?')); | |
| 91 bypass_rules->AddRuleForHostname(scheme, pattern, -1); | |
|
Ryan Sleevi
2012/05/07 17:12:20
This method returns bool. As it stands, you'll sim
John Knottenbelt
2012/05/08 11:43:12
The default java.net.ProxySelector ( eloper.androi
| |
| 92 } | |
| 93 } | |
| 94 | |
| 95 // returns true if a valid proxy was found. | |
| 96 bool GetProxyRules( | |
| 97 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 98 ProxyConfig::ProxyRules* rules) { | |
| 99 // See libcore/luni/src/main/java/java/net/ProxySelectorImpl.java for the | |
| 100 // semantics we're trying to match. | |
| 101 rules->type = ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; | |
| 102 rules->proxy_for_http = LookupProxy("http", delegate, | |
| 103 ProxyServer::SCHEME_HTTP); | |
| 104 rules->proxy_for_https = LookupProxy("https", delegate, | |
| 105 ProxyServer::SCHEME_HTTPS); | |
| 106 rules->proxy_for_ftp = LookupProxy("ftp", delegate, ProxyServer::SCHEME_HTTP); | |
| 107 rules->fallback_proxy = LookupSocksProxy(delegate); | |
| 108 rules->bypass_rules.Clear(); | |
| 109 AddBypassRules("ftp", delegate, &rules->bypass_rules); | |
| 110 AddBypassRules("http", delegate, &rules->bypass_rules); | |
| 111 AddBypassRules("https", delegate, &rules->bypass_rules); | |
| 112 return rules->proxy_for_http.is_valid() || | |
| 113 rules->proxy_for_https.is_valid() || | |
| 114 rules->proxy_for_ftp.is_valid() || | |
| 115 rules->fallback_proxy.is_valid(); | |
| 116 }; | |
| 117 | |
| 118 void GetLatestProxyConfigInternal( | |
| 119 ProxyConfigServiceAndroid::Delegate* delegate, | |
| 120 ProxyConfig* config) { | |
| 121 if (!GetProxyRules(delegate, &config->proxy_rules())) | |
| 122 *config = ProxyConfig::CreateDirect(); | |
| 123 } | |
| 124 | |
| 125 // This class implements the link to Android's proxy broadcast mechanism. | |
| 126 class DelegateImpl : public ProxyConfigServiceAndroid::Delegate { | |
| 127 public: | |
| 128 DelegateImpl() : service_(NULL) {} | |
| 129 | |
| 130 virtual void Start(ProxyConfigServiceAndroid* service) OVERRIDE { | |
| 131 DCHECK(!service_); | |
| 132 JNIEnv* env = AttachCurrentThread(); | |
| 133 service_ = service; | |
| 134 if (java_proxy_change_listener_android_.is_null()) { | |
| 135 java_proxy_change_listener_android_.Reset( | |
| 136 Java_ProxyChangeListener_create( | |
| 137 env, base::android::GetApplicationContext())); | |
| 138 CHECK(!java_proxy_change_listener_android_.is_null()); | |
| 139 } | |
| 140 Java_ProxyChangeListener_start( | |
| 141 env, | |
| 142 java_proxy_change_listener_android_.obj(), | |
| 143 reinterpret_cast<jint>(service)); | |
| 144 } | |
| 145 | |
| 146 virtual void Stop() OVERRIDE { | |
| 147 // The java object will be NULL if Stop() was already called (e.g. after a | |
| 148 // Start()), or if Start() was never called. Stop() is called from the | |
| 149 // destructor of ProxyConfigServiceAndroid. | |
| 150 if (java_proxy_change_listener_android_.is_null()) | |
| 151 return; | |
| 152 JNIEnv* env = AttachCurrentThread(); | |
| 153 Java_ProxyChangeListener_stop(env, | |
| 154 java_proxy_change_listener_android_.obj()); | |
| 155 } | |
| 156 | |
| 157 virtual std::string GetProperty(const std::string& property) OVERRIDE { | |
| 158 // Use Java System.getProperty to get configuration information. | |
| 159 // Question: Conversion to/from UTF8 ok here? | |
| 160 JNIEnv* env = AttachCurrentThread(); | |
| 161 ScopedJavaLocalRef<jstring> str = ConvertUTF8ToJavaString(env, property); | |
| 162 ScopedJavaLocalRef<jstring> result = | |
| 163 Java_ProxyChangeListener_getProperty(env, str.obj()); | |
| 164 return result.is_null() ? "" : ConvertJavaStringToUTF8(env, result.obj()); | |
| 165 } | |
| 166 | |
| 167 private: | |
| 168 ProxyConfigServiceAndroid* service_; | |
| 169 base::android::ScopedJavaGlobalRef<jobject> | |
| 170 java_proxy_change_listener_android_; | |
| 171 }; | |
| 172 | |
| 173 } // namespace | |
| 174 | |
| 175 struct ProxyConfigServiceAndroid::SharedState | |
| 176 : public base::RefCountedThreadSafe<SharedState> { | |
| 177 SharedState(Delegate* delegate) : delegate_(delegate) { | |
| 178 DCHECK(delegate); | |
| 179 } | |
| 180 | |
| 181 // Non-reassignable pointer thus safely readable by any thread. | |
| 182 const scoped_ptr<Delegate> delegate_; | |
| 183 // Note that this is only accessed from the observer thread. | |
| 184 ObserverList<Observer> observers_; | |
| 185 }; | |
| 186 | |
| 187 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid( | |
| 188 scoped_refptr<base::MessageLoopProxy> observer_loop) | |
| 189 : shared_state_(new SharedState(new DelegateImpl)), | |
| 190 observer_loop_(observer_loop) { | |
| 191 DCHECK(observer_loop_.get()); | |
| 192 } | |
| 193 | |
| 194 ProxyConfigServiceAndroid::ProxyConfigServiceAndroid( | |
| 195 scoped_refptr<base::MessageLoopProxy> observer_loop, | |
| 196 ProxyConfigServiceAndroid::Delegate* delegate) | |
| 197 : shared_state_(new SharedState(delegate)), | |
| 198 observer_loop_(observer_loop) { | |
| 199 DCHECK(observer_loop_.get()); | |
| 200 } | |
| 201 | |
| 202 ProxyConfigServiceAndroid::~ProxyConfigServiceAndroid() { | |
| 203 DCHECK(OnObserverThread()); | |
|
Ryan Sleevi
2012/05/07 17:12:20
I think it's still very subtle/unexpected that thi
John Knottenbelt
2012/05/08 11:43:12
I think it should be OK to relax this requirement
Philippe
2012/05/09 11:48:40
I'm waiting for Ryan's feedback before I address t
| |
| 204 shared_state_->delegate_->Stop(); | |
| 205 } | |
| 206 | |
| 207 // static | |
| 208 bool ProxyConfigServiceAndroid::Init(JNIEnv* env) { | |
| 209 return RegisterNativesImpl(env); | |
| 210 } | |
| 211 | |
| 212 void ProxyConfigServiceAndroid::AddObserver(Observer* observer) { | |
| 213 DCHECK(OnObserverThread()); | |
| 214 if (shared_state_->observers_.size() == 0) | |
| 215 shared_state_->delegate_->Start(this); | |
| 216 // Note that any callbacks from the delegate will result in a scheduled task | |
| 217 // on this same thread, so there is no race between delegate_->Start() and | |
| 218 // AddObserver(). | |
| 219 shared_state_->observers_.AddObserver(observer); | |
| 220 } | |
| 221 | |
| 222 void ProxyConfigServiceAndroid::RemoveObserver(Observer* observer) { | |
| 223 DCHECK(OnObserverThread()); | |
| 224 shared_state_->observers_.RemoveObserver(observer); | |
| 225 if (shared_state_->observers_.size() == 0) { | |
| 226 shared_state_->delegate_->Stop(); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 ProxyConfigService::ConfigAvailability | |
| 231 ProxyConfigServiceAndroid::GetLatestProxyConfig(ProxyConfig* config) { | |
| 232 if (!config) | |
| 233 return ProxyConfigService::CONFIG_UNSET; | |
| 234 GetLatestProxyConfigInternal(shared_state_->delegate_.get(), config); | |
| 235 return ProxyConfigService::CONFIG_VALID; | |
| 236 } | |
| 237 | |
| 238 void ProxyConfigServiceAndroid::ProxySettingsChanged() { | |
| 239 observer_loop_->PostTask( | |
| 240 FROM_HERE, base::Bind(&ProxySettingsChangedCallback, shared_state_)); | |
| 241 } | |
| 242 | |
| 243 // static | |
| 244 void ProxyConfigServiceAndroid::ProxySettingsChangedCallback( | |
| 245 scoped_refptr<SharedState> callback_state) { | |
| 246 // Allow another task to be scheduled (before running observers). | |
| 247 ProxyConfig config; | |
| 248 GetLatestProxyConfigInternal(callback_state->delegate_.get(), &config); | |
| 249 FOR_EACH_OBSERVER(Observer, callback_state->observers_, | |
| 250 OnProxyConfigChanged(config, | |
| 251 ProxyConfigService::CONFIG_VALID)); | |
| 252 } | |
| 253 | |
| 254 bool ProxyConfigServiceAndroid::OnObserverThread() const { | |
| 255 return observer_loop_->BelongsToCurrentThread(); | |
| 256 } | |
| 257 | |
| 258 } // namespace net | |
| OLD | NEW |