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 file, michael@0: * 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 java.lang.IllegalArgumentException; michael@0: import java.util.HashMap; michael@0: michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.GeckoEvent; michael@0: import org.mozilla.gecko.db.BrowserContract.FormHistory; michael@0: import org.mozilla.gecko.db.BrowserContract.DeletedFormHistory; michael@0: import org.mozilla.gecko.db.BrowserContract; michael@0: import org.mozilla.gecko.sqlite.SQLiteBridge; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: import android.content.ContentValues; michael@0: import android.content.UriMatcher; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: import android.text.TextUtils; michael@0: michael@0: public class FormHistoryProvider extends SQLiteBridgeContentProvider { michael@0: static final String TABLE_FORM_HISTORY = "moz_formhistory"; michael@0: static final String TABLE_DELETED_FORM_HISTORY = "moz_deleted_formhistory"; michael@0: michael@0: private static final int FORM_HISTORY = 100; michael@0: private static final int DELETED_FORM_HISTORY = 101; michael@0: michael@0: private static final UriMatcher URI_MATCHER; michael@0: michael@0: michael@0: // This should be kept in sync with the db version in toolkit/components/satchel/nsFormHistory.js michael@0: private static int DB_VERSION = 4; michael@0: private static String DB_FILENAME = "formhistory.sqlite"; michael@0: private static final String TELEMETRY_TAG = "SQLITEBRIDGE_PROVIDER_FORMS"; michael@0: michael@0: private static final String WHERE_GUID_IS_NULL = BrowserContract.DeletedFormHistory.GUID + " IS NULL"; michael@0: private static final String WHERE_GUID_IS_VALUE = BrowserContract.DeletedFormHistory.GUID + " = ?"; michael@0: michael@0: private static final String LOG_TAG = "FormHistoryProvider"; michael@0: michael@0: static { michael@0: URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); michael@0: URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "formhistory", FORM_HISTORY); michael@0: URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "deleted-formhistory", DELETED_FORM_HISTORY); michael@0: } michael@0: michael@0: public FormHistoryProvider() { michael@0: super(LOG_TAG); michael@0: } michael@0: michael@0: michael@0: @Override michael@0: public String getType(Uri uri) { michael@0: final int match = URI_MATCHER.match(uri); michael@0: michael@0: switch (match) { michael@0: case FORM_HISTORY: michael@0: return FormHistory.CONTENT_TYPE; michael@0: michael@0: case DELETED_FORM_HISTORY: michael@0: return DeletedFormHistory.CONTENT_TYPE; michael@0: michael@0: default: michael@0: throw new UnsupportedOperationException("Unknown type " + uri); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public String getTable(Uri uri) { michael@0: String table = null; michael@0: final int match = URI_MATCHER.match(uri); michael@0: switch (match) { michael@0: case DELETED_FORM_HISTORY: michael@0: table = TABLE_DELETED_FORM_HISTORY; michael@0: break; michael@0: michael@0: case FORM_HISTORY: michael@0: table = TABLE_FORM_HISTORY; michael@0: break; michael@0: michael@0: default: michael@0: throw new UnsupportedOperationException("Unknown table " + uri); michael@0: } michael@0: return table; michael@0: } michael@0: michael@0: @Override michael@0: public String getSortOrder(Uri uri, String aRequested) { michael@0: if (!TextUtils.isEmpty(aRequested)) { michael@0: return aRequested; michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: @Override michael@0: public void setupDefaults(Uri uri, ContentValues values) { michael@0: int match = URI_MATCHER.match(uri); michael@0: long now = System.currentTimeMillis(); michael@0: michael@0: switch (match) { michael@0: case DELETED_FORM_HISTORY: michael@0: values.put(DeletedFormHistory.TIME_DELETED, now); michael@0: michael@0: // Deleted entries must contain a guid michael@0: if (!values.containsKey(FormHistory.GUID)) { michael@0: throw new IllegalArgumentException("Must provide a GUID for a deleted form history"); michael@0: } michael@0: break; michael@0: michael@0: case FORM_HISTORY: michael@0: // Generate GUID for new entry. Don't override specified GUIDs. michael@0: if (!values.containsKey(FormHistory.GUID)) { michael@0: String guid = Utils.generateGuid(); michael@0: values.put(FormHistory.GUID, guid); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: throw new UnsupportedOperationException("Unknown insert URI " + uri); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void initGecko() { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null)); michael@0: } michael@0: michael@0: @Override michael@0: public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) { michael@0: if (!values.containsKey(FormHistory.GUID)) { michael@0: return; michael@0: } michael@0: michael@0: String guid = values.getAsString(FormHistory.GUID); michael@0: if (guid == null) { michael@0: db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_NULL, null); michael@0: return; michael@0: } michael@0: String[] args = new String[] { guid }; michael@0: db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_VALUE, args); michael@0: } michael@0: michael@0: @Override michael@0: public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { } michael@0: michael@0: @Override michael@0: public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { } michael@0: michael@0: @Override michael@0: protected String getDBName(){ michael@0: return DB_FILENAME; michael@0: } michael@0: michael@0: @Override michael@0: protected String getTelemetryPrefix() { michael@0: return TELEMETRY_TAG; michael@0: } michael@0: michael@0: @Override michael@0: protected int getDBVersion(){ michael@0: return DB_VERSION; michael@0: } michael@0: }