Skip to content

Commit

Permalink
[scheduler] Ui thread task posting API changes.
Browse files Browse the repository at this point in the history
As part of an ongoing effort to migrate all Java task posting to PostTask,
we are deprecating the alternative methods used at the moment
and providing PostTask-based alternatives. We are also providing a
workaround for call sites which can't import content. In order for this to
work, we are adding ThreadUtils initialisation to an early stage in the
initialisation of Chrome.

Bug: 863341
Change-Id: Ie1594a36803dd3b43480098a24e6fbd3e34c5c76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1518513
Commit-Queue: Karolina Soltys <ksolt@chromium.org>
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#642124}
  • Loading branch information
Karolina Soltys authored and Commit Bot committed Mar 19, 2019
1 parent 22bfe60 commit e237674
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 263 deletions.
53 changes: 53 additions & 0 deletions base/android/java/src/org/chromium/base/ThreadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ public static Handler getUiThreadHandler() {
* Run the supplied Runnable on the main thread. The method will block until the Runnable
* completes.
*
* @deprecated Use {@link
* org.chromium.content_public.browser.test.util.TestThreadUtils#runOnUiThreadBlocking(Runnable)
* TestThreadUtils.runOnUiThreadBlocking(r)} instead. For non-test usage (heavily
* discouraged) use {@link org.chromium.base.task.PostTask#runSynchronously(TaskTraits,
* Runnable) PostTask.runSynchronously(TaskTraits, Runnable)} with task traits chosen from
* {@link org.chromium.content_public.browser.UiThreadTaskTraits}. If the call site can't import
* content, it means it shouldn't be posting to the UI thread at all; all such usages will
* gradually get rewritten.
* @param r The Runnable to run.
*/
@Deprecated
public static void runOnUiThreadBlocking(final Runnable r) {
if (runningOnUiThread()) {
r.run();
Expand All @@ -86,9 +95,13 @@ public static void runOnUiThreadBlocking(final Runnable r) {
* Run the supplied Callable on the main thread, wrapping any exceptions in a RuntimeException.
* The method will block until the Callable completes.
*
* @deprecated Use {@link
* org.chromium.content_public.browser.test.util.TestThreadUtils#runOnUiThreadBlockingNoException(Callable)
* TestThreadUtils.runOnUiThreadBlockingNoException(c)} instead.
* @param c The Callable to run
* @return The result of the callable
*/
@Deprecated
@VisibleForTesting
public static <T> T runOnUiThreadBlockingNoException(Callable<T> c) {
try {
Expand All @@ -102,10 +115,14 @@ public static <T> T runOnUiThreadBlockingNoException(Callable<T> c) {
* Run the supplied Callable on the main thread, The method will block until the Callable
* completes.
*
* @deprecated Use {@link
* org.chromium.content_public.browser.test.util.TestThreadUtils#runOnUiThreadBlocking(Callable)
* TestThreadUtils.runOnUiThreadBlocking(c)} instead.
* @param c The Callable to run
* @return The result of the callable
* @throws ExecutionException c's exception
*/
@Deprecated
public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
FutureTask<T> task = new FutureTask<T>(c);
runOnUiThread(task);
Expand All @@ -120,9 +137,15 @@ public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionExcepti
* Run the supplied FutureTask on the main thread. The method will block only if the current
* thread is the main thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#runOrPostTask(TaskTraits, Runnable)
* PostTask.runOrPostTask(TaskTraits, Runnable)} with task traits chosen from {@link
* org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param task The FutureTask to run
* @return The queried task (to aid inline construction)
*/
@Deprecated
public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) {
if (runningOnUiThread()) {
task.run();
Expand All @@ -136,9 +159,15 @@ public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) {
* Run the supplied Callable on the main thread. The method will block only if the current
* thread is the main thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#runOrPostTask(TaskTraits, Runnable)
* PostTask.runOrPostTask(TaskTraits, Runnable)} with task traits chosen from {@link
* org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param c The Callable to run
* @return A FutureTask wrapping the callable to retrieve results
*/
@Deprecated
public static <T> FutureTask<T> runOnUiThread(Callable<T> c) {
return runOnUiThread(new FutureTask<T>(c));
}
Expand All @@ -147,8 +176,14 @@ public static <T> FutureTask<T> runOnUiThread(Callable<T> c) {
* Run the supplied Runnable on the main thread. The method will block only if the current
* thread is the main thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#runOrPostTask(TaskTraits, Runnable)
* PostTask.runOrPostTask(TaskTraits, Runnable)} with task traits chosen from {@link
* org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param r The Runnable to run
*/
@Deprecated
public static void runOnUiThread(Runnable r) {
if (runningOnUiThread()) {
r.run();
Expand All @@ -161,9 +196,15 @@ public static void runOnUiThread(Runnable r) {
* Post the supplied FutureTask to run on the main thread. The method will not block, even if
* called on the UI thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#postTask(TaskTraits, Runnable)
* PostTask.postTask(TaskTraits, Runnable)} with task traits chosen from {@link
* org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param task The FutureTask to run
* @return The queried task (to aid inline construction)
*/
@Deprecated
public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) {
getUiThreadHandler().post(task);
return task;
Expand All @@ -173,8 +214,14 @@ public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) {
* Post the supplied Runnable to run on the main thread. The method will not block, even if
* called on the UI thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#postTask(TaskTraits, Runnable)
* PostTask.postTask(TaskTraits, Runnable)} with task traits chosen from {@link
* org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param task The Runnable to run
*/
@Deprecated
public static void postOnUiThread(Runnable task) {
getUiThreadHandler().post(task);
}
Expand All @@ -183,10 +230,16 @@ public static void postOnUiThread(Runnable task) {
* Post the supplied Runnable to run on the main thread after the given amount of time. The
* method will not block, even if called on the UI thread.
*
* @deprecated Use {@link org.chromium.base.task.PostTask#postDelayedTask(TaskTraits, Runnable,
* long) PostTask.postDelayedTask(TaskTraits, Runnable, long)} with task traits chosen
* from {@link org.chromium.content_public.browser.UiThreadTaskTraits}.
* If the call site can't import content, it means it shouldn't be posting to the UI
* thread at all; all such usages will gradually get rewritten.
* @param task The Runnable to run
* @param delayMillis The delay in milliseconds until the Runnable will be run
*/
@VisibleForTesting
@Deprecated
public static void postOnUiThreadDelayed(Runnable task, long delayMillis) {
getUiThreadHandler().postDelayed(task, delayMillis);
}
Expand Down
34 changes: 34 additions & 0 deletions base/android/java/src/org/chromium/base/task/PostTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

/**
* Java interface to the native chromium scheduler. Note tasks can be posted before native
Expand Down Expand Up @@ -109,6 +110,39 @@ public static void runOrPostTask(TaskTraits taskTraits, Runnable task) {
}
}

/**
* This function executes the task immediately if the current thread is the
* same as the one corresponding to the SingleThreadTaskRunner, otherwise it
* posts it and blocks until the task finishes.
*
* It should be executed only for tasks with traits corresponding to
* executors backed by a SingleThreadTaskRunner, like UiThreadTaskTraits.
*
* Use this only for trivial tasks as it ignores task priorities.
*
* @deprecated In tests, use {@link
* org.chromium.content_public.browser.test.util.TestThreadUtils#runOnUiThreadBlocking(Runnable)
* TestThreadUtils.runOnUiThreadBlocking(Runnable)} instead. Non-test usage is heavily
* discouraged. For non-tests, use callbacks rather than blocking threads. If you
* absolutely must block the thread, use FutureTask.get().
* @param taskTraits The TaskTraits that describe the desired TaskRunner.
* @param task The task to be run with the specified traits.
*/
@Deprecated
public static void runSynchronously(TaskTraits taskTraits, Runnable r) {
if (getTaskExecutorForTraits(taskTraits).canRunTaskImmediately(taskTraits)) {
r.run();
} else {
FutureTask<Void> task = new FutureTask<Void>(r, null);
postTask(taskTraits, task);
try {
task.get();
} catch (Exception e) {
throw new RuntimeException("Exception occurred while waiting for runnable", e);
}
}
}

/**
* Registers a TaskExecutor, this must be called before any other usages of this API.
*
Expand Down
2 changes: 0 additions & 2 deletions content/public/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ android_library("content_java") {
"java/src/org/chromium/content/browser/BackgroundSyncNetworkObserver.java",
"java/src/org/chromium/content/browser/BindingManager.java",
"java/src/org/chromium/content/browser/BrowserStartupControllerImpl.java",
"java/src/org/chromium/content/browser/BrowserThreadUtilsImpl.java",
"java/src/org/chromium/content/browser/ServicificationStartupUma.java",
"java/src/org/chromium/content/browser/ChildProcessCreationParamsImpl.java",
"java/src/org/chromium/content/browser/ChildProcessLauncherHelperImpl.java",
Expand Down Expand Up @@ -243,7 +242,6 @@ android_library("content_java") {
"java/src/org/chromium/content_public/browser/ActionModeCallbackHelper.java",
"java/src/org/chromium/content_public/browser/BrowserStartupController.java",
"java/src/org/chromium/content_public/browser/BrowserTaskExecutor.java",
"java/src/org/chromium/content_public/browser/BrowserThreadUtils.java",
"java/src/org/chromium/content_public/browser/ContentViewStatics.java",
"java/src/org/chromium/content_public/browser/DeviceUtils.java",
"java/src/org/chromium/content_public/browser/InputMethodManagerWrapper.java",
Expand Down

This file was deleted.

Loading

0 comments on commit e237674

Please sign in to comment.