summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/GeckoProfilesProvider.java
blob: 8a9c461c5420cc8546404dd52aef1d60ebe9cae7 (plain)
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
/* 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;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;

import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException;
import org.mozilla.gecko.db.BrowserContract;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;

/**
 * This is not a per-profile provider. This provider allows read-only,
 * restricted access to certain attributes of Fennec profiles.
 */
public class GeckoProfilesProvider extends ContentProvider {
    private static final String LOG_TAG = "GeckoProfilesProvider";

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int PROFILES = 100;
    private static final int PROFILES_NAME = 101;
    private static final int PROFILES_DEFAULT = 200;

    private static final String[] DEFAULT_ARGS = {
        BrowserContract.Profiles.NAME,
        BrowserContract.Profiles.PATH,
    };

    static {
        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles", PROFILES);
        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles/*", PROFILES_NAME);
        URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "default", PROFILES_DEFAULT);
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public boolean onCreate() {
        // Successfully loaded.
        return true;
    }

    private String[] profileValues(final String name, final String path, int len, int nameIndex, int pathIndex) {
        final String[] values = new String[len];
        if (nameIndex >= 0) {
            values[nameIndex] = name;
        }
        if (pathIndex >= 0) {
            values[pathIndex] = path;
        }
        return values;
    }

    protected void addRowForProfile(final MatrixCursor cursor, final int len, final int nameIndex, final int pathIndex, final String name, final String path) {
        if (path == null || name == null) {
            return;
        }

        cursor.addRow(profileValues(name, path, len, nameIndex, pathIndex));
    }

    protected Cursor getCursorForProfiles(final String[] args, Map<String, String> profiles) {
        // Compute the projection.
        int nameIndex = -1;
        int pathIndex = -1;
        for (int i = 0; i < args.length; ++i) {
            if (BrowserContract.Profiles.NAME.equals(args[i])) {
                nameIndex = i;
            } else if (BrowserContract.Profiles.PATH.equals(args[i])) {
                pathIndex = i;
            }
        }

        final MatrixCursor cursor = new MatrixCursor(args);
        for (Entry<String, String> entry : profiles.entrySet()) {
            addRowForProfile(cursor, args.length, nameIndex, pathIndex, entry.getKey(), entry.getValue());
        }
        return cursor;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        final String[] args = (projection == null) ? DEFAULT_ARGS : projection;

        final File mozillaDir;
        try {
            mozillaDir = GeckoProfileDirectories.getMozillaDirectory(getContext());
        } catch (NoMozillaDirectoryException e) {
            Log.d(LOG_TAG, "No Mozilla directory; cannot query for profiles. Assuming there are none.");
            return new MatrixCursor(projection);
        }

        final Map<String, String> matchingProfiles;

        final int match = URI_MATCHER.match(uri);
        switch (match) {
        case PROFILES:
            // Return all profiles.
            matchingProfiles = GeckoProfileDirectories.getAllProfiles(mozillaDir);
            break;
        case PROFILES_NAME:
            // Return data about the specified profile.
            final String name = uri.getLastPathSegment();
            matchingProfiles = GeckoProfileDirectories.getProfilesNamed(mozillaDir,
                                                                       name);
            break;
        case PROFILES_DEFAULT:
            matchingProfiles = GeckoProfileDirectories.getDefaultProfile(mozillaDir);
            break;
        default:
            throw new UnsupportedOperationException("Unknown query URI " + uri);
        }

        return getCursorForProfiles(args, matchingProfiles);
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        throw new IllegalStateException("Inserts not supported.");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        throw new IllegalStateException("Deletes not supported.");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        throw new IllegalStateException("Updates not supported.");
    }

}