Merge changes I4bc29b10,I94a56b2b,I9ca025b1 into main

* changes:
  Add a queue system server-side for NetworkAgent
  Unify registration and NM assignment in NAI
  Return the registry in registerNetworkAgent.
This commit is contained in:
Jean Chalard 2025-03-16 15:57:51 -07:00 • committed by Gerrit Code Review
commit f04de0efc1

View file

@ -228,6 +228,7 @@ import android.net.NattSocketKeepalive;
import android.net.Network;
import android.net.NetworkAgent;
import android.net.NetworkAgentConfig;
import android.net.NetworkAndAgentRegistryParcelable;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
@ -526,6 +527,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private final boolean mBackgroundFirewallChainEnabled;
private final boolean mUseDeclaredMethodsForCallbacksEnabled;
private final boolean mQueueNetworkAgentEventsInSystemServer;
// Flag to delay callbacks for frozen apps, suppressing duplicate and stale callbacks.
private final boolean mQueueCallbacksForFrozenApps;
@ -1928,6 +1930,9 @@ public class ConnectivityService extends IConnectivityManager.Stub {
mUseDeclaredMethodsForCallbacksEnabled =
mDeps.isFeatureNotChickenedOut(context,
ConnectivityFlags.USE_DECLARED_METHODS_FOR_CALLBACKS);
mQueueNetworkAgentEventsInSystemServer =
mDeps.isFeatureNotChickenedOut(context,
ConnectivityFlags.QUEUE_NETWORK_AGENT_EVENTS_IN_SYSTEM_SERVER);
// registerUidFrozenStateChangedCallback is only available on U+
mQueueCallbacksForFrozenApps = mDeps.isAtLeastU()
&& mDeps.isFeatureNotChickenedOut(context, QUEUE_CALLBACKS_FOR_FROZEN_APPS);
@ -4688,18 +4693,30 @@ public class ConnectivityService extends IConnectivityManager.Stub {
private void maybeHandleNetworkAgentMessage(Message msg) {
final Pair<NetworkAgentInfo, Object> arg = (Pair<NetworkAgentInfo, Object>) msg.obj;
final NetworkAgentInfo nai = arg.first;
if (!mNetworkAgentInfos.contains(nai)) {
if (VDBG) {
log(String.format("%s from unknown NetworkAgent", eventName(msg.what)));
}
return;
}
// If the network has been destroyed, the only thing that it can do is disconnect.
if (nai.isDestroyed() && !isDisconnectRequest(msg)) {
return;
}
if (mQueueNetworkAgentEventsInSystemServer && nai.maybeEnqueueMessage(msg)) {
// If the message is enqueued, the NAI will replay it immediately
// when registration is complete. It does this by sending all the
// messages in the order received immediately after the
// EVENT_AGENT_REGISTERED message.
return;
}
// If the nai has been registered (and doesn't enqueue), it should now be
// in the list of NAIs.
if (!mNetworkAgentInfos.contains(nai)) {
// TODO : this is supposed to be impossible
if (VDBG) {
log(String.format("%s from unknown NetworkAgent", eventName(msg.what)));
}
return;
}
switch (msg.what) {
case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: {
final NetworkCapabilities proposed = (NetworkCapabilities) arg.second;
@ -9328,7 +9345,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
* @param providerId the ID of the provider owning this NetworkAgent.
* @return the network created for this agent.
*/
public Network registerNetworkAgent(INetworkAgent na,
public NetworkAndAgentRegistryParcelable registerNetworkAgent(INetworkAgent na,
NetworkInfo networkInfo,
LinkProperties linkProperties,
NetworkCapabilities networkCapabilities,
@ -9371,7 +9388,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
}
}
private Network registerNetworkAgentInternal(INetworkAgent na, NetworkInfo networkInfo,
private NetworkAndAgentRegistryParcelable registerNetworkAgentInternal(
INetworkAgent na, NetworkInfo networkInfo,
LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
NetworkScore currentScore, NetworkAgentConfig networkAgentConfig,
@Nullable LocalNetworkConfig localNetworkConfig, int providerId,
@ -9403,8 +9421,11 @@ public class ConnectivityService extends IConnectivityManager.Stub {
// NetworkAgentInfo registration will finish when the NetworkMonitor is created.
// If the network disconnects or sends any other event before that, messages are deferred by
// NetworkAgent until nai.connect(), which will be called when finalizing the
// registration.
return nai.network;
// registration. TODO : have NetworkAgentInfo defer them instead.
final NetworkAndAgentRegistryParcelable result = new NetworkAndAgentRegistryParcelable();
result.network = nai.network;
result.registry = nai.getRegistry();
return result;
}
private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) {
@ -9416,8 +9437,6 @@ public class ConnectivityService extends IConnectivityManager.Stub {
nai.getDeclaredCapabilitiesSanitized(mCarrierPrivilegeAuthenticator)));
processLinkPropertiesFromAgent(nai, nai.linkProperties);
nai.onNetworkMonitorCreated(networkMonitor);
mNetworkAgentInfos.add(nai);
synchronized (mNetworkForNetId) {
mNetworkForNetId.put(nai.network.getNetId(), nai);
@ -9432,7 +9451,7 @@ public class ConnectivityService extends IConnectivityManager.Stub {
if (nai.isLocalNetwork()) {
handleUpdateLocalNetworkConfig(nai, null /* oldConfig */, nai.localNetworkConfig);
}
nai.notifyRegistered();
nai.notifyRegistered(networkMonitor);
NetworkInfo networkInfo = nai.networkInfo;
updateNetworkInfo(nai, networkInfo);
updateVpnUids(nai, null, nai.networkCapabilities);