blob: 86f3dfd412f6491084f6d8eea0af09644983025a [file] [log] [blame]
Remi NGUYEN VANc094a542018-12-07 16:52:24 +09001/*
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 */
16package android.net;
17
Chiachang Wange05d9d82019-04-09 19:42:52 +080018import static android.Manifest.permission.NETWORK_STACK;
19import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20
21import android.annotation.NonNull;
Remi NGUYEN VAN53f01932020-03-18 18:29:30 +090022import android.annotation.Nullable;
Remi NGUYEN VANd8c75a02019-01-30 21:45:56 +090023import android.annotation.SystemApi;
Remi NGUYEN VANd8c75a02019-01-30 21:45:56 +090024import android.annotation.TestApi;
Chiachang Wange05d9d82019-04-09 19:42:52 +080025import android.content.Context;
Remi NGUYEN VAN53f01932020-03-18 18:29:30 +090026import android.os.IBinder;
27import android.os.ServiceManager;
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090028
Chiachang Wange05d9d82019-04-09 19:42:52 +080029import java.util.ArrayList;
30import java.util.Arrays;
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090031/**
Remi NGUYEN VAN53f01932020-03-18 18:29:30 +090032 * Constants and utilities for client code communicating with the network stack service.
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090033 * @hide
34 */
Remi NGUYEN VANd8c75a02019-01-30 21:45:56 +090035@SystemApi
36@TestApi
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090037public class NetworkStack {
Remi NGUYEN VANd8c75a02019-01-30 21:45:56 +090038 /**
39 * Permission granted only to the NetworkStack APK, defined in NetworkStackStub with signature
40 * protection level.
41 * @hide
42 */
43 @SystemApi
44 @TestApi
45 public static final String PERMISSION_MAINLINE_NETWORK_STACK =
46 "android.permission.MAINLINE_NETWORK_STACK";
47
Remi NGUYEN VANfc9119d2020-03-19 11:25:50 +090048 @Nullable
49 private static volatile IBinder sMockService;
50
Remi NGUYEN VAN53f01932020-03-18 18:29:30 +090051 /**
52 * Get an {@link IBinder} representing the NetworkStack stable AIDL Interface, if registered.
53 * @hide
54 */
55 @Nullable
56 @SystemApi
57 @TestApi
58 public static IBinder getService() {
Remi NGUYEN VANfc9119d2020-03-19 11:25:50 +090059 final IBinder mockService = sMockService;
60 if (mockService != null) return mockService;
Remi NGUYEN VAN53f01932020-03-18 18:29:30 +090061 return ServiceManager.getService(Context.NETWORK_STACK_SERVICE);
62 }
63
Remi NGUYEN VANfc9119d2020-03-19 11:25:50 +090064 /**
65 * Set a mock service for testing, to be returned by future calls to {@link #getService()}.
66 *
67 * <p>Passing a {@code null} {@code mockService} resets {@link #getService()} to normal
68 * behavior.
69 * @hide
70 */
71 @TestApi
72 public static void setServiceForTest(@Nullable IBinder mockService) {
73 sMockService = mockService;
74 }
75
Remi NGUYEN VAN5db454c2019-02-14 18:04:20 +090076 private NetworkStack() {}
Chiachang Wange05d9d82019-04-09 19:42:52 +080077
78 /**
79 * If the NetworkStack, MAINLINE_NETWORK_STACK are not allowed for a particular process, throw a
80 * {@link SecurityException}.
81 *
82 * @param context {@link android.content.Context} for the process.
83 *
84 * @hide
85 */
86 public static void checkNetworkStackPermission(final @NonNull Context context) {
87 checkNetworkStackPermissionOr(context);
88 }
89
90 /**
91 * If the NetworkStack, MAINLINE_NETWORK_STACK or other specified permissions are not allowed
92 * for a particular process, throw a {@link SecurityException}.
93 *
94 * @param context {@link android.content.Context} for the process.
95 * @param otherPermissions The set of permissions that could be the candidate permissions , or
96 * empty string if none of other permissions needed.
97 * @hide
98 */
99 public static void checkNetworkStackPermissionOr(final @NonNull Context context,
100 final @NonNull String... otherPermissions) {
101 ArrayList<String> permissions = new ArrayList<String>(Arrays.asList(otherPermissions));
102 permissions.add(NETWORK_STACK);
103 permissions.add(PERMISSION_MAINLINE_NETWORK_STACK);
104 enforceAnyPermissionOf(context, permissions.toArray(new String[0]));
105 }
106
107 private static void enforceAnyPermissionOf(final @NonNull Context context,
108 final @NonNull String... permissions) {
109 if (!checkAnyPermissionOf(context, permissions)) {
110 throw new SecurityException("Requires one of the following permissions: "
111 + String.join(", ", permissions) + ".");
112 }
113 }
114
115 private static boolean checkAnyPermissionOf(final @NonNull Context context,
116 final @NonNull String... permissions) {
117 for (String permission : permissions) {
118 if (context.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) {
119 return true;
120 }
121 }
122 return false;
123 }
124
Remi NGUYEN VANc094a542018-12-07 16:52:24 +0900125}