mobile/android/base/db/PerProfileDatabases.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/db/PerProfileDatabases.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.db;
     1.9 +
    1.10 +import java.io.File;
    1.11 +import java.util.HashMap;
    1.12 +
    1.13 +import org.mozilla.gecko.GeckoProfile;
    1.14 +
    1.15 +import android.content.Context;
    1.16 +import android.database.sqlite.SQLiteOpenHelper;
    1.17 +import android.text.TextUtils;
    1.18 +
    1.19 +/**
    1.20 + * Manages a set of per-profile database storage helpers.
    1.21 + */
    1.22 +public class PerProfileDatabases<T extends SQLiteOpenHelper> {
    1.23 +
    1.24 +    private final HashMap<String, T> mStorages = new HashMap<String, T>();
    1.25 +
    1.26 +    private final Context mContext;
    1.27 +    private final String mDatabaseName;
    1.28 +    private final DatabaseHelperFactory<T> mHelperFactory;
    1.29 +
    1.30 +    public interface DatabaseHelperFactory<T> {
    1.31 +        public T makeDatabaseHelper(Context context, String databasePath);
    1.32 +    }
    1.33 +
    1.34 +    public PerProfileDatabases(final Context context, final String databaseName, final DatabaseHelperFactory<T> helperFactory) {
    1.35 +        mContext = context;
    1.36 +        mDatabaseName = databaseName;
    1.37 +        mHelperFactory = helperFactory;
    1.38 +    }
    1.39 +
    1.40 +    public String getDatabasePathForProfile(String profile) {
    1.41 +        final File profileDir = GeckoProfile.get(mContext, profile).getDir();
    1.42 +        if (profileDir == null) {
    1.43 +            return null;
    1.44 +        }
    1.45 +
    1.46 +        return new File(profileDir, mDatabaseName).getAbsolutePath();
    1.47 +    }
    1.48 +
    1.49 +    public T getDatabaseHelperForProfile(String profile) {
    1.50 +        return getDatabaseHelperForProfile(profile, false);
    1.51 +    }
    1.52 +
    1.53 +    public T getDatabaseHelperForProfile(String profile, boolean isTest) {
    1.54 +        // Always fall back to default profile if none has been provided.
    1.55 +        if (TextUtils.isEmpty(profile)) {
    1.56 +            profile = GeckoProfile.get(mContext).getName();
    1.57 +        }
    1.58 +
    1.59 +        synchronized (this) {
    1.60 +            if (mStorages.containsKey(profile)) {
    1.61 +                return mStorages.get(profile);
    1.62 +            }
    1.63 +
    1.64 +            final String databasePath = isTest ? mDatabaseName : getDatabasePathForProfile(profile);
    1.65 +            if (databasePath == null) {
    1.66 +                throw new IllegalStateException("Database path is null for profile: " + profile);
    1.67 +            }
    1.68 +
    1.69 +            final T helper = mHelperFactory.makeDatabaseHelper(mContext, databasePath);
    1.70 +            DBUtils.ensureDatabaseIsNotLocked(helper, databasePath);
    1.71 +
    1.72 +            mStorages.put(profile, helper);
    1.73 +            return helper;
    1.74 +        }
    1.75 +    }
    1.76 +}

mercurial