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 17 matching lines...) Expand all Loading... |
28 } | 28 } |
29 | 29 |
30 void JavaHandlerThread::Start() { | 30 void JavaHandlerThread::Start() { |
31 // Check the thread has not already been started. | 31 // Check the thread has not already been started. |
32 DCHECK(!message_loop_); | 32 DCHECK(!message_loop_); |
33 | 33 |
34 JNIEnv* env = base::android::AttachCurrentThread(); | 34 JNIEnv* env = base::android::AttachCurrentThread(); |
35 base::WaitableEvent initialize_event( | 35 base::WaitableEvent initialize_event( |
36 WaitableEvent::ResetPolicy::AUTOMATIC, | 36 WaitableEvent::ResetPolicy::AUTOMATIC, |
37 WaitableEvent::InitialState::NOT_SIGNALED); | 37 WaitableEvent::InitialState::NOT_SIGNALED); |
38 Java_JavaHandlerThread_start(env, | 38 Java_JavaHandlerThread_start(env, java_thread_.obj(), |
39 java_thread_.obj(), | |
40 reinterpret_cast<intptr_t>(this), | 39 reinterpret_cast<intptr_t>(this), |
41 reinterpret_cast<intptr_t>(&initialize_event)); | 40 reinterpret_cast<intptr_t>(&initialize_event)); |
42 // Wait for thread to be initialized so it is ready to be used when Start | 41 // Wait for thread to be initialized so it is ready to be used when Start |
43 // returns. | 42 // returns. |
44 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 43 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
45 initialize_event.Wait(); | 44 initialize_event.Wait(); |
46 } | 45 } |
47 | 46 |
| 47 void JavaHandlerThread::StartForTesting(base::WaitableEvent* test_done_event) { |
| 48 // Check the thread has not already been started. |
| 49 DCHECK(!message_loop_); |
| 50 |
| 51 JNIEnv* env = base::android::AttachCurrentThread(); |
| 52 base::WaitableEvent initialize_event( |
| 53 WaitableEvent::ResetPolicy::AUTOMATIC, |
| 54 WaitableEvent::InitialState::NOT_SIGNALED); |
| 55 Java_JavaHandlerThread_startForTesting( |
| 56 env, java_thread_.obj(), reinterpret_cast<intptr_t>(this), |
| 57 reinterpret_cast<intptr_t>(&initialize_event), |
| 58 reinterpret_cast<intptr_t>(test_done_event)); |
| 59 // Wait for thread to be initialized so it is ready to be used when Start |
| 60 // returns. |
| 61 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
| 62 initialize_event.Wait(); |
| 63 } |
| 64 |
48 void JavaHandlerThread::Stop() { | 65 void JavaHandlerThread::Stop() { |
49 JNIEnv* env = base::android::AttachCurrentThread(); | 66 JNIEnv* env = base::android::AttachCurrentThread(); |
50 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 67 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
51 WaitableEvent::InitialState::NOT_SIGNALED); | 68 WaitableEvent::InitialState::NOT_SIGNALED); |
52 Java_JavaHandlerThread_stop(env, | 69 Java_JavaHandlerThread_stop(env, |
53 java_thread_.obj(), | 70 java_thread_.obj(), |
54 reinterpret_cast<intptr_t>(this), | 71 reinterpret_cast<intptr_t>(this), |
55 reinterpret_cast<intptr_t>(&shutdown_event)); | 72 reinterpret_cast<intptr_t>(&shutdown_event)); |
56 // Wait for thread to shut down before returning. | 73 // Wait for thread to shut down before returning. |
57 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 74 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
58 shutdown_event.Wait(); | 75 shutdown_event.Wait(); |
59 } | 76 } |
60 | 77 |
| 78 void JavaHandlerThread::StopForTesting() { |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 81 WaitableEvent::InitialState::NOT_SIGNALED); |
| 82 Java_JavaHandlerThread_stopForTesting( |
| 83 env, java_thread_.obj(), reinterpret_cast<intptr_t>(this), |
| 84 reinterpret_cast<intptr_t>(&shutdown_event)); |
| 85 // Wait for thread to shut down before returning. |
| 86 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
| 87 shutdown_event.Wait(); |
| 88 } |
| 89 |
61 void JavaHandlerThread::InitializeThread(JNIEnv* env, | 90 void JavaHandlerThread::InitializeThread(JNIEnv* env, |
62 const JavaParamRef<jobject>& obj, | 91 const JavaParamRef<jobject>& obj, |
63 jlong event) { | 92 jlong event) { |
64 // TYPE_JAVA to get the Android java style message loop. | 93 // TYPE_JAVA to get the Android java style message loop. |
65 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA)); | 94 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA)); |
66 static_cast<MessageLoopForUI*>(message_loop_.get())->Start(); | 95 static_cast<MessageLoopForUI*>(message_loop_.get())->Start(); |
67 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); | 96 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
68 } | 97 } |
69 | 98 |
| 99 void JavaHandlerThread::InitializeThreadForTesting( |
| 100 JNIEnv* env, |
| 101 const JavaParamRef<jobject>& obj, |
| 102 jlong event, |
| 103 jlong test_done_event) { |
| 104 // TYPE_JAVA to get the Android java style message loop. |
| 105 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_JAVA)); |
| 106 static_cast<MessageLoopForUI*>(message_loop_.get()) |
| 107 ->StartForTesting( |
| 108 reinterpret_cast<base::WaitableEvent*>(test_done_event)); |
| 109 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
| 110 } |
| 111 |
70 void JavaHandlerThread::StopThread(JNIEnv* env, | 112 void JavaHandlerThread::StopThread(JNIEnv* env, |
71 const JavaParamRef<jobject>& obj, | 113 const JavaParamRef<jobject>& obj, |
72 jlong event) { | 114 jlong event) { |
73 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitWhenIdle(); | 115 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitWhenIdle(); |
74 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); | 116 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
75 } | 117 } |
76 | 118 |
| 119 void JavaHandlerThread::StopThreadForTesting(JNIEnv* env, |
| 120 const JavaParamRef<jobject>& obj, |
| 121 jlong event) { |
| 122 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitNow(); |
| 123 // The message loop must be destroyed on the thread it is attached to. |
| 124 message_loop_.reset(); |
| 125 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
| 126 } |
| 127 |
77 // static | 128 // static |
78 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { | 129 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { |
79 return RegisterNativesImpl(env); | 130 return RegisterNativesImpl(env); |
80 } | 131 } |
81 | 132 |
82 } // namespace android | 133 } // namespace android |
83 } // namespace base | 134 } // namespace base |
OLD | NEW |