OSDN Git Service

Fix BackupManager#getUserForSerialNumber
authorRuslan Tkhakokhov <rthakohov@google.com>
Wed, 20 Feb 2019 14:33:58 +0000 (14:33 +0000)
committerRuslan Tkhakokhov <rthakohov@google.com>
Thu, 21 Feb 2019 15:23:53 +0000 (15:23 +0000)
Test: atest ProfileSerialNumberHostSideTest

Currently BackupManager#getUserForSerialNumber goes through users
associated with the calling user (inluding the calling user itself) and
looks for the given serial number. However, to get the list of users it calls
UserManager#getUserProfiles that operates for the users associated with
the calling process (in our case - system user since the call is made
from BMS). While this works for our current use case (only support
backup for system user and their work profile, meaning
BackupManager#getUserForSerialNumber will only be called for system
user), it won't work for a full multi-user case.

Change-Id: Ia42a69a1216ed9a5dae596e44336290d2718de7a

services/backup/java/com/android/server/backup/BackupManagerService.java

index 0dd1ded..ffda581 100644 (file)
@@ -464,15 +464,25 @@ public class BackupManagerService {
      */
     @Nullable
     public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) {
-        for (UserHandle handle : mContext.getSystemService(UserManager.class).getUserProfiles()) {
-            UserBackupManagerService userBackupManagerService = getServiceUsers().get(
-                    handle.getIdentifier());
+        int callingUserId = Binder.getCallingUserHandle().getIdentifier();
+        long oldId = Binder.clearCallingIdentity();
+        int[] userIds;
+        try {
+            userIds = mContext.getSystemService(UserManager.class).getProfileIds(callingUserId,
+                    false);
+        } finally {
+            Binder.restoreCallingIdentity(oldId);
+        }
+
+        for (int userId : userIds) {
+            UserBackupManagerService userBackupManagerService = getServiceUsers().get(userId);
             if (userBackupManagerService != null) {
                 if (userBackupManagerService.getAncestralSerialNumber() == ancestralSerialNumber) {
-                    return handle;
+                    return UserHandle.of(userId);
                 }
             }
         }
+
         return null;
     }