Preconditions.checkNotNull(uri, "uri");
try {
ActivityManager.getService().takePersistableUriPermission(
- ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
+ ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
+ resolveUserId(uri));
+ } catch (RemoteException e) {
+ }
+ }
+
+ /**
+ * @hide
+ */
+ public void takePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
+ @Intent.AccessUriMode int modeFlags) {
+ Preconditions.checkNotNull(toPackage, "toPackage");
+ Preconditions.checkNotNull(uri, "uri");
+ try {
+ ActivityManager.getService().takePersistableUriPermission(
+ ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
+ resolveUserId(uri));
} catch (RemoteException e) {
}
}
Preconditions.checkNotNull(uri, "uri");
try {
ActivityManager.getService().releasePersistableUriPermission(
- ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
+ ContentProvider.getUriWithoutUserId(uri), modeFlags, /* toPackage= */ null,
+ resolveUserId(uri));
+ } catch (RemoteException e) {
+ }
+ }
+
+ /**
+ * @hide
+ */
+ public void releasePersistableUriPermission(@NonNull String toPackage, @NonNull Uri uri,
+ @Intent.AccessUriMode int modeFlags) {
+ Preconditions.checkNotNull(toPackage, "toPackage");
+ Preconditions.checkNotNull(uri, "uri");
+ try {
+ ActivityManager.getService().releasePersistableUriPermission(
+ ContentProvider.getUriWithoutUserId(uri), modeFlags, toPackage,
+ resolveUserId(uri));
} catch (RemoteException e) {
}
}
}
/**
- * Updates (grants or revokes) a persitable URI permission.
- *
- * @param uri URI to be granted or revoked.
- * @param prefix if {@code false}, permission apply to this specific URI; if {@code true}, it
- * applies to all URIs that are prefixed by this URI.
- * @param packageName target package.
- * @param grant if {@code true} a new permission will be granted, otherwise an existing
- * permission will be revoked.
- * @param userId user handle
- *
- * @return whether or not the requested succeeded.
- *
- * @deprecated TODO(b/72055774): caller should use takePersistableUriPermission() or
- * releasePersistableUriPermission() instead, but such change will be made in a separate CL
- * so it can be easily reverted if it breaks existing functionality.
- */
- @Deprecated // STOPSHIP if not removed
- @Override
- public boolean updatePersistableUriPermission(Uri uri, boolean prefix, String packageName,
- boolean grant, int userId) {
- enforceCallingPermission(android.Manifest.permission.GET_APP_GRANTED_URI_PERMISSIONS,
- "updatePersistableUriPermission");
- final int uid = mPackageManagerInt.getPackageUid(packageName, 0, userId);
-
- final GrantUri grantUri = new GrantUri(userId, uri, prefix);
-
- boolean persistChanged = false;
- synchronized (this) {
- if (grant) { // Grant
- final String authority = uri.getAuthority();
- final ProviderInfo pi = getProviderInfoLocked(authority, userId, 0);
- if (pi == null) {
- Slog.w(TAG, "No content provider found for authority " + authority);
- return false;
- }
- final UriPermission permission = findOrCreateUriPermissionLocked(pi.packageName,
- packageName, uid, grantUri);
- if (permission.isNew()) {
- final int modeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION
- | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
- permission.initPersistedModes(modeFlags, System.currentTimeMillis());
- persistChanged = true;
- } else {
- // Caller should not try to grant permission that is already granted.
- Slog.w(TAG_URI_PERMISSION,
- "permission already granted for " + grantUri.toSafeString());
- return false;
- }
- persistChanged |= maybePrunePersistedUriGrantsLocked(uid);
- } else { // Revoke
- final UriPermission permission = findUriPermissionLocked(uid, grantUri);
- if (permission == null) {
- // Caller should not try to revoke permission that is not granted.
- Slog.v(TAG_URI_PERMISSION, "no permission for " + grantUri.toSafeString());
- return false;
- } else {
- permission.modeFlags = 0;
- removeUriPermissionIfNeededLocked(permission);
- persistChanged = true;
- }
- }
- if (persistChanged) {
- schedulePersistUriGrants();
- }
- }
- return true;
- }
-
- /**
* @param uri This uri must NOT contain an embedded userId.
+ * @param toPackage Name of package whose uri is being granted to (if {@code null}, uses
+ * calling uid)
* @param userId The userId in which the uri is to be resolved.
*/
@Override
- public void takePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
- enforceNotIsolatedCaller("takePersistableUriPermission");
+ public void takePersistableUriPermission(Uri uri, final int modeFlags,
+ @Nullable String toPackage, int userId) {
+ final int uid;
+ if (toPackage != null) {
+ enforceCallingPermission(android.Manifest.permission.FORCE_PERSISTABLE_URI_PERMISSIONS,
+ "takePersistableUriPermission");
+ uid = mPackageManagerInt.getPackageUid(toPackage, 0, userId);
+ } else {
+ enforceNotIsolatedCaller("takePersistableUriPermission");
+ uid = Binder.getCallingUid();
+ }
Preconditions.checkFlagsArgument(modeFlags,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
synchronized (this) {
- final int callingUid = Binder.getCallingUid();
boolean persistChanged = false;
GrantUri grantUri = new GrantUri(userId, uri, false);
- UriPermission exactPerm = findUriPermissionLocked(callingUid, grantUri);
- UriPermission prefixPerm = findUriPermissionLocked(callingUid,
+ UriPermission exactPerm = findUriPermissionLocked(uid, grantUri);
+ UriPermission prefixPerm = findUriPermissionLocked(uid,
new GrantUri(userId, uri, true));
final boolean exactValid = (exactPerm != null)
if (!(exactValid || prefixValid)) {
throw new SecurityException("No persistable permission grants found for UID "
- + callingUid + " and Uri " + grantUri.toSafeString());
+ + uid + " and Uri " + grantUri.toSafeString());
}
if (exactValid) {
persistChanged |= prefixPerm.takePersistableModes(modeFlags);
}
- persistChanged |= maybePrunePersistedUriGrantsLocked(callingUid);
+ persistChanged |= maybePrunePersistedUriGrantsLocked(uid);
if (persistChanged) {
schedulePersistUriGrants();
/**
* @param uri This uri must NOT contain an embedded userId.
+ * @param toPackage Name of the target package whose uri is being released (if {@code null},
+ * uses calling uid)
* @param userId The userId in which the uri is to be resolved.
*/
@Override
- public void releasePersistableUriPermission(Uri uri, final int modeFlags, int userId) {
- enforceNotIsolatedCaller("releasePersistableUriPermission");
+ public void releasePersistableUriPermission(Uri uri, final int modeFlags,
+ @Nullable String toPackage, int userId) {
+
+ final int uid;
+ if (toPackage != null) {
+ enforceCallingPermission(android.Manifest.permission.FORCE_PERSISTABLE_URI_PERMISSIONS,
+ "releasePersistableUriPermission");
+ uid = mPackageManagerInt.getPackageUid(toPackage, 0, userId);
+ } else {
+ enforceNotIsolatedCaller("releasePersistableUriPermission");
+ uid = Binder.getCallingUid();
+ }
Preconditions.checkFlagsArgument(modeFlags,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
synchronized (this) {
- final int callingUid = Binder.getCallingUid();
boolean persistChanged = false;
- UriPermission exactPerm = findUriPermissionLocked(callingUid,
+ UriPermission exactPerm = findUriPermissionLocked(uid,
new GrantUri(userId, uri, false));
- UriPermission prefixPerm = findUriPermissionLocked(callingUid,
+ UriPermission prefixPerm = findUriPermissionLocked(uid,
new GrantUri(userId, uri, true));
- if (exactPerm == null && prefixPerm == null) {
- throw new SecurityException("No permission grants found for UID " + callingUid
+ if (exactPerm == null && prefixPerm == null && toPackage == null) {
+ throw new SecurityException("No permission grants found for UID " + uid
+ " and Uri " + uri.toSafeString());
}