| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/android/system/base_run_loop.h" | 5 #include "mojo/android/system/base_run_loop.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/base_jni_registrar.h" | 9 #include "base/android/base_jni_registrar.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_registrar.h" | 11 #include "base/android/jni_registrar.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "jni/BaseRunLoop_jni.h" | 14 #include "jni/BaseRunLoop_jni.h" |
| 15 #include "mojo/message_pump/message_pump_mojo.h" | 15 #include "mojo/message_pump/message_pump_mojo.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace android { | 18 namespace android { |
| 19 | 19 |
| 20 static jlong CreateBaseRunLoop(JNIEnv* env, jobject jcaller) { | 20 static jlong CreateBaseRunLoop(JNIEnv* env, |
| 21 const JavaParamRef<jobject>& jcaller) { |
| 21 base::MessageLoop* message_loop = | 22 base::MessageLoop* message_loop = |
| 22 new base::MessageLoop(common::MessagePumpMojo::Create()); | 23 new base::MessageLoop(common::MessagePumpMojo::Create()); |
| 23 return reinterpret_cast<uintptr_t>(message_loop); | 24 return reinterpret_cast<uintptr_t>(message_loop); |
| 24 } | 25 } |
| 25 | 26 |
| 26 static void Run(JNIEnv* env, jobject jcaller, jlong runLoopID) { | 27 static void Run(JNIEnv* env, |
| 28 const JavaParamRef<jobject>& jcaller, |
| 29 jlong runLoopID) { |
| 27 reinterpret_cast<base::MessageLoop*>(runLoopID)->Run(); | 30 reinterpret_cast<base::MessageLoop*>(runLoopID)->Run(); |
| 28 } | 31 } |
| 29 | 32 |
| 30 static void RunUntilIdle(JNIEnv* env, jobject jcaller, jlong runLoopID) { | 33 static void RunUntilIdle(JNIEnv* env, |
| 34 const JavaParamRef<jobject>& jcaller, |
| 35 jlong runLoopID) { |
| 31 reinterpret_cast<base::MessageLoop*>(runLoopID)->RunUntilIdle(); | 36 reinterpret_cast<base::MessageLoop*>(runLoopID)->RunUntilIdle(); |
| 32 } | 37 } |
| 33 | 38 |
| 34 static void Quit(JNIEnv* env, jobject jcaller, jlong runLoopID) { | 39 static void Quit(JNIEnv* env, |
| 40 const JavaParamRef<jobject>& jcaller, |
| 41 jlong runLoopID) { |
| 35 reinterpret_cast<base::MessageLoop*>(runLoopID)->Quit(); | 42 reinterpret_cast<base::MessageLoop*>(runLoopID)->Quit(); |
| 36 } | 43 } |
| 37 | 44 |
| 38 static void RunJavaRunnable( | 45 static void RunJavaRunnable( |
| 39 const base::android::ScopedJavaGlobalRef<jobject>& runnable_ref) { | 46 const base::android::ScopedJavaGlobalRef<jobject>& runnable_ref) { |
| 40 Java_BaseRunLoop_runRunnable(base::android::AttachCurrentThread(), | 47 Java_BaseRunLoop_runRunnable(base::android::AttachCurrentThread(), |
| 41 runnable_ref.obj()); | 48 runnable_ref.obj()); |
| 42 } | 49 } |
| 43 | 50 |
| 44 static void PostDelayedTask(JNIEnv* env, | 51 static void PostDelayedTask(JNIEnv* env, |
| 45 jobject jcaller, | 52 const JavaParamRef<jobject>& jcaller, |
| 46 jlong runLoopID, | 53 jlong runLoopID, |
| 47 jobject runnable, | 54 const JavaParamRef<jobject>& runnable, |
| 48 jlong delay) { | 55 jlong delay) { |
| 49 base::android::ScopedJavaGlobalRef<jobject> runnable_ref; | 56 base::android::ScopedJavaGlobalRef<jobject> runnable_ref; |
| 50 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to | 57 // ScopedJavaGlobalRef do not hold onto the env reference, so it is safe to |
| 51 // use it across threads. |RunJavaRunnable| will acquire a new JNIEnv before | 58 // use it across threads. |RunJavaRunnable| will acquire a new JNIEnv before |
| 52 // running the Runnable. | 59 // running the Runnable. |
| 53 runnable_ref.Reset(env, runnable); | 60 runnable_ref.Reset(env, runnable); |
| 54 reinterpret_cast<base::MessageLoop*>(runLoopID)->PostDelayedTask( | 61 reinterpret_cast<base::MessageLoop*>(runLoopID)->PostDelayedTask( |
| 55 FROM_HERE, base::Bind(&RunJavaRunnable, runnable_ref), | 62 FROM_HERE, base::Bind(&RunJavaRunnable, runnable_ref), |
| 56 base::TimeDelta::FromMicroseconds(delay)); | 63 base::TimeDelta::FromMicroseconds(delay)); |
| 57 } | 64 } |
| 58 | 65 |
| 59 static void DeleteMessageLoop(JNIEnv* env, jobject jcaller, jlong runLoopID) { | 66 static void DeleteMessageLoop(JNIEnv* env, |
| 67 const JavaParamRef<jobject>& jcaller, |
| 68 jlong runLoopID) { |
| 60 base::MessageLoop* message_loop = | 69 base::MessageLoop* message_loop = |
| 61 reinterpret_cast<base::MessageLoop*>(runLoopID); | 70 reinterpret_cast<base::MessageLoop*>(runLoopID); |
| 62 delete message_loop; | 71 delete message_loop; |
| 63 } | 72 } |
| 64 | 73 |
| 65 bool RegisterBaseRunLoop(JNIEnv* env) { | 74 bool RegisterBaseRunLoop(JNIEnv* env) { |
| 66 return RegisterNativesImpl(env); | 75 return RegisterNativesImpl(env); |
| 67 } | 76 } |
| 68 | 77 |
| 69 } // namespace android | 78 } // namespace android |
| 70 } // namespace mojo | 79 } // namespace mojo |
| 71 | 80 |
| 72 | 81 |
| OLD | NEW |