2 ** Copyright 2016, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import android.annotation.NonNull;
20 import android.annotation.UserIdInt;
21 import android.app.ActivityManagerNative;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Binder;
25 import android.os.IBinder;
26 import android.os.RemoteException;
28 import com.android.internal.annotations.GuardedBy;
29 import com.android.internal.inputmethod.IInputContentUriToken;
31 final class InputContentUriTokenHandler extends IInputContentUriToken.Stub {
34 private final Uri mUri;
35 private final int mSourceUid;
37 private final String mTargetPackage;
39 private final int mSourceUserId;
41 private final int mTargetUserId;
43 private final Object mLock = new Object();
46 private IBinder mPermissionOwnerToken = null;
48 InputContentUriTokenHandler(@NonNull Uri contentUri, int sourceUid,
49 @NonNull String targetPackage, @UserIdInt int sourceUserId,
50 @UserIdInt int targetUserId) {
52 mSourceUid = sourceUid;
53 mTargetPackage = targetPackage;
54 mSourceUserId = sourceUserId;
55 mTargetUserId = targetUserId;
60 synchronized (mLock) {
61 if (mPermissionOwnerToken != null) {
62 // Permission is already granted.
67 mPermissionOwnerToken = ActivityManagerNative.getDefault()
68 .newUriPermissionOwner("InputContentUriTokenHandler");
69 } catch (RemoteException e) {
70 e.rethrowFromSystemServer();
73 doTakeLocked(mPermissionOwnerToken);
77 private void doTakeLocked(@NonNull IBinder permissionOwner) {
78 long origId = Binder.clearCallingIdentity();
81 ActivityManagerNative.getDefault().grantUriPermissionFromOwner(
82 permissionOwner, mSourceUid, mTargetPackage, mUri,
83 Intent.FLAG_GRANT_READ_URI_PERMISSION, mSourceUserId, mTargetUserId);
84 } catch (RemoteException e) {
85 e.rethrowFromSystemServer();
88 Binder.restoreCallingIdentity(origId);
93 public void release() {
94 synchronized (mLock) {
95 if (mPermissionOwnerToken == null) {
99 ActivityManagerNative.getDefault().revokeUriPermissionFromOwner(
100 mPermissionOwnerToken, mUri,
101 Intent.FLAG_GRANT_READ_URI_PERMISSION, mSourceUserId);
102 } catch (RemoteException e) {
103 e.rethrowFromSystemServer();
105 mPermissionOwnerToken = null;
114 protected void finalize() throws Throwable {