From 59fa1276a7d1d25311a0698e9d80965695824a92 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Sat, 16 Jan 2021 00:23:46 +0900 Subject: [PATCH] Mark several NetworkAgentInfo members @NonNull and address TODO. 1. network cannot be null because it's final and the only caller of the constructor passes in a fresh copy. 2. linkProperties cannot be null because: - The only caller of the constructor passes in a fresh copy. - The only assignment to nai.linkProperties is in updateLinkProperties, and that dereferences newLp a few lines before assigning it. 3. networkCapabilities cannot be null because: - The only caller of the constructor passes in a fresh copy. - There are no direct assignments to nai.networkCapabilities. - All 4 assignments happen via getAndSetNetworkCapabilities: - registerNetworkAgentInternal, updateCapabilities and applyBackgroundChangeForRematch all pass in the return value of mixInCapabilities, which is always non-null. - updateNetworkInfo re-assigns the capabilities that were already set in the object. 4. networkAgentConfig cannot be null because it's final and the only caller of the NetworkAgentInfo constructor passes in a fresh copy. Address a TODO in ConnectivityService to remove a check for the nai's capabilities being null. Also fix a style nit pointed out by the linter. Test: atest FrameworksNetTests Change-Id: I2e265f1c60a73d6da0222dd6711bc53c32db06ec --- .../android/server/ConnectivityService.java | 31 ++++++++----------- .../server/connectivity/NetworkAgentInfo.java | 10 +++--- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 1b7912e704..55774c4da1 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -6590,7 +6590,7 @@ public class ConnectivityService extends IConnectivityManager.Stub } // Don't modify caller's NetworkCapabilities. - NetworkCapabilities newNc = new NetworkCapabilities(nc); + final NetworkCapabilities newNc = new NetworkCapabilities(nc); if (nai.lastValidated) { newNc.addCapability(NET_CAPABILITY_VALIDATED); } else { @@ -6678,26 +6678,21 @@ public class ConnectivityService extends IConnectivityManager.Stub notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); } - // TODO : static analysis indicates that prevNc can't be null here (getAndSetNetworkCaps - // never returns null), so mark the relevant members and functions in nai as @NonNull and - // remove this test - if (prevNc != null) { - final boolean oldMetered = prevNc.isMetered(); - final boolean newMetered = newNc.isMetered(); - final boolean meteredChanged = oldMetered != newMetered; + final boolean oldMetered = prevNc.isMetered(); + final boolean newMetered = newNc.isMetered(); + final boolean meteredChanged = oldMetered != newMetered; - if (meteredChanged) { - maybeNotifyNetworkBlocked(nai, oldMetered, newMetered, mRestrictBackground, - mRestrictBackground, mVpnBlockedUidRanges, mVpnBlockedUidRanges); - } + if (meteredChanged) { + maybeNotifyNetworkBlocked(nai, oldMetered, newMetered, mRestrictBackground, + mRestrictBackground, mVpnBlockedUidRanges, mVpnBlockedUidRanges); + } - final boolean roamingChanged = prevNc.hasCapability(NET_CAPABILITY_NOT_ROAMING) != - newNc.hasCapability(NET_CAPABILITY_NOT_ROAMING); + final boolean roamingChanged = prevNc.hasCapability(NET_CAPABILITY_NOT_ROAMING) + != newNc.hasCapability(NET_CAPABILITY_NOT_ROAMING); - // Report changes that are interesting for network statistics tracking. - if (meteredChanged || roamingChanged) { - notifyIfacesChangedForNetworkStats(); - } + // Report changes that are interesting for network statistics tracking. + if (meteredChanged || roamingChanged) { + notifyIfacesChangedForNetworkStats(); } // This network might have been underlying another network. Propagate its capabilities. diff --git a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java index b0a73f1057..8215e8879d 100644 --- a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java +++ b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java @@ -136,12 +136,12 @@ public class NetworkAgentInfo implements Comparable { // This Network object should always be used if possible, so as to encourage reuse of the // enclosed socket factory and connection pool. Avoid creating other Network objects. // This Network object is always valid. - public final Network network; - public LinkProperties linkProperties; + @NonNull public final Network network; + @NonNull public LinkProperties linkProperties; // This should only be modified by ConnectivityService, via setNetworkCapabilities(). // TODO: make this private with a getter. - public NetworkCapabilities networkCapabilities; - public final NetworkAgentConfig networkAgentConfig; + @NonNull public NetworkCapabilities networkCapabilities; + @NonNull public final NetworkAgentConfig networkAgentConfig; // Underlying networks declared by the agent. Only set if supportsUnderlyingNetworks is true. // The networks in this list might be declared by a VPN app using setUnderlyingNetworks and are @@ -603,7 +603,7 @@ public class NetworkAgentInfo implements Comparable { * * @return the old capabilities of this network. */ - public synchronized NetworkCapabilities getAndSetNetworkCapabilities( + @NonNull public synchronized NetworkCapabilities getAndSetNetworkCapabilities( @NonNull final NetworkCapabilities nc) { final NetworkCapabilities oldNc = networkCapabilities; networkCapabilities = nc;