|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.db; |
|
6 |
|
7 import java.lang.IllegalArgumentException; |
|
8 import java.util.HashMap; |
|
9 |
|
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; |
|
17 |
|
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; |
|
23 |
|
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"; |
|
27 |
|
28 private static final int FORM_HISTORY = 100; |
|
29 private static final int DELETED_FORM_HISTORY = 101; |
|
30 |
|
31 private static final UriMatcher URI_MATCHER; |
|
32 |
|
33 |
|
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"; |
|
38 |
|
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 + " = ?"; |
|
41 |
|
42 private static final String LOG_TAG = "FormHistoryProvider"; |
|
43 |
|
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 } |
|
49 |
|
50 public FormHistoryProvider() { |
|
51 super(LOG_TAG); |
|
52 } |
|
53 |
|
54 |
|
55 @Override |
|
56 public String getType(Uri uri) { |
|
57 final int match = URI_MATCHER.match(uri); |
|
58 |
|
59 switch (match) { |
|
60 case FORM_HISTORY: |
|
61 return FormHistory.CONTENT_TYPE; |
|
62 |
|
63 case DELETED_FORM_HISTORY: |
|
64 return DeletedFormHistory.CONTENT_TYPE; |
|
65 |
|
66 default: |
|
67 throw new UnsupportedOperationException("Unknown type " + uri); |
|
68 } |
|
69 } |
|
70 |
|
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; |
|
79 |
|
80 case FORM_HISTORY: |
|
81 table = TABLE_FORM_HISTORY; |
|
82 break; |
|
83 |
|
84 default: |
|
85 throw new UnsupportedOperationException("Unknown table " + uri); |
|
86 } |
|
87 return table; |
|
88 } |
|
89 |
|
90 @Override |
|
91 public String getSortOrder(Uri uri, String aRequested) { |
|
92 if (!TextUtils.isEmpty(aRequested)) { |
|
93 return aRequested; |
|
94 } |
|
95 |
|
96 return null; |
|
97 } |
|
98 |
|
99 @Override |
|
100 public void setupDefaults(Uri uri, ContentValues values) { |
|
101 int match = URI_MATCHER.match(uri); |
|
102 long now = System.currentTimeMillis(); |
|
103 |
|
104 switch (match) { |
|
105 case DELETED_FORM_HISTORY: |
|
106 values.put(DeletedFormHistory.TIME_DELETED, now); |
|
107 |
|
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; |
|
113 |
|
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; |
|
121 |
|
122 default: |
|
123 throw new UnsupportedOperationException("Unknown insert URI " + uri); |
|
124 } |
|
125 } |
|
126 |
|
127 @Override |
|
128 public void initGecko() { |
|
129 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null)); |
|
130 } |
|
131 |
|
132 @Override |
|
133 public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) { |
|
134 if (!values.containsKey(FormHistory.GUID)) { |
|
135 return; |
|
136 } |
|
137 |
|
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 } |
|
146 |
|
147 @Override |
|
148 public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { } |
|
149 |
|
150 @Override |
|
151 public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { } |
|
152 |
|
153 @Override |
|
154 protected String getDBName(){ |
|
155 return DB_FILENAME; |
|
156 } |
|
157 |
|
158 @Override |
|
159 protected String getTelemetryPrefix() { |
|
160 return TELEMETRY_TAG; |
|
161 } |
|
162 |
|
163 @Override |
|
164 protected int getDBVersion(){ |
|
165 return DB_VERSION; |
|
166 } |
|
167 } |