Chromium Code Reviews| Index: mojo/android/system/core_impl.cc |
| diff --git a/mojo/android/system/core_impl.cc b/mojo/android/system/core_impl.cc |
| index d7552c1a291fc3779f5ebda410323684b95634cb..b3964df3f641fc6f84a74ec450c821fb1fa132a9 100644 |
| --- a/mojo/android/system/core_impl.cc |
| +++ b/mojo/android/system/core_impl.cc |
| @@ -8,10 +8,36 @@ |
| #include "base/android/jni_android.h" |
| #include "base/android/jni_registrar.h" |
| #include "base/android/library_loader/library_loader_hooks.h" |
| -#include "base/logging.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| #include "jni/CoreImpl_jni.h" |
| #include "mojo/embedder/embedder.h" |
| +#include "mojo/public/c/environment/async_waiter.h" |
| #include "mojo/public/c/system/core.h" |
| +#include "mojo/public/cpp/environment/default_async_waiter.h" |
| + |
| +namespace { |
| + |
| +struct AsyncWaitCallbackData { |
|
rmcilroy
2014/05/15 22:36:51
Add a constructor and use it below.to setup fields
qsr
2014/05/19 12:12:09
Done.
|
| + JNIEnv* env; |
|
bulach
2014/05/15 19:32:58
better to just call AttachCurrentThread rather tha
qsr
2014/05/19 12:12:09
Done, but any reason for this, knowing that everyt
|
| + base::android::ScopedJavaGlobalRef<jobject> core_impl; |
| + base::android::ScopedJavaGlobalRef<jobject> callback; |
| + base::android::ScopedJavaGlobalRef<jobject> cancellable; |
| +}; |
| + |
| +void AsyncWaitCallback(void* data, MojoResult result) { |
| + scoped_ptr<AsyncWaitCallbackData> callback_data( |
| + static_cast<AsyncWaitCallbackData*>(data)); |
| + mojo::android::Java_CoreImpl_onAsyncWaitResult( |
| + callback_data->env, |
| + callback_data->core_impl.obj(), |
| + result, |
| + callback_data->callback.obj(), |
| + callback_data->cancellable.obj()); |
| +} |
| + |
| +} // namespace |
| namespace mojo { |
| namespace android { |
| @@ -285,6 +311,54 @@ static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { |
| return MojoUnmapBuffer(buffer_start); |
| } |
| +static jobject AsyncWait(JNIEnv* env, |
| + jobject jcaller, |
| + jint mojo_handle, |
| + jint flags, |
| + jlong deadline, |
| + jobject callback) { |
| + AsyncWaitCallbackData* callback_data = new AsyncWaitCallbackData(); |
| + callback_data->env = env; |
| + callback_data->core_impl.Reset(env, jcaller); |
| + callback_data->callback.Reset(env, callback); |
| + MojoAsyncWaiter* async_waiter = mojo::GetDefaultAsyncWaiter(); |
|
rmcilroy
2014/05/15 22:36:51
nit - I think this could be scoped in the if branc
qsr
2014/05/19 12:12:09
Done.
|
| + MojoAsyncWaitID cancel_id; |
| + if (static_cast<MojoHandle>(mojo_handle) != MOJO_HANDLE_INVALID) { |
| + cancel_id = async_waiter->AsyncWait(async_waiter, |
| + mojo_handle, |
| + flags, |
| + deadline, |
| + AsyncWaitCallback, |
| + callback_data); |
| + } else { |
| + cancel_id = 0; |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &AsyncWaitCallback, callback_data, MOJO_RESULT_INVALID_ARGUMENT)); |
| + } |
| + base::android::ScopedJavaLocalRef<jobject> cancellable = |
| + Java_CoreImpl_newAsyncWaiterCancellableImpl( |
| + env, jcaller, cancel_id, reinterpret_cast<jlong>(callback_data)); |
|
rmcilroy
2014/05/15 22:36:51
reinterpret_cast<intptr_t>
qsr
2014/05/19 12:12:09
Done.
|
| + callback_data->cancellable.Reset(env, cancellable.obj()); |
| + return cancellable.Release(); |
| +} |
| + |
| +static void CancelAsyncWait(JNIEnv* env, |
| + jobject jcaller, |
| + jlong id, |
| + jlong data_ptr) { |
| + if (id == 0) { |
| + // If |id| is 0, the async wait was done on an invalid handle, so the |
| + // AsyncWaitCallback will be called and will clear the data_ptr. |
| + return; |
| + } |
| + scoped_ptr<AsyncWaitCallbackData> deleter( |
| + reinterpret_cast<AsyncWaitCallbackData*>(data_ptr)); |
| + MojoAsyncWaiter* async_waiter = mojo::GetDefaultAsyncWaiter(); |
| + async_waiter->CancelWait(async_waiter, id); |
| +} |
| + |
| bool RegisterCoreImpl(JNIEnv* env) { |
| return RegisterNativesImpl(env); |
| } |