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

Commit 0621084c authored by JW Wang's avatar JW Wang
Browse files

Merge mRelinquished and mInPreRebootVerification (7/n)

The 2 flags serve the same purpose and can be merged.

Bug: 161121612
Test: atest CtsAtomicInstallTestCases
Test: atest CtsStagedInstallHostTestCases
Change-Id: I713741cffc1b7fd4c5b69fed1e1195db27abfbc1
parent c240979f
Loading
Loading
Loading
Loading
+22 −38
Original line number Diff line number Diff line
@@ -354,8 +354,13 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {

    private final AtomicBoolean mCommitted = new AtomicBoolean(false);

    /**
     * True if staging files are being used by external entities like {@link PackageSessionVerifier}
     * or {@link PackageManagerService} which means it is not safe for {@link #abandon()} to clean
     * up the files.
     */
    @GuardedBy("mLock")
    private boolean mRelinquished = false;
    private boolean mStageDirInUse = false;

    /** Permissions have been accepted by the user (see {@link #setPermissionsResult}) */
    @GuardedBy("mLock")
@@ -466,12 +471,6 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        @GuardedBy("mLock")
        @Nullable
        private Runnable mPendingAbandonCallback;
        /**
         * {@code true} if pre-reboot verification is ongoing which means it is not safe for
         * {@link #abandon()} to clean up staging directories.
         */
        @GuardedBy("mLock")
        private boolean mInPreRebootVerification;

        StagedSession(boolean isReady, boolean isApplied, boolean isFailed, int errorCode,
                String errorMessage) {
@@ -707,7 +706,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
                    dispatchSessionFinished(INSTALL_FAILED_ABORTED, "Session was abandoned", null);
                    maybeCleanUpChildSessions();
                };
                if (mInPreRebootVerification) {
                if (mStageDirInUse) {
                    // Pre-reboot verification is ongoing, not safe to clean up the session yet.
                    mPendingAbandonCallback = r;
                    mCallback.onStagedSessionChanged(PackageInstallerSession.this);
@@ -717,30 +716,14 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
            r.run();
        }

        /**
         * Called when pre-reboot verification is about to start. This shouldn't be called
         * on a destroyed session.
         */
        private void notifyStartPreRebootVerification() {
            synchronized (mLock) {
                Preconditions.checkState(!mDestroyed);
                if (mInPreRebootVerification) {
                    throw new IllegalStateException("Pre-reboot verification has started");
                }
                mInPreRebootVerification = true;
            }
        }

        /**
         * Called when pre-reboot verification has ended.
         * Now it is safe to clean up the session if {@link #abandon()} has been called previously.
         */
        private void notifyEndPreRebootVerification() {
            synchronized (mLock) {
                if (!mInPreRebootVerification) {
                    throw new IllegalStateException("Pre-reboot verification not started");
                }
                mInPreRebootVerification = false;
                Preconditions.checkState(mStageDirInUse);
                mStageDirInUse = false;
            }
            dispatchPendingAbandonCallback();
        }
@@ -756,7 +739,6 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
            assertCallerIsOwnerOrRootOrSystem();
            Preconditions.checkArgument(isCommitted());
            Preconditions.checkArgument(!isInTerminalState());
            notifyStartPreRebootVerification();
            verify();
        }

@@ -2403,9 +2385,9 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
            return;
        }
        synchronized (mLock) {
            if (mRelinquished) {
            if (mStageDirInUse) {
                throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
                        "Session relinquished");
                        "Session files in use");
            }
            if (mDestroyed) {
                throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
@@ -2467,9 +2449,9 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {

    private void parseApkAndExtractNativeLibraries() throws PackageManagerException {
        synchronized (mLock) {
            if (mRelinquished) {
            if (mStageDirInUse) {
                throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
                        "Session relinquished");
                        "Session files in use");
            }
            if (mDestroyed) {
                throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
@@ -2514,10 +2496,12 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
                throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR,
                        "Session destroyed");
            }
            // TODO(b/161121612): merge mRelinquished and mInPreRebootVerification as they serve
            //   the same purpose to prevent staging files from being deleted while verification
            //   is in progress.
            mRelinquished = true;
            // Set this flag to prevent abandon() from deleting staging files while verification is
            // in progress. For staged sessions, we will reset this flag when verification is done
            // so abandon() can take effect. For non-staged sessions, the staging files will be
            // deleted when install is completed (no matter success or not). No need to reset
            // the flag.
            mStageDirInUse = true;
        }
        mSessionProvider.getSessionVerifier().verifyNonStaged(this, (error, msg) -> {
            mHandler.post(() -> {
@@ -3734,8 +3718,8 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        synchronized (mLock) {
            assertNotChild("abandonNonStaged");
            assertCallerIsOwnerOrRootOrSystem();
            if (mRelinquished) {
                if (LOGD) Slog.d(TAG, "Ignoring abandon after commit relinquished control");
            if (mStageDirInUse) {
                if (LOGD) Slog.d(TAG, "Ignoring abandon for staging files are in use");
                return;
            }
            destroyInternal();
@@ -4363,7 +4347,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub {
        pw.printPair("mCommitted", mCommitted);
        pw.printPair("mSealed", mSealed);
        pw.printPair("mPermissionsManuallyAccepted", mPermissionsManuallyAccepted);
        pw.printPair("mRelinquished", mRelinquished);
        pw.printPair("mStageDirInUse", mStageDirInUse);
        pw.printPair("mDestroyed", mDestroyed);
        pw.printPair("mFds", mFds.size());
        pw.printPair("mBridges", mBridges.size());