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