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

Unified Diff: base/android/java_thread.cc

Issue 18584006: Making a way to create thread with a Java Looper for Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update copyright year, and split a comment over two lines Created 7 years, 5 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: base/android/java_thread.cc
diff --git a/base/android/java_thread.cc b/base/android/java_thread.cc
new file mode 100644
index 0000000000000000000000000000000000000000..523b5a99beb326501547a986ed2ae941094f4583
--- /dev/null
+++ b/base/android/java_thread.cc
@@ -0,0 +1,59 @@
+// Copyright (c) 2013 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.
+
+#include "base/android/java_thread.h"
+
+#include <jni.h>
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/message_loop.h"
+#include "base/synchronization/waitable_event.h"
+#include "jni/JavaThread_jni.h"
+
+namespace base {
+
+namespace android {
+
+JavaThread::JavaThread(const char* name) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+
+ java_thread_.Reset(Java_JavaThread_create(
+ env, ConvertUTF8ToJavaString(env, name).Release()));
+}
+
+void JavaThread::Start() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+
+ waitable_event_ = new base::WaitableEvent(false, false);
+ Java_JavaThread_start(env, java_thread_.obj(), reinterpret_cast<jint>(this));
+ // Wait for thread to be initialized so it is ready to be used when Start
+ // returns
+ waitable_event_->Wait();
+
+ // Delete object only used in initialization
+ delete(waitable_event_);
benm (inactive) 2013/07/10 16:50:06 nit: no parens
Kristian Monsen 2013/07/10 17:50:19 Done.
+ waitable_event_ = NULL;
benm (inactive) 2013/07/10 16:50:06 maybe leave it non null and dcheck == null at the
Kristian Monsen 2013/07/10 17:50:19 I don't like to leave deleted variables around as
+}
+
+void JavaThread::Stop() {
benm (inactive) 2013/07/10 16:50:06 Do we need a way for java to call this so we can c
Kristian Monsen 2013/07/10 17:50:19 I think it is more the other way round. Currently
+}
+
+void JavaThread::InitializeThread(JNIEnv* env, jobject obj) {
+ // TYPE_UI to get the Android java style message loop
+ message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI));
+ MessageLoopForUI::current()->Start();
+ waitable_event_->Signal();
+}
+
+// static
+bool JavaThread::RegisterBindings(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698