michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.db; michael@0: michael@0: import org.mozilla.gecko.mozglue.RobocopTarget; michael@0: michael@0: import android.database.sqlite.SQLiteDatabase; michael@0: import android.database.sqlite.SQLiteOpenHelper; michael@0: import android.net.Uri; michael@0: michael@0: /** michael@0: * The base class for ContentProviders that wish to use a different DB michael@0: * for each profile. michael@0: * michael@0: * This class has logic shared between ordinary per-profile CPs and michael@0: * those that wish to share DB connections between CPs. michael@0: */ michael@0: public abstract class AbstractPerProfileDatabaseProvider extends AbstractTransactionalProvider { michael@0: michael@0: /** michael@0: * Extend this to provide access to your own map of shared databases. This michael@0: * is a method so that your subclass doesn't collide with others! michael@0: */ michael@0: protected abstract PerProfileDatabases getDatabases(); michael@0: michael@0: /* michael@0: * Fetches a readable database based on the profile indicated in the michael@0: * passed URI. If the URI does not contain a profile param, the default profile michael@0: * is used. michael@0: * michael@0: * @param uri content URI optionally indicating the profile of the user michael@0: * @return instance of a readable SQLiteDatabase michael@0: */ michael@0: @Override michael@0: protected SQLiteDatabase getReadableDatabase(Uri uri) { michael@0: String profile = null; michael@0: if (uri != null) { michael@0: profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); michael@0: } michael@0: michael@0: return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase(); michael@0: } michael@0: michael@0: /* michael@0: * Fetches a writable database based on the profile indicated in the michael@0: * passed URI. If the URI does not contain a profile param, the default profile michael@0: * is used michael@0: * michael@0: * @param uri content URI optionally indicating the profile of the user michael@0: * @return instance of a writable SQLiteDatabase michael@0: */ michael@0: @Override michael@0: protected SQLiteDatabase getWritableDatabase(Uri uri) { michael@0: String profile = null; michael@0: if (uri != null) { michael@0: profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); michael@0: } michael@0: michael@0: return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase(); michael@0: } michael@0: michael@0: protected SQLiteDatabase getWritableDatabaseForProfile(String profile, boolean isTest) { michael@0: return getDatabases().getDatabaseHelperForProfile(profile, isTest).getWritableDatabase(); michael@0: } michael@0: michael@0: /** michael@0: * This method should ONLY be used for testing purposes. michael@0: * michael@0: * @param uri content URI optionally indicating the profile of the user michael@0: * @return instance of a writable SQLiteDatabase michael@0: */ michael@0: @Override michael@0: @RobocopTarget michael@0: public SQLiteDatabase getWritableDatabaseForTesting(Uri uri) { michael@0: return getWritableDatabase(uri); michael@0: } michael@0: }