Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2015, The Android Open Source Project |
| 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 20 | import android.app.ActivityTaskManager; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 21 | import android.app.UriGrantsManager; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 22 | import android.content.ClipData; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Binder; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 28 | import com.android.internal.view.IDragAndDropPermissions; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 29 | import com.android.server.LocalServices; |
| 30 | import com.android.server.uri.UriGrantsManagerInternal; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 34 | class DragAndDropPermissionsHandler extends IDragAndDropPermissions.Stub |
| 35 | implements IBinder.DeathRecipient { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 36 | |
| 37 | private final int mSourceUid; |
| 38 | private final String mTargetPackage; |
| 39 | private final int mMode; |
| 40 | private final int mSourceUserId; |
| 41 | private final int mTargetUserId; |
| 42 | |
| 43 | private final ArrayList<Uri> mUris = new ArrayList<Uri>(); |
| 44 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 45 | private IBinder mActivityToken = null; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 46 | private IBinder mPermissionOwnerToken = null; |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 47 | private IBinder mTransientToken = null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 48 | |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 49 | DragAndDropPermissionsHandler(ClipData clipData, int sourceUid, String targetPackage, int mode, |
| 50 | int sourceUserId, int targetUserId) { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 51 | mSourceUid = sourceUid; |
| 52 | mTargetPackage = targetPackage; |
| 53 | mMode = mode; |
| 54 | mSourceUserId = sourceUserId; |
| 55 | mTargetUserId = targetUserId; |
| 56 | |
| 57 | clipData.collectUris(mUris); |
| 58 | } |
| 59 | |
| 60 | @Override |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 61 | public void take(IBinder activityToken) throws RemoteException { |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 62 | if (mActivityToken != null || mPermissionOwnerToken != null) { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 63 | return; |
| 64 | } |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 65 | mActivityToken = activityToken; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 66 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 67 | // Will throw if Activity is not found. |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 68 | IBinder permissionOwner = ActivityTaskManager.getService(). |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 69 | getUriPermissionOwnerForActivity(mActivityToken); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 70 | |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 71 | doTake(permissionOwner); |
| 72 | } |
| 73 | |
| 74 | private void doTake(IBinder permissionOwner) throws RemoteException { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 75 | long origId = Binder.clearCallingIdentity(); |
| 76 | try { |
| 77 | for (int i = 0; i < mUris.size(); i++) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 78 | UriGrantsManager.getService().grantUriPermissionFromOwner( |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 79 | permissionOwner, mSourceUid, mTargetPackage, mUris.get(i), mMode, |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 80 | mSourceUserId, mTargetUserId); |
| 81 | } |
| 82 | } finally { |
| 83 | Binder.restoreCallingIdentity(origId); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 88 | public void takeTransient(IBinder transientToken) throws RemoteException { |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 89 | if (mActivityToken != null || mPermissionOwnerToken != null) { |
| 90 | return; |
| 91 | } |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 92 | mPermissionOwnerToken = LocalServices.getService(UriGrantsManagerInternal.class) |
| 93 | .newUriPermissionOwner("drop"); |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 94 | mTransientToken = transientToken; |
| 95 | mTransientToken.linkToDeath(this, 0); |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 96 | |
| 97 | doTake(mPermissionOwnerToken); |
| 98 | } |
| 99 | |
| 100 | @Override |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 101 | public void release() throws RemoteException { |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 102 | if (mActivityToken == null && mPermissionOwnerToken == null) { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 106 | IBinder permissionOwner = null; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 107 | if (mActivityToken != null) { |
| 108 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 109 | permissionOwner = ActivityTaskManager.getService(). |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 110 | getUriPermissionOwnerForActivity(mActivityToken); |
| 111 | } catch (Exception e) { |
| 112 | // Activity is destroyed, permissions already revoked. |
| 113 | return; |
| 114 | } finally { |
| 115 | mActivityToken = null; |
| 116 | } |
| 117 | } else { |
| 118 | permissionOwner = mPermissionOwnerToken; |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 119 | mPermissionOwnerToken = null; |
Vladislav Kaznacheev | 87c60d6 | 2016-07-21 15:25:12 -0700 | [diff] [blame] | 120 | mTransientToken.unlinkToDeath(this, 0); |
| 121 | mTransientToken = null; |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 124 | UriGrantsManagerInternal ugm = LocalServices.getService(UriGrantsManagerInternal.class); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 125 | for (int i = 0; i < mUris.size(); ++i) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 126 | ugm.revokeUriPermissionFromOwner(permissionOwner, mUris.get(i), mMode, mSourceUserId); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 127 | } |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 128 | } |
Vladislav Kaznacheev | c14df8e | 2016-01-22 11:49:13 -0800 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public void binderDied() { |
| 132 | try { |
| 133 | release(); |
| 134 | } catch (RemoteException e) { |
| 135 | // Cannot happen, local call. |
| 136 | } |
| 137 | } |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 138 | } |