Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.db; |
michael@0 | 6 | |
michael@0 | 7 | import java.lang.IllegalArgumentException; |
michael@0 | 8 | import java.util.HashMap; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.GeckoAppShell; |
michael@0 | 11 | import org.mozilla.gecko.GeckoEvent; |
michael@0 | 12 | import org.mozilla.gecko.db.BrowserContract.FormHistory; |
michael@0 | 13 | import org.mozilla.gecko.db.BrowserContract.DeletedFormHistory; |
michael@0 | 14 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 15 | import org.mozilla.gecko.sqlite.SQLiteBridge; |
michael@0 | 16 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 17 | |
michael@0 | 18 | import android.content.ContentValues; |
michael@0 | 19 | import android.content.UriMatcher; |
michael@0 | 20 | import android.database.Cursor; |
michael@0 | 21 | import android.net.Uri; |
michael@0 | 22 | import android.text.TextUtils; |
michael@0 | 23 | |
michael@0 | 24 | public class FormHistoryProvider extends SQLiteBridgeContentProvider { |
michael@0 | 25 | static final String TABLE_FORM_HISTORY = "moz_formhistory"; |
michael@0 | 26 | static final String TABLE_DELETED_FORM_HISTORY = "moz_deleted_formhistory"; |
michael@0 | 27 | |
michael@0 | 28 | private static final int FORM_HISTORY = 100; |
michael@0 | 29 | private static final int DELETED_FORM_HISTORY = 101; |
michael@0 | 30 | |
michael@0 | 31 | private static final UriMatcher URI_MATCHER; |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | // This should be kept in sync with the db version in toolkit/components/satchel/nsFormHistory.js |
michael@0 | 35 | private static int DB_VERSION = 4; |
michael@0 | 36 | private static String DB_FILENAME = "formhistory.sqlite"; |
michael@0 | 37 | private static final String TELEMETRY_TAG = "SQLITEBRIDGE_PROVIDER_FORMS"; |
michael@0 | 38 | |
michael@0 | 39 | private static final String WHERE_GUID_IS_NULL = BrowserContract.DeletedFormHistory.GUID + " IS NULL"; |
michael@0 | 40 | private static final String WHERE_GUID_IS_VALUE = BrowserContract.DeletedFormHistory.GUID + " = ?"; |
michael@0 | 41 | |
michael@0 | 42 | private static final String LOG_TAG = "FormHistoryProvider"; |
michael@0 | 43 | |
michael@0 | 44 | static { |
michael@0 | 45 | URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
michael@0 | 46 | URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "formhistory", FORM_HISTORY); |
michael@0 | 47 | URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "deleted-formhistory", DELETED_FORM_HISTORY); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | public FormHistoryProvider() { |
michael@0 | 51 | super(LOG_TAG); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | @Override |
michael@0 | 56 | public String getType(Uri uri) { |
michael@0 | 57 | final int match = URI_MATCHER.match(uri); |
michael@0 | 58 | |
michael@0 | 59 | switch (match) { |
michael@0 | 60 | case FORM_HISTORY: |
michael@0 | 61 | return FormHistory.CONTENT_TYPE; |
michael@0 | 62 | |
michael@0 | 63 | case DELETED_FORM_HISTORY: |
michael@0 | 64 | return DeletedFormHistory.CONTENT_TYPE; |
michael@0 | 65 | |
michael@0 | 66 | default: |
michael@0 | 67 | throw new UnsupportedOperationException("Unknown type " + uri); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | @Override |
michael@0 | 72 | public String getTable(Uri uri) { |
michael@0 | 73 | String table = null; |
michael@0 | 74 | final int match = URI_MATCHER.match(uri); |
michael@0 | 75 | switch (match) { |
michael@0 | 76 | case DELETED_FORM_HISTORY: |
michael@0 | 77 | table = TABLE_DELETED_FORM_HISTORY; |
michael@0 | 78 | break; |
michael@0 | 79 | |
michael@0 | 80 | case FORM_HISTORY: |
michael@0 | 81 | table = TABLE_FORM_HISTORY; |
michael@0 | 82 | break; |
michael@0 | 83 | |
michael@0 | 84 | default: |
michael@0 | 85 | throw new UnsupportedOperationException("Unknown table " + uri); |
michael@0 | 86 | } |
michael@0 | 87 | return table; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @Override |
michael@0 | 91 | public String getSortOrder(Uri uri, String aRequested) { |
michael@0 | 92 | if (!TextUtils.isEmpty(aRequested)) { |
michael@0 | 93 | return aRequested; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | return null; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | @Override |
michael@0 | 100 | public void setupDefaults(Uri uri, ContentValues values) { |
michael@0 | 101 | int match = URI_MATCHER.match(uri); |
michael@0 | 102 | long now = System.currentTimeMillis(); |
michael@0 | 103 | |
michael@0 | 104 | switch (match) { |
michael@0 | 105 | case DELETED_FORM_HISTORY: |
michael@0 | 106 | values.put(DeletedFormHistory.TIME_DELETED, now); |
michael@0 | 107 | |
michael@0 | 108 | // Deleted entries must contain a guid |
michael@0 | 109 | if (!values.containsKey(FormHistory.GUID)) { |
michael@0 | 110 | throw new IllegalArgumentException("Must provide a GUID for a deleted form history"); |
michael@0 | 111 | } |
michael@0 | 112 | break; |
michael@0 | 113 | |
michael@0 | 114 | case FORM_HISTORY: |
michael@0 | 115 | // Generate GUID for new entry. Don't override specified GUIDs. |
michael@0 | 116 | if (!values.containsKey(FormHistory.GUID)) { |
michael@0 | 117 | String guid = Utils.generateGuid(); |
michael@0 | 118 | values.put(FormHistory.GUID, guid); |
michael@0 | 119 | } |
michael@0 | 120 | break; |
michael@0 | 121 | |
michael@0 | 122 | default: |
michael@0 | 123 | throw new UnsupportedOperationException("Unknown insert URI " + uri); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | @Override |
michael@0 | 128 | public void initGecko() { |
michael@0 | 129 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null)); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | @Override |
michael@0 | 133 | public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) { |
michael@0 | 134 | if (!values.containsKey(FormHistory.GUID)) { |
michael@0 | 135 | return; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | String guid = values.getAsString(FormHistory.GUID); |
michael@0 | 139 | if (guid == null) { |
michael@0 | 140 | db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_NULL, null); |
michael@0 | 141 | return; |
michael@0 | 142 | } |
michael@0 | 143 | String[] args = new String[] { guid }; |
michael@0 | 144 | db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_VALUE, args); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | @Override |
michael@0 | 148 | public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { } |
michael@0 | 149 | |
michael@0 | 150 | @Override |
michael@0 | 151 | public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { } |
michael@0 | 152 | |
michael@0 | 153 | @Override |
michael@0 | 154 | protected String getDBName(){ |
michael@0 | 155 | return DB_FILENAME; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | @Override |
michael@0 | 159 | protected String getTelemetryPrefix() { |
michael@0 | 160 | return TELEMETRY_TAG; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | @Override |
michael@0 | 164 | protected int getDBVersion(){ |
michael@0 | 165 | return DB_VERSION; |
michael@0 | 166 | } |
michael@0 | 167 | } |