OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/android/jni_map.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/logging.h" |
| 13 #include "jni/HashMap_jni.h" |
| 14 #include "jni/Iterator_jni.h" |
| 15 #include "jni/Set_jni.h" |
| 16 |
| 17 namespace base { |
| 18 namespace android { |
| 19 |
| 20 // Converts a std::map<std::string, string16> to a java HashMap<String, String>. |
| 21 ScopedJavaLocalRef<jobject> ConvertMapToJavaMap( |
| 22 JNIEnv* env, |
| 23 const std::map<std::string, |
| 24 string16>& cmap) { |
| 25 ScopedJavaLocalRef<jobject> jmap( |
| 26 JNI_HashMap::Java_HashMap_ConstructorJUHM(env)); |
| 27 |
| 28 for (std::map<std::string, string16>::const_iterator i = cmap.begin(); |
| 29 i != cmap.end(); i++) { |
| 30 ScopedJavaLocalRef<jstring> key = ConvertUTF8ToJavaString(env, i->first); |
| 31 ScopedJavaLocalRef<jstring> value = ConvertUTF16ToJavaString(env, |
| 32 i->second); |
| 33 JNI_HashMap::Java_HashMap_put(env, jmap.obj(), key.obj(), value.obj()); |
| 34 } |
| 35 return jmap; |
| 36 } |
| 37 |
| 38 std::map<std::string, string16> ConvertJavaMapToMap(JNIEnv* env, |
| 39 jobject jmap) { |
| 40 std::map<std::string, string16> cmap; |
| 41 |
| 42 ScopedJavaLocalRef<jobject> jset = |
| 43 JNI_HashMap::Java_HashMap_entrySet(env, jmap); |
| 44 DCHECK(!jset.is_null()); |
| 45 |
| 46 ScopedJavaLocalRef<jobject> iter = |
| 47 JNI_Set::Java_Set_iterator(env, jset.obj()); |
| 48 DCHECK(!iter.is_null()); |
| 49 |
| 50 // TODO(yfriedman): Convert this to JNI bindings when the JNI generator can |
| 51 // support inner classes from jar files. |
| 52 ScopedJavaLocalRef<jclass> map_entry_clazz = |
| 53 GetClass(env, "java/util/Map$Entry"); |
| 54 jmethodID map_entry_getkey = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 55 env, map_entry_clazz.obj(), "getKey", "()Ljava/lang/Object;"); |
| 56 jmethodID map_entry_getvalue = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 57 env, map_entry_clazz.obj(), "getValue", "()Ljava/lang/Object;"); |
| 58 |
| 59 while (JNI_Iterator::Java_Iterator_hasNext(env, iter.obj())) { |
| 60 ScopedJavaLocalRef<jobject> map_entry = |
| 61 JNI_Iterator::Java_Iterator_next(env, iter.obj()); |
| 62 DCHECK(env->IsInstanceOf(map_entry.obj(), map_entry_clazz.obj())); |
| 63 ScopedJavaLocalRef<jstring> jkey(env, |
| 64 static_cast<jstring>(env->CallObjectMethod(map_entry.obj(), |
| 65 map_entry_getkey))); |
| 66 ScopedJavaLocalRef<jstring> jvalue(env, |
| 67 static_cast<jstring>(env->CallObjectMethod(map_entry.obj(), |
| 68 map_entry_getvalue))); |
| 69 if (!jkey.is_null() && !jvalue.is_null() && |
| 70 env->GetStringLength(jvalue.obj())) { |
| 71 cmap[ConvertJavaStringToUTF8(jkey)] = ConvertJavaStringToUTF16(jvalue); |
| 72 } |
| 73 } |
| 74 return cmap; |
| 75 } |
| 76 |
| 77 bool RegisterMap(JNIEnv* env) { |
| 78 return JNI_HashMap::RegisterNativesImpl(env) && |
| 79 JNI_Iterator::RegisterNativesImpl(env) && |
| 80 JNI_Set::RegisterNativesImpl(env); |
| 81 } |
| 82 |
| 83 } // namespace android |
| 84 } // namespace base |
OLD | NEW |