Bug 941357 - (Part 2) Update BrowserProvider to use PerProfileDatases. r=rnewman

This commit is contained in:
Margaret Leibovic 2013-12-09 09:06:02 -08:00
parent 4c1903ffde
commit c70ac95274
2 changed files with 28 additions and 63 deletions

View File

@ -19,6 +19,7 @@ import org.mozilla.gecko.db.BrowserContract.Schema;
import org.mozilla.gecko.db.BrowserContract.SyncColumns;
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
import org.mozilla.gecko.db.BrowserContract.URLColumns;
import org.mozilla.gecko.db.PerProfileDatabases.DatabaseHelperFactory;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.mozglue.RobocopTarget;
import org.mozilla.gecko.sync.Utils;
@ -67,6 +68,8 @@ public class BrowserProvider extends ContentProvider {
private static final String LOGTAG = "GeckoBrowserProvider";
private Context mContext;
private PerProfileDatabases<BrowserDatabaseHelper> mDatabases;
static final String DATABASE_NAME = "browser.db";
static final int DATABASE_VERSION = 17;
@ -313,8 +316,6 @@ public class BrowserProvider extends ContentProvider {
SEARCH_SUGGEST_PROJECTION_MAP = Collections.unmodifiableMap(map);
}
private HashMap<String, DatabaseHelper> mDatabasePerProfile;
private interface BookmarkMigrator {
public void updateForNewTable(ContentValues bookmark);
}
@ -364,8 +365,8 @@ public class BrowserProvider extends ContentProvider {
}
}
final class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String databasePath) {
final class BrowserDatabaseHelper extends SQLiteOpenHelper {
public BrowserDatabaseHelper(Context context, String databasePath) {
super(context, databasePath, null, DATABASE_VERSION);
}
@ -1953,63 +1954,9 @@ public class BrowserProvider extends ContentProvider {
}
}
private DatabaseHelper getDatabaseHelperForProfile(String profile, boolean isTest) {
// Each profile has a separate browser.db database. The target
// profile is provided using a URI query argument in each request
// to our content provider.
// Always fallback to default profile if none has been provided.
if (TextUtils.isEmpty(profile)) {
profile = GeckoProfile.get(mContext).getName();
}
DatabaseHelper dbHelper;
synchronized (this) {
dbHelper = mDatabasePerProfile.get(profile);
if (dbHelper != null) {
return dbHelper;
}
String databasePath = getDatabasePath(profile, isTest);
// Before bug 768532, the database was located outside if the
// profile on Android 2.2. Make sure it is moved inside the profile
// directory.
if (Build.VERSION.SDK_INT == 8) {
File oldPath = mContext.getDatabasePath("browser-" + profile + ".db");
if (oldPath.exists()) {
oldPath.renameTo(new File(databasePath));
}
}
dbHelper = new DatabaseHelper(getContext(), databasePath);
mDatabasePerProfile.put(profile, dbHelper);
DBUtils.ensureDatabaseIsNotLocked(dbHelper, databasePath);
}
debug("Created database helper for profile: " + profile);
return dbHelper;
}
@RobocopTarget
public String getDatabasePath(String profile, boolean isTest) {
trace("Getting database path for profile: " + profile);
if (isTest) {
return DATABASE_NAME;
}
File profileDir = GeckoProfile.get(mContext, profile).getDir();
if (profileDir == null) {
debug("Couldn't find directory for profile: " + profile);
return null;
}
String databasePath = new File(profileDir, DATABASE_NAME).getAbsolutePath();
debug("Successfully created database path for profile: " + databasePath);
return databasePath;
return mDatabases.getDatabasePathForProfile(profile, isTest);
}
private SQLiteDatabase getReadableDatabase(Uri uri) {
@ -2020,7 +1967,7 @@ public class BrowserProvider extends ContentProvider {
if (uri != null)
profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
return getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase();
return mDatabases.getDatabaseHelperForProfile(profile, isTest(uri)).getReadableDatabase();
}
private SQLiteDatabase getWritableDatabase(Uri uri) {
@ -2031,7 +1978,7 @@ public class BrowserProvider extends ContentProvider {
if (uri != null)
profile = uri.getQueryParameter(BrowserContract.PARAM_PROFILE);
return getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase();
return mDatabases.getDatabaseHelperForProfile(profile, isTest(uri)).getWritableDatabase();
}
private void cleanupSomeDeletedRecords(Uri fromUri, Uri targetUri, String tableName) {
@ -2178,7 +2125,13 @@ public class BrowserProvider extends ContentProvider {
synchronized (this) {
mContext = getContext();
mDatabasePerProfile = new HashMap<String, DatabaseHelper>();
mDatabases = new PerProfileDatabases<BrowserDatabaseHelper>(
getContext(), DATABASE_NAME, new DatabaseHelperFactory<BrowserDatabaseHelper>() {
@Override
public BrowserDatabaseHelper makeDatabaseHelper(Context context, String databasePath) {
return new BrowserDatabaseHelper(context, databasePath);
}
});
}
return true;

View File

@ -35,6 +35,14 @@ public class PerProfileDatabases<T extends SQLiteOpenHelper> {
}
public String getDatabasePathForProfile(String profile) {
return getDatabasePathForProfile(profile, false);
}
public String getDatabasePathForProfile(String profile, boolean isTest) {
if (isTest) {
return mDatabaseName;
}
final File profileDir = GeckoProfile.get(mContext, profile).getDir();
if (profileDir == null) {
return null;
@ -44,6 +52,10 @@ public class PerProfileDatabases<T extends SQLiteOpenHelper> {
}
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 (TextUtils.isEmpty(profile)) {
profile = GeckoProfile.get(mContext).getName();
@ -54,7 +66,7 @@ public class PerProfileDatabases<T extends SQLiteOpenHelper> {
return mStorages.get(profile);
}
final String databasePath = getDatabasePathForProfile(profile);
final String databasePath = getDatabasePathForProfile(profile, isTest);
if (databasePath == null) {
throw new IllegalStateException("Database path is null for profile: " + profile);
}