Index: mojo/android/system/watcher_impl.cc |
diff --git a/mojo/android/system/watcher_impl.cc b/mojo/android/system/watcher_impl.cc |
index 2a6c54cf4b5dd60b83a70899bc0b230da4bf5d96..756ffe3f3bcaf317cf0b72f7b8c9dc9c6366a7ec 100644 |
--- a/mojo/android/system/watcher_impl.cc |
+++ b/mojo/android/system/watcher_impl.cc |
@@ -13,7 +13,6 @@ |
#include "base/android/library_loader/library_loader_hooks.h" |
#include "base/android/scoped_java_ref.h" |
#include "base/bind.h" |
-#include "base/message_loop/message_loop.h" |
#include "jni/WatcherImpl_jni.h" |
#include "mojo/public/cpp/system/handle.h" |
#include "mojo/public/cpp/system/watcher.h" |
@@ -25,41 +24,32 @@ using base::android::JavaParamRef; |
namespace { |
-class WatcherWithMessageLoopObserver |
- : public base::MessageLoop::DestructionObserver { |
+class WatcherImpl { |
public: |
- WatcherWithMessageLoopObserver() : watcher_(FROM_HERE) {} |
+ WatcherImpl() : watcher_(FROM_HERE) {} |
- ~WatcherWithMessageLoopObserver() override { StopObservingIfNecessary(); } |
+ ~WatcherImpl() = default; |
jint Start(JNIEnv* env, |
const JavaParamRef<jobject>& jcaller, |
jint mojo_handle, |
jint signals) { |
- if (!is_observing_) { |
- is_observing_ = true; |
- base::MessageLoop::current()->AddDestructionObserver(this); |
- } |
- |
java_watcher_.Reset(env, jcaller); |
- auto ready_callback = base::Bind( |
- &WatcherWithMessageLoopObserver::OnHandleReady, base::Unretained(this)); |
+ auto ready_callback = |
+ base::Bind(&WatcherImpl::OnHandleReady, base::Unretained(this)); |
MojoResult result = |
watcher_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), |
static_cast<MojoHandleSignals>(signals), ready_callback); |
- if (result != MOJO_RESULT_OK) { |
- StopObservingIfNecessary(); |
+ if (result != MOJO_RESULT_OK) |
java_watcher_.Reset(); |
- } |
return result; |
} |
void Cancel() { |
- StopObservingIfNecessary(); |
java_watcher_.Reset(); |
watcher_.Cancel(); |
} |
@@ -69,10 +59,8 @@ class WatcherWithMessageLoopObserver |
DCHECK(!java_watcher_.is_null()); |
base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; |
- if (result == MOJO_RESULT_CANCELLED) { |
- StopObservingIfNecessary(); |
+ if (result == MOJO_RESULT_CANCELLED) |
java_watcher_preserver = std::move(java_watcher_); |
- } |
Java_WatcherImpl_onHandleReady( |
base::android::AttachCurrentThread(), |
@@ -80,30 +68,16 @@ class WatcherWithMessageLoopObserver |
result); |
} |
- // base::MessageLoop::DestructionObserver: |
- void WillDestroyCurrentMessageLoop() override { |
- StopObservingIfNecessary(); |
- OnHandleReady(MOJO_RESULT_ABORTED); |
- } |
- |
- void StopObservingIfNecessary() { |
- if (is_observing_) { |
- is_observing_ = false; |
- base::MessageLoop::current()->RemoveDestructionObserver(this); |
- } |
- } |
- |
- bool is_observing_ = false; |
Watcher watcher_; |
base::android::ScopedJavaGlobalRef<jobject> java_watcher_; |
- DISALLOW_COPY_AND_ASSIGN(WatcherWithMessageLoopObserver); |
+ DISALLOW_COPY_AND_ASSIGN(WatcherImpl); |
}; |
} // namespace |
static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { |
- return reinterpret_cast<jlong>(new WatcherWithMessageLoopObserver); |
+ return reinterpret_cast<jlong>(new WatcherImpl); |
} |
static jint Start(JNIEnv* env, |
@@ -111,20 +85,20 @@ static jint Start(JNIEnv* env, |
jlong watcher_ptr, |
jint mojo_handle, |
jint signals) { |
- auto watcher = reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); |
+ auto watcher = reinterpret_cast<WatcherImpl*>(watcher_ptr); |
return watcher->Start(env, jcaller, mojo_handle, signals); |
} |
static void Cancel(JNIEnv* env, |
const JavaParamRef<jobject>& jcaller, |
jlong watcher_ptr) { |
- reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr)->Cancel(); |
+ reinterpret_cast<WatcherImpl*>(watcher_ptr)->Cancel(); |
} |
static void Delete(JNIEnv* env, |
const JavaParamRef<jobject>& jcaller, |
jlong watcher_ptr) { |
- delete reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); |
+ delete reinterpret_cast<WatcherImpl*>(watcher_ptr); |
} |
bool RegisterWatcherImpl(JNIEnv* env) { |