diff options
author | jcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-24 23:55:20 +0000 |
---|---|---|
committer | jcivelli@chromium.org <jcivelli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-24 23:55:20 +0000 |
commit | 80ec0c328c49a012c21b6b47e1239f5460fa5348 (patch) | |
tree | 779bf5d1782b6916783af2ceb8dab4df438af7dc /base | |
parent | 6effa2013aebb02bd3fbd69c96566c5f0c192129 (diff) | |
download | chromium_src-80ec0c328c49a012c21b6b47e1239f5460fa5348.zip chromium_src-80ec0c328c49a012c21b6b47e1239f5460fa5348.tar.gz chromium_src-80ec0c328c49a012c21b6b47e1239f5460fa5348.tar.bz2 |
Adding new JNI util methods that deal with Java string arrays and use string16.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/10216001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133809 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/android/jni_array.cc | 27 | ||||
-rw-r--r-- | base/android/jni_array.h | 9 |
2 files changed, 36 insertions, 0 deletions
diff --git a/base/android/jni_array.cc b/base/android/jni_array.cc index 8e0547c..b287b46 100644 --- a/base/android/jni_array.cc +++ b/base/android/jni_array.cc @@ -53,6 +53,33 @@ ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( return ScopedJavaLocalRef<jobjectArray>(env, joa); } +ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( + JNIEnv* env, const std::vector<string16>& v) { + ScopedJavaLocalRef<jclass> string_clazz = GetClass(env, "java/lang/String"); + jobjectArray joa = env->NewObjectArray(v.size(), string_clazz.obj(), NULL); + CheckException(env); + + for (size_t i = 0; i < v.size(); ++i) { + ScopedJavaLocalRef<jstring> item = ConvertUTF16ToJavaString(env, v[i]); + env->SetObjectArrayElement(joa, i, item.obj()); + } + return ScopedJavaLocalRef<jobjectArray>(env, joa); +} + +void AppendJavaStringArrayToStringVector(JNIEnv* env, + const JavaRef<jobjectArray>& array, + std::vector<string16>* out) { + DCHECK(out); + if (array.is_null()) + return; + jsize len = env->GetArrayLength(array.obj()); + for (jsize i = 0; i < len; ++i) { + ScopedJavaLocalRef<jstring> str(env, + static_cast<jstring>(env->GetObjectArrayElement(array.obj(), i))); + out->push_back(ConvertJavaStringToUTF16(str)); + } +} + void AppendJavaByteArrayToByteVector(JNIEnv* env, jbyteArray byte_array, std::vector<uint8>* out) { diff --git a/base/android/jni_array.h b/base/android/jni_array.h index fea5548..087eb37 100644 --- a/base/android/jni_array.h +++ b/base/android/jni_array.h @@ -11,6 +11,7 @@ #include "base/android/scoped_java_ref.h" #include "base/basictypes.h" +#include "base/string16.h" namespace base { namespace android { @@ -26,6 +27,14 @@ ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray( ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( JNIEnv* env, const std::vector<std::string>& v); +ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( + JNIEnv* env, const std::vector<string16>& v); + +// Converts a Java string array to a native array. +void AppendJavaStringArrayToStringVector(JNIEnv* env, + const JavaRef<jobjectArray>& array, + std::vector<string16>* out); + // Appends the Java bytes in |bytes_array| onto the end of |out|. void AppendJavaByteArrayToByteVector(JNIEnv* env, jbyteArray byte_array, |