Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: base/android/jni_map.cc

Issue 12255042: [Android] Introduce a Java wrapper for TemplateUrlService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
14 namespace base {
15 namespace android {
16
17 // Converts a std::map<std::string, string16> to a java HashMap<String,String>.
nyquist 2013/02/20 07:33:09 Nit: insert space between Java generics types.
Yaron 2013/02/21 21:39:15 Done.
18 ScopedJavaLocalRef<jobject> ConvertMapToJavaMap(
19 JNIEnv* env,
20 const std::map<std::string, string16>& cmap) {
21 ScopedJavaLocalRef<jclass> map_clazz = GetClass(env, "java/util/HashMap");
bulach 2013/02/20 14:41:25 a kitten has died.... :D any chance of generating
Yaron 2013/02/21 21:39:15 That seems suboptimal to me. I feel like what you'
22 jmethodID map_constructor = MethodID::Get<MethodID::TYPE_INSTANCE>(
23 env, map_clazz.obj(), "<init>", "()V");
24 jmethodID map_put = MethodID::Get<MethodID::TYPE_INSTANCE>(
25 env, map_clazz.obj(), "put",
26 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
27
28 jobject jmap = env->NewObject(map_clazz.obj(), map_constructor);
29 DCHECK(jmap);
30 CheckException(env);
31
32 for (std::map<std::string, string16>::const_iterator i = cmap.begin();
33 i != cmap.end(); i++) {
34 ScopedJavaLocalRef<jstring> key = ConvertUTF8ToJavaString(env, i->first);
35 ScopedJavaLocalRef<jstring> value = ConvertUTF16ToJavaString(env,
36 i->second);
37 // Assign the result to a ScopedJavaLocalRef so we don't leak the local ref.
38 ScopedJavaLocalRef<jobject> result(env,
39 env->CallObjectMethod(jmap, map_put, key.obj(), value.obj()));
40 CheckException(env);
41 }
42 return ScopedJavaLocalRef<jobject>(env, jmap);
43 }
44
45 std::map<std::string, string16> ConvertJavaMapToMap(JNIEnv* env,
46 jobject jmap) {
47 std::map<std::string, string16> cmap;
48
49 // Get a Set of all items.
50 ScopedJavaLocalRef<jclass> map_clazz = GetClass(env, "java/util/HashMap");
51 jmethodID map_entryset = MethodID::Get<MethodID::TYPE_INSTANCE>(
52 env, map_clazz.obj(), "entrySet", "()Ljava/util/Set;");
53 ScopedJavaLocalRef<jobject> jset(env,
54 env->CallObjectMethod(jmap, map_entryset));
55 DCHECK(!jset.is_null());
56 CheckException(env);
57
58 // Get an iterator for this set.
59 ScopedJavaLocalRef<jclass> set_clazz = GetClass(env, "java/util/Set");
60 jmethodID set_iterator = MethodID::Get<MethodID::TYPE_INSTANCE>(
61 env, set_clazz.obj(), "iterator", "()Ljava/util/Iterator;");
62 ScopedJavaLocalRef<jobject> iter(env,
63 env->CallObjectMethod(jset.obj(), set_iterator));
64 DCHECK(!iter.is_null());
65 CheckException(env);
66
67 // Loop over them.
nyquist 2013/02/20 07:33:09 Nit: Technically you don't start looping yet, you'
Yaron 2013/02/21 21:39:15 Dropped most comments as with bindings, the readab
68 ScopedJavaLocalRef<jclass> iter_clazz = GetClass(env, "java/util/Iterator");
69 jmethodID iter_hasnext = MethodID::Get<MethodID::TYPE_INSTANCE>(
70 env, iter_clazz.obj(), "hasNext", "()Z");
71 jmethodID iter_next = MethodID::Get<MethodID::TYPE_INSTANCE>(
72 env, iter_clazz.obj(), "next", "()Ljava/lang/Object;");
73
74 ScopedJavaLocalRef<jclass> map_entry_clazz =
75 GetClass(env, "java/util/Map$Entry");
76 jmethodID map_entry_getkey = MethodID::Get<MethodID::TYPE_INSTANCE>(
77 env, map_entry_clazz.obj(), "getKey", "()Ljava/lang/Object;");
78 jmethodID map_entry_getvalue = MethodID::Get<MethodID::TYPE_INSTANCE>(
79 env, map_entry_clazz.obj(), "getValue", "()Ljava/lang/Object;");
80
81 // Dump items into a std::map.
82 while (env->CallBooleanMethod(iter.obj(), iter_hasnext)) {
83 CheckException(env);
84 ScopedJavaLocalRef<jobject> map_entry(env,
85 env->CallObjectMethod(iter.obj(), iter_next));
86 DCHECK(env->IsInstanceOf(map_entry.obj(), map_entry_clazz.obj()));
87 ScopedJavaLocalRef<jstring> jkey(env,
88 static_cast<jstring>(env->CallObjectMethod(map_entry.obj(),
89 map_entry_getkey)));
90 ScopedJavaLocalRef<jstring> jvalue(env,
91 static_cast<jstring>(env->CallObjectMethod(map_entry.obj(),
92 map_entry_getvalue)));
93 // For profiles, jvalue can be null if not found in the contacts database.
nyquist 2013/02/20 07:33:09 Which contacts database? What does this have to do
Yaron 2013/02/21 21:39:15 dropped. Copied costant from the original implemen
94 if (!jkey.is_null() && !jvalue.is_null() &&
95 env->GetStringLength(jvalue.obj())) {
96 cmap[ConvertJavaStringToUTF8(jkey)] = ConvertJavaStringToUTF16(jvalue);
97 }
98 }
99 return cmap;
100 }
101
102
103 } // namespace android
104 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698