OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.components.webrestriction; |
| 6 |
| 7 import android.content.ContentProvider; |
| 8 import android.content.ContentValues; |
| 9 import android.content.Context; |
| 10 import android.content.UriMatcher; |
| 11 import android.content.pm.ProviderInfo; |
| 12 import android.database.AbstractCursor; |
| 13 import android.database.Cursor; |
| 14 import android.net.Uri; |
| 15 import android.util.Pair; |
| 16 |
| 17 import java.util.regex.Matcher; |
| 18 import java.util.regex.Pattern; |
| 19 |
| 20 /** |
| 21 * Abstract content provider for providing web restrictions, i.e. for providing
a filter for URLs so |
| 22 * that they can be blocked or permitted, and a means of requesting permission f
or new URLs. It |
| 23 * provides two (virtual) tables; an 'authorized' table listing the the status o
f every URL, and a |
| 24 * 'requested' table containing the requests for access to new URLs. The 'author
ized' table is read |
| 25 * only and the 'requested' table is write only. |
| 26 */ |
| 27 public abstract class WebRestrictionsContentProvider extends ContentProvider { |
| 28 public static final int BLOCKED = 0; |
| 29 public static final int PROCEED = 1; |
| 30 private static final int WEB_RESTRICTIONS = 1; |
| 31 private static final int AUTHORIZED = 2; |
| 32 private static final int REQUESTED = 3; |
| 33 |
| 34 private final Pattern mSelectionPattern; |
| 35 private UriMatcher mContentUriMatcher; |
| 36 private Uri mContentUri; |
| 37 |
| 38 protected WebRestrictionsContentProvider() { |
| 39 // Pattern to extract the URL from the selection. |
| 40 // Matches patterns of the form "url = '<url>'" with arbitrary spacing a
round the "=" etc. |
| 41 mSelectionPattern = Pattern.compile("\\s*url\\s*=\\s*'([^']*)'"); |
| 42 } |
| 43 |
| 44 @Override |
| 45 public boolean onCreate() { |
| 46 return true; |
| 47 } |
| 48 |
| 49 @Override |
| 50 public void attachInfo(Context context, ProviderInfo info) { |
| 51 super.attachInfo(context, info); |
| 52 mContentUri = new Uri.Builder().scheme("content").authority(info.authori
ty).build(); |
| 53 mContentUriMatcher = new UriMatcher(WEB_RESTRICTIONS); |
| 54 mContentUriMatcher.addURI(info.authority, "authorized", AUTHORIZED); |
| 55 mContentUriMatcher.addURI(info.authority, "requested", REQUESTED); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public Cursor query(Uri uri, String[] projection, String selection, String[]
selectionArgs, |
| 60 String sortOrder) { |
| 61 // Check that this is the a query on the 'authorized' table |
| 62 // TODO(aberent): Provide useful queries on the 'requested' table. |
| 63 if (mContentUriMatcher.match(uri) != AUTHORIZED) return null; |
| 64 // If the selection is of the right form get the url we are querying. |
| 65 Matcher matcher = mSelectionPattern.matcher(selection); |
| 66 if (!matcher.find()) return null; |
| 67 final String url = matcher.group(1); |
| 68 final Pair<Boolean, String> result = shouldProceed(url); |
| 69 |
| 70 return new AbstractCursor() { |
| 71 |
| 72 @Override |
| 73 public int getCount() { |
| 74 return 1; |
| 75 } |
| 76 |
| 77 @Override |
| 78 public String[] getColumnNames() { |
| 79 return new String[] {"Should Proceed", "Error message"}; |
| 80 } |
| 81 |
| 82 @Override |
| 83 public String getString(int column) { |
| 84 if (column == 1) return result.second; |
| 85 return null; |
| 86 } |
| 87 |
| 88 @Override |
| 89 public short getShort(int column) { |
| 90 return 0; |
| 91 } |
| 92 |
| 93 @Override |
| 94 public int getInt(int column) { |
| 95 if (column == 0) return result.first ? PROCEED : BLOCKED; |
| 96 return 0; |
| 97 } |
| 98 |
| 99 @Override |
| 100 public long getLong(int column) { |
| 101 return 0; |
| 102 } |
| 103 |
| 104 @Override |
| 105 public float getFloat(int column) { |
| 106 return 0; |
| 107 } |
| 108 |
| 109 @Override |
| 110 public double getDouble(int column) { |
| 111 return 0; |
| 112 } |
| 113 |
| 114 @Override |
| 115 public boolean isNull(int column) { |
| 116 return false; |
| 117 } |
| 118 }; |
| 119 } |
| 120 |
| 121 @Override |
| 122 public String getType(Uri uri) { |
| 123 // Abused to return whether we can insert |
| 124 if (mContentUriMatcher.match(uri) != REQUESTED) return null; |
| 125 return canInsert() ? "text/plain" : null; |
| 126 } |
| 127 |
| 128 @Override |
| 129 public Uri insert(Uri uri, ContentValues values) { |
| 130 if (mContentUriMatcher.match(uri) != REQUESTED) return null; |
| 131 String url = values.getAsString("url"); |
| 132 if (requestInsert(url)) { |
| 133 // TODO(aberent): If we ever make the 'requested' table readable the
n we might want to |
| 134 // change this to a more conventional content URI (with a row number
). |
| 135 return uri.buildUpon().appendPath(url).build(); |
| 136 } else { |
| 137 return null; |
| 138 } |
| 139 } |
| 140 |
| 141 @Override |
| 142 public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 143 return 0; |
| 144 } |
| 145 |
| 146 @Override |
| 147 public int update(Uri uri, ContentValues values, String selection, String[]
selectionArgs) { |
| 148 return 0; |
| 149 } |
| 150 |
| 151 /** |
| 152 * @param url the URL that is wanted. |
| 153 * @return a pair containing the Result and the HTML Error Message. result i
s true if safe to |
| 154 * proceed, false otherwise. error message is only meaningful if res
ult is false, a null |
| 155 * error message means use application default. |
| 156 */ |
| 157 protected abstract Pair<Boolean, String> shouldProceed(final String url); |
| 158 |
| 159 /** |
| 160 * @return whether the content provider allows insertions. |
| 161 */ |
| 162 protected abstract boolean canInsert(); |
| 163 |
| 164 /** |
| 165 * Start a request that a URL should be permitted |
| 166 * |
| 167 * @param url the URL that is wanted. |
| 168 */ |
| 169 protected abstract boolean requestInsert(final String url); |
| 170 |
| 171 /** |
| 172 * Call to tell observers that the filter has changed. |
| 173 */ |
| 174 protected void onFilterChanged() { |
| 175 getContext().getContentResolver().notifyChange( |
| 176 mContentUri.buildUpon().appendPath("authorized").build(), null); |
| 177 } |
| 178 } |
OLD | NEW |