java_handler_thread.h 3.6 KB

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