Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.BroadcastReceiver; | 7 import android.content.BroadcastReceiver; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.IntentFilter; | 10 import android.content.IntentFilter; |
| 11 import android.net.Proxy; | 11 import android.net.Proxy; |
| 12 import android.util.Log; | |
| 13 | |
| 14 import java.lang.reflect.Method; | |
| 15 import java.util.Set; | |
|
Philippe
2013/10/15 14:33:21
Nit: nor that one.
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 12 | 16 |
| 13 import org.chromium.base.CalledByNative; | 17 import org.chromium.base.CalledByNative; |
| 14 import org.chromium.base.JNINamespace; | 18 import org.chromium.base.JNINamespace; |
| 15 import org.chromium.base.NativeClassQualifiedName; | 19 import org.chromium.base.NativeClassQualifiedName; |
| 16 | 20 |
| 17 // This class partners with native ProxyConfigServiceAndroid to listen for | 21 // This class partners with native ProxyConfigServiceAndroid to listen for |
| 18 // proxy change notifications from Android. | 22 // proxy change notifications from Android. |
| 19 @JNINamespace("net") | 23 @JNINamespace("net") |
| 20 public class ProxyChangeListener { | 24 public class ProxyChangeListener { |
| 21 private static final String TAG = "ProxyChangeListener"; | 25 private static final String TAG = "ProxyChangeListener"; |
| 22 private static boolean sEnabled = true; | 26 private static boolean sEnabled = true; |
| 23 | 27 |
| 24 private int mNativePtr; | 28 private int mNativePtr; |
| 25 private Context mContext; | 29 private Context mContext; |
| 26 private ProxyReceiver mProxyReceiver; | 30 private ProxyReceiver mProxyReceiver; |
| 27 private Delegate mDelegate; | 31 private Delegate mDelegate; |
| 28 | 32 |
| 33 private class ProxyConfig { | |
|
Philippe
2013/10/15 14:33:21
Nit: you can make this 'private static class' sinc
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 34 public ProxyConfig(String host, int port) { | |
| 35 host_ = host; | |
| 36 port_ = port; | |
| 37 } | |
| 38 public String host_; | |
|
cbentzel
2013/10/15 12:29:07
Nit: Java uses mHost rather than host_;
FindBugs
Philippe
2013/10/15 14:33:21
+1
Also consider either making those fields 'fina
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 39 public int port_; | |
| 40 }; | |
| 41 | |
| 29 public interface Delegate { | 42 public interface Delegate { |
| 30 public void proxySettingsChanged(); | 43 public void proxySettingsChanged(); |
| 31 } | 44 } |
| 32 | 45 |
| 33 private ProxyChangeListener(Context context) { | 46 private ProxyChangeListener(Context context) { |
| 34 mContext = context; | 47 mContext = context; |
| 35 } | 48 } |
| 36 | 49 |
| 37 public static void setEnabled(boolean enabled) { | 50 public static void setEnabled(boolean enabled) { |
| 38 sEnabled = enabled; | 51 sEnabled = enabled; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 59 registerReceiver(); | 72 registerReceiver(); |
| 60 } | 73 } |
| 61 | 74 |
| 62 @CalledByNative | 75 @CalledByNative |
| 63 public void stop() { | 76 public void stop() { |
| 64 mNativePtr = 0; | 77 mNativePtr = 0; |
| 65 unregisterReceiver(); | 78 unregisterReceiver(); |
| 66 } | 79 } |
| 67 | 80 |
| 68 private class ProxyReceiver extends BroadcastReceiver { | 81 private class ProxyReceiver extends BroadcastReceiver { |
| 82 | |
|
Philippe
2013/10/15 14:33:21
Nit: other places in this file don't have this bla
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 69 @Override | 83 @Override |
| 70 public void onReceive(Context context, Intent intent) { | 84 public void onReceive(Context context, Intent intent) { |
| 71 if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { | 85 if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { |
| 72 proxySettingsChanged(); | 86 ProxyConfig cfg = extractNewProxy(intent); |
| 87 if (cfg != null) { | |
| 88 proxySettingsChanged(cfg); | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // Extract a ProxyConfig object from the supplied Intent's extra data | |
| 94 // bundle. The android.net.ProxyProperties class is not exported from | |
| 95 // tne Android SDK, so we have to use reflection to get at it and invoke | |
| 96 // methods on it. | |
| 97 // TODO(ellyjones): once android.net.ProxyProperties is exported, | |
| 98 // rewrite this. | |
| 99 private ProxyConfig extractNewProxy(Intent intent) { | |
|
Philippe
2013/10/15 14:33:21
Nit: you can make this method 'static' if you neve
Elly Fong-Jones
2013/10/15 16:37:15
Alas:
../net/android/java/src/org/chromium/net/Pr
Philippe
2013/10/15 16:45:59
Wow, didn't know about that!
| |
| 100 try { | |
| 101 final String kClassName = "android.net.ProxyProperties"; | |
|
Philippe
2013/10/15 14:33:21
Nit: I believe we use capital letters for constant
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 102 final String kGetHostName = "getHost"; | |
| 103 final String kGetPortName = "getPort"; | |
| 104 Object props = intent.getExtras().get("proxy"); | |
| 105 if (props == null) { | |
| 106 return null; | |
| 107 } | |
| 108 Class cls = Class.forName(kClassName); | |
| 109 Method gethost = cls.getDeclaredMethod(kGetHostName); | |
|
Philippe
2013/10/15 14:33:21
Nit: s/gethost/getHostMethod maybe (same below).
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 110 Method getport = cls.getDeclaredMethod(kGetPortName); | |
| 111 | |
| 112 Object host_obj = gethost.invoke(props); | |
|
Philippe
2013/10/15 14:33:21
Nit: feel free to drop these two lines (that shoul
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 113 Object port_obj = getport.invoke(props); | |
| 114 | |
| 115 String host = (String)host_obj; | |
| 116 int port = ((Integer)port_obj); | |
| 117 | |
| 118 return new ProxyConfig(host, port); | |
| 119 } catch (Exception ex) { | |
| 120 return null; | |
| 73 } | 121 } |
| 74 } | 122 } |
| 75 } | 123 } |
| 76 | 124 |
| 77 private void proxySettingsChanged() { | 125 private void proxySettingsChanged(ProxyConfig cfg) { |
| 78 if (!sEnabled) { | 126 if (!sEnabled) { |
| 79 return; | 127 return; |
| 80 } | 128 } |
| 81 if (mDelegate != null) { | 129 if (mDelegate != null) { |
| 82 mDelegate.proxySettingsChanged(); | 130 mDelegate.proxySettingsChanged(); |
| 83 } | 131 } |
| 84 if (mNativePtr == 0) { | 132 if (mNativePtr == 0) { |
| 85 return; | 133 return; |
| 86 } | 134 } |
| 87 // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but | 135 // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but |
| 88 // the C++ code must run the callbacks on the network thread. | 136 // the C++ code must run the callbacks on the network thread. |
| 89 nativeProxySettingsChanged(mNativePtr); | 137 nativeProxySettingsChanged(mNativePtr, cfg); |
| 90 } | 138 } |
| 91 | 139 |
| 92 private void registerReceiver() { | 140 private void registerReceiver() { |
| 93 if (mProxyReceiver != null) { | 141 if (mProxyReceiver != null) { |
| 94 return; | 142 return; |
| 95 } | 143 } |
| 96 IntentFilter filter = new IntentFilter(); | 144 IntentFilter filter = new IntentFilter(); |
| 97 filter.addAction(Proxy.PROXY_CHANGE_ACTION); | 145 filter.addAction(Proxy.PROXY_CHANGE_ACTION); |
| 98 mProxyReceiver = new ProxyReceiver(); | 146 mProxyReceiver = new ProxyReceiver(); |
| 99 mContext.getApplicationContext().registerReceiver(mProxyReceiver, filter ); | 147 mContext.getApplicationContext().registerReceiver(mProxyReceiver, filter ); |
| 100 } | 148 } |
| 101 | 149 |
| 102 private void unregisterReceiver() { | 150 private void unregisterReceiver() { |
| 103 if (mProxyReceiver == null) { | 151 if (mProxyReceiver == null) { |
| 104 return; | 152 return; |
| 105 } | 153 } |
| 106 mContext.unregisterReceiver(mProxyReceiver); | 154 mContext.unregisterReceiver(mProxyReceiver); |
| 107 mProxyReceiver = null; | 155 mProxyReceiver = null; |
| 108 } | 156 } |
| 109 | 157 |
| 110 /** | 158 /** |
| 111 * See net/proxy/proxy_config_service_android.cc | 159 * See net/proxy/proxy_config_service_android.cc |
| 112 */ | 160 */ |
| 113 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") | 161 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") |
| 114 private native void nativeProxySettingsChanged(int nativePtr); | 162 private native void nativeProxySettingsChanged(int nativePtr, |
| 163 ProxyConfig cfg); | |
|
Philippe
2013/10/15 14:33:21
We only pass PODs or Java "primitive" types (e.g.
Elly Fong-Jones
2013/10/15 16:37:15
Done.
| |
| 115 } | 164 } |
| OLD | NEW |