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 "jni/WatcherImpl_jni.h" | 16 #include "jni/WatcherImpl_jni.h" |
17 #include "mojo/public/cpp/system/handle.h" | 17 #include "mojo/public/cpp/system/handle.h" |
18 #include "mojo/public/cpp/system/simple_watcher.h" | 18 #include "mojo/public/cpp/system/watcher.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 namespace android { | 21 namespace android { |
22 | 22 |
23 using base::android::JavaParamRef; | 23 using base::android::JavaParamRef; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 class WatcherImpl { | 27 class WatcherImpl { |
28 public: | 28 public: |
29 WatcherImpl() : watcher_(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC) {} | 29 WatcherImpl() : watcher_(FROM_HERE) {} |
30 | 30 |
31 ~WatcherImpl() = default; | 31 ~WatcherImpl() = default; |
32 | 32 |
33 jint Start(JNIEnv* env, | 33 jint Start(JNIEnv* env, |
34 const JavaParamRef<jobject>& jcaller, | 34 const JavaParamRef<jobject>& jcaller, |
35 jint mojo_handle, | 35 jint mojo_handle, |
36 jint signals) { | 36 jint signals) { |
37 java_watcher_.Reset(env, jcaller); | 37 java_watcher_.Reset(env, jcaller); |
38 | 38 |
39 auto ready_callback = | 39 auto ready_callback = |
40 base::Bind(&WatcherImpl::OnHandleReady, base::Unretained(this)); | 40 base::Bind(&WatcherImpl::OnHandleReady, base::Unretained(this)); |
41 | 41 |
42 MojoResult result = | 42 MojoResult result = |
43 watcher_.Watch(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), | 43 watcher_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), |
44 static_cast<MojoHandleSignals>(signals), ready_callback); | 44 static_cast<MojoHandleSignals>(signals), ready_callback); |
| 45 |
45 if (result != MOJO_RESULT_OK) | 46 if (result != MOJO_RESULT_OK) |
46 java_watcher_.Reset(); | 47 java_watcher_.Reset(); |
47 | 48 |
48 return result; | 49 return result; |
49 } | 50 } |
50 | 51 |
51 void Cancel() { | 52 void Cancel() { |
52 java_watcher_.Reset(); | 53 java_watcher_.Reset(); |
53 watcher_.Cancel(); | 54 watcher_.Cancel(); |
54 } | 55 } |
55 | 56 |
56 private: | 57 private: |
57 void OnHandleReady(MojoResult result) { | 58 void OnHandleReady(MojoResult result) { |
58 DCHECK(!java_watcher_.is_null()); | 59 DCHECK(!java_watcher_.is_null()); |
59 | 60 |
60 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; | 61 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; |
61 if (result == MOJO_RESULT_CANCELLED) | 62 if (result == MOJO_RESULT_CANCELLED) |
62 java_watcher_preserver = std::move(java_watcher_); | 63 java_watcher_preserver = std::move(java_watcher_); |
63 | 64 |
64 Java_WatcherImpl_onHandleReady( | 65 Java_WatcherImpl_onHandleReady( |
65 base::android::AttachCurrentThread(), | 66 base::android::AttachCurrentThread(), |
66 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, | 67 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, |
67 result); | 68 result); |
68 } | 69 } |
69 | 70 |
70 SimpleWatcher watcher_; | 71 Watcher watcher_; |
71 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; | 72 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; |
72 | 73 |
73 DISALLOW_COPY_AND_ASSIGN(WatcherImpl); | 74 DISALLOW_COPY_AND_ASSIGN(WatcherImpl); |
74 }; | 75 }; |
75 | 76 |
76 } // namespace | 77 } // namespace |
77 | 78 |
78 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { | 79 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { |
79 return reinterpret_cast<jlong>(new WatcherImpl); | 80 return reinterpret_cast<jlong>(new WatcherImpl); |
80 } | 81 } |
(...skipping 18 matching lines...) Expand all Loading... |
99 jlong watcher_ptr) { | 100 jlong watcher_ptr) { |
100 delete reinterpret_cast<WatcherImpl*>(watcher_ptr); | 101 delete reinterpret_cast<WatcherImpl*>(watcher_ptr); |
101 } | 102 } |
102 | 103 |
103 bool RegisterWatcherImpl(JNIEnv* env) { | 104 bool RegisterWatcherImpl(JNIEnv* env) { |
104 return RegisterNativesImpl(env); | 105 return RegisterNativesImpl(env); |
105 } | 106 } |
106 | 107 |
107 } // namespace android | 108 } // namespace android |
108 } // namespace mojo | 109 } // namespace mojo |
OLD | NEW |