Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.File; |
michael@0 | 8 | import java.util.Map; |
michael@0 | 9 | import java.util.Map.Entry; |
michael@0 | 10 | |
michael@0 | 11 | import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException; |
michael@0 | 12 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 13 | |
michael@0 | 14 | import android.content.ContentProvider; |
michael@0 | 15 | import android.content.ContentValues; |
michael@0 | 16 | import android.content.UriMatcher; |
michael@0 | 17 | import android.database.Cursor; |
michael@0 | 18 | import android.database.MatrixCursor; |
michael@0 | 19 | import android.net.Uri; |
michael@0 | 20 | import android.util.Log; |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * This is not a per-profile provider. This provider allows read-only, |
michael@0 | 24 | * restricted access to certain attributes of Fennec profiles. |
michael@0 | 25 | */ |
michael@0 | 26 | public class GeckoProfilesProvider extends ContentProvider { |
michael@0 | 27 | private static final String LOG_TAG = "GeckoProfilesProvider"; |
michael@0 | 28 | |
michael@0 | 29 | private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
michael@0 | 30 | |
michael@0 | 31 | private static final int PROFILES = 100; |
michael@0 | 32 | private static final int PROFILES_NAME = 101; |
michael@0 | 33 | private static final int PROFILES_DEFAULT = 200; |
michael@0 | 34 | |
michael@0 | 35 | private static final String[] DEFAULT_ARGS = { |
michael@0 | 36 | BrowserContract.Profiles.NAME, |
michael@0 | 37 | BrowserContract.Profiles.PATH, |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | static { |
michael@0 | 41 | URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles", PROFILES); |
michael@0 | 42 | URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "profiles/*", PROFILES_NAME); |
michael@0 | 43 | URI_MATCHER.addURI(BrowserContract.PROFILES_AUTHORITY, "default", PROFILES_DEFAULT); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | @Override |
michael@0 | 47 | public String getType(Uri uri) { |
michael@0 | 48 | return null; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | @Override |
michael@0 | 52 | public boolean onCreate() { |
michael@0 | 53 | // Successfully loaded. |
michael@0 | 54 | return true; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | private String[] profileValues(final String name, final String path, int len, int nameIndex, int pathIndex) { |
michael@0 | 58 | final String[] values = new String[len]; |
michael@0 | 59 | if (nameIndex >= 0) { |
michael@0 | 60 | values[nameIndex] = name; |
michael@0 | 61 | } |
michael@0 | 62 | if (pathIndex >= 0) { |
michael@0 | 63 | values[pathIndex] = path; |
michael@0 | 64 | } |
michael@0 | 65 | return values; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | protected void addRowForProfile(final MatrixCursor cursor, final int len, final int nameIndex, final int pathIndex, final String name, final String path) { |
michael@0 | 69 | if (path == null || name == null) { |
michael@0 | 70 | return; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | String[] values = new String[len]; |
michael@0 | 74 | if (nameIndex >= 0) { |
michael@0 | 75 | values[nameIndex] = name; |
michael@0 | 76 | } |
michael@0 | 77 | if (pathIndex >= 0) { |
michael@0 | 78 | values[pathIndex] = path; |
michael@0 | 79 | } |
michael@0 | 80 | cursor.addRow(profileValues(name, path, len, nameIndex, pathIndex)); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | protected Cursor getCursorForProfiles(final String[] args, Map<String, String> profiles) { |
michael@0 | 84 | // Compute the projection. |
michael@0 | 85 | int nameIndex = -1; |
michael@0 | 86 | int pathIndex = -1; |
michael@0 | 87 | for (int i = 0; i < args.length; ++i) { |
michael@0 | 88 | if (BrowserContract.Profiles.NAME.equals(args[i])) { |
michael@0 | 89 | nameIndex = i; |
michael@0 | 90 | } else if (BrowserContract.Profiles.PATH.equals(args[i])) { |
michael@0 | 91 | pathIndex = i; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | final MatrixCursor cursor = new MatrixCursor(args); |
michael@0 | 96 | for (Entry<String, String> entry : profiles.entrySet()) { |
michael@0 | 97 | addRowForProfile(cursor, args.length, nameIndex, pathIndex, entry.getKey(), entry.getValue()); |
michael@0 | 98 | } |
michael@0 | 99 | return cursor; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | @Override |
michael@0 | 103 | public Cursor query(Uri uri, String[] projection, String selection, |
michael@0 | 104 | String[] selectionArgs, String sortOrder) { |
michael@0 | 105 | |
michael@0 | 106 | final String[] args = (projection == null) ? DEFAULT_ARGS : projection; |
michael@0 | 107 | |
michael@0 | 108 | final File mozillaDir; |
michael@0 | 109 | try { |
michael@0 | 110 | mozillaDir = GeckoProfileDirectories.getMozillaDirectory(getContext()); |
michael@0 | 111 | } catch (NoMozillaDirectoryException e) { |
michael@0 | 112 | Log.d(LOG_TAG, "No Mozilla directory; cannot query for profiles. Assuming there are none."); |
michael@0 | 113 | return new MatrixCursor(projection); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | final Map<String, String> matchingProfiles; |
michael@0 | 117 | |
michael@0 | 118 | final int match = URI_MATCHER.match(uri); |
michael@0 | 119 | switch (match) { |
michael@0 | 120 | case PROFILES: |
michael@0 | 121 | // Return all profiles. |
michael@0 | 122 | matchingProfiles = GeckoProfileDirectories.getAllProfiles(mozillaDir); |
michael@0 | 123 | break; |
michael@0 | 124 | case PROFILES_NAME: |
michael@0 | 125 | // Return data about the specified profile. |
michael@0 | 126 | final String name = uri.getLastPathSegment(); |
michael@0 | 127 | matchingProfiles = GeckoProfileDirectories.getProfilesNamed(mozillaDir, |
michael@0 | 128 | name); |
michael@0 | 129 | break; |
michael@0 | 130 | case PROFILES_DEFAULT: |
michael@0 | 131 | matchingProfiles = GeckoProfileDirectories.getDefaultProfile(mozillaDir); |
michael@0 | 132 | break; |
michael@0 | 133 | default: |
michael@0 | 134 | throw new UnsupportedOperationException("Unknown query URI " + uri); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | return getCursorForProfiles(args, matchingProfiles); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | @Override |
michael@0 | 141 | public Uri insert(Uri uri, ContentValues values) { |
michael@0 | 142 | throw new IllegalStateException("Inserts not supported."); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | @Override |
michael@0 | 146 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
michael@0 | 147 | throw new IllegalStateException("Deletes not supported."); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | @Override |
michael@0 | 151 | public int update(Uri uri, ContentValues values, String selection, |
michael@0 | 152 | String[] selectionArgs) { |
michael@0 | 153 | throw new IllegalStateException("Updates not supported."); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | } |