OLD | NEW |
---|---|
(Empty) | |
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 "mojo/android/system/base_run_loop.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/base_jni_registrar.h" | |
10 #include "base/android/jni_android.h" | |
11 #include "base/android/jni_registrar.h" | |
12 #include "base/bind.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "jni/BaseRunLoop_jni.h" | |
15 #include "mojo/common/message_pump_mojo.h" | |
16 | |
17 namespace mojo { | |
18 namespace android { | |
19 | |
20 static jlong CreateBaseRunLoop(JNIEnv* env, jobject jcaller) { | |
21 base::MessageLoop* message_loop = | |
22 new base::MessageLoop(common::MessagePumpMojo::Create()); | |
23 return reinterpret_cast<uintptr_t>(message_loop); | |
24 } | |
25 | |
26 static void Run(JNIEnv* env, jobject jcaller, jlong runLoopID) { | |
27 reinterpret_cast<base::MessageLoop*>(runLoopID)->Run(); | |
28 } | |
29 | |
30 static void RunUntilIdle(JNIEnv* env, jobject jcaller, jlong runLoopID) { | |
31 reinterpret_cast<base::MessageLoop*>(runLoopID)->RunUntilIdle(); | |
32 } | |
33 | |
34 static void Quit(JNIEnv* env, jobject jcaller, jlong runLoopID) { | |
35 reinterpret_cast<base::MessageLoop*>(runLoopID)->Quit(); | |
36 } | |
37 | |
38 static void RunJavaRunnable( | |
39 base::android::ScopedJavaGlobalRef<jobject>* runnable_ref) { | |
40 Java_BaseRunLoop_runRunnable(base::android::AttachCurrentThread(), | |
41 runnable_ref->Release()); | |
42 delete runnable_ref; | |
43 } | |
44 | |
45 static void PostDelayedTask(JNIEnv* env, | |
46 jobject jcaller, | |
47 jlong runLoopID, | |
48 jobject runnable, | |
49 jlong delay) { | |
50 base::android::ScopedJavaGlobalRef<jobject>* runnable_ref = | |
51 new base::android::ScopedJavaGlobalRef<jobject>(); | |
qsr
2015/02/09 15:54:52
Look if you can either directly pass a ScopedJavaG
etiennej
2015/02/09 16:41:29
Done.
| |
52 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to | |
53 // use it across threads. |RunJavaRunnable| will acquire a new JNIEnv before | |
54 // running the Runnable. | |
55 runnable_ref->Reset(env, runnable); | |
56 reinterpret_cast<base::MessageLoop*>(runLoopID) | |
57 ->PostDelayedTask(FROM_HERE, base::Bind(&RunJavaRunnable, runnable_ref), | |
58 base::TimeDelta::FromMicroseconds(delay)); | |
59 } | |
60 | |
61 static void DeleteMessageLoop(JNIEnv* env, jobject jcaller, jlong runLoopID) { | |
62 base::MessageLoop* message_loop = | |
63 reinterpret_cast<base::MessageLoop*>(runLoopID); | |
64 delete message_loop; | |
65 } | |
66 | |
67 bool RegisterBaseRunLoop(JNIEnv* env) { | |
68 return RegisterNativesImpl(env); | |
69 } | |
70 | |
71 } // namespace android | |
72 } // namespace mojo | |
73 | |
74 | |
OLD | NEW |