OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.webrestrictions.browser; | 5 package org.chromium.components.webrestrictions.browser; |
6 | 6 |
| 7 import android.annotation.TargetApi; |
7 import android.content.ContentProvider; | 8 import android.content.ContentProvider; |
8 import android.content.ContentValues; | 9 import android.content.ContentValues; |
9 import android.content.Context; | 10 import android.content.Context; |
10 import android.content.UriMatcher; | 11 import android.content.UriMatcher; |
11 import android.content.pm.ProviderInfo; | 12 import android.content.pm.ProviderInfo; |
12 import android.database.AbstractCursor; | 13 import android.database.AbstractCursor; |
13 import android.database.Cursor; | 14 import android.database.Cursor; |
14 import android.net.Uri; | 15 import android.net.Uri; |
| 16 import android.os.Build; |
15 | 17 |
16 import java.util.regex.Matcher; | 18 import java.util.regex.Matcher; |
17 import java.util.regex.Pattern; | 19 import java.util.regex.Pattern; |
18 | 20 |
19 /** | 21 /** |
20 * Abstract content provider for providing web restrictions, i.e. for providing
a filter for URLs so | 22 * Abstract content provider for providing web restrictions, i.e. for providing
a filter for URLs so |
21 * that they can be blocked or permitted, and a means of requesting permission f
or new URLs. It | 23 * that they can be blocked or permitted, and a means of requesting permission f
or new URLs. It |
22 * provides two (virtual) tables; an 'authorized' table listing the the status o
f every URL, and a | 24 * provides two (virtual) tables; an 'authorized' table listing the the status o
f every URL, and a |
23 * 'requested' table containing the requests for access to new URLs. The 'author
ized' table is read | 25 * 'requested' table containing the requests for access to new URLs. The 'author
ized' table is read |
24 * only and the 'requested' table is write only. | 26 * only and the 'requested' table is write only. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 public Cursor query(Uri uri, String[] projection, String selection, String[]
selectionArgs, | 103 public Cursor query(Uri uri, String[] projection, String selection, String[]
selectionArgs, |
102 String sortOrder) { | 104 String sortOrder) { |
103 if (!contentProviderEnabled()) return null; | 105 if (!contentProviderEnabled()) return null; |
104 // Check that this is the a query on the 'authorized' table | 106 // Check that this is the a query on the 'authorized' table |
105 // TODO(aberent): Provide useful queries on the 'requested' table. | 107 // TODO(aberent): Provide useful queries on the 'requested' table. |
106 if (mContentUriMatcher.match(uri) != AUTHORIZED) return null; | 108 if (mContentUriMatcher.match(uri) != AUTHORIZED) return null; |
107 // If the selection is of the right form get the url we are querying. | 109 // If the selection is of the right form get the url we are querying. |
108 Matcher matcher = mSelectionPattern.matcher(selection); | 110 Matcher matcher = mSelectionPattern.matcher(selection); |
109 if (!matcher.find()) return null; | 111 if (!matcher.find()) return null; |
110 final String url = matcher.group(1); | 112 final String url = matcher.group(1); |
111 final WebRestrictionsResult result = shouldProceed(url); | 113 final WebRestrictionsResult result = shouldProceed(maybeGetCallingPackag
e(), url); |
112 if (result == null) return null; | 114 if (result == null) return null; |
113 | 115 |
114 return new AbstractCursor() { | 116 return new AbstractCursor() { |
115 | 117 |
116 @Override | 118 @Override |
117 public int getCount() { | 119 public int getCount() { |
118 return 1; | 120 return 1; |
119 } | 121 } |
120 | 122 |
121 @Override | 123 @Override |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 return FIELD_TYPE_NULL; | 196 return FIELD_TYPE_NULL; |
195 } | 197 } |
196 | 198 |
197 @Override | 199 @Override |
198 public int getColumnCount() { | 200 public int getColumnCount() { |
199 return result.errorIntCount() + result.errorStringCount() + 1; | 201 return result.errorIntCount() + result.errorStringCount() + 1; |
200 } | 202 } |
201 }; | 203 }; |
202 } | 204 } |
203 | 205 |
| 206 @TargetApi(Build.VERSION_CODES.KITKAT) |
| 207 private String maybeGetCallingPackage() { |
| 208 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null; |
| 209 |
| 210 return getCallingPackage(); |
| 211 } |
| 212 |
204 @Override | 213 @Override |
205 public String getType(Uri uri) { | 214 public String getType(Uri uri) { |
206 // Abused to return whether we can insert | 215 // Abused to return whether we can insert |
207 if (!contentProviderEnabled()) return null; | 216 if (!contentProviderEnabled()) return null; |
208 if (mContentUriMatcher.match(uri) != REQUESTED) return null; | 217 if (mContentUriMatcher.match(uri) != REQUESTED) return null; |
209 return canInsert() ? "text/plain" : null; | 218 return canInsert() ? "text/plain" : null; |
210 } | 219 } |
211 | 220 |
212 @Override | 221 @Override |
213 public Uri insert(Uri uri, ContentValues values) { | 222 public Uri insert(Uri uri, ContentValues values) { |
(...skipping 13 matching lines...) Expand all Loading... |
227 public int delete(Uri uri, String selection, String[] selectionArgs) { | 236 public int delete(Uri uri, String selection, String[] selectionArgs) { |
228 return 0; | 237 return 0; |
229 } | 238 } |
230 | 239 |
231 @Override | 240 @Override |
232 public int update(Uri uri, ContentValues values, String selection, String[]
selectionArgs) { | 241 public int update(Uri uri, ContentValues values, String selection, String[]
selectionArgs) { |
233 return 0; | 242 return 0; |
234 } | 243 } |
235 | 244 |
236 /** | 245 /** |
| 246 * @param the package calling the content provider, or null if the package i
s not available. |
237 * @param url the URL that is wanted. | 247 * @param url the URL that is wanted. |
238 * @return a pair containing the Result and the HTML Error Message. result i
s true if safe to | 248 * @return a pair containing the Result and the HTML Error Message. result i
s true if safe to |
239 * proceed, false otherwise. error message is only meaningful if res
ult is false, a null | 249 * proceed, false otherwise. error message is only meaningful if res
ult is false, a null |
240 * error message means use application default. | 250 * error message means use application default. |
241 */ | 251 */ |
242 protected abstract WebRestrictionsResult shouldProceed(final String url); | 252 protected abstract WebRestrictionsResult shouldProceed(String callingPackage
, String url); |
243 | 253 |
244 /** | 254 /** |
245 * @return whether the content provider allows insertions. | 255 * @return whether the content provider allows insertions. |
246 */ | 256 */ |
247 protected abstract boolean canInsert(); | 257 protected abstract boolean canInsert(); |
248 | 258 |
249 /** | 259 /** |
250 * @return the names of the custom error columns, integer valued columns mus
t proceed string | 260 * @return the names of the custom error columns, integer valued columns mus
t proceed string |
251 * valued columns. | 261 * valued columns. |
252 */ | 262 */ |
(...skipping 12 matching lines...) Expand all Loading... |
265 protected abstract boolean contentProviderEnabled(); | 275 protected abstract boolean contentProviderEnabled(); |
266 | 276 |
267 /** | 277 /** |
268 * Call to tell observers that the filter has changed. | 278 * Call to tell observers that the filter has changed. |
269 */ | 279 */ |
270 protected void onFilterChanged() { | 280 protected void onFilterChanged() { |
271 getContext().getContentResolver().notifyChange( | 281 getContext().getContentResolver().notifyChange( |
272 mContentUri.buildUpon().appendPath("authorized").build(), null); | 282 mContentUri.buildUpon().appendPath("authorized").build(), null); |
273 } | 283 } |
274 } | 284 } |
OLD | NEW |