Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // This file contain convenience functions and classes for JNI. | 11 // This file contain convenience functions and classes for JNI. |
| 12 // Before using any of the methods, InitGlobalJniVariables must be called. | 12 // Before using any of the methods, InitGlobalJniVariables must be called. |
| 13 | 13 |
| 14 #ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 14 #ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| 15 #define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 15 #define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| 16 | 16 |
| 17 #include <jni.h> | 17 #include <jni.h> |
| 18 #include <string> | 18 #include <string> |
| 19 | 19 |
| 20 #include "webrtc/base/constructormagic.h" | |
| 20 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
| 21 | 22 |
| 22 // Abort the process if |jni| has a Java exception pending. | 23 // Abort the process if |jni| has a Java exception pending. |
| 23 // This macros uses the comma operator to execute ExceptionDescribe | 24 // This macros uses the comma operator to execute ExceptionDescribe |
| 24 // and ExceptionClear ignoring their return values and sending "" | 25 // and ExceptionClear ignoring their return values and sending "" |
| 25 // to the error stream. | 26 // to the error stream. |
| 26 #define CHECK_EXCEPTION(jni) \ | 27 #define CHECK_EXCEPTION(jni) \ |
| 27 RTC_CHECK(!jni->ExceptionCheck()) \ | 28 RTC_CHECK(!jni->ExceptionCheck()) \ |
| 28 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") | 29 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") |
| 29 | 30 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 ~ScopedGlobalRef() { | 116 ~ScopedGlobalRef() { |
| 116 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_); | 117 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_); |
| 117 } | 118 } |
| 118 T operator*() const { | 119 T operator*() const { |
| 119 return obj_; | 120 return obj_; |
| 120 } | 121 } |
| 121 private: | 122 private: |
| 122 T obj_; | 123 T obj_; |
| 123 }; | 124 }; |
| 124 | 125 |
| 126 // Provides a convenient way to iterate over a Java collection using the | |
| 127 // C++ range-for loop. | |
| 128 // E.g. for (jobject value : JavaCollection(jni, j_collection)) { ... } | |
| 129 // Note: Since Java iterators cannot be duplicated, this class is not copyable | |
| 130 // to prevent creating multiple C++ iterators that refer to the same Java | |
| 131 // iterator. | |
| 132 class JavaCollectionIterator { | |
|
pthatcher1
2016/03/21 21:57:00
It would actually be JavaIterableIterator.
How ab
skvlad
2016/03/21 22:27:39
Done.
| |
| 133 public: | |
| 134 // Creates an iterator representing the end of any collection. | |
| 135 JavaCollectionIterator() : iterator_(NULL) {} | |
| 136 // Creates an iterator pointing to the beginning of the specified collection. | |
| 137 JavaCollectionIterator(JNIEnv* jni, jobject collection) : jni_(jni) { | |
| 138 jclass j_class = GetObjectClass(jni, collection); | |
| 139 jmethodID iterator_id = | |
| 140 GetMethodID(jni, j_class, "iterator", "()Ljava/util/Iterator;"); | |
| 141 iterator_ = jni->CallObjectMethod(collection, iterator_id); | |
| 142 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; | |
| 143 | |
| 144 if (iterator_ == NULL) { | |
| 145 // TODO(skvlad): Should this be an exception instead? | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 jclass iterator_class = GetObjectClass(jni, iterator_); | |
| 150 has_next_id_ = GetMethodID(jni, iterator_class, "hasNext", "()Z"); | |
| 151 next_id_ = GetMethodID(jni, iterator_class, "next", "()Ljava/lang/Object;"); | |
| 152 | |
| 153 // Start at the first element in the collection | |
| 154 ++(*this); | |
| 155 } | |
| 156 | |
| 157 // Move constructor - necessary to be able to return iterator types from | |
| 158 // functions | |
| 159 JavaCollectionIterator(JavaCollectionIterator&& other) | |
| 160 : jni_(std::move(other.jni_)), | |
| 161 iterator_(std::move(other.iterator_)), | |
| 162 value_(std::move(other.value_)), | |
| 163 has_next_id_(std::move(other.has_next_id_)), | |
| 164 next_id_(std::move(other.next_id_)){}; | |
| 165 | |
| 166 // Move assignment should not be used. | |
| 167 JavaCollectionIterator& operator=(JavaCollectionIterator&&) = delete; | |
| 168 | |
| 169 // Advances the iterator one step | |
| 170 JavaCollectionIterator& operator++() { | |
| 171 if (iterator_ == NULL) { | |
| 172 // Can't iterate past the end | |
| 173 return *this; | |
| 174 } | |
| 175 bool have_next = jni_->CallBooleanMethod(iterator_, has_next_id_); | |
|
pthatcher1
2016/03/21 21:57:00
have_next => has_next
skvlad
2016/03/21 22:27:39
Done.
| |
| 176 CHECK_EXCEPTION(jni_) << "error during CallBooleanMethod"; | |
| 177 if (!have_next) { | |
| 178 iterator_ = NULL; | |
| 179 return *this; | |
| 180 } | |
| 181 | |
| 182 value_ = jni_->CallObjectMethod(iterator_, next_id_); | |
| 183 CHECK_EXCEPTION(jni_) << "error during CallObjectMethod"; | |
| 184 return *this; | |
| 185 } | |
| 186 | |
| 187 bool operator==(const JavaCollectionIterator& other) { | |
| 188 return (iterator_ == other.iterator_); | |
| 189 } | |
| 190 | |
| 191 bool operator!=(const JavaCollectionIterator& other) { | |
| 192 return !(*this == other); | |
| 193 } | |
| 194 | |
| 195 jobject operator*() { return value_; } | |
| 196 | |
| 197 private: | |
| 198 JNIEnv* jni_ = NULL; | |
| 199 jobject iterator_ = NULL; | |
| 200 jobject value_ = NULL; | |
| 201 jmethodID has_next_id_ = NULL; | |
| 202 jmethodID next_id_ = NULL; | |
| 203 | |
| 204 RTC_DISALLOW_COPY_AND_ASSIGN(JavaCollectionIterator); | |
| 205 }; | |
| 206 | |
| 207 class JavaCollection { | |
|
pthatcher1
2016/03/21 21:57:01
webrtc_jni::Iterable?
skvlad
2016/03/21 22:27:39
Done.
| |
| 208 public: | |
| 209 using iterator = JavaCollectionIterator; | |
| 210 explicit JavaCollection(JNIEnv* jni, jobject collection) | |
|
pthatcher1
2016/03/21 21:57:01
collection => iterable
skvlad
2016/03/21 22:27:39
Done.
| |
| 211 : jni_(jni), collection_(collection) {} | |
| 212 | |
| 213 iterator begin() { return JavaCollectionIterator(jni_, collection_); } | |
| 214 | |
| 215 iterator end() { return JavaCollectionIterator(); } | |
| 216 | |
| 217 private: | |
| 218 JNIEnv* jni_; | |
| 219 jobject collection_; | |
|
pthatcher1
2016/03/21 21:57:00
iterable_
skvlad
2016/03/21 22:27:39
Done.
| |
| 220 | |
| 221 RTC_DISALLOW_COPY_AND_ASSIGN(JavaCollection); | |
| 222 }; | |
| 223 | |
| 125 } // namespace webrtc_jni | 224 } // namespace webrtc_jni |
| 126 | 225 |
| 127 #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 226 #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| OLD | NEW |