Chromium Code Reviews| Index: net/android/java/src/org/chromium/net/ProxyChangeListener.java |
| diff --git a/net/android/java/src/org/chromium/net/ProxyChangeListener.java b/net/android/java/src/org/chromium/net/ProxyChangeListener.java |
| index 9c59bcccf6df36c4ed2032a3d9b517656a89fb3d..61dc1241c68d40df439e811c0dfaeed9c590d308 100644 |
| --- a/net/android/java/src/org/chromium/net/ProxyChangeListener.java |
| +++ b/net/android/java/src/org/chromium/net/ProxyChangeListener.java |
| @@ -10,6 +10,8 @@ import android.content.Intent; |
| import android.content.IntentFilter; |
| import android.net.Proxy; |
| +import java.lang.reflect.Method; |
| + |
| import org.chromium.base.CalledByNative; |
| import org.chromium.base.JNINamespace; |
| import org.chromium.base.NativeClassQualifiedName; |
| @@ -26,6 +28,15 @@ public class ProxyChangeListener { |
| private ProxyReceiver mProxyReceiver; |
| private Delegate mDelegate; |
| + private static class ProxyConfig { |
| + public ProxyConfig(String host, int port) { |
| + mHost = host; |
| + mPort = port; |
| + } |
| + public final String mHost; |
| + public final int mPort; |
| + }; |
| + |
| public interface Delegate { |
| public void proxySettingsChanged(); |
| } |
| @@ -69,12 +80,43 @@ public class ProxyChangeListener { |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { |
| - proxySettingsChanged(); |
| + ProxyConfig cfg = extractNewProxy(intent); |
|
cbentzel
2013/10/15 18:59:42
Do older versions of Android set a "proxy" object
Elly Fong-Jones
2013/10/15 19:50:42
All versions ICS and later do.
|
| + if (cfg != null) { |
| + proxySettingsChanged(cfg); |
| + } |
| + } |
| + } |
| + |
| + // Extract a ProxyConfig object from the supplied Intent's extra data |
| + // bundle. The android.net.ProxyProperties class is not exported from |
| + // tne Android SDK, so we have to use reflection to get at it and invoke |
| + // methods on it. |
| + // TODO(ellyjones): once android.net.ProxyProperties is exported, |
| + // rewrite this. |
| + private ProxyConfig extractNewProxy(Intent intent) { |
|
cbentzel
2013/10/15 18:59:42
How hard would it be to test this?
Elly Fong-Jones
2013/10/15 19:50:42
Not difficult per se, just ugly. It would basicall
|
| + try { |
| + final String CLASS_NAME = "android.net.ProxyProperties"; |
| + final String GET_HOST_NAME = "getHost"; |
| + final String GET_PORT_NAME = "getPort"; |
| + Object props = intent.getExtras().get("proxy"); |
| + if (props == null) { |
| + return null; |
| + } |
| + Class cls = Class.forName(CLASS_NAME); |
| + Method getHostMethod = cls.getDeclaredMethod(GET_HOST_NAME); |
| + Method getPortMethod = cls.getDeclaredMethod(GET_PORT_NAME); |
| + |
| + String host = (String)getHostMethod.invoke(props); |
|
cbentzel
2013/10/15 18:59:42
Stupid java question: what happens if the getHost
Elly Fong-Jones
2013/10/15 19:50:42
I don't know. pliard?
Philippe
2013/10/15 20:23:37
This would indeed throw an InvalidCastException (t
|
| + int port = (Integer)getPortMethod.invoke(props); |
| + |
| + return new ProxyConfig(host, port); |
| + } catch (Exception ex) { |
| + return null; |
| } |
| } |
| } |
| - private void proxySettingsChanged() { |
| + private void proxySettingsChanged(ProxyConfig cfg) { |
| if (!sEnabled) { |
| return; |
| } |
| @@ -86,7 +128,7 @@ public class ProxyChangeListener { |
| } |
| // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but |
| // the C++ code must run the callbacks on the network thread. |
| - nativeProxySettingsChanged(mNativePtr); |
| + nativeProxySettingsChanged(mNativePtr, cfg.mHost, cfg.mPort); |
| } |
| private void registerReceiver() { |
| @@ -111,5 +153,7 @@ public class ProxyChangeListener { |
| * See net/proxy/proxy_config_service_android.cc |
| */ |
| @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") |
| - private native void nativeProxySettingsChanged(int nativePtr); |
| + private native void nativeProxySettingsChanged(int nativePtr, |
| + String host, |
| + int port); |
| } |