The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Amith Yamasani | 30f8eb4 | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 17 | package com.android.server.clipboard; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 20 | import android.app.AppGlobals; |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 21 | import android.app.AppOpsManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 22 | import android.app.IActivityManager; |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 24 | import android.content.ClipData; |
| 25 | import android.content.ClipDescription; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 26 | import android.content.IClipboard; |
| 27 | import android.content.IOnPrimaryClipChangedListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 31 | import android.content.pm.IPackageManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageInfo; |
| 33 | import android.content.pm.PackageManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
| 35 | import android.os.Binder; |
| 36 | import android.os.IBinder; |
| 37 | import android.os.Parcel; |
| 38 | import android.os.Process; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 39 | import android.os.RemoteCallbackList; |
| 40 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 42 | import android.util.Slog; |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 43 | import android.util.SparseArray; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 44 | |
| 45 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Implementation of the clipboard for copy and paste. |
| 49 | */ |
| 50 | public class ClipboardService extends IClipboard.Stub { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 51 | |
| 52 | private static final String TAG = "ClipboardService"; |
| 53 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 54 | private final Context mContext; |
| 55 | private final IActivityManager mAm; |
| 56 | private final PackageManager mPm; |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 57 | private final AppOpsManager mAppOps; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 58 | private final IBinder mPermissionOwner; |
| 59 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 60 | private class ListenerInfo { |
| 61 | final int mUid; |
| 62 | final String mPackageName; |
| 63 | ListenerInfo(int uid, String packageName) { |
| 64 | mUid = uid; |
| 65 | mPackageName = packageName; |
| 66 | } |
| 67 | } |
| 68 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 69 | private class PerUserClipboard { |
| 70 | final int userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 72 | final RemoteCallbackList<IOnPrimaryClipChangedListener> primaryClipListeners |
| 73 | = new RemoteCallbackList<IOnPrimaryClipChangedListener>(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 74 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 75 | ClipData primaryClip; |
| 76 | |
| 77 | final HashSet<String> activePermissionOwners |
| 78 | = new HashSet<String>(); |
| 79 | |
| 80 | PerUserClipboard(int userId) { |
| 81 | this.userId = userId; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private SparseArray<PerUserClipboard> mClipboards = new SparseArray<PerUserClipboard>(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Instantiates the clipboard. |
| 89 | */ |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 90 | public ClipboardService(Context context) { |
| 91 | mContext = context; |
| 92 | mAm = ActivityManagerNative.getDefault(); |
| 93 | mPm = context.getPackageManager(); |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 94 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 95 | IBinder permOwner = null; |
| 96 | try { |
| 97 | permOwner = mAm.newUriPermissionOwner("clipboard"); |
| 98 | } catch (RemoteException e) { |
| 99 | Slog.w("clipboard", "AM dead", e); |
| 100 | } |
| 101 | mPermissionOwner = permOwner; |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 102 | |
| 103 | // Remove the clipboard if a user is removed |
| 104 | IntentFilter userFilter = new IntentFilter(); |
| 105 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 106 | mContext.registerReceiver(new BroadcastReceiver() { |
| 107 | @Override |
| 108 | public void onReceive(Context context, Intent intent) { |
| 109 | String action = intent.getAction(); |
| 110 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 111 | removeClipboard(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | }, userFilter); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 119 | throws RemoteException { |
| 120 | try { |
| 121 | return super.onTransact(code, data, reply, flags); |
| 122 | } catch (RuntimeException e) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 123 | if (!(e instanceof SecurityException)) { |
| 124 | Slog.wtf("clipboard", "Exception: ", e); |
| 125 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 126 | throw e; |
| 127 | } |
| 128 | |
| 129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 131 | private PerUserClipboard getClipboard() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 132 | return getClipboard(UserHandle.getCallingUserId()); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private PerUserClipboard getClipboard(int userId) { |
| 136 | synchronized (mClipboards) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 137 | PerUserClipboard puc = mClipboards.get(userId); |
| 138 | if (puc == null) { |
| 139 | puc = new PerUserClipboard(userId); |
| 140 | mClipboards.put(userId, puc); |
| 141 | } |
| 142 | return puc; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private void removeClipboard(int userId) { |
| 147 | synchronized (mClipboards) { |
| 148 | mClipboards.remove(userId); |
| 149 | } |
| 150 | } |
| 151 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 152 | public void setPrimaryClip(ClipData clip, String callingPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | synchronized (this) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 154 | if (clip != null && clip.getItemCount() <= 0) { |
| 155 | throw new IllegalArgumentException("No items"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | } |
Dianne Hackborn | f098984 | 2013-07-29 18:11:02 -0700 | [diff] [blame] | 157 | final int callingUid = Binder.getCallingUid(); |
| 158 | if (mAppOps.noteOp(AppOpsManager.OP_WRITE_CLIPBOARD, callingUid, |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 159 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 160 | return; |
| 161 | } |
Dianne Hackborn | f098984 | 2013-07-29 18:11:02 -0700 | [diff] [blame] | 162 | checkDataOwnerLocked(clip, callingUid); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 163 | clearActiveOwnersLocked(); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 164 | PerUserClipboard clipboard = getClipboard(); |
| 165 | clipboard.primaryClip = clip; |
Dianne Hackborn | f098984 | 2013-07-29 18:11:02 -0700 | [diff] [blame] | 166 | final long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 167 | final int n = clipboard.primaryClipListeners.beginBroadcast(); |
Dianne Hackborn | f098984 | 2013-07-29 18:11:02 -0700 | [diff] [blame] | 168 | try { |
| 169 | for (int i = 0; i < n; i++) { |
| 170 | try { |
| 171 | ListenerInfo li = (ListenerInfo) |
| 172 | clipboard.primaryClipListeners.getBroadcastCookie(i); |
| 173 | if (mAppOps.checkOpNoThrow(AppOpsManager.OP_READ_CLIPBOARD, li.mUid, |
| 174 | li.mPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 175 | clipboard.primaryClipListeners.getBroadcastItem(i) |
| 176 | .dispatchPrimaryClipChanged(); |
| 177 | } |
| 178 | } catch (RemoteException e) { |
| 179 | // The RemoteCallbackList will take care of removing |
| 180 | // the dead object for us. |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 181 | } |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 182 | } |
Dianne Hackborn | f098984 | 2013-07-29 18:11:02 -0700 | [diff] [blame] | 183 | } finally { |
| 184 | clipboard.primaryClipListeners.finishBroadcast(); |
| 185 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 186 | } |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 190 | public ClipData getPrimaryClip(String pkg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | synchronized (this) { |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 192 | if (mAppOps.noteOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 193 | pkg) != AppOpsManager.MODE_ALLOWED) { |
| 194 | return null; |
| 195 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 196 | addActiveOwnerLocked(Binder.getCallingUid(), pkg); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 197 | return getClipboard().primaryClip; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 201 | public ClipDescription getPrimaryClipDescription(String callingPackage) { |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 202 | synchronized (this) { |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 203 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 204 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 205 | return null; |
| 206 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 207 | PerUserClipboard clipboard = getClipboard(); |
| 208 | return clipboard.primaryClip != null ? clipboard.primaryClip.getDescription() : null; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 212 | public boolean hasPrimaryClip(String callingPackage) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 213 | synchronized (this) { |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 214 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 215 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 216 | return false; |
| 217 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 218 | return getClipboard().primaryClip != null; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 222 | public void addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener, |
| 223 | String callingPackage) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 224 | synchronized (this) { |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 225 | getClipboard().primaryClipListeners.register(listener, |
| 226 | new ListenerInfo(Binder.getCallingUid(), callingPackage)); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | public void removePrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) { |
| 231 | synchronized (this) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 232 | getClipboard().primaryClipListeners.unregister(listener); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 236 | public boolean hasClipboardText(String callingPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | synchronized (this) { |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 238 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 239 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 240 | return false; |
| 241 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 242 | PerUserClipboard clipboard = getClipboard(); |
| 243 | if (clipboard.primaryClip != null) { |
| 244 | CharSequence text = clipboard.primaryClip.getItemAt(0).getText(); |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 245 | return text != null && text.length() > 0; |
| 246 | } |
| 247 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 250 | |
| 251 | private final void checkUriOwnerLocked(Uri uri, int uid) { |
| 252 | if (!"content".equals(uri.getScheme())) { |
| 253 | return; |
| 254 | } |
| 255 | long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 256 | try { |
| 257 | // This will throw SecurityException for us. |
| 258 | mAm.checkGrantUriPermission(uid, null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 259 | } catch (RemoteException e) { |
| 260 | } finally { |
| 261 | Binder.restoreCallingIdentity(ident); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private final void checkItemOwnerLocked(ClipData.Item item, int uid) { |
| 266 | if (item.getUri() != null) { |
| 267 | checkUriOwnerLocked(item.getUri(), uid); |
| 268 | } |
| 269 | Intent intent = item.getIntent(); |
| 270 | if (intent != null && intent.getData() != null) { |
| 271 | checkUriOwnerLocked(intent.getData(), uid); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private final void checkDataOwnerLocked(ClipData data, int uid) { |
| 276 | final int N = data.getItemCount(); |
| 277 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 278 | checkItemOwnerLocked(data.getItemAt(i), uid); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | private final void grantUriLocked(Uri uri, String pkg) { |
| 283 | long ident = Binder.clearCallingIdentity(); |
| 284 | try { |
| 285 | mAm.grantUriPermissionFromOwner(mPermissionOwner, Process.myUid(), pkg, uri, |
| 286 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 287 | } catch (RemoteException e) { |
| 288 | } finally { |
| 289 | Binder.restoreCallingIdentity(ident); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | private final void grantItemLocked(ClipData.Item item, String pkg) { |
| 294 | if (item.getUri() != null) { |
| 295 | grantUriLocked(item.getUri(), pkg); |
| 296 | } |
| 297 | Intent intent = item.getIntent(); |
| 298 | if (intent != null && intent.getData() != null) { |
| 299 | grantUriLocked(intent.getData(), pkg); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | private final void addActiveOwnerLocked(int uid, String pkg) { |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 304 | final IPackageManager pm = AppGlobals.getPackageManager(); |
| 305 | final int targetUserHandle = UserHandle.getCallingUserId(); |
| 306 | final long oldIdentity = Binder.clearCallingIdentity(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 307 | try { |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 308 | PackageInfo pi = pm.getPackageInfo(pkg, 0, targetUserHandle); |
| 309 | if (pi == null) { |
| 310 | throw new IllegalArgumentException("Unknown package " + pkg); |
| 311 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 312 | if (!UserHandle.isSameApp(pi.applicationInfo.uid, uid)) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 313 | throw new SecurityException("Calling uid " + uid |
| 314 | + " does not own package " + pkg); |
| 315 | } |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 316 | } catch (RemoteException e) { |
| 317 | // Can't happen; the package manager is in the same process |
| 318 | } finally { |
| 319 | Binder.restoreCallingIdentity(oldIdentity); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 320 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 321 | PerUserClipboard clipboard = getClipboard(); |
| 322 | if (clipboard.primaryClip != null && !clipboard.activePermissionOwners.contains(pkg)) { |
| 323 | final int N = clipboard.primaryClip.getItemCount(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 324 | for (int i=0; i<N; i++) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 325 | grantItemLocked(clipboard.primaryClip.getItemAt(i), pkg); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 326 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 327 | clipboard.activePermissionOwners.add(pkg); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | private final void revokeUriLocked(Uri uri) { |
| 332 | long ident = Binder.clearCallingIdentity(); |
| 333 | try { |
| 334 | mAm.revokeUriPermissionFromOwner(mPermissionOwner, uri, |
| 335 | Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 336 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 337 | } catch (RemoteException e) { |
| 338 | } finally { |
| 339 | Binder.restoreCallingIdentity(ident); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | private final void revokeItemLocked(ClipData.Item item) { |
| 344 | if (item.getUri() != null) { |
| 345 | revokeUriLocked(item.getUri()); |
| 346 | } |
| 347 | Intent intent = item.getIntent(); |
| 348 | if (intent != null && intent.getData() != null) { |
| 349 | revokeUriLocked(intent.getData()); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private final void clearActiveOwnersLocked() { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 354 | PerUserClipboard clipboard = getClipboard(); |
| 355 | clipboard.activePermissionOwners.clear(); |
| 356 | if (clipboard.primaryClip == null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 359 | final int N = clipboard.primaryClip.getItemCount(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 360 | for (int i=0; i<N; i++) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 361 | revokeItemLocked(clipboard.primaryClip.getItemAt(i)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |