1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/android/java_handler_thread.h"
6
7 #include <jni.h>
8
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "jni/JavaHandlerThread_jni.h"
15
16 namespace base {
17
18 namespace android {
19
JavaHandlerThread(const char * name)20 JavaHandlerThread::JavaHandlerThread(const char* name) {
21 JNIEnv* env = base::android::AttachCurrentThread();
22
23 java_thread_.Reset(
24 Java_JavaHandlerThread_create(env, ConvertUTF8ToJavaString(env, name)));
25 }
26
~JavaHandlerThread()27 JavaHandlerThread::~JavaHandlerThread() {
28 }
29
Start()30 void JavaHandlerThread::Start() {
31 // Check the thread has not already been started.
32 DCHECK(!message_loop_);
33
34 JNIEnv* env = base::android::AttachCurrentThread();
35 base::WaitableEvent initialize_event(
36 WaitableEvent::ResetPolicy::AUTOMATIC,
37 WaitableEvent::InitialState::NOT_SIGNALED);
38 Java_JavaHandlerThread_start(env, java_thread_,
39 reinterpret_cast<intptr_t>(this),
40 reinterpret_cast<intptr_t>(&initialize_event));
41 // Wait for thread to be initialized so it is ready to be used when Start
42 // returns.
43 base::ThreadRestrictions::ScopedAllowWait wait_allowed;
44 initialize_event.Wait();
45 }
46
Stop()47 void JavaHandlerThread::Stop() {
48 JNIEnv* env = base::android::AttachCurrentThread();
49 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC,
50 WaitableEvent::InitialState::NOT_SIGNALED);
51 Java_JavaHandlerThread_stop(env, java_thread_,
52 reinterpret_cast<intptr_t>(this),
53 reinterpret_cast<intptr_t>(&shutdown_event));
54 // Wait for thread to shut down before returning.
55 base::ThreadRestrictions::ScopedAllowWait wait_allowed;
56 shutdown_event.Wait();
57 }
58
InitializeThread(JNIEnv * env,const JavaParamRef<jobject> & obj,jlong event)59 void JavaHandlerThread::InitializeThread(JNIEnv* env,
60 const JavaParamRef<jobject>& obj,
61 jlong event) {
62 // TYPE_JAVA to get the Android java style message loop.
63 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA));
64 StartMessageLoop();
65 reinterpret_cast<base::WaitableEvent*>(event)->Signal();
66 }
67
StopThread(JNIEnv * env,const JavaParamRef<jobject> & obj,jlong event)68 void JavaHandlerThread::StopThread(JNIEnv* env,
69 const JavaParamRef<jobject>& obj,
70 jlong event) {
71 StopMessageLoop();
72 reinterpret_cast<base::WaitableEvent*>(event)->Signal();
73 }
74
StartMessageLoop()75 void JavaHandlerThread::StartMessageLoop() {
76 static_cast<MessageLoopForUI*>(message_loop_.get())->Start();
77 }
78
StopMessageLoop()79 void JavaHandlerThread::StopMessageLoop() {
80 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitWhenIdle();
81 }
82
83 // static
RegisterBindings(JNIEnv * env)84 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) {
85 return RegisterNativesImpl(env);
86 }
87
88 } // namespace android
89 } // namespace base
90