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 "chrome/browser/search_engines/template_url_service_android.h" | |
6 | |
7 #include "base/android/jni_string.h" | |
8 #include "base/format_macros.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/profiles/profile_manager.h" | |
13 #include "chrome/browser/search_engines/template_url.h" | |
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
15 #include "chrome/browser/search_engines/template_url_service.h" | |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
17 #include "chrome/common/chrome_notification_types.h" | |
18 #include "jni/HashMap_jni.h" | |
19 #include "jni/TemplateUrlService_jni.h" | |
20 | |
21 namespace { | |
22 | |
23 const char kSearchEngineId[] = "searchEngineId"; | |
24 const char kSearchEngineShortName[] = "searchEngineShortName"; | |
25 const char kSearchEngineKeyword[] = "searchEngineKeyword"; | |
26 const char kSearchEngineUrl[] = "searchEngineUrl"; | |
27 | |
28 Profile* GetOriginalProfile() { | |
29 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
30 GetOriginalProfile(); | |
31 } | |
32 | |
33 base::android::ScopedJavaLocalRef<jobject> ConvertMapToJavaMap( | |
34 JNIEnv* env, | |
35 const std::map<std::string, string16>& cmap) { | |
36 base::android::ScopedJavaLocalRef<jobject> jmap( | |
37 JNI_HashMap::Java_HashMap_ConstructorJUHM(env)); | |
38 | |
39 for (std::map<std::string, string16>::const_iterator i = cmap.begin(); | |
40 i != cmap.end(); ++i) { | |
41 base::android::ScopedJavaLocalRef<jstring> key = | |
42 base::android::ConvertUTF8ToJavaString(env, i->first); | |
43 base::android::ScopedJavaLocalRef<jstring> value = | |
44 base::android::ConvertUTF16ToJavaString(env, | |
45 i->second); | |
bulach
2013/02/25 12:51:19
nit: fits previous line
Yaron
2013/02/25 18:45:10
Done.
| |
46 JNI_HashMap::Java_HashMap_put(env, jmap.obj(), key.obj(), value.obj()); | |
47 } | |
48 return jmap; | |
49 } | |
50 } // namespace | |
51 | |
52 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, | |
53 jobject obj) | |
54 : weak_java_obj_(env, obj), | |
55 template_url_service_( | |
56 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { | |
57 registrar_.Add(this, | |
58 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
59 content::Source<TemplateURLService>(template_url_service_)); | |
60 } | |
61 | |
62 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { | |
63 } | |
64 | |
65 void TemplateUrlServiceAndroid::Observe( | |
66 int type, | |
67 const content::NotificationSource& source, | |
68 const content::NotificationDetails& details) { | |
69 DCHECK_EQ(chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, type); | |
70 JNIEnv* env = base::android::AttachCurrentThread(); | |
71 if (weak_java_obj_.get(env).is_null()) | |
72 return; | |
73 | |
74 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
75 weak_java_obj_.get(env).obj()); | |
76 } | |
77 | |
78 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { | |
79 template_url_service_->Load(); | |
80 } | |
81 | |
82 void TemplateUrlServiceAndroid::SetSearchEngine(JNIEnv* env, | |
83 jobject obj, | |
84 jint selected_index) { | |
85 std::vector<TemplateURL*> template_urls = | |
86 template_url_service_->GetTemplateURLs(); | |
87 size_t selected_index_size_t = static_cast<size_t>(selected_index); | |
88 DCHECK_LT(selected_index_size_t, template_urls.size()) << | |
89 "Wrong index for search engine"; | |
90 | |
91 TemplateURL* template_url = template_urls[selected_index_size_t]; | |
92 DCHECK_GT(template_url->prepopulate_id(), 0) << | |
93 "Tried to select non-prepopulated search engine"; | |
94 template_url_service_->SetDefaultSearchProvider(template_url); | |
95 } | |
96 | |
97 jint TemplateUrlServiceAndroid::GetSearchEngine(JNIEnv* env, jobject obj) { | |
98 std::vector<TemplateURL*> template_urls = | |
99 template_url_service_->GetTemplateURLs(); | |
100 TemplateURL* default_search_provider = | |
101 template_url_service_->GetDefaultSearchProvider(); | |
102 for (size_t i = 0; i < template_urls.size(); ++i) { | |
103 if (default_search_provider == template_urls[i]) { | |
104 return i; | |
105 } | |
106 } | |
107 return -1; | |
108 } | |
109 | |
110 jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { | |
111 return template_url_service_->loaded(); | |
112 } | |
113 | |
114 base::android::ScopedJavaLocalRef<jobjectArray> | |
115 TemplateUrlServiceAndroid::GetLocalizedSearchEngines(JNIEnv* env, | |
116 jobject obj) { | |
117 TemplateURLService::TemplateURLVector template_urls = | |
118 template_url_service_->GetTemplateURLs(); | |
119 | |
120 int prepopulated_urls_count = 0; | |
121 for (size_t i = 0; i < template_urls.size(); ++i) { | |
122 if (template_urls[i]->prepopulate_id() > 0) | |
123 prepopulated_urls_count++; | |
bulach
2013/02/25 12:51:19
nit: ++prepopulated_urls_count
Yaron
2013/02/25 18:45:10
Done.
| |
124 } | |
125 | |
126 base::android::ScopedJavaLocalRef<jclass> hashmap_class = | |
127 base::android::GetClass(env, "java/util/HashMap"); | |
bulach
2013/02/25 12:51:19
I think there's a handy JNI_HashMap::g_HashMap_cla
Yaron
2013/02/25 18:45:10
it's in anon namesapce but sure.
| |
128 jobjectArray search_engine_array = | |
129 env->NewObjectArray(prepopulated_urls_count, hashmap_class.obj(), NULL); | |
130 | |
131 int prepopulated_url_index = 0; | |
132 for (size_t i = 0; i < template_urls.size(); ++i) { | |
133 TemplateURL* template_url = template_urls[i]; | |
134 // Only show prepopulated template URLs. | |
135 if (template_url->prepopulate_id() <= 0) | |
136 continue; | |
137 | |
138 std::map<std::string, string16> search_engine_map; | |
139 search_engine_map[kSearchEngineId] = UTF8ToUTF16( | |
140 base::StringPrintf("%" PRIuS, i)); | |
141 search_engine_map[kSearchEngineShortName] = template_url->short_name(); | |
142 search_engine_map[kSearchEngineKeyword] = template_url->keyword(); | |
143 search_engine_map[kSearchEngineUrl] = template_url->url_ref().DisplayURL(); | |
bulach
2013/02/25 12:51:19
damn ocean apart... :) sorry, I think I'm not exem
Yaron
2013/02/25 18:45:10
I'm convinced. I didn't realize you meant to get r
| |
144 | |
145 DCHECK_LT(prepopulated_url_index, prepopulated_urls_count); | |
146 env->SetObjectArrayElement(search_engine_array, prepopulated_url_index, | |
147 ConvertMapToJavaMap(env, search_engine_map).obj()); | |
148 base::android::CheckException(env); | |
149 prepopulated_url_index++; | |
bulach
2013/02/25 12:51:19
nit: ++prepopulated_url_index
Yaron
2013/02/25 18:45:10
Done.
| |
150 } | |
151 return ScopedJavaLocalRef<jobjectArray>(env, search_engine_array); | |
152 } | |
153 | |
154 | |
155 static jint Init(JNIEnv* env, jobject obj) { | |
156 TemplateUrlServiceAndroid* template_url_service_android = | |
157 new TemplateUrlServiceAndroid(env, obj); | |
158 return reinterpret_cast<jint>(template_url_service_android); | |
159 } | |
160 | |
161 // static | |
162 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | |
163 return JNI_HashMap::RegisterNativesImpl(env) && | |
164 RegisterNativesImpl(env); | |
165 } | |
OLD | NEW |