Chromium Code Reviews| 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..ae5e479212a97f154131e06881fc71f4bb6983fb |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProvider.java |
| @@ -0,0 +1,133 @@ |
| +// 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 SynchronousQueue<Pair<Boolean, String>> mResultQueue; |
|
Bernhard Bauer
2015/12/14 17:58:37
Can you make this final?
aberent
2015/12/16 15:20:37
Done.
|
| + |
| + public SupervisedUserContentProvider() { |
| + super(); |
|
Bernhard Bauer
2015/12/14 17:58:37
This can be removed, right?
aberent
2015/12/16 15:20:37
Done.
|
| + mResultQueue = new SynchronousQueue<Pair<Boolean, String>>(); |
|
Bernhard Bauer
2015/12/14 17:58:37
Initialize this where it's declared?
aberent
2015/12/16 15:20:37
Done.
|
| + } |
| + |
| + 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 synchronized Pair<Boolean, String> shouldProceed(final String url) { |
|
Bernhard Bauer
2015/12/14 17:58:37
Synchronize on a member variable of type Object. D
aberent
2015/12/16 15:20:37
Done.
|
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + try { |
| + nativeShouldProceed(getSupervisedUserContentProvider(), url); |
| + } catch (ProcessInitException e) { |
| + onQueryComplete(false, null); |
| + } |
| + } |
| + }); |
| + try { |
| + return mResultQueue.take(); |
|
Bernhard Bauer
2015/12/14 17:58:37
Can you add a comment that this will block until a
aberent
2015/12/16 15:20:37
Done.
|
| + } catch (InterruptedException e) { |
| + return new Pair<Boolean, String>(false, null); |
| + } |
| + } |
| + |
| + @Override |
| + protected boolean canInsert() { |
| + // Chromium always allows insertion requests. |
| + return true; |
| + } |
| + |
| + @Override |
| + protected void requestInsert(final String url) { |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + try { |
| + nativeRequestInsert(getSupervisedUserContentProvider(), url); |
| + } catch (ProcessInitException e) { |
| + // There is no way of returning anything sensible here, so ignore the error and |
| + // do nothing. |
| + } |
| + } |
| + }); |
| + } |
| + |
| + @VisibleForTesting |
| + @Override |
| + public Bundle call(String method, String arg, Bundle bundle) { |
| + if (method == "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) { |
| + mResultQueue.add(new Pair<Boolean, String>(result, errorMessage)); |
| + } |
| + |
| + @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); |
| +} |