Merge "Replace NPMI#isUidNetworkingBlocked()" am: 830e1e9576

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1536313

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I6c4b745fa67c8db72d04eb48582d06916f58902d
This commit is contained in:
Paul Hu
2021-02-05 07:01:42 +00:00
committed by Automerger Merge Worker
7 changed files with 62 additions and 42 deletions

View File

@@ -82,4 +82,5 @@ interface INetworkPolicyManager {
boolean isUidNetworkingBlocked(int uid, boolean meteredNetwork);
boolean isUidRestrictedOnMeteredNetworks(int uid);
boolean checkUidNetworkingBlocked(int uid, int uidRules, boolean isNetworkMetered, boolean isBackgroundRestricted);
}

View File

@@ -463,6 +463,31 @@ public class NetworkPolicyManager {
}
}
/**
* Figure out if networking is blocked for a given set of conditions.
*
* This is used by ConnectivityService via passing stale copies of conditions, so it must not
* take any locks.
*
* @param uid The target uid.
* @param uidRules The uid rules which are obtained from NetworkPolicyManagerService.
* @param isNetworkMetered True if the network is metered.
* @param isBackgroundRestricted True if data saver is enabled.
*
* @return true if networking is blocked for the UID under the specified conditions.
*
* @hide
*/
public boolean checkUidNetworkingBlocked(int uid, int uidRules,
boolean isNetworkMetered, boolean isBackgroundRestricted) {
try {
return mService.checkUidNetworkingBlocked(uid, uidRules, isNetworkMetered,
isBackgroundRestricted);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Check that the given uid is restricted from doing networking on metered networks.
*

View File

@@ -2142,8 +2142,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
private boolean isUidBlockedByRules(int uid, int uidRules, boolean isNetworkMetered,
boolean isBackgroundRestricted) {
return NetworkPolicyManagerInternal.isUidNetworkingBlocked(uid, uidRules,
isNetworkMetered, isBackgroundRestricted);
return mPolicyManager.checkUidNetworkingBlocked(uid, uidRules, isNetworkMetered,
isBackgroundRestricted);
}
/**

View File

@@ -16,8 +16,6 @@
package com.android.server.net;
import static com.android.server.net.NetworkPolicyManagerService.isUidNetworkingBlockedInternal;
import android.annotation.NonNull;
import android.net.Network;
import android.net.NetworkTemplate;
@@ -38,28 +36,6 @@ public abstract class NetworkPolicyManagerInternal {
*/
public abstract void resetUserState(int userId);
/**
* Figure out if networking is blocked for a given set of conditions.
*
* This is used by ConnectivityService via passing stale copies of conditions, so it must not
* take any locks.
*
* @param uid The target uid.
* @param uidRules The uid rules which are obtained from NetworkPolicyManagerService.
* @param isNetworkMetered True if the network is metered.
* @param isBackgroundRestricted True if data saver is enabled.
*
* @return true if networking is blocked for the UID under the specified conditions.
*/
public static boolean isUidNetworkingBlocked(int uid, int uidRules, boolean isNetworkMetered,
boolean isBackgroundRestricted) {
// Log of invoking internal function is disabled because it will be called very
// frequently. And metrics are unlikely needed on this method because the callers are
// external and this method doesn't take any locks or perform expensive operations.
return isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered,
isBackgroundRestricted, null);
}
/**
* Informs that an appId has been added or removed from the temp-powersave-allowlist so that
* that network rules for that appId can be updated.

View File

@@ -5403,6 +5403,17 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
return ret;
}
@Override
public boolean checkUidNetworkingBlocked(int uid, int uidRules,
boolean isNetworkMetered, boolean isBackgroundRestricted) {
mContext.enforceCallingOrSelfPermission(OBSERVE_NETWORK_POLICY, TAG);
// Log of invoking this function is disabled because it will be called very frequently. And
// metrics are unlikely needed on this method because the callers are external and this
// method doesn't take any locks or perform expensive operations.
return isUidNetworkingBlockedInternal(uid, uidRules, isNetworkMetered,
isBackgroundRestricted, null);
}
@Override
public boolean isUidRestrictedOnMeteredNetworks(int uid) {
mContext.enforceCallingOrSelfPermission(OBSERVE_NETWORK_POLICY, TAG);
@@ -5412,9 +5423,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
isBackgroundRestricted = mRestrictBackground;
}
//TODO(b/177490332): The logic here might not be correct because it doesn't consider
// RULE_REJECT_METERED condition. And it could be replaced by
// isUidNetworkingBlockedInternal().
// TODO(b/177490332): The logic here might not be correct because it doesn't consider
// RULE_REJECT_METERED condition. And it could be replaced by
// isUidNetworkingBlockedInternal().
return isBackgroundRestricted
&& !hasRule(uidRules, RULE_ALLOW_METERED)
&& !hasRule(uidRules, RULE_TEMPORARY_ALLOW_METERED);

View File

@@ -1829,11 +1829,11 @@ public class NetworkPolicyManagerServiceTest {
}
/**
* Exhaustively test isUidNetworkingBlocked to output the expected results based on external
* Exhaustively test checkUidNetworkingBlocked to output the expected results based on external
* conditions.
*/
@Test
public void testIsUidNetworkingBlocked() {
public void testCheckUidNetworkingBlocked() {
final ArrayList<Pair<Boolean, Integer>> expectedBlockedStates = new ArrayList<>();
// Metered network. Data saver on.
@@ -1877,17 +1877,16 @@ public class NetworkPolicyManagerServiceTest {
private void verifyNetworkBlockedState(boolean metered, boolean backgroundRestricted,
ArrayList<Pair<Boolean, Integer>> expectedBlockedStateForRules) {
final NetworkPolicyManagerInternal npmi = LocalServices
.getService(NetworkPolicyManagerInternal.class);
for (Pair<Boolean, Integer> pair : expectedBlockedStateForRules) {
final boolean expectedResult = pair.first;
final int rule = pair.second;
assertEquals(formatBlockedStateError(UID_A, rule, metered, backgroundRestricted),
expectedResult,
npmi.isUidNetworkingBlocked(UID_A, rule, metered, backgroundRestricted));
expectedResult, mService.checkUidNetworkingBlocked(UID_A, rule,
metered, backgroundRestricted));
assertFalse(formatBlockedStateError(SYSTEM_UID, rule, metered, backgroundRestricted),
npmi.isUidNetworkingBlocked(SYSTEM_UID, rule, metered, backgroundRestricted));
mService.checkUidNetworkingBlocked(SYSTEM_UID, rule, metered,
backgroundRestricted));
}
}

View File

@@ -1262,22 +1262,28 @@ public class ConnectivityServiceTest {
}
}
private void updateUidNetworkingBlocked() {
doAnswer(i -> NetworkPolicyManagerInternal.isUidNetworkingBlocked(
i.getArgument(0) /* uid */, mUidRules, i.getArgument(1) /* metered */,
mRestrictBackground)
private void mockUidNetworkingBlocked() {
doAnswer(i -> mContext.getSystemService(NetworkPolicyManager.class)
.checkUidNetworkingBlocked(i.getArgument(0) /* uid */, mUidRules,
i.getArgument(1) /* metered */, mRestrictBackground)
).when(mNetworkPolicyManager).isUidNetworkingBlocked(anyInt(), anyBoolean());
doAnswer(inv -> mContext.getSystemService(NetworkPolicyManager.class)
.checkUidNetworkingBlocked(inv.getArgument(0) /* uid */,
inv.getArgument(1) /* uidRules */,
inv.getArgument(2) /* isNetworkMetered */,
inv.getArgument(3) /* isBackgroundRestricted */)
).when(mNetworkPolicyManager).checkUidNetworkingBlocked(
anyInt(), anyInt(), anyBoolean(), anyBoolean());
}
private void setUidRulesChanged(int uidRules) throws RemoteException {
mUidRules = uidRules;
updateUidNetworkingBlocked();
mPolicyListener.onUidRulesChanged(Process.myUid(), mUidRules);
}
private void setRestrictBackgroundChanged(boolean restrictBackground) throws RemoteException {
mRestrictBackground = restrictBackground;
updateUidNetworkingBlocked();
mPolicyListener.onRestrictBackgroundChanged(mRestrictBackground);
}
@@ -6809,6 +6815,7 @@ public class ConnectivityServiceTest {
.addTransportType(TRANSPORT_CELLULAR)
.build();
mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
mockUidNetworkingBlocked();
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
mCellNetworkAgent.connect(true);
@@ -6891,6 +6898,7 @@ public class ConnectivityServiceTest {
public void testNetworkBlockedStatusBeforeAndAfterConnect() throws Exception {
final TestNetworkCallback defaultCallback = new TestNetworkCallback();
mCm.registerDefaultNetworkCallback(defaultCallback);
mockUidNetworkingBlocked();
// No Networkcallbacks invoked before any network is active.
setUidRulesChanged(RULE_REJECT_ALL);