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_map.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/format_macros.h" | |
10 #include "base/stringprintf.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/browser/search_engines/template_url.h" | |
15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
16 #include "chrome/browser/search_engines/template_url_service.h" | |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
18 #include "chrome/common/chrome_notification_types.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 } // namespace | |
33 | |
34 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, | |
35 jobject obj) | |
36 : weak_java_obj_(env, obj), | |
37 template_url_service_( | |
38 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { | |
39 registrar_.Add(this, | |
40 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
41 content::Source<TemplateURLService>(template_url_service_)); | |
42 } | |
43 | |
44 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { | |
45 } | |
46 | |
47 void TemplateUrlServiceAndroid::Observe( | |
48 int type, | |
49 const content::NotificationSource& source, | |
50 const content::NotificationDetails& details) { | |
51 DCHECK_EQ(type, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | |
Peter Kasting
2013/02/22 20:02:33
Tiny nit: We normally do (expected, actual) order
Yaron
2013/02/23 00:56:36
Ya, makes sense. Done.
| |
52 JNIEnv* env = base::android::AttachCurrentThread(); | |
53 if (weak_java_obj_.get(env).is_null()) | |
54 return; | |
55 | |
56 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
57 weak_java_obj_.get(env).obj()); | |
58 } | |
59 | |
60 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { | |
61 template_url_service_->Load(); | |
62 } | |
63 | |
64 void TemplateUrlServiceAndroid::SetSearchEngine(JNIEnv* env, | |
65 jobject obj, | |
66 jint selected_index) { | |
67 std::vector<TemplateURL*> template_urls = | |
68 template_url_service_->GetTemplateURLs(); | |
69 size_t selected_index_size_t = size_t(selected_index); | |
Peter Kasting
2013/02/22 20:02:33
Nit: Use static_cast<>
Yaron
2013/02/23 00:56:36
Done.
| |
70 DCHECK_LT(selected_index_size_t, template_urls.size()) << | |
71 "Wrong index for search engine"; | |
72 | |
73 TemplateURL* template_url = template_urls[selected_index_size_t]; | |
74 DCHECK_GT(template_url->prepopulate_id(), 0) << | |
75 "Tried to select non-prepopulated search engine"; | |
76 template_url_service_->SetDefaultSearchProvider(template_url); | |
77 } | |
78 | |
79 jint TemplateUrlServiceAndroid::GetSearchEngine(JNIEnv* env, jobject obj) { | |
80 std::vector<TemplateURL*> template_urls = | |
81 template_url_service_->GetTemplateURLs(); | |
82 TemplateURL* default_search_provider = | |
83 template_url_service_->GetDefaultSearchProvider(); | |
84 for (size_t i = 0; i < template_urls.size(); ++i) { | |
85 if (default_search_provider == template_urls[i]) { | |
86 return i; | |
87 } | |
88 } | |
89 return -1; | |
90 } | |
91 | |
92 jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { | |
93 return template_url_service_->loaded(); | |
94 } | |
95 | |
96 base::android::ScopedJavaLocalRef<jobjectArray> | |
97 TemplateUrlServiceAndroid::GetLocalizedSearchEngines(JNIEnv* env, | |
98 jobject obj) { | |
99 TemplateURLService::TemplateURLVector template_urls = | |
100 template_url_service_->GetTemplateURLs(); | |
101 | |
102 int prepopulated_urls_count = 0; | |
103 for (size_t i = 0; i < template_urls.size(); ++i) { | |
104 if (template_urls[i]->prepopulate_id() > 0) | |
105 prepopulated_urls_count++; | |
106 } | |
107 | |
108 base::android::ScopedJavaLocalRef<jclass> hashmap_class = | |
109 base::android::GetClass(env, "java/util/HashMap"); | |
110 jobjectArray search_engine_array = | |
111 env->NewObjectArray(prepopulated_urls_count, hashmap_class.obj(), NULL); | |
112 | |
113 int prepopulated_url_index = 0; | |
114 for (size_t i = 0; i < template_urls.size(); ++i) { | |
115 TemplateURL* template_url = template_urls[i]; | |
116 // Only show prepopulated template URLs. | |
117 if (template_url->prepopulate_id() <= 0) | |
118 continue; | |
119 | |
120 std::map<std::string, string16> search_engine_map; | |
121 search_engine_map[kSearchEngineId] = UTF8ToUTF16( | |
122 base::StringPrintf("%" PRIuS, i)); | |
123 search_engine_map[kSearchEngineShortName] = template_url->short_name(); | |
124 search_engine_map[kSearchEngineKeyword] = template_url->keyword(); | |
125 search_engine_map[kSearchEngineUrl] = template_url->url_ref().DisplayURL(); | |
126 | |
127 DCHECK(prepopulated_url_index < prepopulated_urls_count); | |
128 env->SetObjectArrayElement(search_engine_array, prepopulated_url_index, | |
129 base::android::ConvertMapToJavaMap(env, search_engine_map).obj()); | |
130 base::android::CheckException(env); | |
131 prepopulated_url_index++; | |
132 } | |
133 return ScopedJavaLocalRef<jobjectArray>(env, search_engine_array); | |
134 } | |
135 | |
136 | |
137 static jint Init(JNIEnv* env, jobject obj) { | |
138 TemplateUrlServiceAndroid* template_url_service_android = | |
139 new TemplateUrlServiceAndroid(env, obj); | |
140 return reinterpret_cast<jint>(template_url_service_android); | |
141 } | |
142 | |
143 // static | |
144 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | |
145 return RegisterNativesImpl(env); | |
146 } | |
OLD | NEW |