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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java

Issue 1452603002: Supervised user web restrictions content provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to comments Created 5 years 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: chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java b/chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..1da2f89e8b820aa5a6a7a12847653bc9f6d69af5
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java
@@ -0,0 +1,155 @@
+// 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.chrome.browser.superviseduser;
+
+import android.os.Bundle;
+import android.util.Pair;
+
+import org.chromium.base.ThreadUtils;
+import org.chromium.base.VisibleForTesting;
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.library_loader.LibraryProcessType;
+import org.chromium.base.library_loader.ProcessInitException;
+import org.chromium.components.webrestriction.WebRestrictionsContentProvider;
+import org.chromium.content.browser.BrowserStartupController;
+
+import java.util.concurrent.SynchronousQueue;
+
+/**
+ * Content provider for telling other apps (e.g. WebView apps) about the supervised user URL filter.
+ */
+public class SupervisedUserContentProvider extends WebRestrictionsContentProvider {
+ private long mNativeSupervisedUserContentProvider = 0;
+
+ private final SynchronousQueue<Pair<Boolean, String>> mQueryResultQueue =
+ new SynchronousQueue<Pair<Boolean, String>>();
+
+ private final Object mQuerySync = new Object();
+
+ private final SynchronousQueue<Boolean> mInsertResultQueue = new SynchronousQueue<Boolean>();
+
+ private final Object mInsertSync = new Object();
+
+ private long getSupervisedUserContentProvider() throws ProcessInitException {
+ if (mNativeSupervisedUserContentProvider != 0) {
+ return mNativeSupervisedUserContentProvider;
+ }
+
+ BrowserStartupController.get(getContext(), LibraryProcessType.PROCESS_BROWSER)
+ .startBrowserProcessesSync(false);
+
+ mNativeSupervisedUserContentProvider = nativeCreateSupervisedUserContentProvider();
+ return mNativeSupervisedUserContentProvider;
+ }
+
+ @VisibleForTesting
+ void setNativeSupervisedUserContentProviderForTesting(long nativeProvider) {
+ mNativeSupervisedUserContentProvider = nativeProvider;
+ }
+
+ @Override
+ protected Pair<Boolean, String> shouldProceed(final String url) {
+ // This may be called on multiple threads, hence the need for synchronization, but will
+ // never be called on the UI thread, hence the (blocking) call to onQueryComplete is safe.
+ // See http://developer.android.com/guide/components/processes-and-threads.html#ThreadSafe.
Bernhard Bauer 2015/12/16 17:52:09 This also means that all requests will effectively
aberent 2015/12/16 22:15:17 This, together with your comment about add vs. put
+ synchronized (mQuerySync) {
+ ThreadUtils.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ nativeShouldProceed(getSupervisedUserContentProvider(), url);
+ } catch (ProcessInitException e) {
+ onQueryComplete(false, null);
+ }
+ }
+ });
+ try {
+ // This will block until an onQueryComplete call on a different thread adds
+ // something to the queue.
+ return mQueryResultQueue.take();
+ } catch (InterruptedException e) {
+ return new Pair<Boolean, String>(false, null);
+ }
+ }
+ }
+
+ @Override
+ protected boolean canInsert() {
+ // Chromium always allows insertion requests.
Bernhard Bauer 2015/12/16 17:52:09 Nit: Chromium is just the name of the project. If
aberent 2015/12/16 22:15:17 Done.
+ return true;
+ }
+
+ @Override
+ protected boolean requestInsert(final String url) {
+ synchronized (mInsertSync) {
+ ThreadUtils.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ nativeRequestInsert(getSupervisedUserContentProvider(), url);
+ } catch (ProcessInitException e) {
+ onInsertRequestSendComplete(false);
+ }
+ }
+ });
+ try {
+ return mInsertResultQueue.take();
+ } catch (InterruptedException e) {
+ return false;
+ }
+ }
+ }
+
+ @VisibleForTesting
+ @Override
+ public Bundle call(String method, String arg, Bundle bundle) {
+ if (method.equals("setFilterForTesting")) setFilterForTesting();
+ return null;
+ }
+
+ @VisibleForTesting
+ void setFilterForTesting() {
+ ThreadUtils.runOnUiThreadBlocking(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ nativeSetFilterForTesting(getSupervisedUserContentProvider());
+ } catch (ProcessInitException e) {
+ // There is no way of returning anything sensible here, so ignore the error and
+ // do nothing.
+ }
+ }
+ });
+ }
+
+ @VisibleForTesting
+ @CalledByNative
+ void onQueryComplete(boolean result, String errorMessage) {
+ // This must be called precisely once per query.
+ mQueryResultQueue.add(new Pair<Boolean, String>(result, errorMessage));
Bernhard Bauer 2015/12/16 17:52:09 Does add() actually work if the other thread isn't
aberent 2015/12/16 22:15:17 See above. put() turned out to be unusable, becaus
Bernhard Bauer 2015/12/17 11:13:21 Cool! FTR, I don't think we actually have to care
+ }
+
+ @VisibleForTesting
+ @CalledByNative
+ void onInsertRequestSendComplete(boolean result) {
+ mInsertResultQueue.add(result);
+ }
+
+ @VisibleForTesting
+ @CalledByNative
+ void onSupervisedUserFilterUpdated() {
+ onFilterChanged();
+ }
+
+ @VisibleForTesting native long nativeCreateSupervisedUserContentProvider();
+
+ @VisibleForTesting
+ native void nativeShouldProceed(long nativeSupervisedUserContentProvider, String url);
+
+ @VisibleForTesting
+ native void nativeRequestInsert(long nativeSupervisedUserContentProvider, String url);
+
+ private native void nativeSetFilterForTesting(long nativeSupervisedUserContentProvider);
+}

Powered by Google App Engine
This is Rietveld 408576698