mobile/android/base/db/FormHistoryProvider.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/db/FormHistoryProvider.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,167 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.db;
     1.9 +
    1.10 +import java.lang.IllegalArgumentException;
    1.11 +import java.util.HashMap;
    1.12 +
    1.13 +import org.mozilla.gecko.GeckoAppShell;
    1.14 +import org.mozilla.gecko.GeckoEvent;
    1.15 +import org.mozilla.gecko.db.BrowserContract.FormHistory;
    1.16 +import org.mozilla.gecko.db.BrowserContract.DeletedFormHistory;
    1.17 +import org.mozilla.gecko.db.BrowserContract;
    1.18 +import org.mozilla.gecko.sqlite.SQLiteBridge;
    1.19 +import org.mozilla.gecko.sync.Utils;
    1.20 +
    1.21 +import android.content.ContentValues;
    1.22 +import android.content.UriMatcher;
    1.23 +import android.database.Cursor;
    1.24 +import android.net.Uri;
    1.25 +import android.text.TextUtils;
    1.26 +
    1.27 +public class FormHistoryProvider extends SQLiteBridgeContentProvider {
    1.28 +    static final String TABLE_FORM_HISTORY = "moz_formhistory";
    1.29 +    static final String TABLE_DELETED_FORM_HISTORY = "moz_deleted_formhistory";
    1.30 +
    1.31 +    private static final int FORM_HISTORY = 100;
    1.32 +    private static final int DELETED_FORM_HISTORY = 101;
    1.33 +
    1.34 +    private static final UriMatcher URI_MATCHER;
    1.35 +
    1.36 +
    1.37 +    // This should be kept in sync with the db version in toolkit/components/satchel/nsFormHistory.js
    1.38 +    private static int DB_VERSION = 4;
    1.39 +    private static String DB_FILENAME = "formhistory.sqlite";
    1.40 +    private static final String TELEMETRY_TAG = "SQLITEBRIDGE_PROVIDER_FORMS";
    1.41 +
    1.42 +    private static final String WHERE_GUID_IS_NULL = BrowserContract.DeletedFormHistory.GUID + " IS NULL";
    1.43 +    private static final String WHERE_GUID_IS_VALUE = BrowserContract.DeletedFormHistory.GUID + " = ?";
    1.44 +
    1.45 +    private static final String LOG_TAG = "FormHistoryProvider";
    1.46 +
    1.47 +    static {
    1.48 +        URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    1.49 +        URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "formhistory", FORM_HISTORY);
    1.50 +        URI_MATCHER.addURI(BrowserContract.FORM_HISTORY_AUTHORITY, "deleted-formhistory", DELETED_FORM_HISTORY);
    1.51 +    }
    1.52 +
    1.53 +    public FormHistoryProvider() {
    1.54 +        super(LOG_TAG);
    1.55 +    }
    1.56 +
    1.57 +
    1.58 +    @Override
    1.59 +    public String getType(Uri uri) {
    1.60 +        final int match = URI_MATCHER.match(uri);
    1.61 +
    1.62 +        switch (match) {
    1.63 +            case FORM_HISTORY:
    1.64 +                return FormHistory.CONTENT_TYPE;
    1.65 +
    1.66 +            case DELETED_FORM_HISTORY:
    1.67 +                return DeletedFormHistory.CONTENT_TYPE;
    1.68 +
    1.69 +            default:
    1.70 +                throw new UnsupportedOperationException("Unknown type " + uri);
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    @Override
    1.75 +    public String getTable(Uri uri) {
    1.76 +        String table = null;
    1.77 +        final int match = URI_MATCHER.match(uri);
    1.78 +        switch (match) {
    1.79 +            case DELETED_FORM_HISTORY:
    1.80 +                table = TABLE_DELETED_FORM_HISTORY;
    1.81 +                break;
    1.82 +
    1.83 +            case FORM_HISTORY:
    1.84 +                table = TABLE_FORM_HISTORY;
    1.85 +                break;
    1.86 +
    1.87 +            default:
    1.88 +                throw new UnsupportedOperationException("Unknown table " + uri);
    1.89 +        }
    1.90 +        return table;
    1.91 +    }
    1.92 +
    1.93 +    @Override
    1.94 +    public String getSortOrder(Uri uri, String aRequested) {
    1.95 +        if (!TextUtils.isEmpty(aRequested)) {
    1.96 +            return aRequested;
    1.97 +        }
    1.98 +
    1.99 +        return null;
   1.100 +    }
   1.101 +
   1.102 +    @Override
   1.103 +    public void setupDefaults(Uri uri, ContentValues values) {
   1.104 +        int match = URI_MATCHER.match(uri);
   1.105 +        long now = System.currentTimeMillis();
   1.106 +
   1.107 +        switch (match) {
   1.108 +            case DELETED_FORM_HISTORY:
   1.109 +                values.put(DeletedFormHistory.TIME_DELETED, now);
   1.110 +
   1.111 +                // Deleted entries must contain a guid
   1.112 +                if (!values.containsKey(FormHistory.GUID)) {
   1.113 +                    throw new IllegalArgumentException("Must provide a GUID for a deleted form history");
   1.114 +                }
   1.115 +                break;
   1.116 +
   1.117 +            case FORM_HISTORY:
   1.118 +                // Generate GUID for new entry. Don't override specified GUIDs.
   1.119 +                if (!values.containsKey(FormHistory.GUID)) {
   1.120 +                    String guid = Utils.generateGuid();
   1.121 +                    values.put(FormHistory.GUID, guid);
   1.122 +                }
   1.123 +                break;
   1.124 +
   1.125 +            default:
   1.126 +                throw new UnsupportedOperationException("Unknown insert URI " + uri);
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +    @Override
   1.131 +    public void initGecko() {
   1.132 +        GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormHistory:Init", null));
   1.133 +    }
   1.134 +
   1.135 +    @Override
   1.136 +    public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) {
   1.137 +        if (!values.containsKey(FormHistory.GUID)) {
   1.138 +            return;
   1.139 +        }
   1.140 +
   1.141 +        String guid = values.getAsString(FormHistory.GUID);
   1.142 +        if (guid == null) {
   1.143 +            db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_NULL, null);
   1.144 +            return;
   1.145 +        }
   1.146 +        String[] args = new String[] { guid };
   1.147 +        db.delete(TABLE_DELETED_FORM_HISTORY, WHERE_GUID_IS_VALUE, args);
   1.148 +     }
   1.149 +
   1.150 +    @Override
   1.151 +    public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { }
   1.152 +
   1.153 +    @Override
   1.154 +    public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { }
   1.155 +
   1.156 +    @Override
   1.157 +    protected String getDBName(){
   1.158 +        return DB_FILENAME;
   1.159 +    }
   1.160 +
   1.161 +    @Override
   1.162 +    protected String getTelemetryPrefix() {
   1.163 +        return TELEMETRY_TAG;
   1.164 +    }
   1.165 +
   1.166 +    @Override
   1.167 +    protected int getDBVersion(){
   1.168 +        return DB_VERSION;
   1.169 +    }
   1.170 +}

mercurial