Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 819fea29 authored by Alan Stokes's avatar Alan Stokes
Browse files

Move more fields to InstallSource.

Move installerPackageName and isOrphaned into InstallSource. Remove
now-redundant uses of installerPackageName in various helper
classes. Make InstallSource non-null everywhere.

Bug: 134746019
Test: atest -p services/core/java/com/android/server/pm
Change-Id: If4583ede7a4d67425320a240f1c6874b4b267df3
parent 9aa75e10
Loading
Loading
Loading
Loading
+74 −11
Original line number Diff line number Diff line
@@ -20,40 +20,103 @@ import android.annotation.Nullable;

import com.android.internal.util.IndentingPrintWriter;

import java.util.Objects;

/**
 * Immutable class holding information about where the request to install or update an app
 * came from.
 */
final class InstallSource {
    private static final InstallSource EMPTY = new InstallSource(null);

    /**
     * The package that requested the installation, if known.
     * An instance of InstallSource representing an absence of knowledge of the source of
     * a package. Used in preference to null.
     */
    static final InstallSource EMPTY = new InstallSource(null, null, false);

    /** The package that requested the installation, if known. */
    @Nullable
    final String initiatingPackageName;

    static InstallSource create(@Nullable String initiatingPackageName) {
        return initiatingPackageName == null
                ? EMPTY : new InstallSource(initiatingPackageName.intern());
    /**
     * Package name of the app that installed this package (the installer of record). Note that
     * this may be modified.
     */
    @Nullable
    final String installerPackageName;

    /** Indicates if the package that was the installerPackageName has been uninstalled. */
    final boolean isOrphaned;

    static InstallSource create(@Nullable String initiatingPackageName,
            @Nullable String installerPackageName) {
        return create(initiatingPackageName, installerPackageName, false);
    }

    static InstallSource create(@Nullable String initiatingPackageName,
            @Nullable String installerPackageName, boolean isOrphaned) {
        if (initiatingPackageName == null && installerPackageName == null && !isOrphaned) {
            return EMPTY;
        }
        return new InstallSource(
                initiatingPackageName == null ? null : initiatingPackageName.intern(),
                installerPackageName == null ? null : installerPackageName.intern(),
                isOrphaned);
    }

    private InstallSource(@Nullable String initiatingPackageName) {
    private InstallSource(@Nullable String initiatingPackageName,
            @Nullable String installerPackageName, boolean isOrphaned) {
        this.initiatingPackageName = initiatingPackageName;
        this.isOrphaned = isOrphaned;
        this.installerPackageName = installerPackageName;
    }

    void dump(IndentingPrintWriter pw) {
        pw.printPair("installerPackageName", installerPackageName);
        pw.printPair("installInitiatingPackageName", initiatingPackageName);
    }

    /**
     * Return an InstallSource the same as this one except with the specified installerPackageName.
     */
    InstallSource setInstallerPackage(String installerPackageName) {
        return Objects.equals(installerPackageName, this.installerPackageName) ? this
                : create(initiatingPackageName, installerPackageName, isOrphaned);
    }

    /**
     * Return an InstallSource the same as this one except with the specified value for isOrphaned.
     */
    InstallSource setIsOrphaned(boolean isOrphaned) {
        return isOrphaned == this.isOrphaned ? this
                : create(initiatingPackageName, installerPackageName, isOrphaned);
    }

    /**
     * Return an InstallSource the same as this one except it does not refer to the specified
     * installer package name.
     * installer package name (which is being uninstalled).
     */
    InstallSource removeInstallerPackage(String packageName) {
        if (packageName != null && packageName.equals(initiatingPackageName)) {
            return create(null);
        }
        if (packageName == null) {
            return this;
        }

        boolean modified = false;
        String initiatingPackageName = this.initiatingPackageName;
        String installerPackageName = this.installerPackageName;
        boolean isOrphaned = this.isOrphaned;

        if (packageName.equals(initiatingPackageName)) {
            initiatingPackageName = null;
            modified = true;
        }
        if (packageName.equals(installerPackageName)) {
            installerPackageName = null;
            isOrphaned = true;
            modified = true;
        }

        return modified
                ? create(initiatingPackageName, installerPackageName, isOrphaned)
                : this;
    }
}
+4 −3
Original line number Diff line number Diff line
@@ -623,10 +623,11 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements
                stageCid = buildExternalStageCid(sessionId);
            }
        }
        InstallSource installSource = InstallSource.create(installerPackageName);
        InstallSource installSource = InstallSource.create(installerPackageName,
                requestedInstallerPackageName);
        session = new PackageInstallerSession(mInternalCallback, mContext, mPm, this,
                mInstallThread.getLooper(), mStagingManager, sessionId, userId,
                requestedInstallerPackageName, callingUid, installSource, params, createdMillis,
                mInstallThread.getLooper(), mStagingManager, sessionId, userId, callingUid,
                installSource, params, createdMillis,
                stageDir, stageCid, false, false, false, null, SessionInfo.INVALID_ID,
                false, false, false, SessionInfo.STAGED_SESSION_NO_ERROR, "");

+15 −22
Original line number Diff line number Diff line
@@ -212,10 +212,6 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
    /** Uid of the creator of this session. */
    private final int mOriginalInstallerUid;

    /** Package of the owner of the installer session */
    @GuardedBy("mLock")
    private @Nullable String mInstallerPackageName;

    /** Uid of the owner of the installer session */
    @GuardedBy("mLock")
    private int mInstallerUid;
