[MS37] Replace NetworkStatsManagerInternal usages in NPMS

This is done by:
1. Add NetworkStatsManagerInternal APIs directly from
   NetworkStatsManager, these APIs are needed by NPMS.
2. Replace all usages with these APIs.
3. Delete NetworkStatsManagerInternal implementation.

Test: atest FrameworksNetTests NetworkPolicyManagerServiceTest
Bug: 204830222
CTS-Coverage-Bug: 213124616
Change-Id: If51b6676915e3a0a8a9f95221d735306911442fc
This commit is contained in:
Junyu Lai
2022-01-04 07:24:40 +00:00
parent f2eb3a58dd
commit a5729ee2dc
6 changed files with 113 additions and 88 deletions

View File

@@ -871,4 +871,74 @@ public class NetworkStatsManager {
return msg.getData().getParcelable(key);
}
}
/**
* Mark given UID as being in foreground for stats purposes.
*
* @hide
*/
// @SystemApi
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK})
public void setUidForeground(int uid, boolean uidForeground) {
try {
mService.setUidForeground(uid, uidForeground);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Advise persistence threshold; may be overridden internally.
*
* @hide
*/
// @SystemApi
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK})
public void advisePersistThreshold(long thresholdBytes) {
try {
mService.advisePersistThreshold(thresholdBytes);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Force update of statistics.
*
* @hide
*/
// @SystemApi
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK})
public void forceUpdate() {
try {
mService.forceUpdate();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Set the warning and limit to all registered custom network stats providers.
* Note that invocation of any interface will be sent to all providers.
*
* @hide
*/
// @SystemApi
@RequiresPermission(anyOf = {
NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK,
android.Manifest.permission.NETWORK_STACK})
public void setStatsProviderWarningAndLimitAsync(@NonNull String iface, long warning,
long limit) {
try {
mService.setStatsProviderWarningAndLimitAsync(iface, warning, limit);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}

View File

@@ -94,4 +94,16 @@ interface INetworkStatsService {
/** Registers a network stats provider */
INetworkStatsProviderCallback registerNetworkStatsProvider(String tag,
in INetworkStatsProvider provider);
/** Mark given UID as being in foreground for stats purposes. */
void setUidForeground(int uid, boolean uidForeground);
/** Advise persistence threshold; may be overridden internally. */
void advisePersistThreshold(long thresholdBytes);
/**
* Set the warning and limit to all registered custom network stats providers.
* Note that invocation of any interface will be sent to all providers.
*/
void setStatsProviderWarningAndLimitAsync(String iface, long warning, long limit);
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.net;
import android.annotation.NonNull;
public abstract class NetworkStatsManagerInternal {
/** Mark given UID as being in foreground for stats purposes. */
public abstract void setUidForeground(int uid, boolean uidForeground);
/** Advise persistance threshold; may be overridden internally. */
public abstract void advisePersistThreshold(long thresholdBytes);
/** Force update of statistics. */
public abstract void forceUpdate();
/**
* Set the warning and limit to all registered custom network stats providers.
* Note that invocation of any interface will be sent to all providers.
*/
public abstract void setStatsProviderWarningAndLimitAsync(@NonNull String iface, long warning,
long limit);
}

View File

@@ -430,7 +430,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
new DefaultNetworkStatsSettings(context), new NetworkStatsFactory(netd),
new NetworkStatsObservers(), getDefaultSystemDir(), getDefaultBaseDir(),
new Dependencies());
service.registerLocalService();
return service;
}
@@ -511,11 +510,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
}
private void registerLocalService() {
LocalServices.addService(NetworkStatsManagerInternal.class,
new NetworkStatsManagerInternalImpl());
}
/**
* Observer that watches for {@link INetdUnsolicitedEventListener} alerts.
*/
@@ -1006,7 +1000,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
@VisibleForTesting
void setUidForeground(int uid, boolean uidForeground) {
public void setUidForeground(int uid, boolean uidForeground) {
PermissionUtils.enforceNetworkStackPermission(mContext);
synchronized (mStatsLock) {
final int set = uidForeground ? SET_FOREGROUND : SET_DEFAULT;
final int oldSet = mActiveUidCounterSet.get(uid, SET_DEFAULT);
@@ -1042,7 +1037,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override
public void forceUpdate() {
mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG);
PermissionUtils.enforceNetworkStackPermission(mContext);
final long token = Binder.clearCallingIdentity();
try {
@@ -1052,7 +1047,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
}
private void advisePersistThreshold(long thresholdBytes) {
/** Advise persistence threshold; may be overridden internally. */
public void advisePersistThreshold(long thresholdBytes) {
PermissionUtils.enforceNetworkStackPermission(mContext);
// clamp threshold into safe range
mPersistThreshold = NetworkStatsUtils.constrain(thresholdBytes,
128 * KB_IN_BYTES, 2 * MB_IN_BYTES);
@@ -1689,32 +1686,19 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
removeUidsLocked(CollectionUtils.toIntArray(uids));
}
private class NetworkStatsManagerInternalImpl extends NetworkStatsManagerInternal {
@Override
public void setUidForeground(int uid, boolean uidForeground) {
NetworkStatsService.this.setUidForeground(uid, uidForeground);
}
@Override
public void advisePersistThreshold(long thresholdBytes) {
NetworkStatsService.this.advisePersistThreshold(thresholdBytes);
}
@Override
public void forceUpdate() {
NetworkStatsService.this.forceUpdate();
}
@Override
public void setStatsProviderWarningAndLimitAsync(
@NonNull String iface, long warning, long limit) {
if (LOGV) {
Log.v(TAG, "setStatsProviderWarningAndLimitAsync("
+ iface + "," + warning + "," + limit + ")");
}
invokeForAllStatsProviderCallbacks((cb) -> cb.mProvider.onSetWarningAndLimit(iface,
warning, limit));
/**
* Set the warning and limit to all registered custom network stats providers.
* Note that invocation of any interface will be sent to all providers.
*/
public void setStatsProviderWarningAndLimitAsync(
@NonNull String iface, long warning, long limit) {
PermissionUtils.enforceNetworkStackPermission(mContext);
if (LOGV) {
Log.v(TAG, "setStatsProviderWarningAndLimitAsync("
+ iface + "," + warning + "," + limit + ")");
}
invokeForAllStatsProviderCallbacks((cb) -> cb.mProvider.onSetWarningAndLimit(iface,
warning, limit));
}
@Override

View File

@@ -444,7 +444,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
private final Context mContext;
private final IActivityManager mActivityManager;
private NetworkStatsManagerInternal mNetworkStats;
private NetworkStatsManager mNetworkStats;
private final INetworkManagementService mNetworkManager;
private UsageStatsManagerInternal mUsageStats;
private AppStandbyInternal mAppStandby;
@@ -799,6 +799,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
mPolicyFile = new AtomicFile(new File(systemDir, "netpolicy.xml"), "net-policy");
mAppOps = context.getSystemService(AppOpsManager.class);
mNetworkStats = context.getSystemService(NetworkStatsManager.class);
mMultipathPolicyTracker = new MultipathPolicyTracker(mContext, mHandler);
// Expose private service for system components to use.
LocalServices.addService(NetworkPolicyManagerInternal.class,
@@ -898,7 +899,6 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class);
mAppStandby = LocalServices.getService(AppStandbyInternal.class);
mNetworkStats = LocalServices.getService(NetworkStatsManagerInternal.class);
synchronized (mUidRulesFirstLock) {
synchronized (mNetworkPoliciesSecondLock) {

View File

@@ -266,7 +266,6 @@ public class NetworkPolicyManagerServiceTest {
ArgumentCaptor.forClass(ConnectivityManager.NetworkCallback.class);
private ActivityManagerInternal mActivityManagerInternal;
private NetworkStatsManagerInternal mStatsService;
private IUidObserver mUidObserver;
private INetworkManagementEventObserver mNetworkObserver;
@@ -333,8 +332,6 @@ public class NetworkPolicyManagerServiceTest {
.setBatterySaverEnabled(false).build();
final PowerManagerInternal pmInternal = addLocalServiceMock(PowerManagerInternal.class);
when(pmInternal.getLowPowerState(anyInt())).thenReturn(state);
mStatsService = addLocalServiceMock(NetworkStatsManagerInternal.class);
}
private class TestDependencies extends NetworkPolicyManagerService.Dependencies {
@@ -524,7 +521,6 @@ public class NetworkPolicyManagerServiceTest {
LocalServices.removeServiceForTest(DeviceIdleInternal.class);
LocalServices.removeServiceForTest(AppStandbyInternal.class);
LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
LocalServices.removeServiceForTest(NetworkStatsManagerInternal.class);
}
@After
@@ -1755,7 +1751,7 @@ public class NetworkPolicyManagerServiceTest {
npmi.onStatsProviderWarningOrLimitReached("TEST");
// Wait for processing of MSG_STATS_PROVIDER_WARNING_OR_LIMIT_REACHED.
postMsgAndWaitForCompletion();
verify(mStatsService).forceUpdate();
verify(mStatsManager).forceUpdate();
// Wait for processing of MSG_*_INTERFACE_QUOTAS.
postMsgAndWaitForCompletion();
}
@@ -1773,7 +1769,7 @@ public class NetworkPolicyManagerServiceTest {
// Get active mobile network in place
expectMobileDefaults();
mService.updateNetworks();
verify(mStatsService).setStatsProviderWarningAndLimitAsync(TEST_IFACE, Long.MAX_VALUE,
verify(mStatsManager).setStatsProviderWarningAndLimitAsync(TEST_IFACE, Long.MAX_VALUE,
Long.MAX_VALUE);
// Set warning to 7KB and limit to 10KB.
@@ -1783,32 +1779,32 @@ public class NetworkPolicyManagerServiceTest {
postMsgAndWaitForCompletion();
// Verifies that remaining quotas are set to providers.
verify(mStatsService).setStatsProviderWarningAndLimitAsync(TEST_IFACE, 2001L, 5001L);
reset(mStatsService);
verify(mStatsManager).setStatsProviderWarningAndLimitAsync(TEST_IFACE, 2001L, 5001L);
reset(mStatsManager);
// Increase the usage and simulates that limit reached fires earlier by provider,
// but actually the quota is not yet reached. Verifies that the limit reached leads to
// a force update and new quotas should be set.
mDeps.increaseMockedTotalBytes(UID_A, 1000, 999);
triggerOnStatsProviderWarningOrLimitReached();
verify(mStatsService).setStatsProviderWarningAndLimitAsync(TEST_IFACE, 2L, 3002L);
reset(mStatsService);
verify(mStatsManager).setStatsProviderWarningAndLimitAsync(TEST_IFACE, 2L, 3002L);
reset(mStatsManager);
// Increase the usage and simulate warning reached, the new warning should be unlimited
// since service will disable warning quota to stop lower layer from keep triggering
// warning reached event.
mDeps.increaseMockedTotalBytes(UID_A, 1000L, 1000);
triggerOnStatsProviderWarningOrLimitReached();
verify(mStatsService).setStatsProviderWarningAndLimitAsync(
verify(mStatsManager).setStatsProviderWarningAndLimitAsync(
TEST_IFACE, Long.MAX_VALUE, 1002L);
reset(mStatsService);
reset(mStatsManager);
// Increase the usage that over the warning and limit, the new limit should set to 1 to
// block the network traffic.
mDeps.increaseMockedTotalBytes(UID_A, 1000L, 1000);
triggerOnStatsProviderWarningOrLimitReached();
verify(mStatsService).setStatsProviderWarningAndLimitAsync(TEST_IFACE, Long.MAX_VALUE, 1L);
reset(mStatsService);
verify(mStatsManager).setStatsProviderWarningAndLimitAsync(TEST_IFACE, Long.MAX_VALUE, 1L);
reset(mStatsManager);
}
private void enableRestrictedMode(boolean enable) throws Exception {
@@ -2098,7 +2094,7 @@ public class NetworkPolicyManagerServiceTest {
}
private void verifyAdvisePersistThreshold() throws Exception {
verify(mStatsService).advisePersistThreshold(anyLong());
verify(mStatsManager).advisePersistThreshold(anyLong());
}
private static class TestAbstractFuture<T> extends AbstractFuture<T> {