diff options
author | Suprabh Shukla <suprabh@google.com> | 2017-03-14 00:51:03 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-03-14 00:51:03 +0000 |
commit | 6eb1898f6588aad3325108e70602ffe78085196d (patch) | |
tree | d68c843863dbd0c103eb1c05757a43ace3337c7e | |
parent | e13f6fa9b6aa1665d0af0df75e1b3a9f7d3d481d (diff) | |
parent | 8ad60457381e6911199c692ce01916f56d2c07cc (diff) | |
download | android_packages_providers_DownloadProvider-6eb1898f6588aad3325108e70602ffe78085196d.tar.gz android_packages_providers_DownloadProvider-6eb1898f6588aad3325108e70602ffe78085196d.tar.bz2 android_packages_providers_DownloadProvider-6eb1898f6588aad3325108e70602ffe78085196d.zip |
DO NOT MERGE Deleting downloads for removed uids on downloadprovider start
am: 8ad6045738
Change-Id: I3cea41b3042850f88e3719243fe8a5ab1bdeb232
-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); + } } |