Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Unified Diff: components/web_restriction/java/src/org/chromium/components/webrestriction/ContentResolverWebRestrictionProvider.java

Issue 1423713015: [WIP] WebRestrictions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: continue review in split cl Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/web_restriction/java/src/org/chromium/components/webrestriction/ContentResolverWebRestrictionProvider.java
diff --git a/components/web_restriction/java/src/org/chromium/components/webrestriction/ContentResolverWebRestrictionProvider.java b/components/web_restriction/java/src/org/chromium/components/webrestriction/ContentResolverWebRestrictionProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..88af8a0011c5da8874f0ff781aea5f6de7ae788a
--- /dev/null
+++ b/components/web_restriction/java/src/org/chromium/components/webrestriction/ContentResolverWebRestrictionProvider.java
@@ -0,0 +1,90 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.components.webrestriction;
+
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.text.TextUtils;
+
+import org.chromium.base.ContextUtils;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+
+/**
+ * This class acts as an interface that talks to the content provider which actually implements the
+ * web restriction provider.
+ */
+@JNINamespace("web_restriction::android")
+public class ContentResolverWebRestrictionProvider {
+ private final ContentResolver mContentResolver;
+
+ private final Uri mRequestUri;
+ private final Uri mQueryUri;
+ private boolean mSupportsRequest;
+
+ /**
+ * Start the web restriction provider and setup the content resolver.
+ */
+ private ContentResolverWebRestrictionProvider(String authority) {
+ assert !TextUtils.isEmpty(authority);
+ Uri baseUri = new Uri.Builder().scheme("content").authority(authority).build();
+ mContentResolver = ContextUtils.getApplicationContext().getContentResolver();
+ mQueryUri = Uri.withAppendedPath(baseUri, "authorized");
+ mRequestUri = Uri.withAppendedPath(baseUri, "requested");
+ mSupportsRequest = mContentResolver.getType(mRequestUri) != null;
+ }
+
+ /**
+ * Simple helper method to expose the constructor over JNI.
+ */
+ @CalledByNative
+ private static ContentResolverWebRestrictionProvider create(String authority) {
+ return new ContentResolverWebRestrictionProvider(authority);
+ }
+
+ /**
+ * @return whether the web restriction provider supports requesting access for a blocked url.
+ */
+ @CalledByNative
+ private boolean supportsRequest() {
+ return mSupportsRequest;
+ }
+
+ /**
+ * Whether we can proceed with loading the {@code url}.
+ * In case the url is not to be loaded, the web restriction provider can return an optional
+ * error page to show instead.
+ */
+ @CalledByNative
+ private void shouldProceed(final long nativeCallback, final String url) {
+ String select = String.format("url = '%s'", url);
+ Cursor result = mContentResolver.query(mQueryUri, null, select, null, null);
+ boolean shouldProceed = result == null || result.getInt(0) > 0;
+ String errorPage = shouldProceed ? null : result.getString(1);
+ nativeShouldProceed(nativeCallback, shouldProceed, errorPage);
+ }
+
+ /**
+ * Request permission to load the {@code url}.
+ * The web restriction provider returns a {@code boolean} variable indicating whether it was
+ * able to forward the request to the appropriate authority who can approve it.
+ */
+ @CalledByNative
+ private void requestPermission(final long nativeCallback, final String url) {
+ // TODO(knn): register for updates
+ ContentValues values = new ContentValues(1);
+ values.put("url", url);
+ boolean requestSuccess = mContentResolver.insert(mRequestUri, values) != null;
+ nativeRequestSuccess(nativeCallback, requestSuccess);
+ }
+
+ private native void nativeShouldProceed(
+ long nativeSelfDeletingCallback, boolean shouldProceed, String errorPage);
+
+ private native void nativeRequestSuccess(
+ long nativeSelfDeletingCallback, boolean requestSuccess);
+}

Powered by Google App Engine
This is Rietveld 408576698