summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/db/AbstractPerProfileDatabaseProvider.java
blob: 2e056cc1ea7cd88987321e11be2658cf5b725f0d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.db;

import org.mozilla.gecko.annotation.RobocopTarget;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

/**
 * The base class for ContentProviders that wish to use a different DB
 * for each profile.
 *
 * This class has logic shared between ordinary per-profile CPs and
 * those that wish to share DB connections between CPs.
 */
public abstract class AbstractPerProfileDatabaseProvider extends AbstractTransactionalProvider {

    /**
     * Extend this to provide access to your own map of shared databases. This
     * is a method so that your subclass doesn't collide with others!
     */
    protected abstract PerProfileDatabases<? extends SQLiteOpenHelper> getDatabases();

    /*
     * Fetches a readable database based on the profile indicated in the
     * passed URI. If the URI does not contain a profile param, the default profile
     * is used.
     *
     * @param uri content URI optionally indicating the profile of the user
     * @return    instance of a readable SQLiteDatabase
     */
    @Override
    protected SQLiteDatabase getReadableDatabase(Uri uri) {
        String profile = null;
        if (uri != null) {
            profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
        }

        return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase();
    }

    /*
     * Fetches a writable database based on the profile indicated in the
     * passed URI. If the URI does not contain a profile param, the default profile
     * is used
     *
     * @param uri content URI optionally indicating the profile of the user
     * @return    instance of a writable SQLiteDatabase
     */
    @Override
    protected SQLiteDatabase getWritableDatabase(Uri uri) {
        String profile = null;
        if (uri != null) {
            profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
        }

        return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase();
    }

    protected SQLiteDatabase getWritableDatabaseForProfile(String profile, boolean isTest) {
        return getDatabases().getDatabaseHelperForProfile(profile, isTest).getWritableDatabase();
    }

    /**
     * This method should ONLY be used for testing purposes.
     *
     * @param uri content URI optionally indicating the profile of the user
     * @return    instance of a writable SQLiteDatabase
     */
    @Override
    @RobocopTarget
    public SQLiteDatabase getWritableDatabaseForTesting(Uri uri) {
        return getWritableDatabase(uri);
    }
}