[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 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. | ||||
4 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 5 | #ifndef BASE_ANDROID_JAVA_HANDLER_THREAD_H_ |
6 | #define BASE_ANDROID_JAVA_HANDLER_THREAD_H_ | ||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 7 | |
8 | #include <jni.h> | ||||
9 | |||||
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 10 | #include <memory> |
11 | |||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 12 | #include "base/android/scoped_java_ref.h" |
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 13 | |
14 | namespace base { | ||||
15 | |||||
16 | class MessageLoop; | ||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 17 | |
18 | namespace android { | ||||
19 | |||||
20 | // A Java Thread with a native message loop. To run tasks, post them | ||||
21 | // to the message loop and they will be scheduled along with Java tasks | ||||
22 | // on the thread. | ||||
23 | // This is useful for callbacks where the receiver expects a thread | ||||
24 | // with a prepared Looper. | ||||
25 | class BASE_EXPORT JavaHandlerThread { | ||||
26 | public: | ||||
27 | JavaHandlerThread(const char* name); | ||||
28 | virtual ~JavaHandlerThread(); | ||||
29 | |||||
30 | base::MessageLoop* message_loop() const { return message_loop_.get(); } | ||||
31 | void Start(); | ||||
32 | void Stop(); | ||||
33 | |||||
34 | // Called from java on the newly created thread. | ||||
35 | // Start() will not return before this methods has finished. | ||||
torne | 70f3fac9 | 2015-12-02 16:31:18 | [diff] [blame] | 36 | void InitializeThread(JNIEnv* env, |
37 | const JavaParamRef<jobject>& obj, | ||||
38 | jlong event); | ||||
39 | void StopThread(JNIEnv* env, | ||||
40 | const JavaParamRef<jobject>& obj, | ||||
41 | jlong event); | ||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 42 | |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 43 | virtual void StartMessageLoop(); |
44 | virtual void StopMessageLoop(); | ||||
45 | |||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 46 | static bool RegisterBindings(JNIEnv* env); |
47 | |||||
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 48 | protected: |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 49 | std::unique_ptr<base::MessageLoop> message_loop_; |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 50 | |
51 | private: | ||||
[email protected] | 349ad58 | 2013-08-08 01:31:52 | [diff] [blame] | 52 | ScopedJavaGlobalRef<jobject> java_thread_; |
53 | }; | ||||
54 | |||||
55 | } // namespace android | ||||
56 | } // namespace base | ||||
57 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 58 | #endif // BASE_ANDROID_JAVA_HANDLER_THREAD_H_ |