| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/watcher_impl.h" | 5 #include "mojo/android/system/watcher_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/android/base_jni_registrar.h" | 10 #include "base/android/base_jni_registrar.h" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_registrar.h" | 12 #include "base/android/jni_registrar.h" |
| 13 #include "base/android/library_loader/library_loader_hooks.h" | 13 #include "base/android/library_loader/library_loader_hooks.h" |
| 14 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/message_loop/message_loop.h" |
| 16 #include "jni/WatcherImpl_jni.h" | 17 #include "jni/WatcherImpl_jni.h" |
| 17 #include "mojo/public/cpp/system/handle.h" | 18 #include "mojo/public/cpp/system/handle.h" |
| 18 #include "mojo/public/cpp/system/watcher.h" | 19 #include "mojo/public/cpp/system/watcher.h" |
| 19 | 20 |
| 20 namespace mojo { | 21 namespace mojo { |
| 21 namespace android { | 22 namespace android { |
| 22 | 23 |
| 23 using base::android::JavaParamRef; | 24 using base::android::JavaParamRef; |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 class JavaWatcherCallback { | 28 class WatcherWithMessageLoopObserver |
| 29 : public base::MessageLoop::DestructionObserver { |
| 28 public: | 30 public: |
| 29 JavaWatcherCallback(JNIEnv* env, const JavaParamRef<jobject>& java_watcher) { | 31 WatcherWithMessageLoopObserver() {} |
| 30 java_watcher_.Reset(env, java_watcher); | 32 |
| 33 ~WatcherWithMessageLoopObserver() override { StopObservingIfNecessary(); } |
| 34 |
| 35 jint Start(JNIEnv* env, |
| 36 const JavaParamRef<jobject>& jcaller, |
| 37 jint mojo_handle, |
| 38 jint signals) { |
| 39 if (!is_observing_) { |
| 40 is_observing_ = true; |
| 41 base::MessageLoop::current()->AddDestructionObserver(this); |
| 42 } |
| 43 |
| 44 java_watcher_.Reset(env, jcaller); |
| 45 |
| 46 auto ready_callback = base::Bind( |
| 47 &WatcherWithMessageLoopObserver::OnHandleReady, base::Unretained(this)); |
| 48 |
| 49 MojoResult result = |
| 50 watcher_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), |
| 51 static_cast<MojoHandleSignals>(signals), ready_callback); |
| 52 |
| 53 if (result != MOJO_RESULT_OK) { |
| 54 StopObservingIfNecessary(); |
| 55 java_watcher_.Reset(); |
| 56 } |
| 57 |
| 58 return result; |
| 31 } | 59 } |
| 32 | 60 |
| 33 void OnHandleReady(MojoResult result) { | 61 void Cancel() { |
| 34 Java_WatcherImpl_onHandleReady(base::android::AttachCurrentThread(), | 62 StopObservingIfNecessary(); |
| 35 java_watcher_, result); | 63 java_watcher_.Reset(); |
| 64 watcher_.Cancel(); |
| 36 } | 65 } |
| 37 | 66 |
| 38 private: | 67 private: |
| 68 void OnHandleReady(MojoResult result) { |
| 69 DCHECK(!java_watcher_.is_null()); |
| 70 |
| 71 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; |
| 72 if (result == MOJO_RESULT_CANCELLED) { |
| 73 StopObservingIfNecessary(); |
| 74 java_watcher_preserver = std::move(java_watcher_); |
| 75 } |
| 76 |
| 77 Java_WatcherImpl_onHandleReady( |
| 78 base::android::AttachCurrentThread(), |
| 79 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, |
| 80 result); |
| 81 } |
| 82 |
| 83 // base::MessageLoop::DestructionObserver: |
| 84 void WillDestroyCurrentMessageLoop() override { |
| 85 StopObservingIfNecessary(); |
| 86 OnHandleReady(MOJO_RESULT_ABORTED); |
| 87 } |
| 88 |
| 89 void StopObservingIfNecessary() { |
| 90 if (is_observing_) { |
| 91 is_observing_ = false; |
| 92 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 93 } |
| 94 } |
| 95 |
| 96 bool is_observing_ = false; |
| 97 Watcher watcher_; |
| 39 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; | 98 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(WatcherWithMessageLoopObserver); |
| 40 }; | 101 }; |
| 41 | 102 |
| 42 } // namespace | 103 } // namespace |
| 43 | 104 |
| 44 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { | 105 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { |
| 45 return reinterpret_cast<jlong>(new Watcher); | 106 return reinterpret_cast<jlong>(new WatcherWithMessageLoopObserver); |
| 46 } | 107 } |
| 47 | 108 |
| 48 static jint Start(JNIEnv* env, | 109 static jint Start(JNIEnv* env, |
| 49 const JavaParamRef<jobject>& jcaller, | 110 const JavaParamRef<jobject>& jcaller, |
| 50 jlong watcher_ptr, | 111 jlong watcher_ptr, |
| 51 jint mojo_handle, | 112 jint mojo_handle, |
| 52 jint signals) { | 113 jint signals) { |
| 53 Watcher* watcher = reinterpret_cast<Watcher*>(watcher_ptr); | 114 auto watcher = reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); |
| 54 return watcher->Start( | 115 return watcher->Start(env, jcaller, mojo_handle, signals); |
| 55 mojo::Handle(static_cast<MojoHandle>(mojo_handle)), | |
| 56 static_cast<MojoHandleSignals>(signals), | |
| 57 base::Bind(&JavaWatcherCallback::OnHandleReady, | |
| 58 base::Owned(new JavaWatcherCallback(env, jcaller)))); | |
| 59 } | 116 } |
| 60 | 117 |
| 61 static void Cancel(JNIEnv* env, | 118 static void Cancel(JNIEnv* env, |
| 62 const JavaParamRef<jobject>& jcaller, | 119 const JavaParamRef<jobject>& jcaller, |
| 63 jlong watcher_ptr) { | 120 jlong watcher_ptr) { |
| 64 reinterpret_cast<Watcher*>(watcher_ptr)->Cancel(); | 121 reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr)->Cancel(); |
| 65 } | 122 } |
| 66 | 123 |
| 67 static void Delete(JNIEnv* env, | 124 static void Delete(JNIEnv* env, |
| 68 const JavaParamRef<jobject>& jcaller, | 125 const JavaParamRef<jobject>& jcaller, |
| 69 jlong watcher_ptr) { | 126 jlong watcher_ptr) { |
| 70 delete reinterpret_cast<Watcher*>(watcher_ptr); | 127 delete reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); |
| 71 } | 128 } |
| 72 | 129 |
| 73 bool RegisterWatcherImpl(JNIEnv* env) { | 130 bool RegisterWatcherImpl(JNIEnv* env) { |
| 74 return RegisterNativesImpl(env); | 131 return RegisterNativesImpl(env); |
| 75 } | 132 } |
| 76 | 133 |
| 77 } // namespace android | 134 } // namespace android |
| 78 } // namespace mojo | 135 } // namespace mojo |
| OLD | NEW |