| Index: base/android/jni_map.cc
|
| diff --git a/base/android/jni_map.cc b/base/android/jni_map.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9f803442f2aa9941ce056f318faeda475fce20ad
|
| --- /dev/null
|
| +++ b/base/android/jni_map.cc
|
| @@ -0,0 +1,84 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/android/jni_map.h"
|
| +
|
| +#include <map>
|
| +
|
| +#include "base/android/jni_android.h"
|
| +#include "base/android/jni_string.h"
|
| +#include "base/android/scoped_java_ref.h"
|
| +#include "base/logging.h"
|
| +#include "jni/HashMap_jni.h"
|
| +#include "jni/Iterator_jni.h"
|
| +#include "jni/Set_jni.h"
|
| +
|
| +namespace base {
|
| +namespace android {
|
| +
|
| +// Converts a std::map<std::string, string16> to a java HashMap<String, String>.
|
| +ScopedJavaLocalRef<jobject> ConvertMapToJavaMap(
|
| + JNIEnv* env,
|
| + const std::map<std::string,
|
| + string16>& cmap) {
|
| + ScopedJavaLocalRef<jobject> jmap(
|
| + JNI_HashMap::Java_HashMap_ConstructorJUHM(env));
|
| +
|
| + for (std::map<std::string, string16>::const_iterator i = cmap.begin();
|
| + i != cmap.end(); i++) {
|
| + ScopedJavaLocalRef<jstring> key = ConvertUTF8ToJavaString(env, i->first);
|
| + ScopedJavaLocalRef<jstring> value = ConvertUTF16ToJavaString(env,
|
| + i->second);
|
| + JNI_HashMap::Java_HashMap_put(env, jmap.obj(), key.obj(), value.obj());
|
| + }
|
| + return jmap;
|
| +}
|
| +
|
| +std::map<std::string, string16> ConvertJavaMapToMap(JNIEnv* env,
|
| + jobject jmap) {
|
| + std::map<std::string, string16> cmap;
|
| +
|
| + ScopedJavaLocalRef<jobject> jset =
|
| + JNI_HashMap::Java_HashMap_entrySet(env, jmap);
|
| + DCHECK(!jset.is_null());
|
| +
|
| + ScopedJavaLocalRef<jobject> iter =
|
| + JNI_Set::Java_Set_iterator(env, jset.obj());
|
| + DCHECK(!iter.is_null());
|
| +
|
| + // TODO(yfriedman): Convert this to JNI bindings when the JNI generator can
|
| + // support inner classes from jar files.
|
| + ScopedJavaLocalRef<jclass> map_entry_clazz =
|
| + GetClass(env, "java/util/Map$Entry");
|
| + jmethodID map_entry_getkey = MethodID::Get<MethodID::TYPE_INSTANCE>(
|
| + env, map_entry_clazz.obj(), "getKey", "()Ljava/lang/Object;");
|
| + jmethodID map_entry_getvalue = MethodID::Get<MethodID::TYPE_INSTANCE>(
|
| + env, map_entry_clazz.obj(), "getValue", "()Ljava/lang/Object;");
|
| +
|
| + while (JNI_Iterator::Java_Iterator_hasNext(env, iter.obj())) {
|
| + ScopedJavaLocalRef<jobject> map_entry =
|
| + JNI_Iterator::Java_Iterator_next(env, iter.obj());
|
| + DCHECK(env->IsInstanceOf(map_entry.obj(), map_entry_clazz.obj()));
|
| + ScopedJavaLocalRef<jstring> jkey(env,
|
| + static_cast<jstring>(env->CallObjectMethod(map_entry.obj(),
|
| + map_entry_getkey)));
|
| + ScopedJavaLocalRef<jstring> jvalue(env,
|
| + static_cast<jstring>(env->CallObjectMethod(map_entry.obj(),
|
| + map_entry_getvalue)));
|
| + if (!jkey.is_null() && !jvalue.is_null() &&
|
| + env->GetStringLength(jvalue.obj())) {
|
| + cmap[ConvertJavaStringToUTF8(jkey)] = ConvertJavaStringToUTF16(jvalue);
|
| + }
|
| + }
|
| + return cmap;
|
| +}
|
| +
|
| +bool RegisterMap(JNIEnv* env) {
|
| + return JNI_HashMap::RegisterNativesImpl(env) &&
|
| + JNI_Iterator::RegisterNativesImpl(env) &&
|
| + JNI_Set::RegisterNativesImpl(env);
|
| +}
|
| +
|
| +} // namespace android
|
| +} // namespace base
|
|
|