@@ -374,7 +370,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        }
        DevicePolicyManagerInternal dpmi =
                LocalServices.getService(DevicePolicyManagerInternal.class);
        return dpmi != null && dpmi.canSilentlyInstallPackage(mInstallerPackageName, mInstallerUid);
        return dpmi != null && dpmi.canSilentlyInstallPackage(
                mInstallSource.installerPackageName, mInstallerUid);
    }

    /**
@@ -418,8 +415,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
    public PackageInstallerSession(PackageInstallerService.InternalCallback callback,
            Context context, PackageManagerService pm,
            PackageSessionProvider sessionProvider, Looper looper, StagingManager stagingManager,
            int sessionId, int userId,
            String installerPackageName, int installerUid, @NonNull InstallSource installSource,
            int sessionId, int userId, int installerUid, @NonNull InstallSource installSource,
            SessionParams params, long createdMillis,
            File stageDir, String stageCid, boolean prepared, boolean committed, boolean sealed,
            @Nullable int[] childSessionIds, int parentSessionId, boolean isReady,
@@ -435,7 +431,6 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        this.sessionId = sessionId;
        this.userId = userId;
        mOriginalInstallerUid = installerUid;
        mInstallerPackageName = installerPackageName;
        mInstallerUid = installerUid;
        mInstallSource = Preconditions.checkNotNull(installSource);
        this.params = params;
@@ -475,7 +470,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        synchronized (mLock) {
            info.sessionId = sessionId;
            info.userId = userId;
            info.installerPackageName = mInstallerPackageName;
            info.installerPackageName = mInstallSource.installerPackageName;
            info.resolvedBaseCodePath = (mResolvedBaseFile != null) ?
                    mResolvedBaseFile.getAbsolutePath() : null;
            info.progress = mProgress;
@@ -1226,14 +1221,13 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
                throw new IllegalArgumentException("Package is not valid", e);
            }

            if (!mPackageName.equals(mInstallerPackageName)) {
            if (!mPackageName.equals(mInstallSource.installerPackageName)) {
                throw new SecurityException("Can only transfer sessions that update the original "
                        + "installer");
            }

            mInstallerPackageName = packageName;
            mInstallerUid = newOwnerAppInfo.uid;
            mInstallSource = InstallSource.create(packageName);
            mInstallSource = InstallSource.create(packageName, packageName);
        }

        // Persist the fact that we've sealed ourselves to prevent
@@ -1246,7 +1240,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        if (isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked()) {
            DevicePolicyEventLogger
                    .createEvent(DevicePolicyEnums.INSTALL_PACKAGE)
                    .setAdmin(mInstallerPackageName)
                    .setAdmin(mInstallSource.installerPackageName)
                    .write();
        }
        if (params.isStaged) {
@@ -1452,8 +1446,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {

        mRelinquished = true;
        return new PackageManagerService.ActiveInstallSession(mPackageName, stageDir,
                localObserver, params, mInstallerPackageName, mInstallerUid, mInstallSource, user,
                mSigningDetails);
                localObserver, params, mInstallerUid, mInstallSource, user, mSigningDetails);
    }

    private static void maybeRenameFile(File from, File to) throws PackageManagerException {
@@ -1902,7 +1895,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {

    String getInstallerPackageName() {
        synchronized (mLock) {
            return mInstallerPackageName;
            return mInstallSource.installerPackageName;
        }
    }

@@ -2343,9 +2336,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {

        pw.printPair("userId", userId);
        pw.printPair("mOriginalInstallerUid", mOriginalInstallerUid);
        pw.printPair("mInstallerPackageName", mInstallerPackageName);
        pw.printPair("mInstallerUid", mInstallerUid);
        mInstallSource.dump(pw);
        pw.printPair("mInstallerUid", mInstallerUid);
        pw.printPair("createdMillis", createdMillis);
        pw.printPair("updatedMillis", updatedMillis);
        pw.printPair("stageDir", stageDir);
@@ -2424,7 +2416,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
            writeIntAttribute(out, ATTR_SESSION_ID, sessionId);
            writeIntAttribute(out, ATTR_USER_ID, userId);
            writeStringAttribute(out, ATTR_INSTALLER_PACKAGE_NAME,
                    mInstallerPackageName);
                    mInstallSource.installerPackageName);
            writeIntAttribute(out, ATTR_INSTALLER_UID, mInstallerUid);
            writeStringAttribute(out, ATTR_INITIATING_PACKAGE_NAME,
                    mInstallSource.initiatingPackageName);
@@ -2626,10 +2618,11 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
            childSessionIdsArray = EMPTY_CHILD_SESSION_ARRAY;
        }

        InstallSource installSource = InstallSource.create(installInitiatingPackageName);
        InstallSource installSource = InstallSource.create(installInitiatingPackageName,
                installerPackageName);
        return new PackageInstallerSession(callback, context, pm, sessionProvider,
                installerThread, stagingManager, sessionId, userId, installerPackageName,
                installerUid, installSource, params, createdMillis, stageDir, stageCid,
                installerThread, stagingManager, sessionId, userId, installerUid,
                installSource, params, createdMillis, stageDir, stageCid,
                prepared, committed, sealed, childSessionIdsArray, parentSessionId,
                isReady, isFailed, isApplied, stagedSessionErrorCode, stagedSessionErrorMessage);
    }
Loading