1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_ANDROID_JAVA_HANDLER_THREAD_H_
6 #define BASE_ANDROID_JAVA_HANDLER_THREAD_H_
12 #include "base/android/scoped_java_ref.h"
13 #include "base/dcheck_is_on.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/task/sequence_manager/sequence_manager.h"
16 #include "base/task/sequence_manager/task_queue.h"
17 #include "base/task/single_thread_task_runner.h"
21 class MessagePumpForUI;
25 // A Java Thread with a native message loop. To run tasks, post them
26 // to the message loop and they will be scheduled along with Java tasks
28 // This is useful for callbacks where the receiver expects a thread
29 // with a prepared Looper.
30 class BASE_EXPORT JavaHandlerThread {
33 explicit JavaHandlerThread(
35 base::ThreadType thread_type = base::ThreadType::kDefault);
36 // Wrap and connect to an existing JavaHandlerThread.
37 // |obj| is an instance of JavaHandlerThread.
38 explicit JavaHandlerThread(
40 const base::android::ScopedJavaLocalRef<jobject>& obj);
41 virtual ~JavaHandlerThread();
43 // Gets the TaskRunner associated with the message loop.
44 // Called from any thread.
45 scoped_refptr<SingleThreadTaskRunner> task_runner() const {
46 return state_ ? state_->default_task_queue->task_runner() : nullptr;
49 // Called from the parent thread.
53 // Called from java on the newly created thread.
54 // Start() will not return before this methods has finished.
55 void InitializeThread(JNIEnv* env,
57 // Called from java on this thread.
58 void OnLooperStopped(JNIEnv* env);
60 // Called from this thread.
61 void StopSequenceManagerForTesting();
62 // Called from this thread.
63 void JoinForTesting();
65 // Called from this thread.
66 // See comment in JavaHandlerThread.java regarding use of this function.
67 void ListenForUncaughtExceptionsForTesting();
68 // Called from this thread.
69 ScopedJavaLocalRef<jthrowable> GetUncaughtExceptionIfAny();
71 // Returns the thread ID. Should not be called before the first Start*()
72 // call. This method is thread-safe.
73 PlatformThreadId GetThreadId() const;
76 // Struct exists so JavaHandlerThread destructor can intentionally leak in an
82 std::unique_ptr<sequence_manager::SequenceManager> sequence_manager;
83 sequence_manager::TaskQueue::Handle default_task_queue;
84 raw_ptr<MessagePumpForUI> pump = nullptr;
87 State* state() const { return state_.get(); }
89 // Semantically the same as base::Thread#Init(), but unlike base::Thread the
90 // Android Looper will already be running. This Init() call will still run
91 // before other tasks are posted to the thread.
92 virtual void Init() {}
94 // Semantically the same as base::Thread#CleanUp(), called after the message
95 // loop ends. The Android Looper will also have been quit by this point.
96 virtual void CleanUp() {}
98 std::unique_ptr<State> state_;
101 void StartMessageLoop();
104 void QuitThreadSafely();
107 base::PlatformThreadId thread_id_{};
108 ScopedJavaGlobalRef<jobject> java_thread_;
110 bool initialized_ = false;
114 } // namespace android
117 #endif // BASE_ANDROID_JAVA_HANDLER_THREAD_H_