Merge "Move NetIdManager, ProfileNetworkPreferences" am: e0316b6018 am: 611cf21fd2

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

Change-Id: I486b0d2759200a5a1bb9679577f5d97ea43c252b
This commit is contained in:
Remi NGUYEN VAN
2021-05-14 07:08:58 +00:00
committed by Automerger Merge Worker
2 changed files with 176 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 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;
import android.annotation.NonNull;
import android.net.ConnectivityManager;
import android.util.SparseBooleanArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
/**
* Class used to reserve and release net IDs.
*
* <p>Instances of this class are thread-safe.
*/
public class NetIdManager {
// Sequence number for Networks; keep in sync with system/netd/NetworkController.cpp
public static final int MIN_NET_ID = 100; // some reserved marks
// Top IDs reserved by IpSecService
public static final int MAX_NET_ID = ConnectivityManager.getIpSecNetIdRange().getLower() - 1;
@GuardedBy("mNetIdInUse")
private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray();
@GuardedBy("mNetIdInUse")
private int mLastNetId = MIN_NET_ID - 1;
private final int mMaxNetId;
public NetIdManager() {
this(MAX_NET_ID);
}
@VisibleForTesting
NetIdManager(int maxNetId) {
mMaxNetId = maxNetId;
}
/**
* Get the first netId that follows the provided lastId and is available.
*/
private int getNextAvailableNetIdLocked(
int lastId, @NonNull SparseBooleanArray netIdInUse) {
int netId = lastId;
for (int i = MIN_NET_ID; i <= mMaxNetId; i++) {
netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID;
if (!netIdInUse.get(netId)) {
return netId;
}
}
throw new IllegalStateException("No free netIds");
}
/**
* Reserve a new ID for a network.
*/
public int reserveNetId() {
synchronized (mNetIdInUse) {
mLastNetId = getNextAvailableNetIdLocked(mLastNetId, mNetIdInUse);
// Make sure NetID unused. http://b/16815182
mNetIdInUse.put(mLastNetId, true);
return mLastNetId;
}
}
/**
* Clear a previously reserved ID for a network.
*/
public void releaseNetId(int id) {
synchronized (mNetIdInUse) {
mNetIdInUse.delete(id);
}
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2021 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.connectivity;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.NetworkCapabilities;
import android.os.UserHandle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A data class containing all the per-profile network preferences.
*
* A given profile can only have one preference.
*/
public class ProfileNetworkPreferences {
/**
* A single preference, as it applies to a given user profile.
*/
public static class Preference {
@NonNull public final UserHandle user;
// Capabilities are only null when sending an object to remove the setting for a user
@Nullable public final NetworkCapabilities capabilities;
public Preference(@NonNull final UserHandle user,
@Nullable final NetworkCapabilities capabilities) {
this.user = user;
this.capabilities = null == capabilities ? null : new NetworkCapabilities(capabilities);
}
/** toString */
public String toString() {
return "[ProfileNetworkPreference user=" + user + " caps=" + capabilities + "]";
}
}
@NonNull public final List<Preference> preferences;
public ProfileNetworkPreferences() {
preferences = Collections.EMPTY_LIST;
}
private ProfileNetworkPreferences(@NonNull final List<Preference> list) {
preferences = Collections.unmodifiableList(list);
}
/**
* Returns a new object consisting of this object plus the passed preference.
*
* If a preference already exists for the same user, it will be replaced by the passed
* preference. Passing a Preference object containing a null capabilities object is equivalent
* to (and indeed, implemented as) removing the preference for this user.
*/
public ProfileNetworkPreferences plus(@NonNull final Preference pref) {
final ArrayList<Preference> newPrefs = new ArrayList<>();
for (final Preference existingPref : preferences) {
if (!existingPref.user.equals(pref.user)) {
newPrefs.add(existingPref);
}
}
if (null != pref.capabilities) {
newPrefs.add(pref);
}
return new ProfileNetworkPreferences(newPrefs);
}
public boolean isEmpty() {
return preferences.isEmpty();
}
}