1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/db/AbstractPerProfileDatabaseProvider.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 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 org.mozilla.gecko.mozglue.RobocopTarget; 1.11 + 1.12 +import android.database.sqlite.SQLiteDatabase; 1.13 +import android.database.sqlite.SQLiteOpenHelper; 1.14 +import android.net.Uri; 1.15 + 1.16 +/** 1.17 + * The base class for ContentProviders that wish to use a different DB 1.18 + * for each profile. 1.19 + * 1.20 + * This class has logic shared between ordinary per-profile CPs and 1.21 + * those that wish to share DB connections between CPs. 1.22 + */ 1.23 +public abstract class AbstractPerProfileDatabaseProvider extends AbstractTransactionalProvider { 1.24 + 1.25 + /** 1.26 + * Extend this to provide access to your own map of shared databases. This 1.27 + * is a method so that your subclass doesn't collide with others! 1.28 + */ 1.29 + protected abstract PerProfileDatabases<? extends SQLiteOpenHelper> getDatabases(); 1.30 + 1.31 + /* 1.32 + * Fetches a readable database based on the profile indicated in the 1.33 + * passed URI. If the URI does not contain a profile param, the default profile 1.34 + * is used. 1.35 + * 1.36 + * @param uri content URI optionally indicating the profile of the user 1.37 + * @return instance of a readable SQLiteDatabase 1.38 + */ 1.39 + @Override 1.40 + protected SQLiteDatabase getReadableDatabase(Uri uri) { 1.41 + String profile = null; 1.42 + if (uri != null) { 1.43 + profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); 1.44 + } 1.45 + 1.46 + return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase(); 1.47 + } 1.48 + 1.49 + /* 1.50 + * Fetches a writable database based on the profile indicated in the 1.51 + * passed URI. If the URI does not contain a profile param, the default profile 1.52 + * is used 1.53 + * 1.54 + * @param uri content URI optionally indicating the profile of the user 1.55 + * @return instance of a writable SQLiteDatabase 1.56 + */ 1.57 + @Override 1.58 + protected SQLiteDatabase getWritableDatabase(Uri uri) { 1.59 + String profile = null; 1.60 + if (uri != null) { 1.61 + profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE); 1.62 + } 1.63 + 1.64 + return getDatabases().getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase(); 1.65 + } 1.66 + 1.67 + protected SQLiteDatabase getWritableDatabaseForProfile(String profile, boolean isTest) { 1.68 + return getDatabases().getDatabaseHelperForProfile(profile, isTest).getWritableDatabase(); 1.69 + } 1.70 + 1.71 + /** 1.72 + * This method should ONLY be used for testing purposes. 1.73 + * 1.74 + * @param uri content URI optionally indicating the profile of the user 1.75 + * @return instance of a writable SQLiteDatabase 1.76 + */ 1.77 + @Override 1.78 + @RobocopTarget 1.79 + public SQLiteDatabase getWritableDatabaseForTesting(Uri uri) { 1.80 + return getWritableDatabase(uri); 1.81 + } 1.82 +}