diff options
author | Suprabh Shukla <suprabh@google.com> | 2017-03-14 00:55:34 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-03-14 00:55:34 +0000 |
commit | 9fdf7777807fc90a5fa3a8dc5e9beac1b69e1e89 (patch) | |
tree | b74cf0ce7345a599088ebc2652c48e1a1e922edc | |
parent | 8e6c8093709b144e69efb8c6bff4be50729f3c25 (diff) | |
parent | 6eb1898f6588aad3325108e70602ffe78085196d (diff) | |
download | android_packages_providers_DownloadProvider-9fdf7777807fc90a5fa3a8dc5e9beac1b69e1e89.tar.gz android_packages_providers_DownloadProvider-9fdf7777807fc90a5fa3a8dc5e9beac1b69e1e89.tar.bz2 android_packages_providers_DownloadProvider-9fdf7777807fc90a5fa3a8dc5e9beac1b69e1e89.zip |
DO NOT MERGE Deleting downloads for removed uids on downloadprovider start am: 8ad6045738
am: 6eb1898f65
Change-Id: I33c176c144ba8bbbf53446cf1c9bec8e057f95b8
-rw-r--r-- | src/com/android/providers/downloads/DownloadProvider.java | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java index ee48af16..d6521d0b 100644 --- a/src/com/android/providers/downloads/DownloadProvider.java +++ b/src/com/android/providers/downloads/DownloadProvider.java @@ -460,6 +460,32 @@ public final class DownloadProvider extends ContentProvider { if (appInfo != null) { mDefContainerUid = appInfo.uid; } + + // Grant access permissions for all known downloads to the owning apps + final SQLiteDatabase db = mOpenHelper.getReadableDatabase(); + final Cursor cursor = db.query(DB_TABLE, new String[] { + Downloads.Impl._ID, Constants.UID }, null, null, null, null, null); + final ArrayList<Long> idsToDelete = new ArrayList<Long>(); + try { + while (cursor.moveToNext()) { + final long downloadId = cursor.getLong(0); + final int uid = cursor.getInt(1); + final String ownerPackage = getPackageForUid(uid); + if (ownerPackage == null) { + idsToDelete.add(downloadId); + } else { + grantAllDownloadsPermission(ownerPackage, downloadId); + } + } + } finally { + cursor.close(); + } + if (idsToDelete.size() > 0) { + Log.i(Constants.TAG, + "Deleting downloads with ids " + idsToDelete + " as owner package is missing"); + deleteDownloadsWithIds(idsToDelete); + } + // start the DownloadService class. don't wait for the 1st download to be issued. // saves us by getting some initialization code in DownloadService out of the way. Context context = getContext(); @@ -473,6 +499,19 @@ public final class DownloadProvider extends ContentProvider { return true; } + private void deleteDownloadsWithIds(ArrayList<Long> downloadIds) { + final int N = downloadIds.size(); + if (N == 0) { + return; + } + final StringBuilder queryBuilder = new StringBuilder(Downloads.Impl._ID + " in ("); + for (int i = 0; i < N; i++) { + queryBuilder.append(downloadIds.get(i)); + queryBuilder.append((i == N - 1) ? ")" : ","); + } + delete(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, queryBuilder.toString(), null); + } + /** * Returns the content-provider-style MIME types of the various * types accessible through this content provider. @@ -683,6 +722,13 @@ public final class DownloadProvider extends ContentProvider { } insertRequestHeaders(db, rowID, values); + + final String callingPackage = getPackageForUid(Binder.getCallingUid()); + if (callingPackage == null) { + Log.e(Constants.TAG, "Package does not exist for calling uid"); + return null; + } + grantAllDownloadsPermission(callingPackage, rowID); notifyContentChanged(uri, match); // Always start service to handle notifications and/or scanning @@ -692,6 +738,15 @@ public final class DownloadProvider extends ContentProvider { return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, rowID); } + private String getPackageForUid(int uid) { + String[] packages = getContext().getPackageManager().getPackagesForUid(uid); + if (packages == null || packages.length == 0) { + return null; + } + // For permission related purposes, any package belonging to the given uid should work. + return packages[0]; + } + /** * Check that the file URI provided for DESTINATION_FILE_URI is valid. */ @@ -1139,7 +1194,9 @@ public final class DownloadProvider extends ContentProvider { public int delete(final Uri uri, final String where, final String[] whereArgs) { - Helpers.validateSelection(where, sAppReadableColumnsSet); + if (shouldRestrictVisibility()) { + Helpers.validateSelection(where, sAppReadableColumnsSet); + } SQLiteDatabase db = mOpenHelper.getWritableDatabase(); int count; @@ -1336,4 +1393,15 @@ public final class DownloadProvider extends ContentProvider { to.put(key, defaultValue); } } + + private void grantAllDownloadsPermission(String toPackage, long id) { + final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, id); + getContext().grantUriPermission(toPackage, uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + } + + private void revokeAllDownloadsPermission(long id) { + final Uri uri = ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, id); + getContext().revokeUriPermission(uri, ~0); + } } |