blob: 1709ff4fce8c72bc9619cb87130dd06ceea62949 [file] [log] [blame]
Luis Hector Chavez645501c2016-12-28 10:56:26 -08001// 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
14namespace base {
15
16class MessageLoop;
Luis Hector Chavez21a249e2017-07-26 17:38:05 +000017class WaitableEvent;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080018
19namespace android {
20
21// A Java Thread with a native message loop. To run tasks, post them
22// to the message loop and they will be scheduled along with Java tasks
23// on the thread.
24// This is useful for callbacks where the receiver expects a thread
25// with a prepared Looper.
26class BASE_EXPORT JavaHandlerThread {
27 public:
28 JavaHandlerThread(const char* name);
29 virtual ~JavaHandlerThread();
30
31 base::MessageLoop* message_loop() const { return message_loop_.get(); }
32 void Start();
33 void Stop();
34
35 // Called from java on the newly created thread.
36 // Start() will not return before this methods has finished.
37 void InitializeThread(JNIEnv* env,
38 const JavaParamRef<jobject>& obj,
39 jlong event);
40 void StopThread(JNIEnv* env,
41 const JavaParamRef<jobject>& obj,
42 jlong event);
43
44 static bool RegisterBindings(JNIEnv* env);
45
Jay Civelli8ac91032017-03-29 16:17:00 -070046 private:
Luis Hector Chavez21a249e2017-07-26 17:38:05 +000047 std::unique_ptr<base::MessageLoop> message_loop_;
Luis Hector Chavez645501c2016-12-28 10:56:26 -080048 ScopedJavaGlobalRef<jobject> java_thread_;
49};
50
51} // namespace android
52} // namespace base
53
54#endif // BASE_ANDROID_JAVA_HANDLER_THREAD_H_