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