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.Nullable;
20 
21 import java.io.PrintWriter;
22 
23 /**
24  * Overrides the unlocked methods in {@link AppsFilterBase} and guards them with locks.
25  * These are used by {@link AppsFilterImpl} which contains modifications to the class members
26  */
27 abstract class AppsFilterLocked extends AppsFilterBase {
28     /**
29      * The following locks guard the accesses for the list/set class members
30      */
31     protected final PackageManagerTracedLock mForceQueryableLock =
32             new PackageManagerTracedLock();
33     protected final PackageManagerTracedLock mQueriesViaPackageLock =
34             new PackageManagerTracedLock();
35     protected final PackageManagerTracedLock mQueriesViaComponentLock =
36             new PackageManagerTracedLock();
37     /**
38      * This lock covers both {@link #mImplicitlyQueryable} and {@link #mRetainedImplicitlyQueryable}
39      */
40     protected final PackageManagerTracedLock mImplicitlyQueryableLock =
41         new PackageManagerTracedLock();
42     protected final PackageManagerTracedLock mQueryableViaUsesLibraryLock =
43         new PackageManagerTracedLock();
44     protected final PackageManagerTracedLock mProtectedBroadcastsLock =
45         new PackageManagerTracedLock();
46     protected final PackageManagerTracedLock mQueryableViaUsesPermissionLock =
47         new PackageManagerTracedLock();
48 
49     /**
50      * Guards the access for {@link AppsFilterBase#mShouldFilterCache};
51      */
52     protected final PackageManagerTracedLock mCacheLock = new PackageManagerTracedLock();
53 
54     @Override
isForceQueryable(int appId)55     protected boolean isForceQueryable(int appId) {
56         synchronized (mForceQueryableLock) {
57             return super.isForceQueryable(appId);
58         }
59     }
60 
61     @Override
isQueryableViaPackage(int callingAppId, int targetAppId)62     protected boolean isQueryableViaPackage(int callingAppId, int targetAppId) {
63         synchronized (mQueriesViaPackageLock) {
64             return super.isQueryableViaPackage(callingAppId, targetAppId);
65         }
66     }
67 
68     @Override
isQueryableViaComponent(int callingAppId, int targetAppId)69     protected boolean isQueryableViaComponent(int callingAppId, int targetAppId) {
70         synchronized (mQueriesViaComponentLock) {
71             return super.isQueryableViaComponent(callingAppId, targetAppId);
72         }
73     }
74 
75     @Override
isImplicitlyQueryable(int callingUid, int targetUid)76     protected boolean isImplicitlyQueryable(int callingUid, int targetUid) {
77         synchronized (mImplicitlyQueryableLock) {
78             return super.isImplicitlyQueryable(callingUid, targetUid);
79         }
80     }
81 
82     @Override
isRetainedImplicitlyQueryable(int callingUid, int targetUid)83     protected boolean isRetainedImplicitlyQueryable(int callingUid, int targetUid) {
84         synchronized (mImplicitlyQueryableLock) {
85             return super.isRetainedImplicitlyQueryable(callingUid, targetUid);
86         }
87     }
88 
89     @Override
isQueryableViaUsesLibrary(int callingAppId, int targetAppId)90     protected boolean isQueryableViaUsesLibrary(int callingAppId, int targetAppId) {
91         synchronized (mQueryableViaUsesLibraryLock) {
92             return super.isQueryableViaUsesLibrary(callingAppId, targetAppId);
93         }
94     }
95 
96     @Override
isQueryableViaUsesPermission(int callingAppId, int targetAppId)97     protected boolean isQueryableViaUsesPermission(int callingAppId, int targetAppId) {
98         synchronized (mQueryableViaUsesPermissionLock) {
99             return super.isQueryableViaUsesPermission(callingAppId, targetAppId);
100         }
101     }
102 
103     @Override
shouldFilterApplicationUsingCache(int callingUid, int appId, int userId)104     protected boolean shouldFilterApplicationUsingCache(int callingUid, int appId, int userId) {
105         synchronized (mCacheLock) {
106             return super.shouldFilterApplicationUsingCache(callingUid, appId, userId);
107         }
108     }
109 
110     @Override
dumpForceQueryable(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)111     protected void dumpForceQueryable(PrintWriter pw, @Nullable Integer filteringAppId,
112             ToString<Integer> expandPackages) {
113         synchronized (mForceQueryableLock) {
114             super.dumpForceQueryable(pw, filteringAppId, expandPackages);
115         }
116     }
117 
118     @Override
dumpQueriesViaPackage(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)119     protected void dumpQueriesViaPackage(PrintWriter pw, @Nullable Integer filteringAppId,
120             ToString<Integer> expandPackages) {
121         synchronized (mQueriesViaPackageLock) {
122             super.dumpQueriesViaPackage(pw, filteringAppId, expandPackages);
123         }
124     }
125 
126     @Override
dumpQueriesViaComponent(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)127     protected void dumpQueriesViaComponent(PrintWriter pw, @Nullable Integer filteringAppId,
128             ToString<Integer> expandPackages) {
129         synchronized (mQueriesViaComponentLock) {
130             super.dumpQueriesViaComponent(pw, filteringAppId, expandPackages);
131         }
132     }
133 
134     @Override
dumpQueriesViaImplicitlyQueryable(PrintWriter pw, @Nullable Integer filteringAppId, int[] users, ToString<Integer> expandPackages)135     protected void dumpQueriesViaImplicitlyQueryable(PrintWriter pw,
136             @Nullable Integer filteringAppId, int[] users, ToString<Integer> expandPackages) {
137         synchronized (mImplicitlyQueryableLock) {
138             super.dumpQueriesViaImplicitlyQueryable(pw, filteringAppId, users, expandPackages);
139         }
140     }
141 
142     @Override
dumpQueriesViaUsesLibrary(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)143     protected void dumpQueriesViaUsesLibrary(PrintWriter pw, @Nullable Integer filteringAppId,
144             ToString<Integer> expandPackages) {
145         synchronized (mQueryableViaUsesLibraryLock) {
146             super.dumpQueriesViaUsesLibrary(pw, filteringAppId, expandPackages);
147         }
148     }
149 }
150