Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_ | 5 #ifndef BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_ | 6 #define BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 T Release() { | 141 T Release() { |
| 142 return static_cast<T>(this->ReleaseInternal()); | 142 return static_cast<T>(this->ReleaseInternal()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 private: | 145 private: |
| 146 // This class is only good for use on the thread it was created on so | 146 // This class is only good for use on the thread it was created on so |
| 147 // it's safe to cache the non-threadsafe JNIEnv* inside this object. | 147 // it's safe to cache the non-threadsafe JNIEnv* inside this object. |
| 148 JNIEnv* env_; | 148 JNIEnv* env_; |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 // Creates a new local reference frame, in which at least a given number of | |
| 152 // local references can be created. Note that local references already created | |
| 153 // in previous local frames are still valid in the current local frame. | |
| 154 class BASE_EXPORT ScopedJavaLocalFrame { | |
|
bulach
2014/04/07 15:51:29
nit: may I ask to move this one either as the firs
reveman
2014/04/07 17:38:29
Done.
| |
| 155 public: | |
| 156 explicit ScopedJavaLocalFrame(JNIEnv* env); | |
| 157 ScopedJavaLocalFrame(JNIEnv* env, int capacity); | |
| 158 ~ScopedJavaLocalFrame(); | |
| 159 | |
| 160 private: | |
| 161 // This class is only good for use on the thread it was created on so | |
| 162 // it's safe to cache the non-threadsafe JNIEnv* inside this object. | |
| 163 JNIEnv* env_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame); | |
| 166 }; | |
| 167 | |
| 151 // Holds a global reference to a Java object. The global reference is scoped | 168 // Holds a global reference to a Java object. The global reference is scoped |
| 152 // to the lifetime of this object. This class does not hold onto any JNIEnv* | 169 // to the lifetime of this object. This class does not hold onto any JNIEnv* |
| 153 // passed to it, hence it is safe to use across threads (within the constraints | 170 // passed to it, hence it is safe to use across threads (within the constraints |
| 154 // imposed by the underlying Java object that it references). | 171 // imposed by the underlying Java object that it references). |
| 155 template<typename T> | 172 template<typename T> |
| 156 class ScopedJavaGlobalRef : public JavaRef<T> { | 173 class ScopedJavaGlobalRef : public JavaRef<T> { |
| 157 public: | 174 public: |
| 158 ScopedJavaGlobalRef() {} | 175 ScopedJavaGlobalRef() {} |
| 159 | 176 |
| 160 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { | 177 explicit ScopedJavaGlobalRef(const ScopedJavaGlobalRef<T>& other) { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 189 // global reference when it is done with it. | 206 // global reference when it is done with it. |
| 190 T Release() { | 207 T Release() { |
| 191 return static_cast<T>(this->ReleaseInternal()); | 208 return static_cast<T>(this->ReleaseInternal()); |
| 192 } | 209 } |
| 193 }; | 210 }; |
| 194 | 211 |
| 195 } // namespace android | 212 } // namespace android |
| 196 } // namespace base | 213 } // namespace base |
| 197 | 214 |
| 198 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ | 215 #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| OLD | NEW |