forked from AndDiSa/platform_manifest-Grouper-AOSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages_provider_downloadprovider.patch
326 lines (313 loc) · 15.8 KB
/
packages_provider_downloadprovider.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java
index e95eb50..db016bb 100644
--- a/src/com/android/providers/downloads/DownloadProvider.java
+++ b/src/com/android/providers/downloads/DownloadProvider.java
@@ -20,7 +20,9 @@ import static android.provider.BaseColumns._ID;
import static android.provider.Downloads.Impl.COLUMN_DESTINATION;
import static android.provider.Downloads.Impl.COLUMN_MEDIA_SCANNED;
import static android.provider.Downloads.Impl.COLUMN_MIME_TYPE;
+import static android.provider.Downloads.Impl.COLUMN_OTHER_UID;
import static android.provider.Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD;
+import static android.provider.Downloads.Impl.PERMISSION_ACCESS_ALL;
import static android.provider.Downloads.Impl._DATA;
import android.app.AppOpsManager;
@@ -42,6 +44,7 @@ import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
+import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Binder;
import android.os.ParcelFileDescriptor;
@@ -96,17 +99,13 @@ public final class DownloadProvider extends ContentProvider {
private static final int MY_DOWNLOADS = 1;
/** URI matcher constant for the URI of an individual download belonging to the calling UID */
private static final int MY_DOWNLOADS_ID = 2;
+ private static final int MY_DOWNLOADS_ID_HEADERS = 3;
/** URI matcher constant for the URI of all downloads in the system */
- private static final int ALL_DOWNLOADS = 3;
+ private static final int ALL_DOWNLOADS = 4;
/** URI matcher constant for the URI of an individual download */
- private static final int ALL_DOWNLOADS_ID = 4;
+ private static final int ALL_DOWNLOADS_ID = 5;
/** URI matcher constant for the URI of a download's request headers */
- private static final int REQUEST_HEADERS_URI = 5;
- /** URI matcher constant for the public URI returned by
- * {@link DownloadManager#getUriForDownloadedFile(long)} if the given downloaded file
- * is publicly accessible.
- */
- private static final int PUBLIC_DOWNLOAD_ID = 6;
+ private static final int ALL_DOWNLOADS_ID_HEADERS = 6;
static {
sURIMatcher.addURI("downloads", "my_downloads", MY_DOWNLOADS);
sURIMatcher.addURI("downloads", "my_downloads/#", MY_DOWNLOADS_ID);
@@ -114,19 +113,16 @@ public final class DownloadProvider extends ContentProvider {
sURIMatcher.addURI("downloads", "all_downloads/#", ALL_DOWNLOADS_ID);
sURIMatcher.addURI("downloads",
"my_downloads/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
- REQUEST_HEADERS_URI);
+ MY_DOWNLOADS_ID_HEADERS);
sURIMatcher.addURI("downloads",
"all_downloads/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
- REQUEST_HEADERS_URI);
+ MY_DOWNLOADS_ID_HEADERS);
// temporary, for backwards compatibility
sURIMatcher.addURI("downloads", "download", MY_DOWNLOADS);
sURIMatcher.addURI("downloads", "download/#", MY_DOWNLOADS_ID);
sURIMatcher.addURI("downloads",
"download/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
- REQUEST_HEADERS_URI);
- sURIMatcher.addURI("downloads",
- Downloads.Impl.PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT + "/#",
- PUBLIC_DOWNLOAD_ID);
+ MY_DOWNLOADS_ID_HEADERS);
}
/** Different base URIs that could be used to access an individual download */
@@ -188,43 +184,6 @@ public final class DownloadProvider extends ContentProvider {
private int mSystemUid = -1;
private int mDefContainerUid = -1;
- /**
- * This class encapsulates a SQL where clause and its parameters. It makes it possible for
- * shared methods (like {@link DownloadProvider#getWhereClause(Uri, String, String[], int)})
- * to return both pieces of information, and provides some utility logic to ease piece-by-piece
- * construction of selections.
- */
- private static class SqlSelection {
- public StringBuilder mWhereClause = new StringBuilder();
- public List<String> mParameters = new ArrayList<String>();
-
- public <T> void appendClause(String newClause, final T... parameters) {
- if (newClause == null || newClause.isEmpty()) {
- return;
- }
- if (mWhereClause.length() != 0) {
- mWhereClause.append(" AND ");
- }
- mWhereClause.append("(");
- mWhereClause.append(newClause);
- mWhereClause.append(")");
- if (parameters != null) {
- for (Object parameter : parameters) {
- mParameters.add(parameter.toString());
- }
- }
- }
-
- public String getSelection() {
- return mWhereClause.toString();
- }
-
- public String[] getParameters() {
- String[] array = new String[mParameters.size()];
- return mParameters.toArray(array);
- }
- }
-
/**
* Creates and updated database on demand when opening it.
* Helper class to create database the first time the provider is
@@ -522,8 +481,7 @@ public final class DownloadProvider extends ContentProvider {
return DOWNLOAD_LIST_TYPE;
}
case MY_DOWNLOADS_ID:
- case ALL_DOWNLOADS_ID:
- case PUBLIC_DOWNLOAD_ID: {
+ case ALL_DOWNLOADS_ID: {
// return the mimetype of this id from the database
final String id = getDownloadIdFromUri(uri);
final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
@@ -947,16 +905,22 @@ public final class DownloadProvider extends ContentProvider {
throw new IllegalArgumentException("Unknown URI: " + uri);
}
- if (match == REQUEST_HEADERS_URI) {
+ if (match == MY_DOWNLOADS_ID_HEADERS || match == ALL_DOWNLOADS_ID_HEADERS) {
if (projection != null || selection != null || sort != null) {
throw new UnsupportedOperationException("Request header queries do not support "
+ "projections, selections or sorting");
}
- return queryRequestHeaders(db, uri);
- }
-
- SqlSelection fullSelection = getWhereClause(uri, selection, selectionArgs, match);
+ // Headers are only available to callers with full access.
+ getContext().enforceCallingOrSelfPermission(
+ Downloads.Impl.PERMISSION_ACCESS_ALL, Constants.TAG);
+ final SQLiteQueryBuilder qb = getQueryBuilder(uri, match);
+ projection = new String[] {
+ Downloads.Impl.RequestHeaders.COLUMN_HEADER,
+ Downloads.Impl.RequestHeaders.COLUMN_VALUE
+ };
+ return qb.query(db, projection, null, null, null, null, null);
+ }
if (shouldRestrictVisibility()) {
if (projection == null) {
projection = sAppReadableColumnsArray.clone();
@@ -983,8 +947,8 @@ public final class DownloadProvider extends ContentProvider {
logVerboseQueryInfo(projection, selection, selectionArgs, sort, db);
}
- Cursor ret = db.query(DB_TABLE, projection, fullSelection.getSelection(),
- fullSelection.getParameters(), null, null, sort);
+ final SQLiteQueryBuilder qb = getQueryBuilder(uri, match);
+ final Cursor ret = qb.query(db, projection, selection, selectionArgs, null, null, sort);
if (ret != null) {
ret.setNotificationUri(getContext().getContentResolver(), uri);
@@ -1069,35 +1033,6 @@ public final class DownloadProvider extends ContentProvider {
}
}
- /**
- * Handle a query for the custom request headers registered for a download.
- */
- private Cursor queryRequestHeaders(SQLiteDatabase db, Uri uri) {
- String where = Downloads.Impl.RequestHeaders.COLUMN_DOWNLOAD_ID + "="
- + getDownloadIdFromUri(uri);
- String[] projection = new String[] {Downloads.Impl.RequestHeaders.COLUMN_HEADER,
- Downloads.Impl.RequestHeaders.COLUMN_VALUE};
- return db.query(Downloads.Impl.RequestHeaders.HEADERS_DB_TABLE, projection, where,
- null, null, null, null);
- }
-
- /**
- * Delete request headers for downloads matching the given query.
- */
- private void deleteRequestHeaders(SQLiteDatabase db, String where, String[] whereArgs) {
- String[] projection = new String[] {Downloads.Impl._ID};
- Cursor cursor = db.query(DB_TABLE, projection, where, whereArgs, null, null, null, null);
- try {
- for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
- long id = cursor.getLong(0);
- String idWhere = Downloads.Impl.RequestHeaders.COLUMN_DOWNLOAD_ID + "=" + id;
- db.delete(Downloads.Impl.RequestHeaders.HEADERS_DB_TABLE, idWhere, null);
- }
- } finally {
- cursor.close();
- }
- }
-
/**
* @return true if we should restrict the columns readable by this caller
*/
@@ -1179,13 +1114,11 @@ public final class DownloadProvider extends ContentProvider {
break;
}
- final SqlSelection selection = getWhereClause(uri, where, whereArgs, match);
- count = db.update(DB_TABLE, filteredValues, selection.getSelection(),
- selection.getParameters());
+ final SQLiteQueryBuilder qb = getQueryBuilder(uri, match);
+ count = qb.update(db, filteredValues, where, whereArgs);
if (updateSchedule || isCompleting) {
final long token = Binder.clearCallingIdentity();
- try (Cursor cursor = db.query(DB_TABLE, null, selection.getSelection(),
- selection.getParameters(), null, null, null)) {
+ try (Cursor cursor = qb.query(db, null, where, whereArgs, null, null, null)) {
final DownloadInfo.Reader reader = new DownloadInfo.Reader(resolver,
cursor);
final DownloadInfo info = new DownloadInfo(context);
@@ -1231,24 +1164,65 @@ public final class DownloadProvider extends ContentProvider {
}
}
- private SqlSelection getWhereClause(final Uri uri, final String where, final String[] whereArgs,
- int uriMatch) {
- SqlSelection selection = new SqlSelection();
- selection.appendClause(where, whereArgs);
- if (uriMatch == MY_DOWNLOADS_ID || uriMatch == ALL_DOWNLOADS_ID ||
- uriMatch == PUBLIC_DOWNLOAD_ID) {
- selection.appendClause(Downloads.Impl._ID + " = ?", getDownloadIdFromUri(uri));
- }
- if ((uriMatch == MY_DOWNLOADS || uriMatch == MY_DOWNLOADS_ID)
- && getContext().checkCallingOrSelfPermission(Downloads.Impl.PERMISSION_ACCESS_ALL)
- != PackageManager.PERMISSION_GRANTED) {
- selection.appendClause(
- Constants.UID + "= ? OR " + Downloads.Impl.COLUMN_OTHER_UID + "= ?",
- Binder.getCallingUid(), Binder.getCallingUid());
- }
- return selection;
+ /**
+ * Create a query builder that filters access to the underlying database
+ * based on both the requested {@link Uri} and permissions of the caller.
+ */
+ private SQLiteQueryBuilder getQueryBuilder(final Uri uri, int match) {
+ final String table;
+ final StringBuilder where = new StringBuilder();
+ switch (match) {
+ // The "my_downloads" view normally limits the caller to operating
+ // on downloads that they either directly own, or have been given
+ // indirect ownership of via OTHER_UID.
+ case MY_DOWNLOADS_ID:
+ appendWhereExpression(where, _ID + "=" + getDownloadIdFromUri(uri));
+ // fall-through
+ case MY_DOWNLOADS:
+ table = DB_TABLE;
+ if (getContext().checkCallingOrSelfPermission(
+ PERMISSION_ACCESS_ALL) != PackageManager.PERMISSION_GRANTED) {
+ appendWhereExpression(where, Constants.UID + "=" + Binder.getCallingUid()
+ + " OR " + COLUMN_OTHER_UID + "=" + Binder.getCallingUid());
+ }
+ break;
+
+ // The "all_downloads" view is already limited via <path-permission>
+ // to only callers holding the ACCESS_ALL_DOWNLOADS permission, but
+ // access may also be delegated via Uri permission grants.
+ case ALL_DOWNLOADS_ID:
+ appendWhereExpression(where, _ID + "=" + getDownloadIdFromUri(uri));
+ // fall-through
+ case ALL_DOWNLOADS:
+ table = DB_TABLE;
+ break;
+
+ // Headers are limited to callers holding the ACCESS_ALL_DOWNLOADS
+ // permission, since they're only needed for executing downloads.
+ case MY_DOWNLOADS_ID_HEADERS:
+ case ALL_DOWNLOADS_ID_HEADERS:
+ table = Downloads.Impl.RequestHeaders.HEADERS_DB_TABLE;
+ appendWhereExpression(where, Downloads.Impl.RequestHeaders.COLUMN_DOWNLOAD_ID + "="
+ + getDownloadIdFromUri(uri));
+ break;
+
+ default:
+ throw new UnsupportedOperationException("Unknown URI: " + uri);
+ }
+
+ final SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
+ qb.setStrict(true);
+ qb.setTables(table);
+ qb.appendWhere(where);
+ return qb;
}
+ private static void appendWhereExpression(StringBuilder sb, String expression) {
+ if (sb.length() > 0) {
+ sb.append(" AND ");
+ }
+ sb.append('(').append(expression).append(')');
+ }
/**
* Deletes a row in the database
*/
@@ -1270,11 +1244,8 @@ public final class DownloadProvider extends ContentProvider {
case MY_DOWNLOADS_ID:
case ALL_DOWNLOADS:
case ALL_DOWNLOADS_ID:
- final SqlSelection selection = getWhereClause(uri, where, whereArgs, match);
- deleteRequestHeaders(db, selection.getSelection(), selection.getParameters());
-
- try (Cursor cursor = db.query(DB_TABLE, null, selection.getSelection(),
- selection.getParameters(), null, null, null)) {
+ final SQLiteQueryBuilder qb = getQueryBuilder(uri, match);
+ try (Cursor cursor = qb.query(db, null, where, whereArgs, null, null, null)) {
final DownloadInfo.Reader reader = new DownloadInfo.Reader(resolver, cursor);
final DownloadInfo info = new DownloadInfo(context);
while (cursor.moveToNext()) {
@@ -1314,10 +1285,15 @@ public final class DownloadProvider extends ContentProvider {
if (!Downloads.Impl.isStatusCompleted(info.mStatus)) {
info.sendIntentIfRequested();
}
+
+ // Delete any headers for this download
+ db.delete(Downloads.Impl.RequestHeaders.HEADERS_DB_TABLE,
+ Downloads.Impl.RequestHeaders.COLUMN_DOWNLOAD_ID + "=?",
+ new String[] { Long.toString(info.mId) });
}
}
- count = db.delete(DB_TABLE, selection.getSelection(), selection.getParameters());
+ count = qb.delete(db, where, whereArgs);
break;
default: