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

Commit da088199 authored by JW Wang's avatar JW Wang
Browse files

Move mNumPackageSessionsWithSuccess into Rollback (4/n)

Bug: 147400979
Test: atest RollbackTest RollbackUnitTest
Change-Id: Id117b6d6602b126a341ca7da36972283e5089567
parent 99e390e9
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -186,6 +186,14 @@ class Rollback {
     */
    private final int[] mPackageSessionIds;

    /**
     * The number of sessions in the install which are notified with success by
     * {@link PackageInstaller.SessionCallback#onFinished(int, boolean)}.
     * This rollback will be enabled only after all child sessions finished with success.
     */
    @GuardedBy("mLock")
    private int mNumPackageSessionsWithSuccess;

    /**
     * Constructs a new, empty Rollback instance.
     *
@@ -840,6 +848,17 @@ class Rollback {
        return mPackageSessionIds.length;
    }

    /**
     * Called when a child session finished with success.
     * Returns true when all child sessions are notified with success. This rollback will be
     * enabled only after all child sessions finished with success.
     */
    boolean notifySessionWithSuccess() {
        synchronized (mLock) {
            return ++mNumPackageSessionsWithSuccess == mPackageSessionIds.length;
        }
    }

    static String rollbackStateToString(@RollbackState int state) {
        switch (state) {
            case Rollback.ROLLBACK_STATE_ENABLING: return "enabling";
+1 −20
Original line number Diff line number Diff line
@@ -1200,7 +1200,7 @@ class RollbackManagerServiceImpl extends IRollbackManager.Stub {
                NewRollback newRollback;
                synchronized (mLock) {
                    newRollback = getNewRollbackForPackageSessionLocked(sessionId);
                    if (newRollback != null && newRollback.notifySessionWithSuccess()) {
                    if (newRollback != null && newRollback.rollback.notifySessionWithSuccess()) {
                        mNewRollbacks.remove(newRollback);
                    } else {
                        // Not all child sessions finished with success.
@@ -1344,30 +1344,11 @@ class RollbackManagerServiceImpl extends IRollbackManager.Stub {
    private static class NewRollback {
        public final Rollback rollback;

        /**
         * The number of sessions in the install which are notified with success by
         * {@link PackageInstaller.SessionCallback#onFinished(int, boolean)}.
         * This NewRollback will be enabled only after all child sessions finished with success.
         */
        @GuardedBy("mNewRollbackLock")
        private int mNumPackageSessionsWithSuccess;

        private final Object mNewRollbackLock = new Object();

        NewRollback(Rollback rollback) {
            this.rollback = rollback;
        }

        /**
         * Called when a child session finished with success.
         * Returns true when all child sessions are notified with success. This NewRollback will be
         * enabled only after all child sessions finished with success.
         */
        boolean notifySessionWithSuccess() {
            synchronized (mNewRollbackLock) {
                return ++mNumPackageSessionsWithSuccess == rollback.getPackageSessionIdCount();
            }
        }
    }

    @WorkerThread
+11 −0
Original line number Diff line number Diff line
@@ -291,6 +291,17 @@ public class RollbackUnitTest {
        verify(mMockDataHelper).restoreAppData(123, pkgInfo1, 7, 333, "blah");
    }

    @Test
    public void notifySessionWithSuccess() {
        int[] sessionIds = new int[]{ 7777, 8888 };
        Rollback rollback = new Rollback(123, new File("/test/testing"), -1, USER, INSTALLER,
                sessionIds);
        // The 1st invocation returns false because not all child sessions are notified.
        assertThat(rollback.notifySessionWithSuccess()).isFalse();
        // The 2nd invocation returns true because now all child sessions are notified.
        assertThat(rollback.notifySessionWithSuccess()).isTrue();
    }

    private static PackageRollbackInfo newPkgInfoFor(
            String packageName, long fromVersion, long toVersion, boolean isApex) {
        return new PackageRollbackInfo(new VersionedPackage(packageName, fromVersion),