Index: android/scoped_java_ref.h |
diff --git a/android/scoped_java_ref.h b/android/scoped_java_ref.h |
index 37e7fd3507ca480ee29209df24d3e7e2cf2b7472..62749693fd7ed52258ad05e68b269fde29c20fda 100644 |
--- a/android/scoped_java_ref.h |
+++ b/android/scoped_java_ref.h |
@@ -94,6 +94,27 @@ class JavaRef : public JavaRef<jobject> { |
DISALLOW_COPY_AND_ASSIGN(JavaRef); |
}; |
+// Holds a local reference to a JNI method parameter. |
+// Method parameters should not be deleted, and so this class exists purely to |
+// wrap them as a JavaRef<T> in the JNI binding generator. Do not create |
+// instances manually. |
+template <typename T> |
+class JavaParamRef : public JavaRef<T> { |
+ public: |
+ // Assumes that |obj| is a parameter passed to a JNI method from Java. |
+ // Does not assume ownership as parameters should not be deleted. |
+ JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {} |
+ |
+ ~JavaParamRef() {} |
+ |
+ // TODO(torne): remove this cast once we're using JavaRef consistently. |
+ // http://crbug.com/506850 |
+ operator T() const { return JavaRef<T>::obj(); } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(JavaParamRef); |
+}; |
+ |
// Holds a local reference to a Java object. The local reference is scoped |
// to the lifetime of this object. |
// Instances of this class may hold onto any JNIEnv passed into it until |
@@ -207,7 +228,12 @@ class ScopedJavaGlobalRef : public JavaRef<T> { |
this->Reset(NULL, other.obj()); |
} |
- template<typename U> |
+ template <typename U> |
+ void Reset(JNIEnv* env, const JavaParamRef<U>& other) { |
+ this->Reset(env, other.obj()); |
+ } |
+ |
+ template <typename U> |
void Reset(JNIEnv* env, U obj) { |
implicit_cast<T>(obj); // Ensure U is assignable to T |
this->SetNewGlobalRef(env, obj); |