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 Object mForceQueryableLock = new Object();
32     protected final Object mQueriesViaPackageLock = new Object();
33     protected final Object mQueriesViaComponentLock = new Object();
34     /**
35      * This lock covers both {@link #mImplicitlyQueryable} and {@link #mRetainedImplicitlyQueryable}
36      */
37     protected final Object mImplicitlyQueryableLock = new Object();
38     protected final Object mQueryableViaUsesLibraryLock = new Object();
39     protected final Object mProtectedBroadcastsLock = new Object();
40     protected final Object mQueryableViaUsesPermissionLock = new Object();
41 
42     /**
43      * Guards the access for {@link AppsFilterBase#mShouldFilterCache};
44      */
45     protected final Object mCacheLock = new Object();
46 
47     @Override
isForceQueryable(int appId)48     protected boolean isForceQueryable(int appId) {
49         synchronized (mForceQueryableLock) {
50             return super.isForceQueryable(appId);
51         }
52     }
53 
54     @Override
isQueryableViaPackage(int callingAppId, int targetAppId)55     protected boolean isQueryableViaPackage(int callingAppId, int targetAppId) {
56         synchronized (mQueriesViaPackageLock) {
57             return super.isQueryableViaPackage(callingAppId, targetAppId);
58         }
59     }
60 
61     @Override
isQueryableViaComponent(int callingAppId, int targetAppId)62     protected boolean isQueryableViaComponent(int callingAppId, int targetAppId) {
63         synchronized (mQueriesViaComponentLock) {
64             return super.isQueryableViaComponent(callingAppId, targetAppId);
65         }
66     }
67 
68     @Override
isImplicitlyQueryable(int callingUid, int targetUid)69     protected boolean isImplicitlyQueryable(int callingUid, int targetUid) {
70         synchronized (mImplicitlyQueryableLock) {
71             return super.isImplicitlyQueryable(callingUid, targetUid);
72         }
73     }
74 
75     @Override
isRetainedImplicitlyQueryable(int callingUid, int targetUid)76     protected boolean isRetainedImplicitlyQueryable(int callingUid, int targetUid) {
77         synchronized (mImplicitlyQueryableLock) {
78             return super.isRetainedImplicitlyQueryable(callingUid, targetUid);
79         }
80     }
81 
82     @Override
isQueryableViaUsesLibrary(int callingAppId, int targetAppId)83     protected boolean isQueryableViaUsesLibrary(int callingAppId, int targetAppId) {
84         synchronized (mQueryableViaUsesLibraryLock) {
85             return super.isQueryableViaUsesLibrary(callingAppId, targetAppId);
86         }
87     }
88 
89     @Override
isQueryableViaUsesPermission(int callingAppId, int targetAppId)90     protected boolean isQueryableViaUsesPermission(int callingAppId, int targetAppId) {
91         synchronized (mQueryableViaUsesPermissionLock) {
92             return super.isQueryableViaUsesPermission(callingAppId, targetAppId);
93         }
94     }
95 
96     @Override
shouldFilterApplicationUsingCache(int callingUid, int appId, int userId)97     protected boolean shouldFilterApplicationUsingCache(int callingUid, int appId, int userId) {
98         synchronized (mCacheLock) {
99             return super.shouldFilterApplicationUsingCache(callingUid, appId, userId);
100         }
101     }
102 
103     @Override
dumpForceQueryable(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)104     protected void dumpForceQueryable(PrintWriter pw, @Nullable Integer filteringAppId,
105             ToString<Integer> expandPackages) {
106         synchronized (mForceQueryableLock) {
107             super.dumpForceQueryable(pw, filteringAppId, expandPackages);
108         }
109     }
110 
111     @Override
dumpQueriesViaPackage(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)112     protected void dumpQueriesViaPackage(PrintWriter pw, @Nullable Integer filteringAppId,
113             ToString<Integer> expandPackages) {
114         synchronized (mQueriesViaPackageLock) {
115             super.dumpQueriesViaPackage(pw, filteringAppId, expandPackages);
116         }
117     }
118 
119     @Override
dumpQueriesViaComponent(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)120     protected void dumpQueriesViaComponent(PrintWriter pw, @Nullable Integer filteringAppId,
121             ToString<Integer> expandPackages) {
122         synchronized (mQueriesViaComponentLock) {
123             super.dumpQueriesViaComponent(pw, filteringAppId, expandPackages);
124         }
125     }
126 
127     @Override
dumpQueriesViaImplicitlyQueryable(PrintWriter pw, @Nullable Integer filteringAppId, int[] users, ToString<Integer> expandPackages)128     protected void dumpQueriesViaImplicitlyQueryable(PrintWriter pw,
129             @Nullable Integer filteringAppId, int[] users, ToString<Integer> expandPackages) {
130         synchronized (mImplicitlyQueryableLock) {
131             super.dumpQueriesViaImplicitlyQueryable(pw, filteringAppId, users, expandPackages);
132         }
133     }
134 
135     @Override
dumpQueriesViaUsesLibrary(PrintWriter pw, @Nullable Integer filteringAppId, ToString<Integer> expandPackages)136     protected void dumpQueriesViaUsesLibrary(PrintWriter pw, @Nullable Integer filteringAppId,
137             ToString<Integer> expandPackages) {
138         synchronized (mQueryableViaUsesLibraryLock) {
139             super.dumpQueriesViaUsesLibrary(pw, filteringAppId, expandPackages);
140         }
141     }
142 }
143