blob: 68cfaab02cdb26968d2298b7eea8390ad7c0547e [file] [log] [blame]
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -08001/*
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
17package com.android.server.wm;
18
19import android.app.ActivityManagerNative;
20import android.content.ClipData;
21import android.net.Uri;
22import android.os.Binder;
23import android.os.IBinder;
24import android.os.RemoteException;
25
26import com.android.internal.view.IDropPermissions;
27
28import java.util.ArrayList;
29
30class 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 Kaznacheevb23a7572015-12-18 12:23:43 -080040 private IBinder mActivityToken = null;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080041
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 Kaznacheevb23a7572015-12-18 12:23:43 -080054 public void take(IBinder activityToken) throws RemoteException {
55 if (mActivityToken != null) {
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080056 return;
57 }
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080058 mActivityToken = activityToken;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080059
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080060 // Will throw if Activity is not found.
61 IBinder permissionOwner = ActivityManagerNative.getDefault().
62 getUriPermissionOwnerForActivity(mActivityToken);
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080063
64 long origId = Binder.clearCallingIdentity();
65 try {
66 for (int i = 0; i < mUris.size(); i++) {
67 ActivityManagerNative.getDefault().grantUriPermissionFromOwner(
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080068 permissionOwner, mSourceUid, mTargetPackage, mUris.get(i), mMode,
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080069 mSourceUserId, mTargetUserId);
70 }
71 } finally {
72 Binder.restoreCallingIdentity(origId);
73 }
74 }
75
76 @Override
77 public void release() throws RemoteException {
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080078 if (mActivityToken == null) {
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080079 return;
80 }
81
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080082 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 Kaznacheev9149d2b2015-12-15 12:16:28 -080093 for (int i = 0; i < mUris.size(); ++i) {
94 ActivityManagerNative.getDefault().revokeUriPermissionFromOwner(
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -080095 permissionOwner, mUris.get(i), mMode, mSourceUserId);
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080096 }
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080097 }
98}