| // Copyright 2016 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <stddef.h> |
| #include <stdint.h> |
| |
| #include "base/android/jni_android.h" |
| #include "base/android/scoped_java_ref.h" |
| #include "base/functional/bind.h" |
| #include "base/memory/advanced_memory_safety_checks.h" |
| #include "base/task/sequenced_task_runner.h" |
| #include "mojo/public/cpp/system/handle.h" |
| #include "mojo/public/cpp/system/simple_watcher.h" |
| |
| // Must come after all headers that specialize FromJniType() / ToJniType(). |
| #include "mojo/public/java/system/system_impl_java_jni_headers/WatcherImpl_jni.h" |
| |
| namespace mojo { |
| namespace android { |
| |
| using base::android::JavaRef; |
| |
| namespace { |
| |
| class WatcherImpl { |
| // TODO(https://crbug.com/496639647): Remove this macro. |
| ADVANCED_MEMORY_SAFETY_CHECKS(); |
| |
| public: |
| WatcherImpl() |
| : watcher_(FROM_HERE, |
| SimpleWatcher::ArmingPolicy::AUTOMATIC, |
| base::SequencedTaskRunner::GetCurrentDefault()) {} |
| |
| WatcherImpl(const WatcherImpl&) = delete; |
| WatcherImpl& operator=(const WatcherImpl&) = delete; |
| |
| ~WatcherImpl() = default; |
| |
| int32_t Start(JNIEnv* env, |
| const JavaRef<jobject>& obj, |
| int64_t mojo_handle, |
| int32_t signals) { |
| java_watcher_.Reset(env, obj); |
| |
| auto ready_callback = base::BindRepeating(&WatcherImpl::OnHandleReady, |
| base::Unretained(this)); |
| |
| MojoResult result = |
| watcher_.Watch(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), |
| static_cast<MojoHandleSignals>(signals), ready_callback); |
| if (result != MOJO_RESULT_OK) { |
| java_watcher_.Reset(); |
| } |
| |
| return result; |
| } |
| |
| void Cancel() { |
| java_watcher_.Reset(); |
| watcher_.Cancel(); |
| } |
| |
| private: |
| void OnHandleReady(MojoResult result) { |
| DCHECK(!java_watcher_.is_null()); |
| |
| base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; |
| if (result == MOJO_RESULT_CANCELLED) { |
| java_watcher_preserver = std::move(java_watcher_); |
| } |
| |
| Java_WatcherImpl_onHandleReady( |
| base::android::AttachCurrentThread(), |
| java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, |
| result); |
| } |
| |
| SimpleWatcher watcher_; |
| base::android::ScopedJavaGlobalRef<jobject> java_watcher_; |
| }; |
| |
| } // namespace |
| |
| static int64_t JNI_WatcherImpl_CreateWatcher(JNIEnv* env) { |
| return reinterpret_cast<int64_t>(new WatcherImpl); |
| } |
| |
| static int32_t JNI_WatcherImpl_Start(JNIEnv* env, |
| const JavaRef<jobject>& obj, |
| int64_t watcher_ptr, |
| int64_t mojo_handle, |
| int32_t signals) { |
| auto* watcher = reinterpret_cast<WatcherImpl*>(watcher_ptr); |
| return watcher->Start(env, obj, mojo_handle, signals); |
| } |
| |
| static void JNI_WatcherImpl_Cancel(JNIEnv* env, int64_t watcher_ptr) { |
| reinterpret_cast<WatcherImpl*>(watcher_ptr)->Cancel(); |
| } |
| |
| static void JNI_WatcherImpl_Delete(JNIEnv* env, int64_t watcher_ptr) { |
| delete reinterpret_cast<WatcherImpl*>(watcher_ptr); |
| } |
| |
| } // namespace android |
| } // namespace mojo |
| |
| DEFINE_JNI(WatcherImpl) |