1 // Copyright 2015 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 #include <jni.h>
6
7 #include "base/android/jni_android.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "jni/BaseRunLoop_jni.h"
15
16 using base::android::JavaParamRef;
17
18 namespace mojo {
19 namespace android {
20
JNI_BaseRunLoop_CreateBaseRunLoop(JNIEnv * env,const JavaParamRef<jobject> & jcaller)21 static jlong JNI_BaseRunLoop_CreateBaseRunLoop(
22 JNIEnv* env,
23 const JavaParamRef<jobject>& jcaller) {
24 base::MessageLoop* message_loop = new base::MessageLoop;
25 return reinterpret_cast<uintptr_t>(message_loop);
26 }
27
JNI_BaseRunLoop_Run(JNIEnv * env,const JavaParamRef<jobject> & jcaller)28 static void JNI_BaseRunLoop_Run(JNIEnv* env,
29 const JavaParamRef<jobject>& jcaller) {
30 base::RunLoop().Run();
31 }
32
JNI_BaseRunLoop_RunUntilIdle(JNIEnv * env,const JavaParamRef<jobject> & jcaller)33 static void JNI_BaseRunLoop_RunUntilIdle(JNIEnv* env,
34 const JavaParamRef<jobject>& jcaller) {
35 base::RunLoop().RunUntilIdle();
36 }
37
JNI_BaseRunLoop_Quit(JNIEnv * env,const JavaParamRef<jobject> & jcaller)38 static void JNI_BaseRunLoop_Quit(JNIEnv* env,
39 const JavaParamRef<jobject>& jcaller) {
40 base::RunLoop::QuitCurrentWhenIdleDeprecated();
41 }
42
RunJavaRunnable(const base::android::ScopedJavaGlobalRef<jobject> & runnable_ref)43 static void RunJavaRunnable(
44 const base::android::ScopedJavaGlobalRef<jobject>& runnable_ref) {
45 Java_BaseRunLoop_runRunnable(base::android::AttachCurrentThread(),
46 runnable_ref);
47 }
48
JNI_BaseRunLoop_PostDelayedTask(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong runLoopID,const JavaParamRef<jobject> & runnable,jlong delay)49 static void JNI_BaseRunLoop_PostDelayedTask(
50 JNIEnv* env,
51 const JavaParamRef<jobject>& jcaller,
52 jlong runLoopID,
53 const JavaParamRef<jobject>& runnable,
54 jlong delay) {
55 base::android::ScopedJavaGlobalRef<jobject> runnable_ref;
56 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to
57 // use it across threads. |RunJavaRunnable| will acquire a new JNIEnv before
58 // running the Runnable.
59 runnable_ref.Reset(env, runnable);
60 reinterpret_cast<base::MessageLoop*>(runLoopID)
61 ->task_runner()
62 ->PostDelayedTask(FROM_HERE,
63 base::BindOnce(&RunJavaRunnable, runnable_ref),
64 base::TimeDelta::FromMicroseconds(delay));
65 }
66
JNI_BaseRunLoop_DeleteMessageLoop(JNIEnv * env,const JavaParamRef<jobject> & jcaller,jlong runLoopID)67 static void JNI_BaseRunLoop_DeleteMessageLoop(
68 JNIEnv* env,
69 const JavaParamRef<jobject>& jcaller,
70 jlong runLoopID) {
71 base::MessageLoop* message_loop =
72 reinterpret_cast<base::MessageLoop*>(runLoopID);
73 delete message_loop;
74 }
75
76 } // namespace android
77 } // namespace mojo
78