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" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "jni/JavaHandlerThread_jni.h" | 14 #include "jni/JavaHandlerThread_jni.h" |
15 | 15 |
| 16 using base::android::AttachCurrentThread; |
| 17 |
16 namespace base { | 18 namespace base { |
17 | 19 |
18 namespace android { | 20 namespace android { |
19 | 21 |
20 JavaHandlerThread::JavaHandlerThread(const char* name) { | 22 JavaHandlerThread::JavaHandlerThread(const char* name) |
21 JNIEnv* env = base::android::AttachCurrentThread(); | 23 : JavaHandlerThread(Java_JavaHandlerThread_create( |
| 24 AttachCurrentThread(), |
| 25 ConvertUTF8ToJavaString(AttachCurrentThread(), name))) {} |
22 | 26 |
23 java_thread_.Reset( | 27 JavaHandlerThread::JavaHandlerThread( |
24 Java_JavaHandlerThread_create(env, ConvertUTF8ToJavaString(env, name))); | 28 const base::android::ScopedJavaLocalRef<jobject>& obj) |
25 } | 29 : java_thread_(obj) {} |
26 | 30 |
27 JavaHandlerThread::~JavaHandlerThread() { | 31 JavaHandlerThread::~JavaHandlerThread() { |
28 } | 32 } |
29 | 33 |
30 void JavaHandlerThread::Start() { | 34 void JavaHandlerThread::Start() { |
31 // Check the thread has not already been started. | 35 // Check the thread has not already been started. |
32 DCHECK(!message_loop_); | 36 DCHECK(!message_loop_); |
33 | 37 |
34 JNIEnv* env = base::android::AttachCurrentThread(); | 38 JNIEnv* env = base::android::AttachCurrentThread(); |
35 base::WaitableEvent initialize_event( | 39 base::WaitableEvent initialize_event( |
36 WaitableEvent::ResetPolicy::AUTOMATIC, | 40 WaitableEvent::ResetPolicy::AUTOMATIC, |
37 WaitableEvent::InitialState::NOT_SIGNALED); | 41 WaitableEvent::InitialState::NOT_SIGNALED); |
38 Java_JavaHandlerThread_start(env, java_thread_, | 42 Java_JavaHandlerThread_startAndInitialize( |
39 reinterpret_cast<intptr_t>(this), | 43 env, java_thread_, reinterpret_cast<intptr_t>(this), |
40 reinterpret_cast<intptr_t>(&initialize_event)); | 44 reinterpret_cast<intptr_t>(&initialize_event)); |
41 // Wait for thread to be initialized so it is ready to be used when Start | 45 // Wait for thread to be initialized so it is ready to be used when Start |
42 // returns. | 46 // returns. |
43 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 47 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
44 initialize_event.Wait(); | 48 initialize_event.Wait(); |
45 } | 49 } |
46 | 50 |
47 void JavaHandlerThread::Stop() { | 51 void JavaHandlerThread::Stop() { |
48 JNIEnv* env = base::android::AttachCurrentThread(); | 52 JNIEnv* env = base::android::AttachCurrentThread(); |
49 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 53 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
50 WaitableEvent::InitialState::NOT_SIGNALED); | 54 WaitableEvent::InitialState::NOT_SIGNALED); |
(...skipping 29 matching lines...) Expand all Loading... |
80 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitWhenIdle(); | 84 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitWhenIdle(); |
81 } | 85 } |
82 | 86 |
83 // static | 87 // static |
84 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { | 88 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { |
85 return RegisterNativesImpl(env); | 89 return RegisterNativesImpl(env); |
86 } | 90 } |
87 | 91 |
88 } // namespace android | 92 } // namespace android |
89 } // namespace base | 93 } // namespace base |
OLD | NEW |