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.db.BrowserContract.CommonColumns; michael@0: import org.mozilla.gecko.db.BrowserContract.SyncColumns; michael@0: import org.mozilla.gecko.db.PerProfileDatabases.DatabaseHelperFactory; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.database.sqlite.SQLiteDatabase; michael@0: import android.net.Uri; michael@0: import android.util.Log; michael@0: michael@0: /** michael@0: * A ContentProvider subclass that provides per-profile browser.db access michael@0: * that can be safely shared between multiple providers. michael@0: * michael@0: * If multiple ContentProvider classes wish to share a database, it's michael@0: * vitally important that they use the same SQLiteOpenHelpers for access. michael@0: * michael@0: * Failure to do so can cause accidental concurrent writes, with the result michael@0: * being unexpected SQLITE_BUSY errors. michael@0: * michael@0: * This class provides a static {@link PerProfileDatabases} instance, lazily michael@0: * initialized within {@link SharedBrowserDatabaseProvider#onCreate()}. michael@0: */ michael@0: public abstract class SharedBrowserDatabaseProvider extends AbstractPerProfileDatabaseProvider { michael@0: private static final String LOGTAG = SharedBrowserDatabaseProvider.class.getSimpleName(); michael@0: michael@0: private static PerProfileDatabases databases; michael@0: michael@0: @Override michael@0: protected PerProfileDatabases getDatabases() { michael@0: return databases; michael@0: } michael@0: michael@0: @Override michael@0: public boolean onCreate() { michael@0: // If necessary, do the shared DB work. michael@0: synchronized (SharedBrowserDatabaseProvider.class) { michael@0: if (databases != null) { michael@0: return true; michael@0: } michael@0: michael@0: final DatabaseHelperFactory helperFactory = new DatabaseHelperFactory() { michael@0: @Override michael@0: public BrowserDatabaseHelper makeDatabaseHelper(Context context, String databasePath) { michael@0: return new BrowserDatabaseHelper(context, databasePath); michael@0: } michael@0: }; michael@0: michael@0: databases = new PerProfileDatabases(getContext(), BrowserDatabaseHelper.DATABASE_NAME, helperFactory); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Clean up some deleted records from the specified table. michael@0: * michael@0: * If called in an existing transaction, it is the caller's responsibility michael@0: * to ensure that the transaction is already upgraded to a writer, because michael@0: * this method issues a read followed by a write, and thus is potentially michael@0: * vulnerable to an unhandled SQLITE_BUSY failure during the upgrade. michael@0: * michael@0: * If not called in an existing transaction, no new explicit transaction michael@0: * will be begun. michael@0: */ michael@0: protected void cleanUpSomeDeletedRecords(Uri fromUri, String tableName) { michael@0: Log.d(LOGTAG, "Cleaning up deleted records from " + tableName); michael@0: michael@0: // We clean up records marked as deleted that are older than a michael@0: // predefined max age. It's important not be too greedy here and michael@0: // remove only a few old deleted records at a time. michael@0: michael@0: // we cleanup records marked as deleted that are older than a michael@0: // predefined max age. It's important not be too greedy here and michael@0: // remove only a few old deleted records at a time. michael@0: michael@0: // Maximum age of deleted records to be cleaned up (20 days in ms) michael@0: final long MAX_AGE_OF_DELETED_RECORDS = 86400000 * 20; michael@0: michael@0: // Number of records marked as deleted to be removed michael@0: final long DELETED_RECORDS_PURGE_LIMIT = 5; michael@0: michael@0: // Android SQLite doesn't have LIMIT on DELETE. Instead, query for the michael@0: // IDs of matching rows, then delete them in one go. michael@0: final long now = System.currentTimeMillis(); michael@0: final String selection = SyncColumns.IS_DELETED + " = 1 AND " + michael@0: SyncColumns.DATE_MODIFIED + " <= " + michael@0: (now - MAX_AGE_OF_DELETED_RECORDS); michael@0: michael@0: final String profile = fromUri.getQueryParameter(BrowserContract.PARAM_PROFILE); michael@0: final SQLiteDatabase db = getWritableDatabaseForProfile(profile, isTest(fromUri)); michael@0: final String[] ids; michael@0: final String limit = Long.toString(DELETED_RECORDS_PURGE_LIMIT, 10); michael@0: final Cursor cursor = db.query(tableName, new String[] { CommonColumns._ID }, selection, null, null, null, null, limit); michael@0: try { michael@0: ids = new String[cursor.getCount()]; michael@0: int i = 0; michael@0: while (cursor.moveToNext()) { michael@0: ids[i++] = Long.toString(cursor.getLong(0), 10); michael@0: } michael@0: } finally { michael@0: cursor.close(); michael@0: } michael@0: michael@0: final String inClause = computeSQLInClause(ids.length, michael@0: CommonColumns._ID); michael@0: db.delete(tableName, inClause, ids); michael@0: } michael@0: }