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 | |
| 19 | import android.app.ActivityManagerNative; |
| 20 | import android.content.ClipData; |
| 21 | import android.net.Uri; |
| 22 | import android.os.Binder; |
| 23 | import android.os.IBinder; |
| 24 | import android.os.RemoteException; |
| 25 | |
| 26 | import com.android.internal.view.IDropPermissions; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | |
| 30 | class DropPermissionsHandler extends IDropPermissions.Stub { |
| 31 | |
| 32 | private final int mSourceUid; |
| 33 | private final String mTargetPackage; |
| 34 | private final int mMode; |
| 35 | private final int mSourceUserId; |
| 36 | private final int mTargetUserId; |
| 37 | |
| 38 | private final ArrayList<Uri> mUris = new ArrayList<Uri>(); |
| 39 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 40 | private IBinder mActivityToken = null; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 41 | |
| 42 | DropPermissionsHandler(ClipData clipData, int sourceUid, String targetPackage, int mode, |
| 43 | int sourceUserId, int targetUserId) { |
| 44 | mSourceUid = sourceUid; |
| 45 | mTargetPackage = targetPackage; |
| 46 | mMode = mode; |
| 47 | mSourceUserId = sourceUserId; |
| 48 | mTargetUserId = targetUserId; |
| 49 | |
| 50 | clipData.collectUris(mUris); |
| 51 | } |
| 52 | |
| 53 | @Override |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 54 | public void take(IBinder activityToken) throws RemoteException { |
| 55 | if (mActivityToken != null) { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 58 | mActivityToken = activityToken; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 59 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 60 | // Will throw if Activity is not found. |
| 61 | IBinder permissionOwner = ActivityManagerNative.getDefault(). |
| 62 | getUriPermissionOwnerForActivity(mActivityToken); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 63 | |
| 64 | long origId = Binder.clearCallingIdentity(); |
| 65 | try { |
| 66 | for (int i = 0; i < mUris.size(); i++) { |
| 67 | ActivityManagerNative.getDefault().grantUriPermissionFromOwner( |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 68 | permissionOwner, mSourceUid, mTargetPackage, mUris.get(i), mMode, |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 69 | mSourceUserId, mTargetUserId); |
| 70 | } |
| 71 | } finally { |
| 72 | Binder.restoreCallingIdentity(origId); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void release() throws RemoteException { |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 78 | if (mActivityToken == null) { |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 82 | IBinder permissionOwner = null; |
| 83 | try { |
| 84 | permissionOwner = ActivityManagerNative.getDefault(). |
| 85 | getUriPermissionOwnerForActivity(mActivityToken); |
| 86 | } catch (Exception e) { |
| 87 | // Activity is destroyed, permissions already revoked. |
| 88 | return; |
| 89 | } finally { |
| 90 | mActivityToken = null; |
| 91 | } |
| 92 | |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 93 | for (int i = 0; i < mUris.size(); ++i) { |
| 94 | ActivityManagerNative.getDefault().revokeUriPermissionFromOwner( |
Vladislav Kaznacheev | b23a757 | 2015-12-18 12:23:43 -0800 | [diff] [blame] | 95 | permissionOwner, mUris.get(i), mMode, mSourceUserId); |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 96 | } |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 97 | } |
| 98 | } |