OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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; |
(...skipping 18 matching lines...) Expand all Loading... |
29 public class ProxyChangeListener { | 29 public class ProxyChangeListener { |
30 private static final String TAG = "ProxyChangeListener"; | 30 private static final String TAG = "ProxyChangeListener"; |
31 private static boolean sEnabled = true; | 31 private static boolean sEnabled = true; |
32 | 32 |
33 private long mNativePtr; | 33 private long mNativePtr; |
34 private Context mContext; | 34 private Context mContext; |
35 private ProxyReceiver mProxyReceiver; | 35 private ProxyReceiver mProxyReceiver; |
36 private Delegate mDelegate; | 36 private Delegate mDelegate; |
37 | 37 |
38 private static class ProxyConfig { | 38 private static class ProxyConfig { |
39 public ProxyConfig(String host, int port, String pacUrl) { | 39 public ProxyConfig(String host, int port, String pacUrl, String[] exclus
ionList) { |
40 mHost = host; | 40 mHost = host; |
41 mPort = port; | 41 mPort = port; |
42 mPacUrl = pacUrl; | 42 mPacUrl = pacUrl; |
| 43 mExclusionList = exclusionList; |
43 } | 44 } |
44 public final String mHost; | 45 public final String mHost; |
45 public final int mPort; | 46 public final int mPort; |
46 public final String mPacUrl; | 47 public final String mPacUrl; |
| 48 public final String[] mExclusionList; |
47 } | 49 } |
48 | 50 |
| 51 /** |
| 52 * The delegate for ProxyChangeListener. Use for testing. |
| 53 */ |
49 public interface Delegate { | 54 public interface Delegate { |
50 public void proxySettingsChanged(); | 55 public void proxySettingsChanged(); |
51 } | 56 } |
52 | 57 |
53 private ProxyChangeListener(Context context) { | 58 private ProxyChangeListener(Context context) { |
54 mContext = context; | 59 mContext = context; |
55 } | 60 } |
56 | 61 |
57 public static void setEnabled(boolean enabled) { | 62 public static void setEnabled(boolean enabled) { |
58 sEnabled = enabled; | 63 sEnabled = enabled; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 // bundle. The android.net.ProxyProperties class is not exported from | 102 // bundle. The android.net.ProxyProperties class is not exported from |
98 // the Android SDK, so we have to use reflection to get at it and invoke | 103 // the Android SDK, so we have to use reflection to get at it and invoke |
99 // methods on it. If we fail, return an empty proxy config (meaning | 104 // methods on it. If we fail, return an empty proxy config (meaning |
100 // 'direct'). | 105 // 'direct'). |
101 // TODO(sgurun): once android.net.ProxyInfo is public, rewrite this. | 106 // TODO(sgurun): once android.net.ProxyInfo is public, rewrite this. |
102 private ProxyConfig extractNewProxy(Intent intent) { | 107 private ProxyConfig extractNewProxy(Intent intent) { |
103 try { | 108 try { |
104 final String getHostName = "getHost"; | 109 final String getHostName = "getHost"; |
105 final String getPortName = "getPort"; | 110 final String getPortName = "getPort"; |
106 final String getPacFileUrl = "getPacFileUrl"; | 111 final String getPacFileUrl = "getPacFileUrl"; |
| 112 final String getExclusionList = "getExclusionList"; |
107 String className; | 113 String className; |
108 String proxyInfo; | 114 String proxyInfo; |
109 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { | 115 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { |
110 className = "android.net.ProxyProperties"; | 116 className = "android.net.ProxyProperties"; |
111 proxyInfo = "proxy"; | 117 proxyInfo = "proxy"; |
112 } else { | 118 } else { |
113 className = "android.net.ProxyInfo"; | 119 className = "android.net.ProxyInfo"; |
114 proxyInfo = "android.intent.extra.PROXY_INFO"; | 120 proxyInfo = "android.intent.extra.PROXY_INFO"; |
115 } | 121 } |
116 | 122 |
117 Object props = intent.getExtras().get(proxyInfo); | 123 Object props = intent.getExtras().get(proxyInfo); |
118 if (props == null) { | 124 if (props == null) { |
119 return null; | 125 return null; |
120 } | 126 } |
121 | 127 |
122 Class<?> cls = Class.forName(className); | 128 Class<?> cls = Class.forName(className); |
123 Method getHostMethod = cls.getDeclaredMethod(getHostName); | 129 Method getHostMethod = cls.getDeclaredMethod(getHostName); |
124 Method getPortMethod = cls.getDeclaredMethod(getPortName); | 130 Method getPortMethod = cls.getDeclaredMethod(getPortName); |
| 131 Method getExclusionListMethod = cls.getDeclaredMethod(getExclusi
onList); |
125 | 132 |
126 String host = (String) getHostMethod.invoke(props); | 133 String host = (String) getHostMethod.invoke(props); |
127 int port = (Integer) getPortMethod.invoke(props); | 134 int port = (Integer) getPortMethod.invoke(props); |
128 | 135 |
| 136 String[] exclusionList; |
| 137 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { |
| 138 String s = (String) getExclusionListMethod.invoke(props); |
| 139 exclusionList = s.split(","); |
| 140 } else { |
| 141 exclusionList = (String[]) getExclusionListMethod.invoke(pro
ps); |
| 142 } |
129 // TODO(xunjieli): rewrite this once the API is public. | 143 // TODO(xunjieli): rewrite this once the API is public. |
130 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { | 144 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { |
131 Method getPacFileUrlMethod = | 145 Method getPacFileUrlMethod = |
132 cls.getDeclaredMethod(getPacFileUrl); | 146 cls.getDeclaredMethod(getPacFileUrl); |
133 String pacFileUrl = (String) getPacFileUrlMethod.invoke(prop
s); | 147 String pacFileUrl = (String) getPacFileUrlMethod.invoke(prop
s); |
134 if (!TextUtils.isEmpty(pacFileUrl)) { | 148 if (!TextUtils.isEmpty(pacFileUrl)) { |
135 return new ProxyConfig(host, port, pacFileUrl); | 149 return new ProxyConfig(host, port, pacFileUrl, exclusionL
ist); |
136 } | 150 } |
137 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { | 151 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { |
138 Method getPacFileUrlMethod = | 152 Method getPacFileUrlMethod = |
139 cls.getDeclaredMethod(getPacFileUrl); | 153 cls.getDeclaredMethod(getPacFileUrl); |
140 Uri pacFileUrl = (Uri) getPacFileUrlMethod.invoke(props); | 154 Uri pacFileUrl = (Uri) getPacFileUrlMethod.invoke(props); |
141 if (!Uri.EMPTY.equals(pacFileUrl)) { | 155 if (!Uri.EMPTY.equals(pacFileUrl)) { |
142 return new ProxyConfig(host, port, pacFileUrl.toString()); | 156 return new ProxyConfig(host, port, pacFileUrl.toString(),
exclusionList); |
143 } | 157 } |
144 } | 158 } |
145 return new ProxyConfig(host, port, null); | 159 return new ProxyConfig(host, port, null, exclusionList); |
146 } catch (ClassNotFoundException ex) { | 160 } catch (ClassNotFoundException ex) { |
147 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); | 161 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); |
148 return null; | 162 return null; |
149 } catch (NoSuchMethodException ex) { | 163 } catch (NoSuchMethodException ex) { |
150 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); | 164 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); |
151 return null; | 165 return null; |
152 } catch (IllegalAccessException ex) { | 166 } catch (IllegalAccessException ex) { |
153 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); | 167 Log.e(TAG, "Using no proxy configuration due to exception:" + ex
); |
154 return null; | 168 return null; |
155 } catch (InvocationTargetException ex) { | 169 } catch (InvocationTargetException ex) { |
(...skipping 12 matching lines...) Expand all Loading... |
168 } | 182 } |
169 if (mDelegate != null) { | 183 if (mDelegate != null) { |
170 mDelegate.proxySettingsChanged(); | 184 mDelegate.proxySettingsChanged(); |
171 } | 185 } |
172 if (mNativePtr == 0) { | 186 if (mNativePtr == 0) { |
173 return; | 187 return; |
174 } | 188 } |
175 // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but | 189 // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but |
176 // the C++ code must run the callbacks on the network thread. | 190 // the C++ code must run the callbacks on the network thread. |
177 if (cfg != null) { | 191 if (cfg != null) { |
178 nativeProxySettingsChangedTo(mNativePtr, cfg.mHost, cfg.mPort, cfg.m
PacUrl); | 192 nativeProxySettingsChangedTo(mNativePtr, cfg.mHost, cfg.mPort, cfg.m
PacUrl, |
| 193 cfg.mExclusionList); |
179 } else { | 194 } else { |
180 nativeProxySettingsChanged(mNativePtr); | 195 nativeProxySettingsChanged(mNativePtr); |
181 } | 196 } |
182 } | 197 } |
183 | 198 |
184 private void registerReceiver() { | 199 private void registerReceiver() { |
185 if (mProxyReceiver != null) { | 200 if (mProxyReceiver != null) { |
186 return; | 201 return; |
187 } | 202 } |
188 IntentFilter filter = new IntentFilter(); | 203 IntentFilter filter = new IntentFilter(); |
(...skipping 10 matching lines...) Expand all Loading... |
199 mProxyReceiver = null; | 214 mProxyReceiver = null; |
200 } | 215 } |
201 | 216 |
202 /** | 217 /** |
203 * See net/proxy/proxy_config_service_android.cc | 218 * See net/proxy/proxy_config_service_android.cc |
204 */ | 219 */ |
205 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") | 220 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") |
206 private native void nativeProxySettingsChangedTo(long nativePtr, | 221 private native void nativeProxySettingsChangedTo(long nativePtr, |
207 String host, | 222 String host, |
208 int port, | 223 int port, |
209 String pacUrl); | 224 String pacUrl, |
210 | 225 String[] exclusionList); |
211 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") | 226 @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate") |
212 private native void nativeProxySettingsChanged(long nativePtr); | 227 private native void nativeProxySettingsChanged(long nativePtr); |
213 } | 228 } |
OLD | NEW |