Roll src/third_party/skia b3eb687:c6f3e2c
[chromium-blink-merge.git] / base / android / java_handler_thread.cc
blob117971eda5d0674468e96325d8da9286c9f20088
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.
5 #include "base/android/java_handler_thread.h"
7 #include <jni.h>
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"
16 namespace base {
18 namespace android {
20 JavaHandlerThread::JavaHandlerThread(const char* name) {
21 JNIEnv* env = base::android::AttachCurrentThread();
23 java_thread_.Reset(Java_JavaHandlerThread_create(
24 env, ConvertUTF8ToJavaString(env, name).Release()));
27 JavaHandlerThread::~JavaHandlerThread() {
30 void JavaHandlerThread::Start() {
31 // Check the thread has not already been started.
32 DCHECK(!message_loop_);
34 JNIEnv* env = base::android::AttachCurrentThread();
35 base::WaitableEvent initialize_event(false, false);
36 Java_JavaHandlerThread_start(env,
37 java_thread_.obj(),
38 reinterpret_cast<intptr_t>(this),
39 reinterpret_cast<intptr_t>(&initialize_event));
40 // Wait for thread to be initialized so it is ready to be used when Start
41 // returns.
42 base::ThreadRestrictions::ScopedAllowWait wait_allowed;
43 initialize_event.Wait();
46 void JavaHandlerThread::Stop() {
47 JNIEnv* env = base::android::AttachCurrentThread();
48 base::WaitableEvent shutdown_event(false, false);
49 Java_JavaHandlerThread_stop(env,
50 java_thread_.obj(),
51 reinterpret_cast<intptr_t>(this),
52 reinterpret_cast<intptr_t>(&shutdown_event));
53 // Wait for thread to shut down before returning.
54 base::ThreadRestrictions::ScopedAllowWait wait_allowed;
55 shutdown_event.Wait();
58 void JavaHandlerThread::InitializeThread(JNIEnv* env, jobject obj,
59 jlong event) {
60 // TYPE_JAVA to get the Android java style message loop.
61 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA));
62 static_cast<MessageLoopForUI*>(message_loop_.get())->Start();
63 reinterpret_cast<base::WaitableEvent*>(event)->Signal();
66 void JavaHandlerThread::StopThread(JNIEnv* env, jobject obj, jlong event) {
67 static_cast<MessageLoopForUI*>(message_loop_.get())->Quit();
68 reinterpret_cast<base::WaitableEvent*>(event)->Signal();
71 // static
72 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) {
73 return RegisterNativesImpl(env);
76 } // namespace android
77 } // namespace base