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 ContentResolver mContentResolver; | |
25 | |
26 private final Uri mRequestUri; | |
27 private final Uri mQueryUri; | |
28 private boolean mSupportsRequest; | |
29 | |
30 /** | |
31 * Start the web restriction provider and setup the content resolver. | |
32 */ | |
33 private ContentResolverWebRestrictionsProvider(String authority, final long nativeProvider) { | |
34 assert !TextUtils.isEmpty(authority); | |
35 Uri baseUri = new Uri.Builder().scheme("content").authority(authority).b uild(); | |
36 mContentResolver = ContextUtils.getApplicationContext().getContentResolv er(); | |
37 mQueryUri = Uri.withAppendedPath(baseUri, "authorized"); | |
38 mRequestUri = Uri.withAppendedPath(baseUri, "requested"); | |
39 mSupportsRequest = mContentResolver.getType(mRequestUri) != null; | |
40 mContentResolver.registerContentObserver(baseUri, true, new ContentObser ver(null) { | |
Bernhard Bauer
2016/01/28 12:15:14
We need to make sure we unregister this content ob
knn
2016/01/28 18:10:37
Done.
| |
41 @Override | |
42 public void onChange(boolean selfChange) { | |
43 onChange(selfChange, null); | |
44 } | |
45 | |
46 @Override | |
47 public void onChange(boolean selfChange, Uri uri) { | |
Bernhard Bauer
2016/01/28 12:15:14
BTW, on which thread is this called? We might agai
knn
2016/01/28 18:10:37
Right! Let's handle that on the native side.
| |
48 nativeNotifyWebRestrictionsChanged(nativeProvider); | |
49 } | |
50 }); | |
51 } | |
52 | |
53 /** | |
54 * Simple helper method to expose the constructor over JNI. | |
55 */ | |
56 @CalledByNative | |
57 private static ContentResolverWebRestrictionsProvider create( | |
58 String authority, long nativeProvider) { | |
59 return new ContentResolverWebRestrictionsProvider(authority, nativeProvi der); | |
60 } | |
61 | |
62 /** | |
63 * @return whether the web restriction provider supports requesting access f or a blocked url. | |
64 */ | |
65 @CalledByNative | |
66 private boolean supportsRequest() { | |
67 return mSupportsRequest; | |
68 } | |
69 | |
70 /** | |
71 * Whether we can proceed with loading the {@code url}. | |
72 * In case the url is not to be loaded, the web restriction provider can ret urn an optional | |
73 * error page to show instead. | |
74 */ | |
75 @CalledByNative | |
76 private void shouldProceed(final long nativeCallback, final String url) { | |
77 String select = String.format("url = '%s'", url); | |
78 Cursor result = mContentResolver.query(mQueryUri, null, select, null, nu ll); | |
79 boolean shouldProceed = result == null || result.getInt(0) > 0; | |
80 String errorPage = shouldProceed ? null : result.getString(1); | |
81 nativeShouldProceed(nativeCallback, shouldProceed, errorPage); | |
82 } | |
83 | |
84 /** | |
85 * Request permission to load the {@code url}. | |
86 * The web restriction provider returns a {@code boolean} variable indicatin g whether it was | |
87 * able to forward the request to the appropriate authority who can approve it. | |
88 */ | |
89 @CalledByNative | |
90 private void requestPermission(final long nativeCallback, final String url) { | |
91 ContentValues values = new ContentValues(1); | |
92 values.put("url", url); | |
93 boolean requestSuccess = mContentResolver.insert(mRequestUri, values) != null; | |
94 nativeRequestSuccess(nativeCallback, requestSuccess); | |
95 } | |
96 | |
97 private static native void nativeShouldProceed( | |
98 long ptrSelfDeletingCallback, boolean shouldProceed, String errorPag e); | |
99 | |
100 private static native void nativeRequestSuccess( | |
101 long ptrSelfDeletingCallback, boolean requestSuccess); | |
102 | |
103 private static native void nativeNotifyWebRestrictionsChanged(long ptrProvid er); | |
104 } | |
OLD | NEW |