1 /*
2  * Copyright (C) 2021 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.app.ActivityManagerInternal;
20 import android.app.backup.IBackupManager;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.os.Handler;
24 import android.os.HandlerExecutor;
25 import android.os.incremental.IncrementalManager;
26 import android.util.DisplayMetrics;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.internal.pm.parsing.PackageParser2;
30 import com.android.server.SystemConfig;
31 import com.android.server.compat.PlatformCompat;
32 import com.android.server.pm.dex.ArtManagerService;
33 import com.android.server.pm.dex.DexManager;
34 import com.android.server.pm.dex.DynamicCodeLogger;
35 import com.android.server.pm.permission.LegacyPermissionManagerInternal;
36 import com.android.server.pm.permission.PermissionManagerServiceInternal;
37 import com.android.server.pm.resolution.ComponentResolver;
38 import com.android.server.pm.verify.domain.DomainVerificationManagerInternal;
39 
40 import java.util.List;
41 import java.util.concurrent.Executor;
42 
43 /**
44  * Unit tests will instantiate, extend and/or mock to mock dependencies / behaviors.
45  *
46  * NOTE: All getters should return the same instance for every call.
47  */
48 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
49 public class PackageManagerServiceInjector {
50     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
51     interface Producer<T> {
52         /** Produce an instance of type {@link T} */
produce(PackageManagerServiceInjector injector, PackageManagerService packageManager)53         T produce(PackageManagerServiceInjector injector, PackageManagerService packageManager);
54     }
55 
56     interface ProducerWithArgument<T, R> {
produce(PackageManagerServiceInjector injector, PackageManagerService packageManager, R argument)57         T produce(PackageManagerServiceInjector injector, PackageManagerService packageManager,
58                 R argument);
59     }
60 
61     interface ServiceProducer {
produce(Class<T> c)62         <T> T produce(Class<T> c);
63     }
64 
65     @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE)
66     static class Singleton<T> {
67         private final Producer<T> mProducer;
68         private volatile T mInstance = null;
69 
Singleton(Producer<T> producer)70         Singleton(Producer<T> producer) {
71             this.mProducer = producer;
72         }
73 
get(PackageManagerServiceInjector injector, PackageManagerService packageManagerService)74         T get(PackageManagerServiceInjector injector,
75                 PackageManagerService packageManagerService) {
76             if (mInstance == null) {
77                 mInstance = mProducer.produce(injector, packageManagerService);
78             }
79             return mInstance;
80         }
81     }
82 
83     private PackageManagerService mPackageManager;
84 
85     private final PackageAbiHelper mAbiHelper;
86     private final Context mContext;
87     private final PackageManagerTracedLock mLock;
88     private final Installer mInstaller;
89     private final PackageManagerTracedLock mInstallLock;
90     private final Handler mBackgroundHandler;
91     private final Executor mBackgroundExecutor;
92     private final List<ScanPartition> mSystemPartitions;
93 
94     // ----- producers -----
95     private final Singleton<ComponentResolver>
96             mComponentResolverProducer;
97     private final Singleton<PermissionManagerServiceInternal>
98             mPermissionManagerServiceProducer;
99     private final Singleton<UserManagerService>
100             mUserManagerProducer;
101     private final Singleton<Settings> mSettingsProducer;
102     private final Singleton<AppsFilterImpl> mAppsFilterProducer;
103     private final Singleton<PlatformCompat>
104             mPlatformCompatProducer;
105     private final Singleton<SystemConfig> mSystemConfigProducer;
106     private final Singleton<PackageDexOptimizer>
107             mPackageDexOptimizerProducer;
108     private final Singleton<DexManager> mDexManagerProducer;
109     private final Singleton<DynamicCodeLogger> mDynamicCodeLoggerProducer;
110     private final Singleton<ArtManagerService>
111             mArtManagerServiceProducer;
112     private final Singleton<ApexManager> mApexManagerProducer;
113     private final Singleton<IncrementalManager>
114             mIncrementalManagerProducer;
115     private final Singleton<DefaultAppProvider>
116             mDefaultAppProviderProducer;
117     private final Singleton<DisplayMetrics>
118             mDisplayMetricsProducer;
119     private final Producer<PackageParser2>
120             mScanningCachingPackageParserProducer;
121     private final Producer<PackageParser2>
122             mScanningPackageParserProducer;
123     private final Producer<PackageParser2>
124             mPreparingPackageParserProducer;
125     private final Singleton<PackageInstallerService>
126             mPackageInstallerServiceProducer;
127 
128     private final ProducerWithArgument<InstantAppResolverConnection, ComponentName>
129             mInstantAppResolverConnectionProducer;
130     private final Singleton<LegacyPermissionManagerInternal>
131             mLegacyPermissionManagerInternalProducer;
132     private final SystemWrapper mSystemWrapper;
133     private final ServiceProducer mGetLocalServiceProducer;
134     private final ServiceProducer mGetSystemServiceProducer;
135     private final Singleton<ModuleInfoProvider>
136             mModuleInfoProviderProducer;
137     private final Singleton<DomainVerificationManagerInternal>
138             mDomainVerificationManagerInternalProducer;
139     private final Singleton<Handler> mHandlerProducer;
140     private final Singleton<IBackupManager> mIBackupManager;
141     private final Singleton<SharedLibrariesImpl> mSharedLibrariesProducer;
142     private final Singleton<CrossProfileIntentFilterHelper> mCrossProfileIntentFilterHelperProducer;
143     private final Singleton<UpdateOwnershipHelper> mUpdateOwnershipHelperProducer;
144     private final Singleton<PackageMonitorCallbackHelper> mPackageMonitorCallbackHelper;
145 
PackageManagerServiceInjector(Context context, PackageManagerTracedLock lock, Installer installer, PackageManagerTracedLock installLock, PackageAbiHelper abiHelper, Handler backgroundHandler, List<ScanPartition> systemPartitions, Producer<ComponentResolver> componentResolverProducer, Producer<PermissionManagerServiceInternal> permissionManagerServiceProducer, Producer<UserManagerService> userManagerProducer, Producer<Settings> settingsProducer, Producer<AppsFilterImpl> appsFilterProducer, Producer<PlatformCompat> platformCompatProducer, Producer<SystemConfig> systemConfigProducer, Producer<PackageDexOptimizer> packageDexOptimizerProducer, Producer<DexManager> dexManagerProducer, Producer<DynamicCodeLogger> dynamicCodeLoggerProducer, Producer<ArtManagerService> artManagerServiceProducer, Producer<ApexManager> apexManagerProducer, Producer<IncrementalManager> incrementalManagerProducer, Producer<DefaultAppProvider> defaultAppProviderProducer, Producer<DisplayMetrics> displayMetricsProducer, Producer<PackageParser2> scanningCachingPackageParserProducer, Producer<PackageParser2> scanningPackageParserProducer, Producer<PackageParser2> preparingPackageParserProducer, Producer<PackageInstallerService> packageInstallerServiceProducer, ProducerWithArgument<InstantAppResolverConnection, ComponentName> instantAppResolverConnectionProducer, Producer<ModuleInfoProvider> moduleInfoProviderProducer, Producer<LegacyPermissionManagerInternal> legacyPermissionManagerInternalProducer, Producer<DomainVerificationManagerInternal> domainVerificationManagerInternalProducer, Producer<Handler> handlerProducer, SystemWrapper systemWrapper, ServiceProducer getLocalServiceProducer, ServiceProducer getSystemServiceProducer, Producer<IBackupManager> iBackupManager, Producer<SharedLibrariesImpl> sharedLibrariesProducer, Producer<CrossProfileIntentFilterHelper> crossProfileIntentFilterHelperProducer, Producer<UpdateOwnershipHelper> updateOwnershipHelperProducer, Producer<PackageMonitorCallbackHelper> packageMonitorCallbackHelper)146     PackageManagerServiceInjector(Context context, PackageManagerTracedLock lock,
147             Installer installer, PackageManagerTracedLock installLock, PackageAbiHelper abiHelper,
148             Handler backgroundHandler,
149             List<ScanPartition> systemPartitions,
150             Producer<ComponentResolver> componentResolverProducer,
151             Producer<PermissionManagerServiceInternal> permissionManagerServiceProducer,
152             Producer<UserManagerService> userManagerProducer,
153             Producer<Settings> settingsProducer,
154             Producer<AppsFilterImpl> appsFilterProducer,
155             Producer<PlatformCompat> platformCompatProducer,
156             Producer<SystemConfig> systemConfigProducer,
157             Producer<PackageDexOptimizer> packageDexOptimizerProducer,
158             Producer<DexManager> dexManagerProducer,
159             Producer<DynamicCodeLogger> dynamicCodeLoggerProducer,
160             Producer<ArtManagerService> artManagerServiceProducer,
161             Producer<ApexManager> apexManagerProducer,
162             Producer<IncrementalManager> incrementalManagerProducer,
163             Producer<DefaultAppProvider> defaultAppProviderProducer,
164             Producer<DisplayMetrics> displayMetricsProducer,
165             Producer<PackageParser2> scanningCachingPackageParserProducer,
166             Producer<PackageParser2> scanningPackageParserProducer,
167             Producer<PackageParser2> preparingPackageParserProducer,
168             Producer<PackageInstallerService> packageInstallerServiceProducer,
169             ProducerWithArgument<InstantAppResolverConnection,
170                     ComponentName>
171                     instantAppResolverConnectionProducer,
172             Producer<ModuleInfoProvider> moduleInfoProviderProducer,
173             Producer<LegacyPermissionManagerInternal> legacyPermissionManagerInternalProducer,
174             Producer<DomainVerificationManagerInternal>
175                     domainVerificationManagerInternalProducer,
176             Producer<Handler> handlerProducer,
177             SystemWrapper systemWrapper,
178             ServiceProducer getLocalServiceProducer,
179             ServiceProducer getSystemServiceProducer,
180             Producer<IBackupManager> iBackupManager,
181             Producer<SharedLibrariesImpl> sharedLibrariesProducer,
182             Producer<CrossProfileIntentFilterHelper> crossProfileIntentFilterHelperProducer,
183             Producer<UpdateOwnershipHelper> updateOwnershipHelperProducer,
184             Producer<PackageMonitorCallbackHelper> packageMonitorCallbackHelper) {
185         mContext = context;
186         mLock = lock;
187         mInstaller = installer;
188         mAbiHelper = abiHelper;
189         mInstallLock = installLock;
190         mBackgroundHandler = backgroundHandler;
191         mBackgroundExecutor = new HandlerExecutor(backgroundHandler);
192         mSystemPartitions = systemPartitions;
193         mComponentResolverProducer = new Singleton<>(
194                 componentResolverProducer);
195         mPermissionManagerServiceProducer = new Singleton<>(
196                 permissionManagerServiceProducer);
197         mUserManagerProducer = new Singleton<>(userManagerProducer);
198         mSettingsProducer = new Singleton<>(settingsProducer);
199         mAppsFilterProducer = new Singleton<>(appsFilterProducer);
200         mPlatformCompatProducer = new Singleton<>(
201                 platformCompatProducer);
202         mSystemConfigProducer = new Singleton<>(systemConfigProducer);
203         mPackageDexOptimizerProducer = new Singleton<>(
204                 packageDexOptimizerProducer);
205         mDexManagerProducer = new Singleton<>(dexManagerProducer);
206         mDynamicCodeLoggerProducer = new Singleton<>(dynamicCodeLoggerProducer);
207         mArtManagerServiceProducer = new Singleton<>(
208                 artManagerServiceProducer);
209         mApexManagerProducer = new Singleton<>(apexManagerProducer);
210         mIncrementalManagerProducer = new Singleton<>(
211                 incrementalManagerProducer);
212         mDefaultAppProviderProducer = new Singleton<>(
213                 defaultAppProviderProducer);
214         mDisplayMetricsProducer = new Singleton<>(
215                 displayMetricsProducer);
216         mScanningCachingPackageParserProducer = scanningCachingPackageParserProducer;
217         mScanningPackageParserProducer = scanningPackageParserProducer;
218         mPreparingPackageParserProducer = preparingPackageParserProducer;
219         mPackageInstallerServiceProducer = new Singleton<>(
220                 packageInstallerServiceProducer);
221         mInstantAppResolverConnectionProducer = instantAppResolverConnectionProducer;
222         mModuleInfoProviderProducer = new Singleton<>(
223                 moduleInfoProviderProducer);
224         mLegacyPermissionManagerInternalProducer = new Singleton<>(
225                 legacyPermissionManagerInternalProducer);
226         mSystemWrapper = systemWrapper;
227         mGetLocalServiceProducer = getLocalServiceProducer;
228         mGetSystemServiceProducer = getSystemServiceProducer;
229         mDomainVerificationManagerInternalProducer =
230                 new Singleton<>(
231                         domainVerificationManagerInternalProducer);
232         mHandlerProducer = new Singleton<>(handlerProducer);
233         mIBackupManager = new Singleton<>(iBackupManager);
234         mSharedLibrariesProducer = new Singleton<>(sharedLibrariesProducer);
235         mCrossProfileIntentFilterHelperProducer = new Singleton<>(
236                 crossProfileIntentFilterHelperProducer);
237         mUpdateOwnershipHelperProducer = new Singleton<>(updateOwnershipHelperProducer);
238         mPackageMonitorCallbackHelper = new Singleton<>(packageMonitorCallbackHelper);
239     }
240 
241     /**
242      * Bootstraps this injector with the {@link PackageManagerService instance to which it
243      * belongs.
244      */
bootstrap(PackageManagerService pm)245     public void bootstrap(PackageManagerService pm) {
246         this.mPackageManager = pm;
247     }
248 
getUserManagerInternal()249     public UserManagerInternal getUserManagerInternal() {
250         return getUserManagerService().getInternalForInjectorOnly();
251     }
252 
getAbiHelper()253     public PackageAbiHelper getAbiHelper() {
254         return mAbiHelper;
255     }
256 
getInstallLock()257     public PackageManagerTracedLock getInstallLock() {
258         return mInstallLock;
259     }
260 
getSystemPartitions()261     public List<ScanPartition> getSystemPartitions() {
262         return mSystemPartitions;
263     }
264 
getUserManagerService()265     public UserManagerService getUserManagerService() {
266         return mUserManagerProducer.get(this, mPackageManager);
267     }
268 
getLock()269     public PackageManagerTracedLock getLock() {
270         return mLock;
271     }
272 
273     /**
274      * {@link CrossProfileIntentFilterHelper} which manages {@link CrossProfileIntentFilter}
275      * @return CrossProfileIntentFilterHelper
276      */
getCrossProfileIntentFilterHelper()277     public CrossProfileIntentFilterHelper getCrossProfileIntentFilterHelper() {
278         return mCrossProfileIntentFilterHelperProducer.get(this, mPackageManager);
279     }
280 
getInstaller()281     public Installer getInstaller() {
282         return mInstaller;
283     }
284 
getComponentResolver()285     public ComponentResolver getComponentResolver() {
286         return mComponentResolverProducer.get(this, mPackageManager);
287     }
288 
getPermissionManagerServiceInternal()289     public PermissionManagerServiceInternal getPermissionManagerServiceInternal() {
290         return mPermissionManagerServiceProducer.get(this, mPackageManager);
291     }
292 
getContext()293     public Context getContext() {
294         return mContext;
295     }
296 
getSettings()297     public Settings getSettings() {
298         return mSettingsProducer.get(this, mPackageManager);
299     }
300 
getAppsFilter()301     public AppsFilterImpl getAppsFilter() {
302         return mAppsFilterProducer.get(this, mPackageManager);
303     }
304 
getCompatibility()305     public PlatformCompat getCompatibility() {
306         return mPlatformCompatProducer.get(this, mPackageManager);
307     }
308 
getSystemConfig()309     public SystemConfig getSystemConfig() {
310         return mSystemConfigProducer.get(this, mPackageManager);
311     }
312 
getPackageDexOptimizer()313     public PackageDexOptimizer getPackageDexOptimizer() {
314         return mPackageDexOptimizerProducer.get(this, mPackageManager);
315     }
316 
getDexManager()317     public DexManager getDexManager() {
318         return mDexManagerProducer.get(this, mPackageManager);
319     }
320 
getDynamicCodeLogger()321     public DynamicCodeLogger getDynamicCodeLogger() {
322         return mDynamicCodeLoggerProducer.get(this, mPackageManager);
323     }
324 
getArtManagerService()325     public ArtManagerService getArtManagerService() {
326         return mArtManagerServiceProducer.get(this, mPackageManager);
327     }
328 
getApexManager()329     public ApexManager getApexManager() {
330         return mApexManagerProducer.get(this, mPackageManager);
331     }
332 
getBackgroundHandler()333     public Handler getBackgroundHandler() {
334         return mBackgroundHandler;
335     }
336 
getBackgroundExecutor()337     public Executor getBackgroundExecutor() {
338         return mBackgroundExecutor;
339     }
340 
getDisplayMetrics()341     public DisplayMetrics getDisplayMetrics() {
342         return mDisplayMetricsProducer.get(this, mPackageManager);
343     }
344 
getLocalService(Class<T> c)345     public <T> T getLocalService(Class<T> c) {
346         return mGetLocalServiceProducer.produce(c);
347     }
348 
getSystemService(Class<T> c)349     public <T> T getSystemService(Class<T> c) {
350         return mGetSystemServiceProducer.produce(c);
351     }
352 
getSystemWrapper()353     public SystemWrapper getSystemWrapper() {
354         return mSystemWrapper;
355     }
356 
getIncrementalManager()357     public IncrementalManager getIncrementalManager() {
358         return mIncrementalManagerProducer.get(this, mPackageManager);
359     }
360 
getDefaultAppProvider()361     public DefaultAppProvider getDefaultAppProvider() {
362         return mDefaultAppProviderProducer.get(this, mPackageManager);
363     }
364 
getScanningCachingPackageParser()365     public PackageParser2 getScanningCachingPackageParser() {
366         return mScanningCachingPackageParserProducer.produce(this, mPackageManager);
367     }
368 
getScanningPackageParser()369     public PackageParser2 getScanningPackageParser() {
370         return mScanningPackageParserProducer.produce(this, mPackageManager);
371     }
372 
getPreparingPackageParser()373     public PackageParser2 getPreparingPackageParser() {
374         return mPreparingPackageParserProducer.produce(this, mPackageManager);
375     }
376 
getPackageInstallerService()377     public PackageInstallerService getPackageInstallerService() {
378         return mPackageInstallerServiceProducer.get(this, mPackageManager);
379     }
380 
getInstantAppResolverConnection( ComponentName instantAppResolverComponent)381     public InstantAppResolverConnection getInstantAppResolverConnection(
382             ComponentName instantAppResolverComponent) {
383         return mInstantAppResolverConnectionProducer.produce(
384                 this, mPackageManager, instantAppResolverComponent);
385     }
386 
getModuleInfoProvider()387     public ModuleInfoProvider getModuleInfoProvider() {
388         return mModuleInfoProviderProducer.get(this, mPackageManager);
389     }
390 
getLegacyPermissionManagerInternal()391     public LegacyPermissionManagerInternal getLegacyPermissionManagerInternal() {
392         return mLegacyPermissionManagerInternalProducer.get(this, mPackageManager);
393     }
394 
getDomainVerificationManagerInternal()395     public DomainVerificationManagerInternal getDomainVerificationManagerInternal() {
396         return mDomainVerificationManagerInternalProducer.get(this, mPackageManager);
397     }
398 
getHandler()399     public Handler getHandler() {
400         return mHandlerProducer.get(this, mPackageManager);
401     }
402 
getActivityManagerInternal()403     public ActivityManagerInternal getActivityManagerInternal() {
404         return getLocalService(ActivityManagerInternal.class);
405     }
406 
getIBackupManager()407     public IBackupManager getIBackupManager() {
408         return mIBackupManager.get(this, mPackageManager);
409     }
410 
getSharedLibrariesImpl()411     public SharedLibrariesImpl getSharedLibrariesImpl() {
412         return mSharedLibrariesProducer.get(this, mPackageManager);
413     }
414 
getUpdateOwnershipHelper()415     public UpdateOwnershipHelper getUpdateOwnershipHelper() {
416         return mUpdateOwnershipHelperProducer.get(this, mPackageManager);
417     }
418 
getPackageMonitorCallbackHelper()419     public PackageMonitorCallbackHelper getPackageMonitorCallbackHelper() {
420         return mPackageMonitorCallbackHelper.get(this, mPackageManager);
421     }
422 
423 
424     /** Provides an abstraction to static access to system state. */
425     public interface SystemWrapper {
disablePackageCaches()426         void disablePackageCaches();
enablePackageCaches()427         void enablePackageCaches();
428     }
429 }
430