| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/android/java_handler_thread.h" | 5 #include "base/android/java_handler_thread.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 java_thread_.obj(), | 37 java_thread_.obj(), |
| 38 reinterpret_cast<intptr_t>(this), | 38 reinterpret_cast<intptr_t>(this), |
| 39 reinterpret_cast<intptr_t>(&initialize_event)); | 39 reinterpret_cast<intptr_t>(&initialize_event)); |
| 40 // Wait for thread to be initialized so it is ready to be used when Start | 40 // Wait for thread to be initialized so it is ready to be used when Start |
| 41 // returns. | 41 // returns. |
| 42 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 42 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
| 43 initialize_event.Wait(); | 43 initialize_event.Wait(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void JavaHandlerThread::Stop() { | 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(); |
| 47 } | 56 } |
| 48 | 57 |
| 49 void JavaHandlerThread::InitializeThread(JNIEnv* env, jobject obj, | 58 void JavaHandlerThread::InitializeThread(JNIEnv* env, jobject obj, |
| 50 jlong event) { | 59 jlong event) { |
| 51 // TYPE_JAVA to get the Android java style message loop. | 60 // TYPE_JAVA to get the Android java style message loop. |
| 52 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA)); | 61 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA)); |
| 53 static_cast<MessageLoopForUI*>(message_loop_.get())->Start(); | 62 static_cast<MessageLoopForUI*>(message_loop_.get())->Start(); |
| 54 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); | 63 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
| 55 } | 64 } |
| 56 | 65 |
| 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(); |
| 69 } |
| 70 |
| 57 // static | 71 // static |
| 58 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { | 72 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { |
| 59 return RegisterNativesImpl(env); | 73 return RegisterNativesImpl(env); |
| 60 } | 74 } |
| 61 | 75 |
| 62 } // namespace android | 76 } // namespace android |
| 63 } // namespace base | 77 } // namespace base |
| OLD | NEW |