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