mobile/android/base/GeckoProfilesProvider.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/GeckoProfilesProvider.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     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
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import java.io.File;
    1.11 +import java.util.Map;
    1.12 +import java.util.Map.Entry;
    1.13 +
    1.14 +import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException;
    1.15 +import org.mozilla.gecko.db.BrowserContract;
    1.16 +
    1.17 +import android.content.ContentProvider;
    1.18 +import android.content.ContentValues;
    1.19 +import android.content.UriMatcher;
    1.20 +import android.database.Cursor;
    1.21 +import android.database.MatrixCursor;
    1.22 +import android.net.Uri;
    1.23 +import android.util.Log;
    1.24 +
    1.25 +/**
    1.26 + * This is not a per-profile provider. This provider allows read-only,
    1.27 + * restricted access to certain attributes of Fennec profiles.
    1.28 + */
    1.29 +public class GeckoProfilesProvider extends ContentProvider {
    1.30 +    private static final String LOG_TAG = "GeckoProfilesProvider";
    1.31 +
    1.32 +    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    1.33 +
    1.34 +    private static final int PROFILES = 100;
    1.35 +    private static final int PROFILES_NAME = 101;
    1.36 +    private static final int PROFILES_DEFAULT = 200;
    1.37 +
    1.38 +    private static final String[] DEFAULT_ARGS = {
    1.39 +        BrowserContract.Profiles.NAME,
    1.40 +        BrowserContract.Profiles.PATH,
    1.41 +    };
    1.42 +
    1.43 +    static {
    1.44 +        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles", PROFILES);
    1.45 +        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles/*", PROFILES_NAME);
    1.46 +        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "default", PROFILES_DEFAULT);
    1.47 +    }
    1.48 +
    1.49 +    @Override
    1.50 +    public String getType(Uri uri) {
    1.51 +        return null;
    1.52 +    }
    1.53 +
    1.54 +    @Override
    1.55 +    public boolean onCreate() {
    1.56 +        // Successfully loaded.
    1.57 +        return true;
    1.58 +    }
    1.59 +
    1.60 +    private String[] profileValues(final String name, final String path, int len, int nameIndex, int pathIndex) {
    1.61 +        final String[] values = new String[len];
    1.62 +        if (nameIndex >= 0) {
    1.63 +            values[nameIndex] = name;
    1.64 +        }
    1.65 +        if (pathIndex >= 0) {
    1.66 +            values[pathIndex] = path;
    1.67 +        }
    1.68 +        return values;
    1.69 +    }
    1.70 +
    1.71 +    protected void addRowForProfile(final MatrixCursor cursor, final int len, final int nameIndex, final int pathIndex, final String name, final String path) {
    1.72 +        if (path == null || name == null) {
    1.73 +            return;
    1.74 +        }
    1.75 +
    1.76 +        String[] values = new String[len];
    1.77 +        if (nameIndex >= 0) {
    1.78 +            values[nameIndex] = name;
    1.79 +        }
    1.80 +        if (pathIndex >= 0) {
    1.81 +            values[pathIndex] = path;
    1.82 +        }
    1.83 +        cursor.addRow(profileValues(name, path, len, nameIndex, pathIndex));
    1.84 +    }
    1.85 +
    1.86 +    protected Cursor getCursorForProfiles(final String[] args, Map<String, String> profiles) {
    1.87 +        // Compute the projection.
    1.88 +        int nameIndex = -1;
    1.89 +        int pathIndex = -1;
    1.90 +        for (int i = 0; i < args.length; ++i) {
    1.91 +            if (BrowserContract.Profiles.NAME.equals(args[i])) {
    1.92 +                nameIndex = i;
    1.93 +            } else if (BrowserContract.Profiles.PATH.equals(args[i])) {
    1.94 +                pathIndex = i;
    1.95 +            }
    1.96 +        }
    1.97 +
    1.98 +        final MatrixCursor cursor = new MatrixCursor(args);
    1.99 +        for (Entry<String, String> entry : profiles.entrySet()) {
   1.100 +            addRowForProfile(cursor, args.length, nameIndex, pathIndex, entry.getKey(), entry.getValue());
   1.101 +        }
   1.102 +        return cursor;
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public Cursor query(Uri uri, String[] projection, String selection,
   1.107 +                        String[] selectionArgs, String sortOrder) {
   1.108 +
   1.109 +        final String[] args = (projection == null) ? DEFAULT_ARGS : projection;
   1.110 +
   1.111 +        final File mozillaDir;
   1.112 +        try {
   1.113 +            mozillaDir = GeckoProfileDirectories.getMozillaDirectory(getContext());
   1.114 +        } catch (NoMozillaDirectoryException e) {
   1.115 +            Log.d(LOG_TAG, "No Mozilla directory; cannot query for profiles. Assuming there are none.");
   1.116 +            return new MatrixCursor(projection);
   1.117 +        }
   1.118 +
   1.119 +        final Map<String, String> matchingProfiles;
   1.120 +
   1.121 +        final int match = URI_MATCHER.match(uri);
   1.122 +        switch (match) {
   1.123 +        case PROFILES:
   1.124 +            // Return all profiles.
   1.125 +            matchingProfiles = GeckoProfileDirectories.getAllProfiles(mozillaDir);
   1.126 +            break;
   1.127 +        case PROFILES_NAME:
   1.128 +            // Return data about the specified profile.
   1.129 +            final String name = uri.getLastPathSegment();
   1.130 +            matchingProfiles = GeckoProfileDirectories.getProfilesNamed(mozillaDir,
   1.131 +                                                                       name);
   1.132 +            break;
   1.133 +        case PROFILES_DEFAULT:
   1.134 +            matchingProfiles = GeckoProfileDirectories.getDefaultProfile(mozillaDir);
   1.135 +            break;
   1.136 +        default:
   1.137 +            throw new UnsupportedOperationException("Unknown query URI " + uri);
   1.138 +        }
   1.139 +
   1.140 +        return getCursorForProfiles(args, matchingProfiles);
   1.141 +    }
   1.142 +
   1.143 +    @Override
   1.144 +    public Uri insert(Uri uri, ContentValues values) {
   1.145 +        throw new IllegalStateException("Inserts not supported.");
   1.146 +    }
   1.147 +
   1.148 +    @Override
   1.149 +    public int delete(Uri uri, String selection, String[] selectionArgs) {
   1.150 +        throw new IllegalStateException("Deletes not supported.");
   1.151 +    }
   1.152 +
   1.153 +    @Override
   1.154 +    public int update(Uri uri, ContentValues values, String selection,
   1.155 +                      String[] selectionArgs) {
   1.156 +        throw new IllegalStateException("Updates not supported.");
   1.157 +    }
   1.158 +
   1.159 +}

mercurial