Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/core_impl.h" | 5 #include "mojo/android/system/core_impl.h" |
| 6 | 6 |
| 7 #include "base/android/base_jni_registrar.h" | 7 #include "base/android/base_jni_registrar.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_registrar.h" | 9 #include "base/android/jni_registrar.h" |
| 10 #include "base/android/library_loader/library_loader_hooks.h" | 10 #include "base/android/library_loader/library_loader_hooks.h" |
| 11 #include "base/logging.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/bind.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 12 #include "jni/CoreImpl_jni.h" | 14 #include "jni/CoreImpl_jni.h" |
| 13 #include "mojo/embedder/embedder.h" | 15 #include "mojo/embedder/embedder.h" |
| 16 #include "mojo/public/c/environment/async_waiter.h" | |
| 14 #include "mojo/public/c/system/core.h" | 17 #include "mojo/public/c/system/core.h" |
| 18 #include "mojo/public/cpp/environment/default_async_waiter.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 struct AsyncWaitCallbackData { | |
| 23 base::android::ScopedJavaGlobalRef<jobject> core_impl; | |
| 24 base::android::ScopedJavaGlobalRef<jobject> callback; | |
| 25 base::android::ScopedJavaGlobalRef<jobject> cancellable; | |
| 26 | |
| 27 AsyncWaitCallbackData(JNIEnv* env, jobject core_impl, jobject callback) { | |
| 28 this->core_impl.Reset(env, core_impl); | |
| 29 this->callback.Reset(env, callback); | |
| 30 } | |
| 31 }; | |
| 32 | |
| 33 void AsyncWaitCallback(void* data, MojoResult result) { | |
| 34 scoped_ptr<AsyncWaitCallbackData> callback_data( | |
| 35 static_cast<AsyncWaitCallbackData*>(data)); | |
| 36 mojo::android::Java_CoreImpl_onAsyncWaitResult( | |
| 37 base::android::AttachCurrentThread(), | |
| 38 callback_data->core_impl.obj(), | |
| 39 result, | |
| 40 callback_data->callback.obj(), | |
| 41 callback_data->cancellable.obj()); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 15 | 45 |
| 16 namespace mojo { | 46 namespace mojo { |
| 17 namespace android { | 47 namespace android { |
| 18 | 48 |
| 19 static void Constructor(JNIEnv* env, jobject jcaller) { | 49 static void Constructor(JNIEnv* env, jobject jcaller) { |
| 20 mojo::embedder::Init(); | 50 mojo::embedder::Init(); |
| 21 } | 51 } |
| 22 | 52 |
| 23 static jlong GetTimeTicksNow(JNIEnv* env, jobject jcaller) { | 53 static jlong GetTimeTicksNow(JNIEnv* env, jobject jcaller) { |
| 24 return MojoGetTimeTicksNow(); | 54 return MojoGetTimeTicksNow(); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) | 308 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) |
| 279 .Release(); | 309 .Release(); |
| 280 } | 310 } |
| 281 | 311 |
| 282 static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { | 312 static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { |
| 283 void* buffer_start = env->GetDirectBufferAddress(buffer); | 313 void* buffer_start = env->GetDirectBufferAddress(buffer); |
| 284 DCHECK(buffer_start); | 314 DCHECK(buffer_start); |
| 285 return MojoUnmapBuffer(buffer_start); | 315 return MojoUnmapBuffer(buffer_start); |
| 286 } | 316 } |
| 287 | 317 |
| 318 static jobject AsyncWait(JNIEnv* env, | |
| 319 jobject jcaller, | |
| 320 jint mojo_handle, | |
| 321 jint flags, | |
| 322 jlong deadline, | |
| 323 jobject callback) { | |
| 324 AsyncWaitCallbackData* callback_data = | |
| 325 new AsyncWaitCallbackData(env, jcaller, callback); | |
| 326 MojoAsyncWaitID cancel_id; | |
| 327 if (static_cast<MojoHandle>(mojo_handle) != MOJO_HANDLE_INVALID) { | |
| 328 MojoAsyncWaiter* async_waiter = mojo::GetDefaultAsyncWaiter(); | |
| 329 cancel_id = async_waiter->AsyncWait(async_waiter, | |
| 330 mojo_handle, | |
| 331 flags, | |
| 332 deadline, | |
| 333 AsyncWaitCallback, | |
| 334 callback_data); | |
| 335 } else { | |
| 336 cancel_id = 0; | |
|
rmcilroy
2014/05/19 13:06:27
nit - maybe replace '0' with a public static final
qsr
2014/05/19 14:43:22
Done.
| |
| 337 base::MessageLoop::current()->PostTask( | |
| 338 FROM_HERE, | |
| 339 base::Bind( | |
| 340 &AsyncWaitCallback, callback_data, MOJO_RESULT_INVALID_ARGUMENT)); | |
| 341 } | |
| 342 base::android::ScopedJavaLocalRef<jobject> cancellable = | |
| 343 Java_CoreImpl_newAsyncWaiterCancellableImpl( | |
| 344 env, jcaller, cancel_id, reinterpret_cast<intptr_t>(callback_data)); | |
| 345 callback_data->cancellable.Reset(env, cancellable.obj()); | |
| 346 return cancellable.Release(); | |
| 347 } | |
| 348 | |
| 349 static void CancelAsyncWait(JNIEnv* env, | |
| 350 jobject jcaller, | |
| 351 jlong id, | |
| 352 jlong data_ptr) { | |
| 353 if (id == 0) { | |
| 354 // If |id| is 0, the async wait was done on an invalid handle, so the | |
| 355 // AsyncWaitCallback will be called and will clear the data_ptr. | |
|
rmcilroy
2014/05/19 13:06:27
With the change above, the comment could be:
//
qsr
2014/05/19 14:43:22
Done.
| |
| 356 return; | |
| 357 } | |
| 358 scoped_ptr<AsyncWaitCallbackData> deleter( | |
| 359 reinterpret_cast<AsyncWaitCallbackData*>( | |
| 360 static_cast<intptr_t>(data_ptr))); | |
| 361 MojoAsyncWaiter* async_waiter = mojo::GetDefaultAsyncWaiter(); | |
| 362 async_waiter->CancelWait(async_waiter, id); | |
| 363 } | |
| 364 | |
| 288 bool RegisterCoreImpl(JNIEnv* env) { | 365 bool RegisterCoreImpl(JNIEnv* env) { |
| 289 return RegisterNativesImpl(env); | 366 return RegisterNativesImpl(env); |
| 290 } | 367 } |
| 291 | 368 |
| 292 } // namespace android | 369 } // namespace android |
| 293 } // namespace mojo | 370 } // namespace mojo |
| OLD | NEW |