Chromium Code Reviews| Index: chrome/browser/search_engines/template_url_service_android.cc |
| diff --git a/chrome/browser/search_engines/template_url_service_android.cc b/chrome/browser/search_engines/template_url_service_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..917db8ff1cd52df1e8569926178dae26cdf3cde7 |
| --- /dev/null |
| +++ b/chrome/browser/search_engines/template_url_service_android.cc |
| @@ -0,0 +1,146 @@ |
| +// 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 "chrome/browser/search_engines/template_url_service_android.h" |
| + |
| +#include "base/android/jni_map.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/format_macros.h" |
| +#include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/search_engines/template_url.h" |
| +#include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| +#include "chrome/browser/search_engines/template_url_service.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "jni/TemplateUrlService_jni.h" |
| + |
| +namespace { |
| + |
| +const char kSearchEngineId[] = "searchEngineId"; |
| +const char kSearchEngineShortName[] = "searchEngineShortName"; |
| +const char kSearchEngineKeyword[] = "searchEngineKeyword"; |
| +const char kSearchEngineUrl[] = "searchEngineUrl"; |
| + |
| +Profile* GetOriginalProfile() { |
| + return g_browser_process->profile_manager()->GetDefaultProfile()-> |
| + GetOriginalProfile(); |
| +} |
| +} // namespace |
| + |
| +TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, |
| + jobject obj) |
| + : weak_java_obj_(env, obj), |
| + template_url_service_( |
| + TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
| + content::Source<TemplateURLService>(template_url_service_)); |
| +} |
| + |
| +TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { |
| +} |
| + |
| +void TemplateUrlServiceAndroid::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + 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.
|
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + if (weak_java_obj_.get(env).is_null()) |
| + return; |
| + |
| + Java_TemplateUrlService_templateUrlServiceLoaded(env, |
| + weak_java_obj_.get(env).obj()); |
| +} |
| + |
| +void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { |
| + template_url_service_->Load(); |
| +} |
| + |
| +void TemplateUrlServiceAndroid::SetSearchEngine(JNIEnv* env, |
| + jobject obj, |
| + jint selected_index) { |
| + std::vector<TemplateURL*> template_urls = |
| + template_url_service_->GetTemplateURLs(); |
| + 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.
|
| + DCHECK_LT(selected_index_size_t, template_urls.size()) << |
| + "Wrong index for search engine"; |
| + |
| + TemplateURL* template_url = template_urls[selected_index_size_t]; |
| + DCHECK_GT(template_url->prepopulate_id(), 0) << |
| + "Tried to select non-prepopulated search engine"; |
| + template_url_service_->SetDefaultSearchProvider(template_url); |
| +} |
| + |
| +jint TemplateUrlServiceAndroid::GetSearchEngine(JNIEnv* env, jobject obj) { |
| + std::vector<TemplateURL*> template_urls = |
| + template_url_service_->GetTemplateURLs(); |
| + TemplateURL* default_search_provider = |
| + template_url_service_->GetDefaultSearchProvider(); |
| + for (size_t i = 0; i < template_urls.size(); ++i) { |
| + if (default_search_provider == template_urls[i]) { |
| + return i; |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| +jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { |
| + return template_url_service_->loaded(); |
| +} |
| + |
| +base::android::ScopedJavaLocalRef<jobjectArray> |
| + TemplateUrlServiceAndroid::GetLocalizedSearchEngines(JNIEnv* env, |
| + jobject obj) { |
| + TemplateURLService::TemplateURLVector template_urls = |
| + template_url_service_->GetTemplateURLs(); |
| + |
| + int prepopulated_urls_count = 0; |
| + for (size_t i = 0; i < template_urls.size(); ++i) { |
| + if (template_urls[i]->prepopulate_id() > 0) |
| + prepopulated_urls_count++; |
| + } |
| + |
| + base::android::ScopedJavaLocalRef<jclass> hashmap_class = |
| + base::android::GetClass(env, "java/util/HashMap"); |
| + jobjectArray search_engine_array = |
| + env->NewObjectArray(prepopulated_urls_count, hashmap_class.obj(), NULL); |
| + |
| + int prepopulated_url_index = 0; |
| + for (size_t i = 0; i < template_urls.size(); ++i) { |
| + TemplateURL* template_url = template_urls[i]; |
| + // Only show prepopulated template URLs. |
| + if (template_url->prepopulate_id() <= 0) |
| + continue; |
| + |
| + std::map<std::string, string16> search_engine_map; |
| + search_engine_map[kSearchEngineId] = UTF8ToUTF16( |
| + base::StringPrintf("%" PRIuS, i)); |
| + search_engine_map[kSearchEngineShortName] = template_url->short_name(); |
| + search_engine_map[kSearchEngineKeyword] = template_url->keyword(); |
| + search_engine_map[kSearchEngineUrl] = template_url->url_ref().DisplayURL(); |
| + |
| + DCHECK(prepopulated_url_index < prepopulated_urls_count); |
| + env->SetObjectArrayElement(search_engine_array, prepopulated_url_index, |
| + base::android::ConvertMapToJavaMap(env, search_engine_map).obj()); |
| + base::android::CheckException(env); |
| + prepopulated_url_index++; |
| + } |
| + return ScopedJavaLocalRef<jobjectArray>(env, search_engine_array); |
| +} |
| + |
| + |
| +static jint Init(JNIEnv* env, jobject obj) { |
| + TemplateUrlServiceAndroid* template_url_service_android = |
| + new TemplateUrlServiceAndroid(env, obj); |
| + return reinterpret_cast<jint>(template_url_service_android); |
| +} |
| + |
| +// static |
| +bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |