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