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

Unified Diff: base/android/java/src/org/chromium/base/JavaHandlerThread.java

Issue 2774363003: android: Java-based launcher thread (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « no previous file | base/android/java_handler_thread.h » ('j') | content/browser/browser_main_loop.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/JavaHandlerThread.java
diff --git a/base/android/java/src/org/chromium/base/JavaHandlerThread.java b/base/android/java/src/org/chromium/base/JavaHandlerThread.java
index cd057282b88513b4cc48d3d70b16978b9ac58849..cb44008539f3ae25ed6dbd45d14b9e64c88fa47d 100644
--- a/base/android/java/src/org/chromium/base/JavaHandlerThread.java
+++ b/base/android/java/src/org/chromium/base/JavaHandlerThread.java
@@ -8,19 +8,24 @@ import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
+import android.os.Looper;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
/**
- * This class is an internal detail of the native counterpart.
- * It is instantiated and owned by the native object.
+ * Thread in Java with an Anroid Handler. This class is not thread safe.
*/
@JNINamespace("base::android")
-class JavaHandlerThread {
- final HandlerThread mThread;
+public class JavaHandlerThread {
+ private final HandlerThread mThread;
+ private boolean mStarted;
- private JavaHandlerThread(String name) {
+ /**
+ * Construct a java-only instance. Can be connected with native side later.
+ * Useful for cases where a java thread is needed before native library is loaded.
+ */
+ public JavaHandlerThread(String name) {
mThread = new HandlerThread(name);
}
@@ -29,9 +34,19 @@ class JavaHandlerThread {
return new JavaHandlerThread(name);
}
- @CalledByNative
- private void start(final long nativeThread, final long nativeEvent) {
+ public Looper getLooper() {
agrieve 2017/03/28 19:59:32 super nit: Might make more sense to expose this by
boliu 2017/03/28 23:30:52 I think composition is still better, to hide thing
+ return mThread.getLooper();
+ }
+
+ public void maybeStart() {
+ if (mStarted) return;
agrieve 2017/03/28 19:59:32 nit: I think you could do away with mStarted by ch
boliu 2017/03/28 23:30:52 Done.
mThread.start();
+ mStarted = true;
+ }
+
+ @CalledByNative
+ private void startAndInitialize(final long nativeThread, final long nativeEvent) {
+ maybeStart();
new Handler(mThread.getLooper()).post(new Runnable() {
@Override
public void run() {
@@ -43,6 +58,7 @@ class JavaHandlerThread {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@CalledByNative
private void stop(final long nativeThread, final long nativeEvent) {
+ assert mStarted;
final boolean quitSafely = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
new Handler(mThread.getLooper()).post(new Runnable() {
@Override
« no previous file with comments | « no previous file | base/android/java_handler_thread.h » ('j') | content/browser/browser_main_loop.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698