Chromium Code Reviews| 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/TemplateUrlService_jni.h" | |
| 19 | |
| 20 using base::android::ConvertUTF16ToJavaString; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 Profile* GetOriginalProfile() { | |
| 25 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
| 26 GetOriginalProfile(); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, | |
| 32 jobject obj) | |
| 33 : weak_java_obj_(env, obj), | |
| 34 template_url_service_( | |
| 35 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { | |
| 36 registrar_.Add(this, | |
| 37 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | |
| 38 content::Source<TemplateURLService>(template_url_service_)); | |
| 39 } | |
| 40 | |
| 41 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { | |
| 42 } | |
| 43 | |
| 44 void TemplateUrlServiceAndroid::Observe( | |
| 45 int type, | |
| 46 const content::NotificationSource& source, | |
| 47 const content::NotificationDetails& details) { | |
| 48 DCHECK_EQ(chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, type); | |
| 49 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 50 if (weak_java_obj_.get(env).is_null()) | |
| 51 return; | |
| 52 | |
| 53 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
| 54 weak_java_obj_.get(env).obj()); | |
| 55 } | |
| 56 | |
| 57 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { | |
| 58 template_url_service_->Load(); | |
| 59 } | |
| 60 | |
| 61 void TemplateUrlServiceAndroid::SetDefaultSearchProvider(JNIEnv* env, | |
| 62 jobject obj, | |
| 63 jint selected_index) { | |
| 64 std::vector<TemplateURL*> template_urls = | |
| 65 template_url_service_->GetTemplateURLs(); | |
| 66 size_t selected_index_size_t = static_cast<size_t>(selected_index); | |
| 67 DCHECK_LT(selected_index_size_t, template_urls.size()) << | |
| 68 "Wrong index for search engine"; | |
| 69 | |
| 70 TemplateURL* template_url = template_urls[selected_index_size_t]; | |
| 71 DCHECK_GT(template_url->prepopulate_id(), 0) << | |
| 72 "Tried to select non-prepopulated search engine"; | |
| 73 template_url_service_->SetDefaultSearchProvider(template_url); | |
| 74 } | |
| 75 | |
| 76 jint TemplateUrlServiceAndroid::GetDefaultSearchProvider(JNIEnv* env, | |
| 77 jobject obj) { | |
| 78 std::vector<TemplateURL*> template_urls = | |
| 79 template_url_service_->GetTemplateURLs(); | |
| 80 TemplateURL* default_search_provider = | |
| 81 template_url_service_->GetDefaultSearchProvider(); | |
| 82 for (size_t i = 0; i < template_urls.size(); ++i) { | |
| 83 if (default_search_provider == template_urls[i]) { | |
| 84 return i; | |
| 85 } | |
| 86 } | |
| 87 return -1; | |
| 88 } | |
| 89 | |
| 90 jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) { | |
| 91 return template_url_service_->loaded(); | |
| 92 } | |
| 93 | |
| 94 jint TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlCount(JNIEnv* env, | |
| 95 jobject obj) { | |
| 96 std::vector<TemplateURL*> template_urls = | |
| 97 template_url_service_->GetTemplateURLs(); | |
| 98 int prepopulated_urls_count = 0; | |
| 99 for (size_t i = 0; i < template_urls.size(); ++i) { | |
| 100 if (template_urls[i]->prepopulate_id() > 0) | |
| 101 ++prepopulated_urls_count; | |
| 102 } | |
| 103 return prepopulated_urls_count; | |
| 104 } | |
| 105 | |
| 106 base::android::ScopedJavaLocalRef<jobject> | |
| 107 TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlAt(JNIEnv* env, | |
|
nyquist
2013/02/25 19:39:55
Nit: It might just be me, but the whitespace for j
Yaron
2013/02/25 21:13:21
Done.
| |
| 108 jobject obj, | |
| 109 jint index) { | |
| 110 TemplateURLService::TemplateURLVector template_urls = | |
| 111 template_url_service_->GetTemplateURLs(); | |
| 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 if (prepopulated_url_index == index) { | |
| 121 return Java_TemplateUrl_create( | |
| 122 env, | |
| 123 prepopulated_url_index, | |
| 124 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), | |
| 125 ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); | |
| 126 } | |
| 127 ++prepopulated_url_index; | |
| 128 } | |
| 129 DCHECK(false) << "Failed to find prepopulated template url " << index; | |
| 130 return ScopedJavaLocalRef<jobject>(); | |
| 131 } | |
| 132 | |
| 133 static jint Init(JNIEnv* env, jobject obj) { | |
| 134 TemplateUrlServiceAndroid* template_url_service_android = | |
| 135 new TemplateUrlServiceAndroid(env, obj); | |
| 136 return reinterpret_cast<jint>(template_url_service_android); | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | |
| 141 return RegisterNativesImpl(env); | |
| 142 } | |
| OLD | NEW |