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; |
Dianne Hackborn | 1040dc4 | 2010-08-26 22:11:06 -0700 | [diff] [blame] | 23 | import android.content.ClipData; |
| 24 | import android.content.ClipDescription; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 25 | import android.content.ContentProvider; |
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; |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 30 | import android.content.pm.IPackageManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
| 32 | import android.content.pm.PackageManager; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 33 | import android.content.pm.UserInfo; |
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; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 37 | import android.os.IUserManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 38 | import android.os.Parcel; |
| 39 | import android.os.Process; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 40 | import android.os.RemoteCallbackList; |
| 41 | import android.os.RemoteException; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 42 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 44 | import android.os.UserManager; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 45 | import android.util.Slog; |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 46 | import android.util.SparseArray; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 47 | |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 48 | import com.android.server.SystemService; |
| 49 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 50 | import java.util.HashSet; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 51 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Implementation of the clipboard for copy and paste. |
| 55 | */ |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 56 | public class ClipboardService extends SystemService { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 57 | |
| 58 | private static final String TAG = "ClipboardService"; |
| 59 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 60 | private final IActivityManager mAm; |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 61 | private final IUserManager mUm; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 62 | private final PackageManager mPm; |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 63 | private final AppOpsManager mAppOps; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 64 | private final IBinder mPermissionOwner; |
| 65 | |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 66 | private final SparseArray<PerUserClipboard> mClipboards = new SparseArray<>(); |
| 67 | |
| 68 | /** |
| 69 | * Instantiates the clipboard. |
| 70 | */ |
| 71 | public ClipboardService(Context context) { |
| 72 | super(context); |
| 73 | |
| 74 | mAm = ActivityManagerNative.getDefault(); |
| 75 | mPm = getContext().getPackageManager(); |
| 76 | mUm = (IUserManager) ServiceManager.getService(Context.USER_SERVICE); |
| 77 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 78 | IBinder permOwner = null; |
| 79 | try { |
| 80 | permOwner = mAm.newUriPermissionOwner("clipboard"); |
| 81 | } catch (RemoteException e) { |
| 82 | Slog.w("clipboard", "AM dead", e); |
| 83 | } |
| 84 | mPermissionOwner = permOwner; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onStart() { |
| 89 | publishBinderService(Context.CLIPBOARD_SERVICE, new ClipboardImpl()); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onCleanupUser(int userId) { |
| 94 | synchronized (mClipboards) { |
| 95 | mClipboards.remove(userId); |
| 96 | } |
| 97 | } |
| 98 | |
Dianne Hackborn | efcc1a2 | 2013-02-25 18:02:35 -0800 | [diff] [blame] | 99 | private class ListenerInfo { |
| 100 | final int mUid; |
| 101 | final String mPackageName; |
| 102 | ListenerInfo(int uid, String packageName) { |
| 103 | mUid = uid; |
| 104 | mPackageName = packageName; |
| 105 | } |
| 106 | } |
| 107 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 108 | private class PerUserClipboard { |
| 109 | final int userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 111 | final RemoteCallbackList<IOnPrimaryClipChangedListener> primaryClipListeners |
| 112 | = new RemoteCallbackList<IOnPrimaryClipChangedListener>(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 113 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 114 | ClipData primaryClip; |
| 115 | |
| 116 | final HashSet<String> activePermissionOwners |
| 117 | = new HashSet<String>(); |
| 118 | |
| 119 | PerUserClipboard(int userId) { |
| 120 | this.userId = userId; |
| 121 | } |
| 122 | } |
| 123 | |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 124 | private class ClipboardImpl extends IClipboard.Stub { |
| 125 | @Override |
| 126 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 127 | throws RemoteException { |
| 128 | try { |
| 129 | return super.onTransact(code, data, reply, flags); |
| 130 | } catch (RuntimeException e) { |
| 131 | if (!(e instanceof SecurityException)) { |
| 132 | Slog.wtf("clipboard", "Exception: ", e); |
| 133 | } |
| 134 | throw e; |
| 135 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 136 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 137 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 138 | |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public void setPrimaryClip(ClipData clip, String callingPackage) { |
| 141 | synchronized (this) { |
| 142 | if (clip != null && clip.getItemCount() <= 0) { |
| 143 | throw new IllegalArgumentException("No items"); |
| 144 | } |
| 145 | final int callingUid = Binder.getCallingUid(); |
| 146 | if (mAppOps.noteOp(AppOpsManager.OP_WRITE_CLIPBOARD, callingUid, |
| 147 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 148 | return; |
| 149 | } |
| 150 | checkDataOwnerLocked(clip, callingUid); |
| 151 | final int userId = UserHandle.getUserId(callingUid); |
| 152 | PerUserClipboard clipboard = getClipboard(userId); |
| 153 | revokeUris(clipboard); |
| 154 | setPrimaryClipInternal(clipboard, clip); |
| 155 | List<UserInfo> related = getRelatedProfiles(userId); |
| 156 | if (related != null) { |
| 157 | int size = related.size(); |
| 158 | if (size > 1) { // Related profiles list include the current profile. |
| 159 | boolean canCopy = false; |
| 160 | try { |
| 161 | canCopy = !mUm.getUserRestrictions(userId).getBoolean( |
| 162 | UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE); |
| 163 | } catch (RemoteException e) { |
| 164 | Slog.e(TAG, "Remote Exception calling UserManager: " + e); |
| 165 | } |
| 166 | // Copy clip data to related users if allowed. If disallowed, then remove |
| 167 | // primary clip in related users to prevent pasting stale content. |
| 168 | if (!canCopy) { |
| 169 | clip = null; |
| 170 | } else { |
| 171 | // We want to fix the uris of the related user's clip without changing the |
| 172 | // uris of the current user's clip. |
| 173 | // So, copy the ClipData, and then copy all the items, so that nothing |
| 174 | // is shared in memmory. |
| 175 | clip = new ClipData(clip); |
| 176 | for (int i = clip.getItemCount() - 1; i >= 0; i--) { |
| 177 | clip.setItemAt(i, new ClipData.Item(clip.getItemAt(i))); |
| 178 | } |
| 179 | clip.fixUrisLight(userId); |
| 180 | } |
| 181 | for (int i = 0; i < size; i++) { |
| 182 | int id = related.get(i).id; |
| 183 | if (id != userId) { |
| 184 | setPrimaryClipInternal(getClipboard(id), clip); |
| 185 | } |
| 186 | } |
| 187 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 190 | } |
Sudheer Shanka | ad70bc6 | 2016-09-29 12:58:04 -0700 | [diff] [blame] | 191 | |
| 192 | @Override |
| 193 | public ClipData getPrimaryClip(String pkg) { |
| 194 | synchronized (this) { |
| 195 | if (mAppOps.noteOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 196 | pkg) != AppOpsManager.MODE_ALLOWED) { |
| 197 | return null; |
| 198 | } |
| 199 | addActiveOwnerLocked(Binder.getCallingUid(), pkg); |
| 200 | return getClipboard().primaryClip; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public ClipDescription getPrimaryClipDescription(String callingPackage) { |
| 206 | synchronized (this) { |
| 207 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 208 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 209 | return null; |
| 210 | } |
| 211 | PerUserClipboard clipboard = getClipboard(); |
| 212 | return clipboard.primaryClip != null ? clipboard.primaryClip.getDescription() : null; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public boolean hasPrimaryClip(String callingPackage) { |
| 218 | synchronized (this) { |
| 219 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 220 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 221 | return false; |
| 222 | } |
| 223 | return getClipboard().primaryClip != null; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener, |
| 229 | String callingPackage) { |
| 230 | synchronized (this) { |
| 231 | getClipboard().primaryClipListeners.register(listener, |
| 232 | new ListenerInfo(Binder.getCallingUid(), callingPackage)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void removePrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) { |
| 238 | synchronized (this) { |
| 239 | getClipboard().primaryClipListeners.unregister(listener); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public boolean hasClipboardText(String callingPackage) { |
| 245 | synchronized (this) { |
| 246 | if (mAppOps.checkOp(AppOpsManager.OP_READ_CLIPBOARD, Binder.getCallingUid(), |
| 247 | callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 248 | return false; |
| 249 | } |
| 250 | PerUserClipboard clipboard = getClipboard(); |
| 251 | if (clipboard.primaryClip != null) { |
| 252 | CharSequence text = clipboard.primaryClip.getItemAt(0).getText(); |
| 253 | return text != null && text.length() > 0; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 260 | private PerUserClipboard getClipboard() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 261 | return getClipboard(UserHandle.getCallingUserId()); |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private PerUserClipboard getClipboard(int userId) { |
| 265 | synchronized (mClipboards) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 266 | PerUserClipboard puc = mClipboards.get(userId); |
| 267 | if (puc == null) { |
| 268 | puc = new PerUserClipboard(userId); |
| 269 | mClipboards.put(userId, puc); |
| 270 | } |
| 271 | return puc; |
| 272 | } |
| 273 | } |
| 274 | |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 275 | List<UserInfo> getRelatedProfiles(int userId) { |
| 276 | final List<UserInfo> related; |
| 277 | final long origId = Binder.clearCallingIdentity(); |
| 278 | try { |
| 279 | related = mUm.getProfiles(userId, true); |
| 280 | } catch (RemoteException e) { |
| 281 | Slog.e(TAG, "Remote Exception calling UserManager: " + e); |
| 282 | return null; |
| 283 | } finally{ |
| 284 | Binder.restoreCallingIdentity(origId); |
| 285 | } |
| 286 | return related; |
| 287 | } |
| 288 | |
| 289 | void setPrimaryClipInternal(PerUserClipboard clipboard, ClipData clip) { |
| 290 | clipboard.activePermissionOwners.clear(); |
| 291 | if (clip == null && clipboard.primaryClip == null) { |
| 292 | return; |
| 293 | } |
| 294 | clipboard.primaryClip = clip; |
| 295 | final long ident = Binder.clearCallingIdentity(); |
| 296 | final int n = clipboard.primaryClipListeners.beginBroadcast(); |
| 297 | try { |
| 298 | for (int i = 0; i < n; i++) { |
| 299 | try { |
| 300 | ListenerInfo li = (ListenerInfo) |
| 301 | clipboard.primaryClipListeners.getBroadcastCookie(i); |
| 302 | if (mAppOps.checkOpNoThrow(AppOpsManager.OP_READ_CLIPBOARD, li.mUid, |
| 303 | li.mPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 304 | clipboard.primaryClipListeners.getBroadcastItem(i) |
| 305 | .dispatchPrimaryClipChanged(); |
| 306 | } |
| 307 | } catch (RemoteException e) { |
| 308 | // The RemoteCallbackList will take care of removing |
| 309 | // the dead object for us. |
| 310 | } |
| 311 | } |
| 312 | } finally { |
| 313 | clipboard.primaryClipListeners.finishBroadcast(); |
| 314 | Binder.restoreCallingIdentity(ident); |
| 315 | } |
| 316 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 317 | |
| 318 | private final void checkUriOwnerLocked(Uri uri, int uid) { |
| 319 | if (!"content".equals(uri.getScheme())) { |
| 320 | return; |
| 321 | } |
| 322 | long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 323 | try { |
| 324 | // This will throw SecurityException for us. |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 325 | mAm.checkGrantUriPermission(uid, null, ContentProvider.getUriWithoutUserId(uri), |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 326 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 327 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(uid))); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 328 | } catch (RemoteException e) { |
| 329 | } finally { |
| 330 | Binder.restoreCallingIdentity(ident); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | private final void checkItemOwnerLocked(ClipData.Item item, int uid) { |
| 335 | if (item.getUri() != null) { |
| 336 | checkUriOwnerLocked(item.getUri(), uid); |
| 337 | } |
| 338 | Intent intent = item.getIntent(); |
| 339 | if (intent != null && intent.getData() != null) { |
| 340 | checkUriOwnerLocked(intent.getData(), uid); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | private final void checkDataOwnerLocked(ClipData data, int uid) { |
| 345 | final int N = data.getItemCount(); |
| 346 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 347 | checkItemOwnerLocked(data.getItemAt(i), uid); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 351 | private final void grantUriLocked(Uri uri, String pkg, int userId) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 352 | long ident = Binder.clearCallingIdentity(); |
| 353 | try { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 354 | int sourceUserId = ContentProvider.getUserIdFromUri(uri, userId); |
| 355 | uri = ContentProvider.getUriWithoutUserId(uri); |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 356 | mAm.grantUriPermissionFromOwner(mPermissionOwner, Process.myUid(), pkg, |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 357 | uri, Intent.FLAG_GRANT_READ_URI_PERMISSION, sourceUserId, userId); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 358 | } catch (RemoteException e) { |
| 359 | } finally { |
| 360 | Binder.restoreCallingIdentity(ident); |
| 361 | } |
| 362 | } |
| 363 | |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 364 | private final void grantItemLocked(ClipData.Item item, String pkg, int userId) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 365 | if (item.getUri() != null) { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 366 | grantUriLocked(item.getUri(), pkg, userId); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 367 | } |
| 368 | Intent intent = item.getIntent(); |
| 369 | if (intent != null && intent.getData() != null) { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 370 | grantUriLocked(intent.getData(), pkg, userId); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | private final void addActiveOwnerLocked(int uid, String pkg) { |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 375 | final IPackageManager pm = AppGlobals.getPackageManager(); |
| 376 | final int targetUserHandle = UserHandle.getCallingUserId(); |
| 377 | final long oldIdentity = Binder.clearCallingIdentity(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 378 | try { |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 379 | PackageInfo pi = pm.getPackageInfo(pkg, 0, targetUserHandle); |
| 380 | if (pi == null) { |
| 381 | throw new IllegalArgumentException("Unknown package " + pkg); |
| 382 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 383 | if (!UserHandle.isSameApp(pi.applicationInfo.uid, uid)) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 384 | throw new SecurityException("Calling uid " + uid |
| 385 | + " does not own package " + pkg); |
| 386 | } |
Christopher Tate | ad9833a | 2012-09-14 13:34:17 -0700 | [diff] [blame] | 387 | } catch (RemoteException e) { |
| 388 | // Can't happen; the package manager is in the same process |
| 389 | } finally { |
| 390 | Binder.restoreCallingIdentity(oldIdentity); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 391 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 392 | PerUserClipboard clipboard = getClipboard(); |
| 393 | if (clipboard.primaryClip != null && !clipboard.activePermissionOwners.contains(pkg)) { |
| 394 | final int N = clipboard.primaryClip.getItemCount(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 395 | for (int i=0; i<N; i++) { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 396 | grantItemLocked(clipboard.primaryClip.getItemAt(i), pkg, UserHandle.getUserId(uid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 397 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 398 | clipboard.activePermissionOwners.add(pkg); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | private final void revokeUriLocked(Uri uri) { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 403 | int userId = ContentProvider.getUserIdFromUri(uri, |
| 404 | UserHandle.getUserId(Binder.getCallingUid())); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 405 | long ident = Binder.clearCallingIdentity(); |
| 406 | try { |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 407 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 408 | mAm.revokeUriPermissionFromOwner(mPermissionOwner, uri, |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 409 | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 410 | userId); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 411 | } catch (RemoteException e) { |
| 412 | } finally { |
| 413 | Binder.restoreCallingIdentity(ident); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private final void revokeItemLocked(ClipData.Item item) { |
| 418 | if (item.getUri() != null) { |
| 419 | revokeUriLocked(item.getUri()); |
| 420 | } |
| 421 | Intent intent = item.getIntent(); |
| 422 | if (intent != null && intent.getData() != null) { |
| 423 | revokeUriLocked(intent.getData()); |
| 424 | } |
| 425 | } |
| 426 | |
Nicolas Prevot | f193990 | 2014-06-25 09:29:02 +0100 | [diff] [blame] | 427 | private final void revokeUris(PerUserClipboard clipboard) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 428 | if (clipboard.primaryClip == null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 431 | final int N = clipboard.primaryClip.getItemCount(); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 432 | for (int i=0; i<N; i++) { |
Amith Yamasani | e9e26cc | 2012-04-20 19:01:50 -0700 | [diff] [blame] | 433 | revokeItemLocked(clipboard.primaryClip.getItemAt(i)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } |