DexManager.java revision dea3fd8cf1000d786ff40e1036a8bf76a55d5fcd
1/*
2 * Copyright (C) 2016 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
17package com.android.server.pm.dex;
18
19import android.content.pm.ApplicationInfo;
20import android.content.pm.IPackageManager;
21import android.content.pm.PackageInfo;
22import android.os.FileUtils;
23import android.os.RemoteException;
24import android.os.storage.StorageManager;
25import android.os.UserHandle;
26
27import android.util.Slog;
28
29import com.android.internal.annotations.GuardedBy;
30import com.android.server.pm.Installer;
31import com.android.server.pm.Installer.InstallerException;
32import com.android.server.pm.PackageDexOptimizer;
33import com.android.server.pm.PackageManagerService;
34import com.android.server.pm.PackageManagerServiceUtils;
35import com.android.server.pm.PackageManagerServiceCompilerMapping;
36
37import java.io.File;
38import java.io.IOException;
39import java.util.List;
40import java.util.HashMap;
41import java.util.HashSet;
42import java.util.Map;
43import java.util.Set;
44
45import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
46import static com.android.server.pm.dex.PackageDexUsage.PackageUseInfo;
47import static com.android.server.pm.dex.PackageDexUsage.DexUseInfo;
48
49/**
50 * This class keeps track of how dex files are used.
51 * Every time it gets a notification about a dex file being loaded it tracks
52 * its owning package and records it in PackageDexUsage (package-dex-usage.list).
53 *
54 * TODO(calin): Extract related dexopt functionality from PackageManagerService
55 * into this class.
56 */
57public class DexManager {
58    private static final String TAG = "DexManager";
59
60    private static final boolean DEBUG = false;
61
62    // Maps package name to code locations.
63    // It caches the code locations for the installed packages. This allows for
64    // faster lookups (no locks) when finding what package owns the dex file.
65    @GuardedBy("mPackageCodeLocationsCache")
66    private final Map<String, PackageCodeLocations> mPackageCodeLocationsCache;
67
68    // PackageDexUsage handles the actual I/O operations. It is responsible to
69    // encode and save the dex usage data.
70    private final PackageDexUsage mPackageDexUsage;
71
72    private final IPackageManager mPackageManager;
73    private final PackageDexOptimizer mPackageDexOptimizer;
74    private final Object mInstallLock;
75    @GuardedBy("mInstallLock")
76    private final Installer mInstaller;
77
78    // Possible outcomes of a dex search.
79    private static int DEX_SEARCH_NOT_FOUND = 0;  // dex file not found
80    private static int DEX_SEARCH_FOUND_PRIMARY = 1;  // dex file is the primary/base apk
81    private static int DEX_SEARCH_FOUND_SPLIT = 2;  // dex file is a split apk
82    private static int DEX_SEARCH_FOUND_SECONDARY = 3;  // dex file is a secondary dex
83
84    public DexManager(IPackageManager pms, PackageDexOptimizer pdo,
85            Installer installer, Object installLock) {
86      mPackageCodeLocationsCache = new HashMap<>();
87      mPackageDexUsage = new PackageDexUsage();
88      mPackageManager = pms;
89      mPackageDexOptimizer = pdo;
90      mInstaller = installer;
91      mInstallLock = installLock;
92    }
93
94    /**
95     * Notify about dex files loads.
96     * Note that this method is invoked when apps load dex files and it should
97     * return as fast as possible.
98     *
99     * @param loadingAppInfo the package performing the load
100     * @param dexPaths the list of dex files being loaded
101     * @param loaderIsa the ISA of the app loading the dex files
102     * @param loaderUserId the user id which runs the code loading the dex files
103     */
104    public void notifyDexLoad(ApplicationInfo loadingAppInfo, List<String> dexPaths,
105            String loaderIsa, int loaderUserId) {
106        try {
107            notifyDexLoadInternal(loadingAppInfo, dexPaths, loaderIsa, loaderUserId);
108        } catch (Exception e) {
109            Slog.w(TAG, "Exception while notifying dex load for package " +
110                    loadingAppInfo.packageName, e);
111        }
112    }
113
114    private void notifyDexLoadInternal(ApplicationInfo loadingAppInfo, List<String> dexPaths,
115            String loaderIsa, int loaderUserId) {
116        if (!PackageManagerServiceUtils.checkISA(loaderIsa)) {
117            Slog.w(TAG, "Loading dex files " + dexPaths + " in unsupported ISA: " +
118                    loaderIsa + "?");
119            return;
120        }
121
122        for (String dexPath : dexPaths) {
123            // Find the owning package name.
124            DexSearchResult searchResult = getDexPackage(loadingAppInfo, dexPath, loaderUserId);
125
126            if (DEBUG) {
127                Slog.i(TAG, loadingAppInfo.packageName
128                    + " loads from " + searchResult + " : " + loaderUserId + " : " + dexPath);
129            }
130
131            if (searchResult.mOutcome != DEX_SEARCH_NOT_FOUND) {
132                // TODO(calin): extend isUsedByOtherApps check to detect the cases where
133                // different apps share the same runtime. In that case we should not mark the dex
134                // file as isUsedByOtherApps. Currently this is a safe approximation.
135                boolean isUsedByOtherApps = !loadingAppInfo.packageName.equals(
136                        searchResult.mOwningPackageName);
137                boolean primaryOrSplit = searchResult.mOutcome == DEX_SEARCH_FOUND_PRIMARY ||
138                        searchResult.mOutcome == DEX_SEARCH_FOUND_SPLIT;
139
140                if (primaryOrSplit && !isUsedByOtherApps) {
141                    // If the dex file is the primary apk (or a split) and not isUsedByOtherApps
142                    // do not record it. This case does not bring any new usable information
143                    // and can be safely skipped.
144                    continue;
145                }
146
147                // Record dex file usage. If the current usage is a new pattern (e.g. new secondary,
148                // or UsedBytOtherApps), record will return true and we trigger an async write
149                // to disk to make sure we don't loose the data in case of a reboot.
150                if (mPackageDexUsage.record(searchResult.mOwningPackageName,
151                        dexPath, loaderUserId, loaderIsa, isUsedByOtherApps, primaryOrSplit)) {
152                    mPackageDexUsage.maybeWriteAsync();
153                }
154            } else {
155                // This can happen in a few situations:
156                // - bogus dex loads
157                // - recent installs/uninstalls that we didn't detect.
158                // - new installed splits
159                // If we can't find the owner of the dex we simply do not track it. The impact is
160                // that the dex file will not be considered for offline optimizations.
161                // TODO(calin): add hooks for move/uninstall notifications to
162                // capture package moves or obsolete packages.
163                if (DEBUG) {
164                    Slog.i(TAG, "Could not find owning package for dex file: " + dexPath);
165                }
166            }
167        }
168    }
169
170    /**
171     * Read the dex usage from disk and populate the code cache locations.
172     * @param existingPackages a map containing information about what packages
173     *          are available to what users. Only packages in this list will be
174     *          recognized during notifyDexLoad().
175     */
176    public void load(Map<Integer, List<PackageInfo>> existingPackages) {
177        try {
178            loadInternal(existingPackages);
179        } catch (Exception e) {
180            mPackageDexUsage.clear();
181            Slog.w(TAG, "Exception while loading package dex usage. " +
182                    "Starting with a fresh state.", e);
183        }
184    }
185
186    /**
187     * Notifies that a new package was installed for {@code userId}.
188     * {@code userId} must not be {@code UserHandle.USER_ALL}.
189     *
190     * @throws IllegalArgumentException if {@code userId} is {@code UserHandle.USER_ALL}.
191     */
192    public void notifyPackageInstalled(PackageInfo pi, int userId) {
193        if (userId == UserHandle.USER_ALL) {
194            throw new IllegalArgumentException(
195                "notifyPackageInstalled called with USER_ALL");
196        }
197        cachePackageInfo(pi, userId);
198    }
199
200    /**
201     * Notifies that package {@code packageName} was updated.
202     * This will clear the UsedByOtherApps mark if it exists.
203     */
204    public void notifyPackageUpdated(String packageName, String baseCodePath,
205            String[] splitCodePaths) {
206        cachePackageCodeLocation(packageName, baseCodePath, splitCodePaths, null, /*userId*/ -1);
207        // In case there was an update, write the package use info to disk async.
208        // Note that we do the writing here and not in PackageDexUsage in order to be
209        // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs
210        // multiple updates in PackageDexUsage before writing it).
211        if (mPackageDexUsage.clearUsedByOtherApps(packageName)) {
212            mPackageDexUsage.maybeWriteAsync();
213        }
214    }
215
216    /**
217     * Notifies that the user {@code userId} data for package {@code packageName}
218     * was destroyed. This will remove all usage info associated with the package
219     * for the given user.
220     * {@code userId} is allowed to be {@code UserHandle.USER_ALL} in which case
221     * all usage information for the package will be removed.
222     */
223    public void notifyPackageDataDestroyed(String packageName, int userId) {
224        boolean updated = userId == UserHandle.USER_ALL
225            ? mPackageDexUsage.removePackage(packageName)
226            : mPackageDexUsage.removeUserPackage(packageName, userId);
227        // In case there was an update, write the package use info to disk async.
228        // Note that we do the writing here and not in PackageDexUsage in order to be
229        // consistent with other methods in DexManager (e.g. reconcileSecondaryDexFiles performs
230        // multiple updates in PackageDexUsage before writing it).
231        if (updated) {
232            mPackageDexUsage.maybeWriteAsync();
233        }
234    }
235
236    /**
237     * Caches the code location from the given package info.
238     */
239    private void cachePackageInfo(PackageInfo pi, int userId) {
240        ApplicationInfo ai = pi.applicationInfo;
241        String[] dataDirs = new String[] {ai.dataDir, ai.deviceProtectedDataDir,
242                ai.credentialProtectedDataDir};
243        cachePackageCodeLocation(pi.packageName, ai.sourceDir, ai.splitSourceDirs,
244                dataDirs, userId);
245    }
246
247    private void cachePackageCodeLocation(String packageName, String baseCodePath,
248            String[] splitCodePaths, String[] dataDirs, int userId) {
249        synchronized (mPackageCodeLocationsCache) {
250            PackageCodeLocations pcl = putIfAbsent(mPackageCodeLocationsCache, packageName,
251                    new PackageCodeLocations(packageName, baseCodePath, splitCodePaths));
252            // TODO(calin): We are forced to extend the scope of this synchronization because
253            // the values of the cache (PackageCodeLocations) are updated in place.
254            // Make PackageCodeLocations immutable to simplify the synchronization reasoning.
255            pcl.updateCodeLocation(baseCodePath, splitCodePaths);
256            if (dataDirs != null) {
257                for (String dataDir : dataDirs) {
258                    // The set of data dirs includes deviceProtectedDataDir and
259                    // credentialProtectedDataDir which might be null for shared
260                    // libraries. Currently we don't track these but be lenient
261                    // and check in case we ever decide to store their usage data.
262                    if (dataDir != null) {
263                        pcl.mergeAppDataDirs(dataDir, userId);
264                    }
265                }
266            }
267        }
268    }
269
270    private void loadInternal(Map<Integer, List<PackageInfo>> existingPackages) {
271        Map<String, Set<Integer>> packageToUsersMap = new HashMap<>();
272        // Cache the code locations for the installed packages. This allows for
273        // faster lookups (no locks) when finding what package owns the dex file.
274        for (Map.Entry<Integer, List<PackageInfo>> entry : existingPackages.entrySet()) {
275            List<PackageInfo> packageInfoList = entry.getValue();
276            int userId = entry.getKey();
277            for (PackageInfo pi : packageInfoList) {
278                // Cache the code locations.
279                cachePackageInfo(pi, userId);
280
281                // Cache a map from package name to the set of user ids who installed the package.
282                // We will use it to sync the data and remove obsolete entries from
283                // mPackageDexUsage.
284                Set<Integer> users = putIfAbsent(
285                        packageToUsersMap, pi.packageName, new HashSet<>());
286                users.add(userId);
287            }
288        }
289
290        mPackageDexUsage.read();
291        mPackageDexUsage.syncData(packageToUsersMap);
292    }
293
294    /**
295     * Get the package dex usage for the given package name.
296     * @return the package data or null if there is no data available for this package.
297     */
298    public PackageUseInfo getPackageUseInfo(String packageName) {
299        return mPackageDexUsage.getPackageUseInfo(packageName);
300    }
301
302    /**
303     * Perform dexopt on with the given {@code options} on the secondary dex files.
304     * @return true if all secondary dex files were processed successfully (compiled or skipped
305     *         because they don't need to be compiled)..
306     */
307    public boolean dexoptSecondaryDex(DexoptOptions options) {
308        // Select the dex optimizer based on the force parameter.
309        // Forced compilation is done through ForcedUpdatePackageDexOptimizer which will adjust
310        // the necessary dexopt flags to make sure that compilation is not skipped. This avoid
311        // passing the force flag through the multitude of layers.
312        // Note: The force option is rarely used (cmdline input for testing, mostly), so it's OK to
313        //       allocate an object here.
314        PackageDexOptimizer pdo = options.isForce()
315                ? new PackageDexOptimizer.ForcedUpdatePackageDexOptimizer(mPackageDexOptimizer)
316                : mPackageDexOptimizer;
317        String packageName = options.getPackageName();
318        PackageUseInfo useInfo = getPackageUseInfo(packageName);
319        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
320            if (DEBUG) {
321                Slog.d(TAG, "No secondary dex use for package:" + packageName);
322            }
323            // Nothing to compile, return true.
324            return true;
325        }
326        boolean success = true;
327        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
328            String dexPath = entry.getKey();
329            DexUseInfo dexUseInfo = entry.getValue();
330            if (options.isDexoptOnlySharedDex() && !dexUseInfo.isUsedByOtherApps()) {
331                continue;
332            }
333            PackageInfo pkg = null;
334            try {
335                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
336                    dexUseInfo.getOwnerUserId());
337            } catch (RemoteException e) {
338                throw new AssertionError(e);
339            }
340            // It may be that the package gets uninstalled while we try to compile its
341            // secondary dex files. If that's the case, just ignore.
342            // Note that we don't break the entire loop because the package might still be
343            // installed for other users.
344            if (pkg == null) {
345                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
346                        + " for user " + dexUseInfo.getOwnerUserId());
347                mPackageDexUsage.removeUserPackage(packageName, dexUseInfo.getOwnerUserId());
348                continue;
349            }
350
351            int result = pdo.dexOptSecondaryDexPath(pkg.applicationInfo, dexPath,
352                    dexUseInfo.getLoaderIsas(), options.getCompilerFilter(),
353                    dexUseInfo.isUsedByOtherApps(), options.isDowngrade());
354            success = success && (result != PackageDexOptimizer.DEX_OPT_FAILED);
355        }
356        return success;
357    }
358
359    /**
360     * Reconcile the information we have about the secondary dex files belonging to
361     * {@code packagName} and the actual dex files. For all dex files that were
362     * deleted, update the internal records and delete any generated oat files.
363     */
364    public void reconcileSecondaryDexFiles(String packageName) {
365        PackageUseInfo useInfo = getPackageUseInfo(packageName);
366        if (useInfo == null || useInfo.getDexUseInfoMap().isEmpty()) {
367            if (DEBUG) {
368                Slog.d(TAG, "No secondary dex use for package:" + packageName);
369            }
370            // Nothing to reconcile.
371            return;
372        }
373
374        boolean updated = false;
375        for (Map.Entry<String, DexUseInfo> entry : useInfo.getDexUseInfoMap().entrySet()) {
376            String dexPath = entry.getKey();
377            DexUseInfo dexUseInfo = entry.getValue();
378            PackageInfo pkg = null;
379            try {
380                // Note that we look for the package in the PackageManager just to be able
381                // to get back the real app uid and its storage kind. These are only used
382                // to perform extra validation in installd.
383                // TODO(calin): maybe a bit overkill.
384                pkg = mPackageManager.getPackageInfo(packageName, /*flags*/0,
385                    dexUseInfo.getOwnerUserId());
386            } catch (RemoteException ignore) {
387                // Can't happen, DexManager is local.
388            }
389            if (pkg == null) {
390                // It may be that the package was uninstalled while we process the secondary
391                // dex files.
392                Slog.d(TAG, "Could not find package when compiling secondary dex " + packageName
393                        + " for user " + dexUseInfo.getOwnerUserId());
394                // Update the usage and continue, another user might still have the package.
395                updated = mPackageDexUsage.removeUserPackage(
396                        packageName, dexUseInfo.getOwnerUserId()) || updated;
397                continue;
398            }
399            ApplicationInfo info = pkg.applicationInfo;
400            int flags = 0;
401            if (info.deviceProtectedDataDir != null &&
402                    FileUtils.contains(info.deviceProtectedDataDir, dexPath)) {
403                flags |= StorageManager.FLAG_STORAGE_DE;
404            } else if (info.credentialProtectedDataDir!= null &&
405                    FileUtils.contains(info.credentialProtectedDataDir, dexPath)) {
406                flags |= StorageManager.FLAG_STORAGE_CE;
407            } else {
408                Slog.e(TAG, "Could not infer CE/DE storage for path " + dexPath);
409                updated = mPackageDexUsage.removeDexFile(
410                        packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated;
411                continue;
412            }
413
414            boolean dexStillExists = true;
415            synchronized(mInstallLock) {
416                try {
417                    String[] isas = dexUseInfo.getLoaderIsas().toArray(new String[0]);
418                    dexStillExists = mInstaller.reconcileSecondaryDexFile(dexPath, packageName,
419                            pkg.applicationInfo.uid, isas, pkg.applicationInfo.volumeUuid, flags);
420                } catch (InstallerException e) {
421                    Slog.e(TAG, "Got InstallerException when reconciling dex " + dexPath +
422                            " : " + e.getMessage());
423                }
424            }
425            if (!dexStillExists) {
426                updated = mPackageDexUsage.removeDexFile(
427                        packageName, dexPath, dexUseInfo.getOwnerUserId()) || updated;
428            }
429
430        }
431        if (updated) {
432            mPackageDexUsage.maybeWriteAsync();
433        }
434    }
435
436    public RegisterDexModuleResult registerDexModule(ApplicationInfo info, String dexPath,
437            boolean isUsedByOtherApps, int userId) {
438        // Find the owning package record.
439        DexSearchResult searchResult = getDexPackage(info, dexPath, userId);
440
441        if (searchResult.mOutcome == DEX_SEARCH_NOT_FOUND) {
442            return new RegisterDexModuleResult(false, "Package not found");
443        }
444        if (!info.packageName.equals(searchResult.mOwningPackageName)) {
445            return new RegisterDexModuleResult(false, "Dex path does not belong to package");
446        }
447        if (searchResult.mOutcome == DEX_SEARCH_FOUND_PRIMARY ||
448                searchResult.mOutcome == DEX_SEARCH_FOUND_SPLIT) {
449            return new RegisterDexModuleResult(false, "Main apks cannot be registered");
450        }
451
452        // We found the package. Now record the usage for all declared ISAs.
453        boolean update = false;
454        Set<String> isas = new HashSet<>();
455        for (String isa : getAppDexInstructionSets(info)) {
456            isas.add(isa);
457            boolean newUpdate = mPackageDexUsage.record(searchResult.mOwningPackageName,
458                dexPath, userId, isa, isUsedByOtherApps, /*primaryOrSplit*/ false);
459            update |= newUpdate;
460        }
461        if (update) {
462            mPackageDexUsage.maybeWriteAsync();
463        }
464
465        // Try to optimize the package according to the install reason.
466        String compilerFilter = PackageManagerServiceCompilerMapping.getCompilerFilterForReason(
467                PackageManagerService.REASON_INSTALL);
468        int result = mPackageDexOptimizer.dexOptSecondaryDexPath(info, dexPath, isas,
469                compilerFilter, isUsedByOtherApps, /* downgrade */ false);
470
471        // If we fail to optimize the package log an error but don't propagate the error
472        // back to the app. The app cannot do much about it and the background job
473        // will rety again when it executes.
474        // TODO(calin): there might be some value to return the error here but it may
475        // cause red herrings since that doesn't mean the app cannot use the module.
476        if (result != PackageDexOptimizer.DEX_OPT_FAILED) {
477            Slog.e(TAG, "Failed to optimize dex module " + dexPath);
478        }
479        return new RegisterDexModuleResult(true, "Dex module registered successfully");
480    }
481
482    /**
483     * Return all packages that contain records of secondary dex files.
484     */
485    public Set<String> getAllPackagesWithSecondaryDexFiles() {
486        return mPackageDexUsage.getAllPackagesWithSecondaryDexFiles();
487    }
488
489    /**
490     * Return true if the profiling data collected for the given app indicate
491     * that the apps's APK has been loaded by another app.
492     * Note that this returns false for all apps without any collected profiling data.
493    */
494    public boolean isUsedByOtherApps(String packageName) {
495        PackageUseInfo useInfo = getPackageUseInfo(packageName);
496        if (useInfo == null) {
497            // No use info, means the package was not used or it was used but not by other apps.
498            // Note that right now we might prune packages which are not used by other apps.
499            // TODO(calin): maybe we should not (prune) so we can have an accurate view when we try
500            // to access the package use.
501            return false;
502        }
503        return useInfo.isUsedByOtherApps();
504    }
505
506    /**
507     * Retrieves the package which owns the given dexPath.
508     */
509    private DexSearchResult getDexPackage(
510            ApplicationInfo loadingAppInfo, String dexPath, int userId) {
511        // Ignore framework code.
512        // TODO(calin): is there a better way to detect it?
513        if (dexPath.startsWith("/system/framework/")) {
514            return new DexSearchResult("framework", DEX_SEARCH_NOT_FOUND);
515        }
516
517        // First, check if the package which loads the dex file actually owns it.
518        // Most of the time this will be true and we can return early.
519        PackageCodeLocations loadingPackageCodeLocations =
520                new PackageCodeLocations(loadingAppInfo, userId);
521        int outcome = loadingPackageCodeLocations.searchDex(dexPath, userId);
522        if (outcome != DEX_SEARCH_NOT_FOUND) {
523            // TODO(calin): evaluate if we bother to detect symlinks at the dexPath level.
524            return new DexSearchResult(loadingPackageCodeLocations.mPackageName, outcome);
525        }
526
527        // The loadingPackage does not own the dex file.
528        // Perform a reverse look-up in the cache to detect if any package has ownership.
529        // Note that we can have false negatives if the cache falls out of date.
530        synchronized (mPackageCodeLocationsCache) {
531            for (PackageCodeLocations pcl : mPackageCodeLocationsCache.values()) {
532                outcome = pcl.searchDex(dexPath, userId);
533                if (outcome != DEX_SEARCH_NOT_FOUND) {
534                    return new DexSearchResult(pcl.mPackageName, outcome);
535                }
536            }
537        }
538
539        if (DEBUG) {
540            // TODO(calin): Consider checking for /data/data symlink.
541            // /data/data/ symlinks /data/user/0/ and there's nothing stopping apps
542            // to load dex files through it.
543            try {
544                String dexPathReal = PackageManagerServiceUtils.realpath(new File(dexPath));
545                if (dexPathReal != dexPath) {
546                    Slog.d(TAG, "Dex loaded with symlink. dexPath=" +
547                            dexPath + " dexPathReal=" + dexPathReal);
548                }
549            } catch (IOException e) {
550                // Ignore
551            }
552        }
553        // Cache miss. The cache is updated during installs and uninstalls,
554        // so if we get here we're pretty sure the dex path does not exist.
555        return new DexSearchResult(null, DEX_SEARCH_NOT_FOUND);
556    }
557
558    private static <K,V> V putIfAbsent(Map<K,V> map, K key, V newValue) {
559        V existingValue = map.putIfAbsent(key, newValue);
560        return existingValue == null ? newValue : existingValue;
561    }
562
563    public static class RegisterDexModuleResult {
564        public RegisterDexModuleResult() {
565            this(false, null);
566        }
567
568        public RegisterDexModuleResult(boolean success, String message) {
569            this.success = success;
570            this.message = message;
571        }
572
573        public final boolean success;
574        public final String message;
575    }
576
577    /**
578     * Convenience class to store the different locations where a package might
579     * own code.
580     */
581    private static class PackageCodeLocations {
582        private final String mPackageName;
583        private String mBaseCodePath;
584        private final Set<String> mSplitCodePaths;
585        // Maps user id to the application private directory.
586        private final Map<Integer, Set<String>> mAppDataDirs;
587
588        public PackageCodeLocations(ApplicationInfo ai, int userId) {
589            this(ai.packageName, ai.sourceDir, ai.splitSourceDirs);
590            mergeAppDataDirs(ai.dataDir, userId);
591        }
592        public PackageCodeLocations(String packageName, String baseCodePath,
593                String[] splitCodePaths) {
594            mPackageName = packageName;
595            mSplitCodePaths = new HashSet<>();
596            mAppDataDirs = new HashMap<>();
597            updateCodeLocation(baseCodePath, splitCodePaths);
598        }
599
600        public void updateCodeLocation(String baseCodePath, String[] splitCodePaths) {
601            mBaseCodePath = baseCodePath;
602            mSplitCodePaths.clear();
603            if (splitCodePaths != null) {
604                for (String split : splitCodePaths) {
605                    mSplitCodePaths.add(split);
606                }
607            }
608        }
609
610        public void mergeAppDataDirs(String dataDir, int userId) {
611            Set<String> dataDirs = putIfAbsent(mAppDataDirs, userId, new HashSet<>());
612            dataDirs.add(dataDir);
613        }
614
615        public int searchDex(String dexPath, int userId) {
616            // First check that this package is installed or active for the given user.
617            // A missing data dir means the package is not installed.
618            Set<String> userDataDirs = mAppDataDirs.get(userId);
619            if (userDataDirs == null) {
620                return DEX_SEARCH_NOT_FOUND;
621            }
622
623            if (mBaseCodePath.equals(dexPath)) {
624                return DEX_SEARCH_FOUND_PRIMARY;
625            }
626            if (mSplitCodePaths.contains(dexPath)) {
627                return DEX_SEARCH_FOUND_SPLIT;
628            }
629            for (String dataDir : userDataDirs) {
630                if (dexPath.startsWith(dataDir)) {
631                    return DEX_SEARCH_FOUND_SECONDARY;
632                }
633            }
634
635            return DEX_SEARCH_NOT_FOUND;
636        }
637    }
638
639    /**
640     * Convenience class to store ownership search results.
641     */
642    private class DexSearchResult {
643        private String mOwningPackageName;
644        private int mOutcome;
645
646        public DexSearchResult(String owningPackageName, int outcome) {
647            this.mOwningPackageName = owningPackageName;
648            this.mOutcome = outcome;
649        }
650
651        @Override
652        public String toString() {
653            return mOwningPackageName + "-" + mOutcome;
654        }
655    }
656}
657