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/watcher.h" | 18 #include "mojo/public/cpp/system/simple_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) {} | 29 WatcherImpl() : watcher_(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC) {} |
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_.Start(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), | 43 watcher_.Watch(mojo::Handle(static_cast<MojoHandle>(mojo_handle)), |
44 static_cast<MojoHandleSignals>(signals), ready_callback); | 44 static_cast<MojoHandleSignals>(signals), ready_callback); |
45 | |
46 if (result != MOJO_RESULT_OK) | 45 if (result != MOJO_RESULT_OK) |
47 java_watcher_.Reset(); | 46 java_watcher_.Reset(); |
48 | 47 |
49 return result; | 48 return result; |
50 } | 49 } |
51 | 50 |
52 void Cancel() { | 51 void Cancel() { |
53 java_watcher_.Reset(); | 52 java_watcher_.Reset(); |
54 watcher_.Cancel(); | 53 watcher_.Cancel(); |
55 } | 54 } |
56 | 55 |
57 private: | 56 private: |
58 void OnHandleReady(MojoResult result) { | 57 void OnHandleReady(MojoResult result) { |
59 DCHECK(!java_watcher_.is_null()); | 58 DCHECK(!java_watcher_.is_null()); |
60 | 59 |
61 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; | 60 base::android::ScopedJavaGlobalRef<jobject> java_watcher_preserver; |
62 if (result == MOJO_RESULT_CANCELLED) | 61 if (result == MOJO_RESULT_CANCELLED) |
63 java_watcher_preserver = std::move(java_watcher_); | 62 java_watcher_preserver = std::move(java_watcher_); |
64 | 63 |
65 Java_WatcherImpl_onHandleReady( | 64 Java_WatcherImpl_onHandleReady( |
66 base::android::AttachCurrentThread(), | 65 base::android::AttachCurrentThread(), |
67 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, | 66 java_watcher_.is_null() ? java_watcher_preserver : java_watcher_, |
68 result); | 67 result); |
69 } | 68 } |
70 | 69 |
71 Watcher watcher_; | 70 SimpleWatcher watcher_; |
72 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; | 71 base::android::ScopedJavaGlobalRef<jobject> java_watcher_; |
73 | 72 |
74 DISALLOW_COPY_AND_ASSIGN(WatcherImpl); | 73 DISALLOW_COPY_AND_ASSIGN(WatcherImpl); |
75 }; | 74 }; |
76 | 75 |
77 } // namespace | 76 } // namespace |
78 | 77 |
79 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { | 78 static jlong CreateWatcher(JNIEnv* env, const JavaParamRef<jobject>& jcaller) { |
80 return reinterpret_cast<jlong>(new WatcherImpl); | 79 return reinterpret_cast<jlong>(new WatcherImpl); |
81 } | 80 } |
(...skipping 18 matching lines...) Expand all Loading... |
100 jlong watcher_ptr) { | 99 jlong watcher_ptr) { |
101 delete reinterpret_cast<WatcherImpl*>(watcher_ptr); | 100 delete reinterpret_cast<WatcherImpl*>(watcher_ptr); |
102 } | 101 } |
103 | 102 |
104 bool RegisterWatcherImpl(JNIEnv* env) { | 103 bool RegisterWatcherImpl(JNIEnv* env) { |
105 return RegisterNativesImpl(env); | 104 return RegisterNativesImpl(env); |
106 } | 105 } |
107 | 106 |
108 } // namespace android | 107 } // namespace android |
109 } // namespace mojo | 108 } // namespace mojo |
OLD | NEW |