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 { | |
|
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.
| |
| 23 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
| |
| 24 base::android::ScopedJavaGlobalRef<jobject> core_impl; | |
| 25 base::android::ScopedJavaGlobalRef<jobject> callback; | |
| 26 base::android::ScopedJavaGlobalRef<jobject> cancellable; | |
| 27 }; | |
| 28 | |
| 29 void AsyncWaitCallback(void* data, MojoResult result) { | |
| 30 scoped_ptr<AsyncWaitCallbackData> callback_data( | |
| 31 static_cast<AsyncWaitCallbackData*>(data)); | |
| 32 mojo::android::Java_CoreImpl_onAsyncWaitResult( | |
| 33 callback_data->env, | |
| 34 callback_data->core_impl.obj(), | |
| 35 result, | |
| 36 callback_data->callback.obj(), | |
| 37 callback_data->cancellable.obj()); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 15 | 41 |
| 16 namespace mojo { | 42 namespace mojo { |
| 17 namespace android { | 43 namespace android { |
| 18 | 44 |
| 19 static void Constructor(JNIEnv* env, jobject jcaller) { | 45 static void Constructor(JNIEnv* env, jobject jcaller) { |
| 20 mojo::embedder::Init(); | 46 mojo::embedder::Init(); |
| 21 } | 47 } |
| 22 | 48 |
| 23 static jlong GetTimeTicksNow(JNIEnv* env, jobject jcaller) { | 49 static jlong GetTimeTicksNow(JNIEnv* env, jobject jcaller) { |
| 24 return MojoGetTimeTicksNow(); | 50 return MojoGetTimeTicksNow(); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) | 304 return Java_CoreImpl_newNativeCodeAndBufferResult(env, result, byte_buffer) |
| 279 .Release(); | 305 .Release(); |
| 280 } | 306 } |
| 281 | 307 |
| 282 static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { | 308 static int Unmap(JNIEnv* env, jobject jcaller, jobject buffer) { |
| 283 void* buffer_start = env->GetDirectBufferAddress(buffer); | 309 void* buffer_start = env->GetDirectBufferAddress(buffer); |
| 284 DCHECK(buffer_start); | 310 DCHECK(buffer_start); |
| 285 return MojoUnmapBuffer(buffer_start); | 311 return MojoUnmapBuffer(buffer_start); |
| 286 } | 312 } |
| 287 | 313 |
| 314 static jobject AsyncWait(JNIEnv* env, | |
| 315 jobject jcaller, | |
| 316 jint mojo_handle, | |
| 317 jint flags, | |
| 318 jlong deadline, | |
| 319 jobject callback) { | |
| 320 AsyncWaitCallbackData* callback_data = new AsyncWaitCallbackData(); | |
| 321 callback_data->env = env; | |
| 322 callback_data->core_impl.Reset(env, jcaller); | |
| 323 callback_data->callback.Reset(env, callback); | |
| 324 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.
| |
| 325 MojoAsyncWaitID cancel_id; | |
| 326 if (static_cast<MojoHandle>(mojo_handle) != MOJO_HANDLE_INVALID) { | |
| 327 cancel_id = async_waiter->AsyncWait(async_waiter, | |
| 328 mojo_handle, | |
| 329 flags, | |
| 330 deadline, | |
| 331 AsyncWaitCallback, | |
| 332 callback_data); | |
| 333 } else { | |
| 334 cancel_id = 0; | |
| 335 base::MessageLoop::current()->PostTask( | |
| 336 FROM_HERE, | |
| 337 base::Bind( | |
| 338 &AsyncWaitCallback, callback_data, MOJO_RESULT_INVALID_ARGUMENT)); | |
| 339 } | |
| 340 base::android::ScopedJavaLocalRef<jobject> cancellable = | |
| 341 Java_CoreImpl_newAsyncWaiterCancellableImpl( | |
| 342 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.
| |
| 343 callback_data->cancellable.Reset(env, cancellable.obj()); | |
| 344 return cancellable.Release(); | |
| 345 } | |
| 346 | |
| 347 static void CancelAsyncWait(JNIEnv* env, | |
| 348 jobject jcaller, | |
| 349 jlong id, | |
| 350 jlong data_ptr) { | |
| 351 if (id == 0) { | |
| 352 // If |id| is 0, the async wait was done on an invalid handle, so the | |
| 353 // AsyncWaitCallback will be called and will clear the data_ptr. | |
| 354 return; | |
| 355 } | |
| 356 scoped_ptr<AsyncWaitCallbackData> deleter( | |
| 357 reinterpret_cast<AsyncWaitCallbackData*>(data_ptr)); | |
| 358 MojoAsyncWaiter* async_waiter = mojo::GetDefaultAsyncWaiter(); | |
| 359 async_waiter->CancelWait(async_waiter, id); | |
| 360 } | |
| 361 | |
| 288 bool RegisterCoreImpl(JNIEnv* env) { | 362 bool RegisterCoreImpl(JNIEnv* env) { |
| 289 return RegisterNativesImpl(env); | 363 return RegisterNativesImpl(env); |
| 290 } | 364 } |
| 291 | 365 |
| 292 } // namespace android | 366 } // namespace android |
| 293 } // namespace mojo | 367 } // namespace mojo |
| OLD | NEW |