| Index: base/android/java_handler_thread.cc
|
| diff --git a/base/android/java_handler_thread.cc b/base/android/java_handler_thread.cc
|
| index 75270348e6ee15756daa12faf09303d53b5b903a..edd9bfe196d466ab654ca29c6b6f3fcfb2832ca8 100644
|
| --- a/base/android/java_handler_thread.cc
|
| +++ b/base/android/java_handler_thread.cc
|
| @@ -35,8 +35,7 @@ void JavaHandlerThread::Start() {
|
| base::WaitableEvent initialize_event(
|
| WaitableEvent::ResetPolicy::AUTOMATIC,
|
| WaitableEvent::InitialState::NOT_SIGNALED);
|
| - Java_JavaHandlerThread_start(env,
|
| - java_thread_.obj(),
|
| + Java_JavaHandlerThread_start(env, java_thread_.obj(),
|
| reinterpret_cast<intptr_t>(this),
|
| reinterpret_cast<intptr_t>(&initialize_event));
|
| // Wait for thread to be initialized so it is ready to be used when Start
|
| @@ -45,6 +44,24 @@ void JavaHandlerThread::Start() {
|
| initialize_event.Wait();
|
| }
|
|
|
| +void JavaHandlerThread::StartForTesting(base::WaitableEvent* test_done_event) {
|
| + // Check the thread has not already been started.
|
| + DCHECK(!message_loop_);
|
| +
|
| + JNIEnv* env = base::android::AttachCurrentThread();
|
| + base::WaitableEvent initialize_event(
|
| + WaitableEvent::ResetPolicy::AUTOMATIC,
|
| + WaitableEvent::InitialState::NOT_SIGNALED);
|
| + Java_JavaHandlerThread_startForTesting(
|
| + env, java_thread_.obj(), reinterpret_cast<intptr_t>(this),
|
| + reinterpret_cast<intptr_t>(&initialize_event),
|
| + reinterpret_cast<intptr_t>(test_done_event));
|
| + // Wait for thread to be initialized so it is ready to be used when Start
|
| + // returns.
|
| + base::ThreadRestrictions::ScopedAllowWait wait_allowed;
|
| + initialize_event.Wait();
|
| +}
|
| +
|
| void JavaHandlerThread::Stop() {
|
| JNIEnv* env = base::android::AttachCurrentThread();
|
| base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC,
|
| @@ -58,6 +75,18 @@ void JavaHandlerThread::Stop() {
|
| shutdown_event.Wait();
|
| }
|
|
|
| +void JavaHandlerThread::StopForTesting() {
|
| + JNIEnv* env = base::android::AttachCurrentThread();
|
| + base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC,
|
| + WaitableEvent::InitialState::NOT_SIGNALED);
|
| + Java_JavaHandlerThread_stopForTesting(
|
| + env, java_thread_.obj(), reinterpret_cast<intptr_t>(this),
|
| + reinterpret_cast<intptr_t>(&shutdown_event));
|
| + // Wait for thread to shut down before returning.
|
| + base::ThreadRestrictions::ScopedAllowWait wait_allowed;
|
| + shutdown_event.Wait();
|
| +}
|
| +
|
| void JavaHandlerThread::InitializeThread(JNIEnv* env,
|
| const JavaParamRef<jobject>& obj,
|
| jlong event) {
|
| @@ -67,6 +96,19 @@ void JavaHandlerThread::InitializeThread(JNIEnv* env,
|
| reinterpret_cast<base::WaitableEvent*>(event)->Signal();
|
| }
|
|
|
| +void JavaHandlerThread::InitializeThreadForTesting(
|
| + JNIEnv* env,
|
| + const JavaParamRef<jobject>& obj,
|
| + jlong event,
|
| + jlong test_done_event) {
|
| + // TYPE_JAVA to get the Android java style message loop.
|
| + message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA));
|
| + static_cast<MessageLoopForUI*>(message_loop_.get())
|
| + ->StartForTesting(
|
| + reinterpret_cast<base::WaitableEvent*>(test_done_event));
|
| + reinterpret_cast<base::WaitableEvent*>(event)->Signal();
|
| +}
|
| +
|
| void JavaHandlerThread::StopThread(JNIEnv* env,
|
| const JavaParamRef<jobject>& obj,
|
| jlong event) {
|
| @@ -74,6 +116,15 @@ void JavaHandlerThread::StopThread(JNIEnv* env,
|
| reinterpret_cast<base::WaitableEvent*>(event)->Signal();
|
| }
|
|
|
| +void JavaHandlerThread::StopThreadForTesting(JNIEnv* env,
|
| + const JavaParamRef<jobject>& obj,
|
| + jlong event) {
|
| + static_cast<MessageLoopForUI*>(message_loop_.get())->QuitNow();
|
| + // The message loop must be destroyed on the thread it is attached to.
|
| + message_loop_.reset();
|
| + reinterpret_cast<base::WaitableEvent*>(event)->Signal();
|
| +}
|
| +
|
| // static
|
| bool JavaHandlerThread::RegisterBindings(JNIEnv* env) {
|
| return RegisterNativesImpl(env);
|
|
|