1 /*
2  * Copyright (C) 2022 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 
17 package com.android.server.pm;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.os.Binder;
24 import android.os.UserHandle;
25 
26 import com.android.server.pm.pkg.PackageState;
27 
28 import java.io.IOException;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.List;
32 import java.util.Map;
33 
34 /**
35  * In-process API for server side PackageManager related infrastructure.
36  *
37  * For now, avoiding adding methods that rely on package data until we solve the snapshot
38  * consistency problem.
39  *
40  * @hide
41  */
42 @SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
43 public interface PackageManagerLocal {
44 
45     /**
46      * Indicates if operation should include device encrypted storage.
47      */
48     int FLAG_STORAGE_DE = Installer.FLAG_STORAGE_DE;
49     /**
50      * Indicates if operation should include credential encrypted storage.
51      */
52     int FLAG_STORAGE_CE = Installer.FLAG_STORAGE_CE;
53 
54     /**
55      * Constants for use with {@link #reconcileSdkData} to specify which storage areas should be
56      * included for operation.
57      *
58      * @hide
59      */
60     @IntDef(prefix = "FLAG_STORAGE_",  value = {
61             FLAG_STORAGE_DE,
62             FLAG_STORAGE_CE,
63     })
64     @Retention(RetentionPolicy.SOURCE)
65     @interface StorageFlags {}
66 
67     /**
68      * Reconcile sdk data sub-directories for the given {@code packageName}.
69      *
70      * Sub directories are created if they do not exist already. If there is an existing per-
71      * sdk directory that is missing from {@code subDirNames}, then it is removed.
72      *
73      * Sdk package path is created if it doesn't exist before creating per-sdk directories.
74      *
75      * @param volumeUuid the volume in which the sdk data should be prepared.
76      * @param packageName package name of the app for which sdk data directory will be prepared.
77      * @param subDirNames names of sub directories that should be reconciled against.
78      * @param userId id of the user to whom the package belongs to.
79      * @param appId id of the package.
80      * @param previousAppId previous id of the package if package is being updated.
81      * @param flags flags from StorageManager to indicate which storage areas should be included.
82      * @param seInfo seInfo tag to be used for selinux policy.
83      * @throws IOException If any error occurs during the operation.
84      */
reconcileSdkData(@ullable String volumeUuid, @NonNull String packageName, @NonNull List<String> subDirNames, int userId, int appId, int previousAppId, @NonNull String seInfo, @StorageFlags int flags)85     void reconcileSdkData(@Nullable String volumeUuid, @NonNull String packageName,
86             @NonNull List<String> subDirNames, int userId, int appId, int previousAppId,
87             @NonNull String seInfo, @StorageFlags int flags) throws IOException;
88 
89     /**
90      * Provides a snapshot scoped class to access snapshot-aware APIs. Should be short-term use and
91      * closed as soon as possible.
92      * <p/>
93      * All reachable types in the snapshot are read-only.
94      * <p/>
95      * The snapshot assumes the caller is acting on behalf of the system and will not filter any
96      * results.
97      */
98     @NonNull
withUnfilteredSnapshot()99     UnfilteredSnapshot withUnfilteredSnapshot();
100 
101     /**
102      * {@link #withFilteredSnapshot(int, UserHandle)} that infers the UID and user from the
103      * caller through {@link Binder#getCallingUid()} and {@link Binder#getCallingUserHandle()}.
104      *
105      * @see #withFilteredSnapshot(int, UserHandle)
106      */
107     @NonNull
withFilteredSnapshot()108     FilteredSnapshot withFilteredSnapshot();
109 
110     /**
111      * Provides a snapshot scoped class to access snapshot-aware APIs. Should be short-term use and
112      * closed as soon as possible.
113      * <p/>
114      * All reachable types in the snapshot are read-only.
115      *
116      * @param callingUid The caller UID to filter results based on. This includes package visibility
117      *                   and permissions, including cross-user enforcement.
118      * @param user       The user to query as, should usually be the user that the caller was
119      *                   invoked from.
120      */
121     @SuppressWarnings("UserHandleName") // Ignore naming convention, not invoking action as user
122     @NonNull
withFilteredSnapshot(int callingUid, @NonNull UserHandle user)123     FilteredSnapshot withFilteredSnapshot(int callingUid, @NonNull UserHandle user);
124 
125     /**
126      * @hide
127      */
128     @SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
129     interface UnfilteredSnapshot extends AutoCloseable {
130 
131         /**
132          * Allows re-use of this snapshot, but in a filtered context. This allows a caller to invoke
133          * itself as multiple other actual callers without having to re-take a snapshot.
134          * <p/>
135          * Note that closing the parent snapshot closes any filtered children generated from it.
136          *
137          * @return An isolated instance of {@link FilteredSnapshot} which can be closed without
138          * affecting this parent snapshot or any sibling snapshots.
139          */
140         @SuppressWarnings("UserHandleName") // Ignore naming convention, not invoking action as user
141         @NonNull
filtered(int callingUid, @NonNull UserHandle user)142         FilteredSnapshot filtered(int callingUid, @NonNull UserHandle user);
143 
144         /**
145          * Returns a map of all {@link PackageState PackageStates} on the device.
146          *
147          * @return Mapping of package name to {@link PackageState}.
148          */
149         @NonNull
getPackageStates()150         Map<String, PackageState> getPackageStates();
151 
152         /**
153          * Returns a map of all disabled system {@link PackageState PackageStates} on the device.
154          *
155          * @return Mapping of package name to disabled system {@link PackageState}.
156          *
157          * @hide Pending API
158          */
159         @NonNull
getDisabledSystemPackageStates()160         Map<String, PackageState> getDisabledSystemPackageStates();
161 
162         @Override
close()163         void close();
164     }
165 
166     /**
167      * @hide
168      */
169     @SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
170     interface FilteredSnapshot extends AutoCloseable {
171 
172         /**
173          * @return {@link PackageState} for the {@code packageName}, filtered if applicable.
174          */
175         @Nullable
getPackageState(@onNull String packageName)176         PackageState getPackageState(@NonNull String packageName);
177 
178         /**
179          * Returns a map of all {@link PackageState PackageStates} on the device.
180          * <p>
181          * This will cause app visibility filtering to be invoked on each state on the device,
182          * which can be expensive. Prefer {@link #getPackageState(String)} if possible.
183          *
184          * @return Mapping of package name to {@link PackageState}.
185          */
186         @NonNull
getPackageStates()187         Map<String, PackageState> getPackageStates();
188 
189         @Override
close()190         void close();
191     }
192 }
193