Merge "Adds sanity checks to session commit and abandon" into qt-dev
am: 116ca5db5b

Change-Id: I2be2b6bdf8fded0689522bcd3f288ac9d01ef0c6
diff --git a/core/java/android/content/pm/PackageInstaller.java b/core/java/android/content/pm/PackageInstaller.java
index a71f7d2..6c72a9a 100644
--- a/core/java/android/content/pm/PackageInstaller.java
+++ b/core/java/android/content/pm/PackageInstaller.java
@@ -1222,7 +1222,7 @@
             try {
                 mSession.addChildSessionId(sessionId);
             } catch (RemoteException e) {
-                throw e.rethrowFromSystemServer();
+                e.rethrowFromSystemServer();
             }
         }
 
@@ -1236,7 +1236,7 @@
             try {
                 mSession.removeChildSessionId(sessionId);
             } catch (RemoteException e) {
-                throw e.rethrowFromSystemServer();
+                e.rethrowFromSystemServer();
             }
         }
     }
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index a935c65..74fb4b2 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -841,6 +841,11 @@
 
     @Override
     public void commit(@NonNull IntentSender statusReceiver, boolean forTransfer) {
+        if (hasParentSessionId()) {
+            throw new IllegalStateException(
+                    "Session " + sessionId + " is a child of multi-package session "
+                            + mParentSessionId +  " and may not be committed directly.");
+        }
         if (!markAsCommitted(statusReceiver, forTransfer)) {
             return;
         }
@@ -2037,6 +2042,11 @@
 
     @Override
     public void abandon() {
+        if (hasParentSessionId()) {
+            throw new IllegalStateException(
+                    "Session " + sessionId + " is a child of multi-package session "
+                            + mParentSessionId +  " and may not be abandoned directly.");
+        }
         synchronized (mLock) {
             assertCallerIsOwnerOrRootLocked();
 
@@ -2079,13 +2089,14 @@
     }
 
     @Override
-    public void addChildSessionId(int childSessionId) throws RemoteException {
+    public void addChildSessionId(int childSessionId) {
         final PackageInstallerSession childSession = mSessionProvider.getSession(childSessionId);
-        if (childSession == null) {
-            throw new RemoteException("Unable to add child.",
-                    new PackageManagerException("Child session " + childSessionId
-                            + " does not exist"),
-                    false, true).rethrowAsRuntimeException();
+        if (childSession == null
+                || (childSession.hasParentSessionId() && childSession.mParentSessionId != sessionId)
+                || childSession.mCommitted
+                || childSession.mDestroyed) {
+            throw new IllegalStateException("Unable to add child session " + childSessionId
+                            + " as it does not exist or is in an invalid state.");
         }
         synchronized (mLock) {
             assertCallerIsOwnerOrRootLocked();
@@ -2124,11 +2135,8 @@
         synchronized (mLock) {
             if (parentSessionId != SessionInfo.INVALID_ID
                     && mParentSessionId != SessionInfo.INVALID_ID) {
-                throw new RemoteException("Unable to set parent session.",
-                        new PackageManagerException(
-                                "The parent of " + sessionId + " is" + " already set to "
-                                        + mParentSessionId), false,
-                        true).rethrowAsRuntimeException();
+                throw new IllegalStateException("The parent of " + sessionId + " is" + " already"
+                        + "set to " + mParentSessionId);
             }
             this.mParentSessionId = parentSessionId;
         }
diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java
index 1908b3f..72d5438 100644
--- a/services/core/java/com/android/server/pm/StagingManager.java
+++ b/services/core/java/com/android/server/pm/StagingManager.java
@@ -487,7 +487,7 @@
                 }
                 try {
                     apkParentSession.addChildSessionId(apkChildSession.sessionId);
-                } catch (RemoteException e) {
+                } catch (IllegalStateException e) {
                     Slog.e(TAG, "Failed to add a child session for installing the APK files", e);
                     return false;
                 }