Merge "[PT13] Simple cleanup of ProxyTracker."

This commit is contained in:
Chalard Jean
2018-10-12 07:34:43 +00:00
committed by Gerrit Code Review

View File

@@ -16,6 +16,12 @@
package com.android.server.connectivity; package com.android.server.connectivity;
import static android.provider.Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST;
import static android.provider.Settings.Global.GLOBAL_HTTP_PROXY_HOST;
import static android.provider.Settings.Global.GLOBAL_HTTP_PROXY_PAC;
import static android.provider.Settings.Global.GLOBAL_HTTP_PROXY_PORT;
import static android.provider.Settings.Global.HTTP_PROXY;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.ContentResolver; import android.content.ContentResolver;
@@ -47,16 +53,14 @@ public class ProxyTracker {
@NonNull @NonNull
private final Context mContext; private final Context mContext;
// TODO : make this private and import as much managing logic from ConnectivityService as
// possible
@NonNull @NonNull
public final Object mProxyLock = new Object(); private final Object mProxyLock = new Object();
// The global proxy is the proxy that is set device-wide, overriding any network-specific // The global proxy is the proxy that is set device-wide, overriding any network-specific
// proxy. Note however that proxies are hints ; the system does not enforce their use. Hence // proxy. Note however that proxies are hints ; the system does not enforce their use. Hence
// this value is only for querying. // this value is only for querying.
@Nullable @Nullable
@GuardedBy("mProxyLock") @GuardedBy("mProxyLock")
public ProxyInfo mGlobalProxy = null; private ProxyInfo mGlobalProxy = null;
// The default proxy is the proxy that applies to no particular network if the global proxy // The default proxy is the proxy that applies to no particular network if the global proxy
// is not set. Individual networks have their own settings that override this. This member // is not set. Individual networks have their own settings that override this. This member
// is set through setDefaultProxy, which is called when the default network changes proxies // is set through setDefaultProxy, which is called when the default network changes proxies
@@ -64,10 +68,10 @@ public class ProxyTracker {
// when PacManager resolves the proxy. // when PacManager resolves the proxy.
@Nullable @Nullable
@GuardedBy("mProxyLock") @GuardedBy("mProxyLock")
public volatile ProxyInfo mDefaultProxy = null; private volatile ProxyInfo mDefaultProxy = null;
// Whether the default proxy is disabled. TODO : make this mDefaultProxyEnabled // Whether the default proxy is enabled.
@GuardedBy("mProxyLock") @GuardedBy("mProxyLock")
public boolean mDefaultProxyDisabled = false; private boolean mDefaultProxyEnabled = true;
// The object responsible for Proxy Auto Configuration (PAC). // The object responsible for Proxy Auto Configuration (PAC).
@NonNull @NonNull
@@ -85,7 +89,7 @@ public class ProxyTracker {
@Nullable @Nullable
private static ProxyInfo canonicalizeProxyInfo(@Nullable final ProxyInfo proxy) { private static ProxyInfo canonicalizeProxyInfo(@Nullable final ProxyInfo proxy) {
if (proxy != null && TextUtils.isEmpty(proxy.getHost()) if (proxy != null && TextUtils.isEmpty(proxy.getHost())
&& (proxy.getPacFileUrl() == null || Uri.EMPTY.equals(proxy.getPacFileUrl()))) { && Uri.EMPTY.equals(proxy.getPacFileUrl())) {
return null; return null;
} }
return proxy; return proxy;
@@ -123,7 +127,7 @@ public class ProxyTracker {
// This information is already available as a world read/writable jvm property. // This information is already available as a world read/writable jvm property.
synchronized (mProxyLock) { synchronized (mProxyLock) {
final ProxyInfo ret = mGlobalProxy; final ProxyInfo ret = mGlobalProxy;
if ((ret == null) && !mDefaultProxyDisabled) return mDefaultProxy; if ((ret == null) && mDefaultProxyEnabled) return mDefaultProxy;
return ret; return ret;
} }
} }
@@ -146,11 +150,10 @@ public class ProxyTracker {
*/ */
public void loadGlobalProxy() { public void loadGlobalProxy() {
ContentResolver res = mContext.getContentResolver(); ContentResolver res = mContext.getContentResolver();
String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); String host = Settings.Global.getString(res, GLOBAL_HTTP_PROXY_HOST);
int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); int port = Settings.Global.getInt(res, GLOBAL_HTTP_PROXY_PORT, 0);
String exclList = Settings.Global.getString(res, String exclList = Settings.Global.getString(res, GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); String pacFileUrl = Settings.Global.getString(res, GLOBAL_HTTP_PROXY_PAC);
String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC);
if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) {
ProxyInfo proxyProperties; ProxyInfo proxyProperties;
if (!TextUtils.isEmpty(pacFileUrl)) { if (!TextUtils.isEmpty(pacFileUrl)) {
@@ -175,8 +178,7 @@ public class ProxyTracker {
* Read the global proxy from the deprecated Settings.Global.HTTP_PROXY setting and apply it. * Read the global proxy from the deprecated Settings.Global.HTTP_PROXY setting and apply it.
*/ */
public void loadDeprecatedGlobalHttpProxy() { public void loadDeprecatedGlobalHttpProxy() {
final String proxy = Settings.Global.getString(mContext.getContentResolver(), final String proxy = Settings.Global.getString(mContext.getContentResolver(), HTTP_PROXY);
Settings.Global.HTTP_PROXY);
if (!TextUtils.isEmpty(proxy)) { if (!TextUtils.isEmpty(proxy)) {
String data[] = proxy.split(":"); String data[] = proxy.split(":");
if (data.length == 0) { if (data.length == 0) {
@@ -259,11 +261,10 @@ public class ProxyTracker {
final ContentResolver res = mContext.getContentResolver(); final ContentResolver res = mContext.getContentResolver();
final long token = Binder.clearCallingIdentity(); final long token = Binder.clearCallingIdentity();
try { try {
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); Settings.Global.putString(res, GLOBAL_HTTP_PROXY_HOST, host);
Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); Settings.Global.putInt(res, GLOBAL_HTTP_PROXY_PORT, port);
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, Settings.Global.putString(res, GLOBAL_HTTP_PROXY_EXCLUSION_LIST, exclList);
exclList); Settings.Global.putString(res, GLOBAL_HTTP_PROXY_PAC, pacFileUrl);
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl);
} finally { } finally {
Binder.restoreCallingIdentity(token); Binder.restoreCallingIdentity(token);
} }
@@ -280,10 +281,7 @@ public class ProxyTracker {
*/ */
public void setDefaultProxy(@Nullable ProxyInfo proxyInfo) { public void setDefaultProxy(@Nullable ProxyInfo proxyInfo) {
synchronized (mProxyLock) { synchronized (mProxyLock) {
if (mDefaultProxy != null && mDefaultProxy.equals(proxyInfo)) { if (Objects.equals(mDefaultProxy, proxyInfo)) return;
return;
}
if (mDefaultProxy == proxyInfo) return; // catches repeated nulls
if (proxyInfo != null && !proxyInfo.isValid()) { if (proxyInfo != null && !proxyInfo.isValid()) {
if (DBG) Slog.d(TAG, "Invalid proxy properties, ignoring: " + proxyInfo); if (DBG) Slog.d(TAG, "Invalid proxy properties, ignoring: " + proxyInfo);
return; return;
@@ -304,7 +302,7 @@ public class ProxyTracker {
mDefaultProxy = proxyInfo; mDefaultProxy = proxyInfo;
if (mGlobalProxy != null) return; if (mGlobalProxy != null) return;
if (!mDefaultProxyDisabled) { if (mDefaultProxyEnabled) {
sendProxyBroadcast(proxyInfo); sendProxyBroadcast(proxyInfo);
} }
} }
@@ -319,8 +317,8 @@ public class ProxyTracker {
*/ */
public void setDefaultProxyEnabled(final boolean enabled) { public void setDefaultProxyEnabled(final boolean enabled) {
synchronized (mProxyLock) { synchronized (mProxyLock) {
if (mDefaultProxyDisabled == enabled) { if (mDefaultProxyEnabled != enabled) {
mDefaultProxyDisabled = !enabled; mDefaultProxyEnabled = enabled;
if (mGlobalProxy == null && mDefaultProxy != null) { if (mGlobalProxy == null && mDefaultProxy != null) {
sendProxyBroadcast(enabled ? mDefaultProxy : null); sendProxyBroadcast(enabled ? mDefaultProxy : null);
} }