Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: mojo/android/system/watcher_impl.cc

Issue 2633053002: Remove the MessageLoop::DestructionObserver from mojo bindings. (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/ipc_test_base.h ('k') | mojo/public/cpp/bindings/interface_endpoint_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
17 #include "jni/WatcherImpl_jni.h" 16 #include "jni/WatcherImpl_jni.h"
18 #include "mojo/public/cpp/system/handle.h" 17 #include "mojo/public/cpp/system/handle.h"
19 #include "mojo/public/cpp/system/watcher.h" 18 #include "mojo/public/cpp/system/watcher.h"
20 19
21 namespace mojo { 20 namespace mojo {
22 namespace android { 21 namespace android {
23 22
24 using base::android::JavaParamRef; 23 using base::android::JavaParamRef;
25 24
26 namespace { 25 namespace {
27 26
28 class WatcherWithMessageLoopObserver 27 class WatcherImpl {
29 : public base::MessageLoop::DestructionObserver {
30 public: 28 public:
31 WatcherWithMessageLoopObserver() : watcher_(FROM_HERE) {} 29 WatcherImpl() : watcher_(FROM_HERE) {}
32 30
33 ~WatcherWithMessageLoopObserver() override { StopObservingIfNecessary(); } 31 ~WatcherImpl() = default;
34 32
35 jint Start(JNIEnv* env, 33 jint Start(JNIEnv* env,
36 const JavaParamRef<jobject>& jcaller, 34 const JavaParamRef<jobject>& jcaller,
37 jint mojo_handle, 35 jint mojo_handle,
38 jint signals) { 36 jint signals) {
39 if (!is_observing_) {
40 is_observing_ = true;
41 base::MessageLoop::current()->AddDestructionObserver(this);
42 }
43
44 java_watcher_.Reset(env, jcaller); 37 java_watcher_.Reset(env, jcaller);
45 38
46 auto ready_callback = base::Bind( 39 auto ready_callback =
47 &WatcherWithMessageLoopObserver::OnHandleReady, base::Unretained(this)); 40 base::Bind(&WatcherImpl::OnHandleReady, base::Unretained(this));
48 41
49 MojoResult result = 42 MojoResult result =
50 watcher_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), 43 watcher_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)),
51 static_cast<MojoHandleSignals>(signals), ready_callback); 44 static_cast<MojoHandleSignals>(signals), ready_callback);
52 45
53 if (result != MOJO_RESULT_OK) { 46 if (result != MOJO_RESULT_OK)
54 StopObservingIfNecessary();
55 java_watcher_.Reset(); 47 java_watcher_.Reset();
56 }
57 48
58 return result; 49 return result;
59 } 50 }
60 51
61 void Cancel() { 52 void Cancel() {
62 StopObservingIfNecessary();
63 java_watcher_.Reset(); 53 java_watcher_.Reset();
64 watcher_.Cancel(); 54 watcher_.Cancel();
65 } 55 }
66 56
67 private: 57 private:
68 void OnHandleReady(MojoResult result) { 58 void OnHandleReady(MojoResult result) {
69 DCHECK(!java_watcher_.is_null()); 59 DCHECK(!java_watcher_.is_null());
70 60
71 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; 61 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver;
72 if (result == MOJO_RESULT_CANCELLED) { 62 if (result == MOJO_RESULT_CANCELLED)
73 StopObservingIfNecessary();
74 java_watcher_preserver = std::move(java_watcher_); 63 java_watcher_preserver = std::move(java_watcher_);
75 }
76 64
77 Java_WatcherImpl_onHandleReady( 65 Java_WatcherImpl_onHandleReady(
78 base::android::AttachCurrentThread(), 66 base::android::AttachCurrentThread(),
79 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, 67 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_,
80 result); 68 result);
81 } 69 }
82 70
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_; 71 Watcher watcher_;
98 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; 72 base::android::ScopedJavaGlobalRef<jobject> java_watcher_;
99 73
100 DISALLOW_COPY_AND_ASSIGN(WatcherWithMessageLoopObserver); 74 DISALLOW_COPY_AND_ASSIGN(WatcherImpl);
101 }; 75 };
102 76
103 } // namespace 77 } // namespace
104 78
105 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { 79 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) {
106 return reinterpret_cast<jlong>(new WatcherWithMessageLoopObserver); 80 return reinterpret_cast<jlong>(new WatcherImpl);
107 } 81 }
108 82
109 static jint Start(JNIEnv* env, 83 static jint Start(JNIEnv* env,
110 const JavaParamRef<jobject>& jcaller, 84 const JavaParamRef<jobject>& jcaller,
111 jlong watcher_ptr, 85 jlong watcher_ptr,
112 jint mojo_handle, 86 jint mojo_handle,
113 jint signals) { 87 jint signals) {
114 auto watcher = reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); 88 auto watcher = reinterpret_cast<WatcherImpl*>(watcher_ptr);
115 return watcher->Start(env, jcaller, mojo_handle, signals); 89 return watcher->Start(env, jcaller, mojo_handle, signals);
116 } 90 }
117 91
118 static void Cancel(JNIEnv* env, 92 static void Cancel(JNIEnv* env,
119 const JavaParamRef<jobject>& jcaller, 93 const JavaParamRef<jobject>& jcaller,
120 jlong watcher_ptr) { 94 jlong watcher_ptr) {
121 reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr)->Cancel(); 95 reinterpret_cast<WatcherImpl*>(watcher_ptr)->Cancel();
122 } 96 }
123 97
124 static void Delete(JNIEnv* env, 98 static void Delete(JNIEnv* env,
125 const JavaParamRef<jobject>& jcaller, 99 const JavaParamRef<jobject>& jcaller,
126 jlong watcher_ptr) { 100 jlong watcher_ptr) {
127 delete reinterpret_cast<WatcherWithMessageLoopObserver*>(watcher_ptr); 101 delete reinterpret_cast<WatcherImpl*>(watcher_ptr);
128 } 102 }
129 103
130 bool RegisterWatcherImpl(JNIEnv* env) { 104 bool RegisterWatcherImpl(JNIEnv* env) {
131 return RegisterNativesImpl(env); 105 return RegisterNativesImpl(env);
132 } 106 }
133 107
134 } // namespace android 108 } // namespace android
135 } // namespace mojo 109 } // namespace mojo
OLDNEW
« no previous file with comments | « ipc/ipc_test_base.h ('k') | mojo/public/cpp/bindings/interface_endpoint_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698