• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_ANDROID_JAVA_HANDLER_THREAD_H_
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/message_loop/message_loop.h"
14 #include "base/single_thread_task_runner.h"
15 
16 namespace base {
17 
18 class MessageLoop;
19 
20 namespace android {
21 
22 // A Java Thread with a native message loop. To run tasks, post them
23 // to the message loop and they will be scheduled along with Java tasks
24 // on the thread.
25 // This is useful for callbacks where the receiver expects a thread
26 // with a prepared Looper.
27 class BASE_EXPORT JavaHandlerThread {
28  public:
29   // Create new thread.
30   explicit JavaHandlerThread(
31       const char* name,
32       base::ThreadPriority priority = base::ThreadPriority::NORMAL);
33   // Wrap and connect to an existing JavaHandlerThread.
34   // |obj| is an instance of JavaHandlerThread.
35   explicit JavaHandlerThread(
36       const base::android::ScopedJavaLocalRef<jobject>& obj);
37   virtual ~JavaHandlerThread();
38 
39   // Called from any thread.
message_loop()40   base::MessageLoop* message_loop() const { return message_loop_.get(); }
41 
42   // Gets the TaskRunner associated with the message loop.
43   // Called from any thread.
task_runner()44   scoped_refptr<SingleThreadTaskRunner> task_runner() const {
45     return message_loop_ ? message_loop_->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                         const JavaParamRef<jobject>& obj,
56                         jlong event);
57   // Called from java on this thread.
58   void OnLooperStopped(JNIEnv* env, const JavaParamRef<jobject>& obj);
59 
60   // Called from this thread.
61   void StopMessageLoopForTesting();
62   // Called from this thread.
63   void JoinForTesting();
64 
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();
70 
71  protected:
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.
Init()75   virtual void Init() {}
76 
77   // Semantically the same as base::Thread#CleanUp(), called after the message
78   // loop ends. The Android Looper will also have been quit by this point.
CleanUp()79   virtual void CleanUp() {}
80 
81   std::unique_ptr<base::MessageLoopForUI> message_loop_;
82 
83  private:
84   void StartMessageLoop();
85 
86   void StopOnThread();
87   void QuitThreadSafely();
88 
89   ScopedJavaGlobalRef<jobject> java_thread_;
90 };
91 
92 }  // namespace android
93 }  // namespace base
94 
95 #endif  // BASE_ANDROID_JAVA_HANDLER_THREAD_H_
96