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