| 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.net; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.annotation.TestApi; |
| 22 | import android.content.Context; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.ServiceManager; |
| 25 | |
| 26 | import com.android.net.module.util.PermissionUtils; |
| 27 | /** |
| 28 | * Constants and utilities for client code communicating with the network stack service. |
| 29 | * @hide |
| 30 | */ |
| 31 | @SystemApi |
| 32 | public class NetworkStack { |
| 33 | /** |
| 34 | * Permission granted only to the NetworkStack APK, defined in NetworkStackStub with signature |
| 35 | * protection level. |
| 36 | * @hide |
| 37 | */ |
| 38 | @SystemApi |
| 39 | public static final String PERMISSION_MAINLINE_NETWORK_STACK = |
| 40 | "android.permission.MAINLINE_NETWORK_STACK"; |
| 41 | |
| 42 | @Nullable |
| 43 | private static volatile IBinder sMockService; |
| 44 | |
| 45 | /** |
| 46 | * Get an {@link IBinder} representing the NetworkStack stable AIDL Interface, if registered. |
| 47 | * @hide |
| 48 | */ |
| 49 | @Nullable |
| 50 | @SystemApi |
| 51 | public static IBinder getService() { |
| 52 | final IBinder mockService = sMockService; |
| 53 | if (mockService != null) return mockService; |
| 54 | return ServiceManager.getService(Context.NETWORK_STACK_SERVICE); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Set a mock service for testing, to be returned by future calls to {@link #getService()}. |
| 59 | * |
| 60 | * <p>Passing a {@code null} {@code mockService} resets {@link #getService()} to normal |
| 61 | * behavior. |
| 62 | * @hide |
| 63 | */ |
| 64 | @TestApi |
| 65 | public static void setServiceForTest(@Nullable IBinder mockService) { |
| 66 | sMockService = mockService; |
| 67 | } |
| 68 | |
| 69 | private NetworkStack() {} |
| 70 | |
| 71 | /** |
| 72 | * If the NetworkStack, MAINLINE_NETWORK_STACK are not allowed for a particular process, throw a |
| 73 | * {@link SecurityException}. |
| 74 | * |
| 75 | * @param context {@link android.content.Context} for the process. |
| 76 | * |
| 77 | * @hide |
| 78 | * |
| 79 | * @deprecated Use {@link PermissionUtils#enforceNetworkStackPermission} instead. |
| 80 | * |
| 81 | * TODO: remove this method and let the users call to PermissionUtils directly. |
| 82 | */ |
| 83 | @Deprecated |
| 84 | public static void checkNetworkStackPermission(final @NonNull Context context) { |
| 85 | PermissionUtils.enforceNetworkStackPermission(context); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * If the NetworkStack, MAINLINE_NETWORK_STACK or other specified permissions are not allowed |
| 90 | * for a particular process, throw a {@link SecurityException}. |
| 91 | * |
| 92 | * @param context {@link android.content.Context} for the process. |
| 93 | * @param otherPermissions The set of permissions that could be the candidate permissions , or |
| 94 | * empty string if none of other permissions needed. |
| 95 | * @hide |
| 96 | * |
| 97 | * @deprecated Use {@link PermissionUtils#enforceNetworkStackPermissionOr} instead. |
| 98 | * |
| 99 | * TODO: remove this method and let the users call to PermissionUtils directly. |
| 100 | */ |
| 101 | @Deprecated |
| 102 | public static void checkNetworkStackPermissionOr(final @NonNull Context context, |
| 103 | final @NonNull String... otherPermissions) { |
| 104 | PermissionUtils.enforceNetworkStackPermissionOr(context, otherPermissions); |
| 105 | } |
| 106 | } |