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..522bb42fa70a785ed408b3918057868aba8fac81 |
| --- /dev/null |
| +++ b/chrome/browser/search_engines/template_url_service_android.cc |
| @@ -0,0 +1,142 @@ |
| +// 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_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" |
| + |
| +using base::android::ConvertUTF16ToJavaString; |
| + |
| +namespace { |
| + |
| +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(chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, type); |
| + 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::SetDefaultSearchProvider(JNIEnv* env, |
| + jobject obj, |
| + jint selected_index) { |
| + std::vector<TemplateURL*> template_urls = |
| + template_url_service_->GetTemplateURLs(); |
| + size_t selected_index_size_t = static_cast<size_t>(selected_index); |
| + 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::GetDefaultSearchProvider(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]) { |
|
bulach
2013/02/26 09:59:28
nit: could get rid of braces here
Yaron
2013/02/26 18:26:47
Done.
|
| + return i; |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| +jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { |
| + return template_url_service_->loaded(); |
| +} |
| + |
| +jint TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlCount(JNIEnv* env, |
| + jobject obj) { |
|
bulach
2013/02/26 09:59:28
since the java side is using an array list rather
Yaron
2013/02/26 18:26:47
Done and it's easier to reason about now.
|
| + std::vector<TemplateURL*> 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) |
|
bulach
2013/02/26 09:59:28
truly nit, but could do with a "IsPrepoluatedTempl
Yaron
2013/02/26 18:26:47
Done.
|
| + ++prepopulated_urls_count; |
| + } |
| + return prepopulated_urls_count; |
| +} |
| + |
| +base::android::ScopedJavaLocalRef<jobject> |
| +TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlAt(JNIEnv* env, |
| + jobject obj, |
| + jint index) { |
| + TemplateURLService::TemplateURLVector template_urls = |
| + template_url_service_->GetTemplateURLs(); |
| + |
| + 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; |
| + |
| + if (prepopulated_url_index == index) { |
| + return Java_TemplateUrl_create( |
|
bulach
2013/02/26 09:59:28
lovely!!! :D
|
| + env, |
| + prepopulated_url_index, |
| + ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), |
| + ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); |
| + } |
| + ++prepopulated_url_index; |
| + } |
| + DCHECK(false) << "Failed to find prepopulated template url " << index; |
| + return ScopedJavaLocalRef<jobject>(); |
| +} |
| + |
| +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); |
| +} |