summaryrefslogtreecommitdiff
path: root/src/com/android/providers/drm/DrmProvider.java
blob: aec88b46d97ddbc50f5c5eb8fb29a6c797283643 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.drm;

import android.content.*;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.DrmStore;
import android.text.TextUtils;
import android.util.Config;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * Drm content provider. See {@link android.provider.DrmStore} for details.
 *
 * @hide
 */
public class DrmProvider extends ContentProvider
{
    /**
     * Creates and updated database on demand when opening it.
     * Helper class to create database the first time the provider is
     * initialized and upgrade it when a new version of the provider needs
     * an updated version of the database.
     */
    private final class OpenDatabaseHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "drm.db";
        private static final int DATABASE_VERSION = 1;

        OpenDatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        /**
         * Creates database the first time we try to open it.
         */
        @Override
        public void onCreate(final SQLiteDatabase db) {
            createTables(db);
        }

        /**
         * Checks data integrity when opening the database
         */
        @Override
        public void onOpen(final SQLiteDatabase db) {
            super.onOpen(db);
            // TODO: validate and/or clean up database in onOpen - should that
            //        be done in the service instead?
        }

        /**
         * Updates the database format when a new content provider is used
         * with an older database format.
         */
        // For now, just deletes the database.
        // TODO: decide on a general policy when updates become relevant
        // TODO: can there even be a downgrade? how can it be handled?
        // TODO: delete temporary files
        @Override
        public void onUpgrade(final SQLiteDatabase db,
                final int oldV, final int newV) {
            Log.i(TAG, "Upgrading downloads database from version " +
                  oldV+ " to " + newV +
                  ", which will destroy all old data");
            dropTables(db);
            createTables(db);
        }
    }

    @Override
    public boolean onCreate()
    {
        mOpenHelper = new OpenDatabaseHelper(getContext());
        return true;
    }

    /**
     * Creates the table that'll hold the download information.
     */
    private void createTables(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE audio (" +
                   "_id INTEGER PRIMARY KEY," +
                   "_data TEXT," +
                   "_size INTEGER," +
                   "title TEXT," +
                   "mime_type TEXT" +
                  ");");

        db.execSQL("CREATE TABLE images (" +
                   "_id INTEGER PRIMARY KEY," +
                   "_data TEXT," +
                   "_size INTEGER," +
                   "title TEXT," +
                   "mime_type TEXT" +
                  ");");
    }

    /**
     * Deletes the table that holds the download information.
     */
    private void dropTables(SQLiteDatabase db) {
        // TODO: error handling
        db.execSQL("DROP TABLE IF EXISTS audio");
        db.execSQL("DROP TABLE IF EXISTS images");
    }

    @Override
    public Cursor query(Uri uri, String[] projectionIn, String selection,
            String[] selectionArgs, String sort) {
        String groupBy = null;

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (URI_MATCHER.match(uri)) {
            case AUDIO:
                qb.setTables("audio");
                break;

            case AUDIO_ID:
                qb.setTables("audio");
                qb.appendWhere("_id=" + uri.getPathSegments().get(1));
                break;

            case IMAGES:
                qb.setTables("images");
                break;

            case IMAGES_ID:
                qb.setTables("images");
                qb.appendWhere("_id=" + uri.getPathSegments().get(1));
                break;

            default:
                throw new IllegalStateException("Unknown URL: " + uri.toString());
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projectionIn, selection,
                selectionArgs, groupBy, null, sort);
        if (c != null) {
            c.setNotificationUri(getContext().getContentResolver(), uri);
        }
        return c;
    }

    @Override
    public String getType(Uri url)
    {
        switch (URI_MATCHER.match(url)) {
            case AUDIO_ID:
            case IMAGES_ID:
                Cursor c = query(url, MIME_TYPE_PROJECTION, null, null, null);
                if (c != null && c.getCount() == 1) {
                    c.moveToFirst();
                    String mimeType = c.getString(1);
                    c.deactivate();
                    return mimeType;
                }
                break;
        }
        throw new IllegalStateException("Unknown URL");
    }

    /**
     * Ensures there is a file in the _data column of values, if one isn't
     * present a new file is created.
     *
     * @param initialValues the values passed to insert by the caller
     * @return the new values
     */
    private ContentValues ensureFile(ContentValues initialValues) {
        try {
            File parent = getContext().getFilesDir();
            parent.mkdirs();
            File file = File.createTempFile("DRM-", ".data", parent);
            ContentValues values = new ContentValues(initialValues);
            values.put("_data", file.toString());
            return values;
       } catch (IOException e) {
            Log.e(TAG, "Failed to create data file in ensureFile");
            return null;
       }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues)
    {
        if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires DRM permission");
        }

        long rowId;
        int match = URI_MATCHER.match(uri);
        Uri newUri = null;
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        if (initialValues == null) {
            initialValues = new ContentValues();
        }

        switch (match) {
            case AUDIO: {
                ContentValues values = ensureFile(initialValues);
                if (values == null) return null;
                rowId = db.insert("audio", "title", values);
                if (rowId > 0) {
                    newUri = ContentUris.withAppendedId(DrmStore.Audio.CONTENT_URI, rowId);
                }
                break;
            }

            case IMAGES: {
                ContentValues values = ensureFile(initialValues);
                if (values == null) return null;
                rowId = db.insert("images", "title", values);
                if (rowId > 0) {
                    newUri = ContentUris.withAppendedId(DrmStore.Images.CONTENT_URI, rowId);
                }
                break;
            }

            default:
                throw new UnsupportedOperationException("Invalid URI " + uri);
        }

        if (newUri != null) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        
        return newUri;
    }

    private static final class GetTableAndWhereOutParameter {
        public String table;
        public String where;
    }

    static final GetTableAndWhereOutParameter sGetTableAndWhereParam =
            new GetTableAndWhereOutParameter();

    private void getTableAndWhere(Uri uri, int match, String userWhere,
            GetTableAndWhereOutParameter out) {
        String where = null;
        switch (match) {
            case AUDIO:
                out.table = "audio";
                break;

            case AUDIO_ID:
                out.table = "audio";
                where = "_id=" + uri.getPathSegments().get(1);
                break;

            case IMAGES:
                out.table = "images";
                break;

            case IMAGES_ID:
                out.table = "images";
                where = "_id=" + uri.getPathSegments().get(1);
                break;

            default:
                throw new UnsupportedOperationException(
                        "Unknown or unsupported URL: " + uri.toString());
        }

        // Add in the user requested WHERE clause, if needed
        if (!TextUtils.isEmpty(userWhere)) {
            if (!TextUtils.isEmpty(where)) {
                out.where = where + " AND (" + userWhere + ")";
            } else {
                out.where = userWhere;
            }
        } else {
            out.where = where;
        }
    }

    private void deleteFiles(Uri uri, String userWhere, String[] whereArgs) {
        Cursor c = query(uri, new String [] { "_data" }, userWhere, whereArgs, null);

        try {
            if (c != null && c.moveToFirst()) {
                String prefix = getContext().getFilesDir().getPath();
                do {
                    String path = c.getString(0);
                    if (!path.startsWith(prefix)) {
                        throw new SecurityException("Attempted to delete a non-DRM file");
                    }
                    new File(path).delete();
                } while (c.moveToNext());
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    @Override
    public int delete(Uri uri, String userWhere, String[] whereArgs) {
        if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires DRM permission");
        }

        int count;
        int match = URI_MATCHER.match(uri);
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        synchronized (sGetTableAndWhereParam) {
            getTableAndWhere(uri, match, userWhere, sGetTableAndWhereParam);
            switch (match) {
                default:
                    deleteFiles(uri, userWhere, whereArgs);
                    count = db.delete(sGetTableAndWhereParam.table,
                            sGetTableAndWhereParam.where, whereArgs);
                    break;
            }
        }

        return count;
    }

    @Override
    public int update(Uri uri, ContentValues initialValues, String userWhere,
            String[] whereArgs) {
        if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires DRM permission");
        }

        int count;
        int match = URI_MATCHER.match(uri);
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        synchronized (sGetTableAndWhereParam) {
            getTableAndWhere(uri, match, userWhere, sGetTableAndWhereParam);

            switch (match) {
                default:
                    count = db.update(sGetTableAndWhereParam.table, initialValues,
                        sGetTableAndWhereParam.where, whereArgs);
                    break;
            }
        }

        return count;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires DRM permission");
        }
        return openFileHelper(uri, mode);
    }

    private static String TAG = "DrmProvider";

    private static final int AUDIO = 100;
    private static final int AUDIO_ID = 101;
    private static final int IMAGES = 102;
    private static final int IMAGES_ID = 103;

    private static final UriMatcher URI_MATCHER =
            new UriMatcher(UriMatcher.NO_MATCH);

    private static final String[] MIME_TYPE_PROJECTION = new String[] {
            DrmStore.Columns._ID, // 0
            DrmStore.Columns.MIME_TYPE, // 1
    };
    
    private SQLiteOpenHelper mOpenHelper;

    static
    {
        URI_MATCHER.addURI(DrmStore.AUTHORITY, "audio", AUDIO);
        URI_MATCHER.addURI(DrmStore.AUTHORITY, "audio/#", AUDIO_ID);
        URI_MATCHER.addURI(DrmStore.AUTHORITY, "images", IMAGES);
        URI_MATCHER.addURI(DrmStore.AUTHORITY, "images/#", IMAGES_ID);
    }
}