java_handler_thread.h 3.3 KB

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