summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/db/PerProfileDatabases.java
blob: 288d9cae7dc3e4efd4a1a05187588e0e352775b7 (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
/* 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 java.io.File;
import java.util.HashMap;

import org.mozilla.gecko.GeckoProfile;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

/**
 * Manages a set of per-profile database storage helpers.
 */
public class PerProfileDatabases<T extends SQLiteOpenHelper> {

    private final HashMap<String, T> mStorages = new HashMap<String, T>();

    private final Context mContext;
    private final String mDatabaseName;
    private final DatabaseHelperFactory<T> mHelperFactory;

    // Only used during tests.
    public void shutdown() {
        synchronized (this) {
            for (T t : mStorages.values()) {
                try {
                    t.close();
                } catch (Throwable e) {
                    // Never mind.
                }
            }
        }
    }

    public interface DatabaseHelperFactory<T> {
        public T makeDatabaseHelper(Context context, String databasePath);
    }

    public PerProfileDatabases(final Context context, final String databaseName, final DatabaseHelperFactory<T> helperFactory) {
        mContext = context;
        mDatabaseName = databaseName;
        mHelperFactory = helperFactory;
    }

    public String getDatabasePathForProfile(String profile) {
        final File profileDir = GeckoProfile.get(mContext, profile).getDir();
        if (profileDir == null) {
            return null;
        }

        return new File(profileDir, mDatabaseName).getAbsolutePath();
    }

    public T getDatabaseHelperForProfile(String profile) {
        return getDatabaseHelperForProfile(profile, false);
    }

    public T getDatabaseHelperForProfile(String profile, boolean isTest) {
        // Always fall back to default profile if none has been provided.
        if (profile == null) {
            profile = GeckoProfile.get(mContext).getName();
        }

        synchronized (this) {
            if (mStorages.containsKey(profile)) {
                return mStorages.get(profile);
            }

            final String databasePath = isTest ? mDatabaseName : getDatabasePathForProfile(profile);
            if (databasePath == null) {
                throw new IllegalStateException("Database path is null for profile: " + profile);
            }

            final T helper = mHelperFactory.makeDatabaseHelper(mContext, databasePath);
            DBUtils.ensureDatabaseIsNotLocked(helper, databasePath);

            mStorages.put(profile, helper);
            return helper;
        }
    }

    public synchronized void shrinkMemory() {
        for (T t : mStorages.values()) {
            final SQLiteDatabase db = t.getWritableDatabase();
            db.execSQL("PRAGMA shrink_memory");
        }
    }
}