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.webrestrictions; |
| 6 |
| 7 import android.content.ContentResolver; |
| 8 import android.content.ContentValues; |
| 9 import android.database.ContentObserver; |
| 10 import android.database.Cursor; |
| 11 import android.net.Uri; |
| 12 import android.text.TextUtils; |
| 13 |
| 14 import org.chromium.base.ContextUtils; |
| 15 import org.chromium.base.annotations.CalledByNative; |
| 16 import org.chromium.base.annotations.JNINamespace; |
| 17 |
| 18 /** |
| 19 * This class acts as an interface that talks to the content provider which actu
ally implements the |
| 20 * web restriction provider. |
| 21 */ |
| 22 @JNINamespace("web_restrictions") |
| 23 public class ContentResolverWebRestrictionsProvider { |
| 24 private final Uri mQueryUri; |
| 25 private final Uri mRequestUri; |
| 26 private final ContentObserver mContentObserver; |
| 27 private ContentResolver mContentResolver; |
| 28 |
| 29 /** |
| 30 * Start the web restriction provider and setup the content resolver. |
| 31 */ |
| 32 private ContentResolverWebRestrictionsProvider(String authority, final long
nativeProvider) { |
| 33 assert !TextUtils.isEmpty(authority); |
| 34 Uri baseUri = new Uri.Builder().scheme("content").authority(authority).b
uild(); |
| 35 mQueryUri = Uri.withAppendedPath(baseUri, "authorized"); |
| 36 mRequestUri = Uri.withAppendedPath(baseUri, "requested"); |
| 37 mContentResolver = ContextUtils.getApplicationContext().getContentResolv
er(); |
| 38 mContentObserver = new ContentObserver(null) { |
| 39 @Override |
| 40 public void onChange(boolean selfChange) { |
| 41 onChange(selfChange, null); |
| 42 } |
| 43 |
| 44 @Override |
| 45 public void onChange(boolean selfChange, Uri uri) { |
| 46 nativeNotifyWebRestrictionsChanged(nativeProvider); |
| 47 } |
| 48 }; |
| 49 mContentResolver.registerContentObserver(baseUri, true, mContentObserver
); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Simple helper method to expose the constructor over JNI. |
| 54 */ |
| 55 @CalledByNative |
| 56 private static ContentResolverWebRestrictionsProvider create( |
| 57 String authority, long nativeProvider) { |
| 58 return new ContentResolverWebRestrictionsProvider(authority, nativeProvi
der); |
| 59 } |
| 60 |
| 61 /** |
| 62 * @return whether the web restriction provider supports requesting access f
or a blocked url. |
| 63 */ |
| 64 @CalledByNative |
| 65 private boolean supportsRequest() { |
| 66 return mContentResolver.getType(mRequestUri) != null; |
| 67 } |
| 68 |
| 69 /** |
| 70 * Called when the ContentResolverWebRestrictionsProvider is about to be des
troyed. |
| 71 */ |
| 72 @CalledByNative |
| 73 private void onDestroy() { |
| 74 mContentResolver.unregisterContentObserver(mContentObserver); |
| 75 // Ensure we don't use it any more. |
| 76 mContentResolver = null; |
| 77 } |
| 78 |
| 79 /** |
| 80 * Whether we can proceed with loading the {@code url}. |
| 81 * In case the url is not to be loaded, the web restriction provider can ret
urn an optional |
| 82 * error page to show instead. |
| 83 */ |
| 84 @CalledByNative |
| 85 private void shouldProceed(final long nativeCallback, final String url) { |
| 86 String select = String.format("url = '%s'", url); |
| 87 Cursor result = mContentResolver.query(mQueryUri, null, select, null, nu
ll); |
| 88 boolean shouldProceed = result == null || result.getInt(0) > 0; |
| 89 String errorPage = shouldProceed ? null : result.getString(1); |
| 90 nativeShouldProceed(nativeCallback, shouldProceed, errorPage); |
| 91 } |
| 92 |
| 93 /** |
| 94 * Request permission to load the {@code url}. |
| 95 * The web restriction provider returns a {@code boolean} variable indicatin
g whether it was |
| 96 * able to forward the request to the appropriate authority who can approve
it. |
| 97 */ |
| 98 @CalledByNative |
| 99 private void requestPermission(final long nativeCallback, final String url)
{ |
| 100 ContentValues values = new ContentValues(1); |
| 101 values.put("url", url); |
| 102 boolean requestSuccess = mContentResolver.insert(mRequestUri, values) !=
null; |
| 103 nativeRequestSuccess(nativeCallback, requestSuccess); |
| 104 } |
| 105 |
| 106 private static native void nativeShouldProceed( |
| 107 long ptrSelfDeletingCallback, boolean shouldProceed, String errorPag
e); |
| 108 |
| 109 private static native void nativeRequestSuccess( |
| 110 long ptrSelfDeletingCallback, boolean requestSuccess); |
| 111 |
| 112 private static native void nativeNotifyWebRestrictionsChanged(long ptrProvid
er); |
| 113 } |
OLD | NEW |