|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.db; |
|
6 |
|
7 import org.mozilla.gecko.mozglue.RobocopTarget; |
|
8 |
|
9 import android.database.sqlite.SQLiteDatabase; |
|
10 import android.database.sqlite.SQLiteOpenHelper; |
|
11 import android.net.Uri; |
|
12 |
|
13 /** |
|
14 * The base class for ContentProviders that wish to use a different DB |
|
15 * for each profile. |
|
16 * |
|
17 * This class has logic shared between ordinary per-profile CPs and |
|
18 * those that wish to share DB connections between CPs. |
|
19 */ |
|
20 public abstract class AbstractPerProfileDatabaseProvider extends AbstractTransactionalProvider { |
|
21 |
|
22 /** |
|
23 * Extend this to provide access to your own map of shared databases. This |
|
24 * is a method so that your subclass doesn't collide with others! |
|
25 */ |
|
26 protected abstract PerProfileDatabases<? extends SQLiteOpenHelper> getDatabases(); |
|
27 |
|
28 /* |
|
29 * Fetches a readable database based on the profile indicated in the |
|
30 * passed URI. If the URI does not contain a profile param, the default profile |
|
31 * is used. |
|
32 * |
|
33 * @param uri content URI optionally indicating the profile of the user |
|
34 * @return instance of a readable SQLiteDatabase |
|
35 */ |
|
36 @Override |
|
37 protected SQLiteDatabase getReadableDatabase(Uri uri) { |
|
38 String profile = null; |
|
39 if (uri != null) { |
|
40 profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); |
|
41 } |
|
42 |
|
43 return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase(); |
|
44 } |
|
45 |
|
46 /* |
|
47 * Fetches a writable database based on the profile indicated in the |
|
48 * passed URI. If the URI does not contain a profile param, the default profile |
|
49 * is used |
|
50 * |
|
51 * @param uri content URI optionally indicating the profile of the user |
|
52 * @return instance of a writable SQLiteDatabase |
|
53 */ |
|
54 @Override |
|
55 protected SQLiteDatabase getWritableDatabase(Uri uri) { |
|
56 String profile = null; |
|
57 if (uri != null) { |
|
58 profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); |
|
59 } |
|
60 |
|
61 return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase(); |
|
62 } |
|
63 |
|
64 protected SQLiteDatabase getWritableDatabaseForProfile(String profile, boolean isTest) { |
|
65 return getDatabases().getDatabaseHelperForProfile(profile, isTest).getWritableDatabase(); |
|
66 } |
|
67 |
|
68 /** |
|
69 * This method should ONLY be used for testing purposes. |
|
70 * |
|
71 * @param uri content URI optionally indicating the profile of the user |
|
72 * @return instance of a writable SQLiteDatabase |
|
73 */ |
|
74 @Override |
|
75 @RobocopTarget |
|
76 public SQLiteDatabase getWritableDatabaseForTesting(Uri uri) { |
|
77 return getWritableDatabase(uri); |
|
78 } |
|
79 } |