| 6 | #define BASE_ANDROID_JAVA_HANDLER_THREAD_H_ |
| 7 |
| 8 | #include <jni.h> |
| 9 |
| 10 | #include <memory> |
| 11 |
| 12 | #include "base/android/scoped_java_ref.h" |
| 13 | #include "base/single_thread_task_runner.h" |
| 14 | #include "base/task/sequence_manager/sequence_manager.h" |
| 15 | #include "base/task/sequence_manager/task_queue.h" |
| 16 | #include "base/threading/thread_task_runner_handle.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | class MessagePumpForUI; |
| 21 |
| 22 | namespace android { |
| 23 |
| 24 | // A Java Thread with a native message loop. To run tasks, post them |
| 25 | // to the message loop and they will be scheduled along with Java tasks |
| 26 | // on the thread. |
| 27 | // This is useful for callbacks where the receiver expects a thread |
| 28 | // with a prepared Looper. |
| 29 | class BASE_EXPORT JavaHandlerThread { |
| 30 | public: |
| 31 | // Create new thread. |
| 32 | explicit JavaHandlerThread( |
| 33 | const char* name, |
| 34 | base::ThreadPriority priority = base::ThreadPriority::NORMAL); |
| 35 | // Wrap and connect to an existing JavaHandlerThread. |
| 36 | // |obj| is an instance of JavaHandlerThread. |
| 37 | explicit JavaHandlerThread( |
| 38 | const char* name, |
| 39 | const base::android::ScopedJavaLocalRef<jobject>& obj); |
| 40 | virtual ~JavaHandlerThread(); |
| 41 |
| 42 | // Gets the TaskRunner associated with the message loop. |
| 43 | // Called from any thread. |
| 44 | scoped_refptr<SingleThreadTaskRunner> task_runner() const { |
| 45 | return state_ ? state_->default_task_queue->task_runner() : nullptr; |
| 46 | } |
| 47 |
| 48 | // Called from the parent thread. |
| 49 | void Start(); |
| 50 | void Stop(); |
| 51 |
| 52 | // Called from java on the newly created thread. |
| 53 | // Start() will not return before this methods has finished. |
| 54 | void InitializeThread(JNIEnv* env, |
| 55 | jlong event); |
| 56 | // Called from java on this thread. |
| 57 | void OnLooperStopped(JNIEnv* env); |
| 58 |
| 59 | // Called from this thread. |
| 60 | void StopSequenceManagerForTesting(); |
| 61 | // Called from this thread. |
| 62 | void JoinForTesting(); |
| 63 |
| 64 | // Called from this thread. |
| 65 | // See comment in JavaHandlerThread.java regarding use of this function. |
| 66 | void ListenForUncaughtExceptionsForTesting(); |
| 67 | // Called from this thread. |
| 68 | ScopedJavaLocalRef<jthrowable> GetUncaughtExceptionIfAny(); |
| 69 |
| 70 | protected: |
| 71 | // Struct exists so JavaHandlerThread destructor can intentionally leak in an |
| 72 | // abort scenario. |
| 73 | struct State { |
| 74 | State(); |
| 75 | ~State(); |
| 76 |
| 77 | std::unique_ptr<sequence_manager::SequenceManager> sequence_manager; |
| 78 | scoped_refptr<sequence_manager::TaskQueue> default_task_queue; |
| 79 | MessagePumpForUI* pump = nullptr; |
| 80 | }; |
| 81 |
| 82 | State* state() const { return state_.get(); } |
| 83 |
| 84 | // Semantically the same as base::Thread#Init(), but unlike base::Thread the |
| 85 | // Android Looper will already be running. This Init() call will still run |
| 86 | // before other tasks are posted to the thread. |
| 87 | virtual void Init() {} |
| 88 |
| 89 | // Semantically the same as base::Thread#CleanUp(), called after the message |
| 90 | // loop ends. The Android Looper will also have been quit by this point. |
| 91 | virtual void CleanUp() {} |
| 92 |
| 93 | std::unique_ptr<State> state_; |
| 94 |
| 95 | private: |
| 96 | void StartMessageLoop(); |
| 97 |
| 98 | void StopOnThread(); |
| 99 | void QuitThreadSafely(); |
| 100 |
| 101 | const char* name_; |
| 102 | ScopedJavaGlobalRef<jobject> java_thread_; |
| 103 | }; |
| 104 |
| 105 | } // namespace android |
| 106 | } // namespace base |
| 107 |
| 108 | #endif // BASE_ANDROID_JAVA_HANDLER_THREAD_H_ |
| 109 |