Merge changes from topic "pacproxy-service"

* changes:
  Make PacProxyService be a system service
  Revert^2 "Refactor setCurrentProxyScriptUrl to a void method"
This commit is contained in:
Aaron Huang
2021-03-15 11:49:45 +00:00
committed by Gerrit Code Review
3 changed files with 37 additions and 13 deletions

View File

@@ -129,7 +129,7 @@ public class ProxyInfo implements Parcelable {
}
/**
* Only used in PacProxyInstaller after Local Proxy is bound.
* Only used in PacProxyService after Local Proxy is bound.
* @hide
*/
public ProxyInfo(@NonNull Uri pacFileUrl, int localProxyPort) {

View File

@@ -4423,7 +4423,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
break;
}
case EVENT_PROXY_HAS_CHANGED: {
handleApplyDefaultProxy((ProxyInfo)msg.obj);
final Pair<Network, ProxyInfo> arg = (Pair<Network, ProxyInfo>) msg.obj;
handleApplyDefaultProxy(arg.second);
break;
}
case EVENT_REGISTER_NETWORK_PROVIDER: {

View File

@@ -27,15 +27,19 @@ import android.annotation.Nullable;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Network;
import android.net.PacProxyManager;
import android.net.Proxy;
import android.net.ProxyInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.GuardedBy;
import com.android.net.module.util.ProxyUtils;
@@ -67,7 +71,7 @@ public class ProxyTracker {
// 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
// in its LinkProperties, or when ConnectivityService switches to a new default network, or
// when PacProxyInstaller resolves the proxy.
// when PacProxyService resolves the proxy.
@Nullable
@GuardedBy("mProxyLock")
private volatile ProxyInfo mDefaultProxy = null;
@@ -77,16 +81,31 @@ public class ProxyTracker {
private final Handler mConnectivityServiceHandler;
// The object responsible for Proxy Auto Configuration (PAC).
@NonNull
private final PacProxyInstaller mPacProxyInstaller;
private final PacProxyManager mPacProxyManager;
private class PacProxyInstalledListener implements PacProxyManager.PacProxyInstalledListener {
private final int mEvent;
PacProxyInstalledListener(int event) {
mEvent = event;
}
public void onPacProxyInstalled(@Nullable Network network, @NonNull ProxyInfo proxy) {
mConnectivityServiceHandler
.sendMessage(mConnectivityServiceHandler
.obtainMessage(mEvent, new Pair<>(network, proxy)));
}
}
public ProxyTracker(@NonNull final Context context,
@NonNull final Handler connectivityServiceInternalHandler, final int pacChangedEvent) {
mContext = context;
mConnectivityServiceHandler = connectivityServiceInternalHandler;
mPacProxyInstaller = new PacProxyInstaller(
context, connectivityServiceInternalHandler, pacChangedEvent);
mPacProxyManager = context.getSystemService(PacProxyManager.class);
PacProxyInstalledListener listener = new PacProxyInstalledListener(pacChangedEvent);
mPacProxyManager.addPacProxyInstalledListener(
new HandlerExecutor(mConnectivityServiceHandler), listener);
}
// Convert empty ProxyInfo's to null as null-checks are used to determine if proxies are present
@@ -182,7 +201,7 @@ public class ProxyTracker {
if (!TextUtils.isEmpty(pacFileUrl)) {
mConnectivityServiceHandler.post(
() -> mPacProxyInstaller.setCurrentProxyScriptUrl(proxyProperties));
() -> mPacProxyManager.setCurrentProxyScriptUrl(proxyProperties));
}
}
}
@@ -226,9 +245,9 @@ public class ProxyTracker {
final ProxyInfo defaultProxy = getDefaultProxy();
final ProxyInfo proxyInfo = null != defaultProxy ?
defaultProxy : ProxyInfo.buildDirectProxy("", 0, Collections.emptyList());
mPacProxyManager.setCurrentProxyScriptUrl(proxyInfo);
if (mPacProxyInstaller.setCurrentProxyScriptUrl(proxyInfo)
== PacProxyInstaller.DONT_SEND_BROADCAST) {
if (!shouldSendBroadcast(proxyInfo)) {
return;
}
if (DBG) Log.d(TAG, "sending Proxy Broadcast for " + proxyInfo);
@@ -244,6 +263,10 @@ public class ProxyTracker {
}
}
private boolean shouldSendBroadcast(ProxyInfo proxy) {
return Uri.EMPTY.equals(proxy.getPacFileUrl()) || proxy.getPort() > 0;
}
/**
* Sets the global proxy in memory. Also writes the values to the global settings of the device.
*
@@ -308,10 +331,10 @@ public class ProxyTracker {
return;
}
// This call could be coming from the PacProxyInstaller, containing the port of the
// This call could be coming from the PacProxyService, containing the port of the
// local proxy. If this new proxy matches the global proxy then copy this proxy to the
// global (to get the correct local port), and send a broadcast.
// TODO: Switch PacProxyInstaller to have its own message to send back rather than
// TODO: Switch PacProxyService to have its own message to send back rather than
// reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy.
if ((mGlobalProxy != null) && (proxyInfo != null)
&& (!Uri.EMPTY.equals(proxyInfo.getPacFileUrl()))