1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_ANDROID_JAVA_HANDLER_THREAD_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_ANDROID_JAVA_HANDLER_THREAD_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <jni.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <memory> 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker #include "base/android/scoped_java_ref.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h" 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker namespace base { 17*635a8641SAndroid Build Coastguard Worker 18*635a8641SAndroid Build Coastguard Worker class MessageLoop; 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Worker namespace android { 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard Worker // A Java Thread with a native message loop. To run tasks, post them 23*635a8641SAndroid Build Coastguard Worker // to the message loop and they will be scheduled along with Java tasks 24*635a8641SAndroid Build Coastguard Worker // on the thread. 25*635a8641SAndroid Build Coastguard Worker // This is useful for callbacks where the receiver expects a thread 26*635a8641SAndroid Build Coastguard Worker // with a prepared Looper. 27*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT JavaHandlerThread { 28*635a8641SAndroid Build Coastguard Worker public: 29*635a8641SAndroid Build Coastguard Worker // Create new thread. 30*635a8641SAndroid Build Coastguard Worker explicit JavaHandlerThread( 31*635a8641SAndroid Build Coastguard Worker const char* name, 32*635a8641SAndroid Build Coastguard Worker base::ThreadPriority priority = base::ThreadPriority::NORMAL); 33*635a8641SAndroid Build Coastguard Worker // Wrap and connect to an existing JavaHandlerThread. 34*635a8641SAndroid Build Coastguard Worker // |obj| is an instance of JavaHandlerThread. 35*635a8641SAndroid Build Coastguard Worker explicit JavaHandlerThread( 36*635a8641SAndroid Build Coastguard Worker const base::android::ScopedJavaLocalRef<jobject>& obj); 37*635a8641SAndroid Build Coastguard Worker virtual ~JavaHandlerThread(); 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker // Called from any thread. message_loop()40*635a8641SAndroid Build Coastguard Worker base::MessageLoop* message_loop() const { return message_loop_.get(); } 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker // Gets the TaskRunner associated with the message loop. 43*635a8641SAndroid Build Coastguard Worker // Called from any thread. task_runner()44*635a8641SAndroid Build Coastguard Worker scoped_refptr<SingleThreadTaskRunner> task_runner() const { 45*635a8641SAndroid Build Coastguard Worker return message_loop_ ? message_loop_->task_runner() : nullptr; 46*635a8641SAndroid Build Coastguard Worker } 47*635a8641SAndroid Build Coastguard Worker 48*635a8641SAndroid Build Coastguard Worker // Called from the parent thread. 49*635a8641SAndroid Build Coastguard Worker void Start(); 50*635a8641SAndroid Build Coastguard Worker void Stop(); 51*635a8641SAndroid Build Coastguard Worker 52*635a8641SAndroid Build Coastguard Worker // Called from java on the newly created thread. 53*635a8641SAndroid Build Coastguard Worker // Start() will not return before this methods has finished. 54*635a8641SAndroid Build Coastguard Worker void InitializeThread(JNIEnv* env, 55*635a8641SAndroid Build Coastguard Worker const JavaParamRef<jobject>& obj, 56*635a8641SAndroid Build Coastguard Worker jlong event); 57*635a8641SAndroid Build Coastguard Worker // Called from java on this thread. 58*635a8641SAndroid Build Coastguard Worker void OnLooperStopped(JNIEnv* env, const JavaParamRef<jobject>& obj); 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker // Called from this thread. 61*635a8641SAndroid Build Coastguard Worker void StopMessageLoopForTesting(); 62*635a8641SAndroid Build Coastguard Worker // Called from this thread. 63*635a8641SAndroid Build Coastguard Worker void JoinForTesting(); 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker // Called from this thread. 66*635a8641SAndroid Build Coastguard Worker // See comment in JavaHandlerThread.java regarding use of this function. 67*635a8641SAndroid Build Coastguard Worker void ListenForUncaughtExceptionsForTesting(); 68*635a8641SAndroid Build Coastguard Worker // Called from this thread. 69*635a8641SAndroid Build Coastguard Worker ScopedJavaLocalRef<jthrowable> GetUncaughtExceptionIfAny(); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker protected: 72*635a8641SAndroid Build Coastguard Worker // Semantically the same as base::Thread#Init(), but unlike base::Thread the 73*635a8641SAndroid Build Coastguard Worker // Android Looper will already be running. This Init() call will still run 74*635a8641SAndroid Build Coastguard Worker // before other tasks are posted to the thread. Init()75*635a8641SAndroid Build Coastguard Worker virtual void Init() {} 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker // Semantically the same as base::Thread#CleanUp(), called after the message 78*635a8641SAndroid Build Coastguard Worker // loop ends. The Android Looper will also have been quit by this point. CleanUp()79*635a8641SAndroid Build Coastguard Worker virtual void CleanUp() {} 80*635a8641SAndroid Build Coastguard Worker 81*635a8641SAndroid Build Coastguard Worker std::unique_ptr<base::MessageLoopForUI> message_loop_; 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker private: 84*635a8641SAndroid Build Coastguard Worker void StartMessageLoop(); 85*635a8641SAndroid Build Coastguard Worker 86*635a8641SAndroid Build Coastguard Worker void StopOnThread(); 87*635a8641SAndroid Build Coastguard Worker void QuitThreadSafely(); 88*635a8641SAndroid Build Coastguard Worker 89*635a8641SAndroid Build Coastguard Worker ScopedJavaGlobalRef<jobject> java_thread_; 90*635a8641SAndroid Build Coastguard Worker }; 91*635a8641SAndroid Build Coastguard Worker 92*635a8641SAndroid Build Coastguard Worker } // namespace android 93*635a8641SAndroid Build Coastguard Worker } // namespace base 94*635a8641SAndroid Build Coastguard Worker 95*635a8641SAndroid Build Coastguard Worker #endif // BASE_ANDROID_JAVA_HANDLER_THREAD_H_ 96