Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/search_engines/template_url_service_android.h" | 5 #include "chrome/browser/search_engines/template_url_service_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/bind.h" | |
| 8 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/search_engines/search_terms_data.h" | 14 #include "chrome/browser/search_engines/search_terms_data.h" |
| 15 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
| 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 17 #include "chrome/browser/search_engines/template_url_service.h" | |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "content/public/browser/notification_source.h" | |
| 20 #include "jni/TemplateUrlService_jni.h" | 18 #include "jni/TemplateUrlService_jni.h" |
| 21 | 19 |
| 22 using base::android::ConvertJavaStringToUTF16; | 20 using base::android::ConvertJavaStringToUTF16; |
| 23 using base::android::ConvertUTF16ToJavaString; | 21 using base::android::ConvertUTF16ToJavaString; |
| 24 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| 27 | 25 |
| 28 Profile* GetOriginalProfile() { | 26 Profile* GetOriginalProfile() { |
| 29 return g_browser_process->profile_manager()->GetDefaultProfile()-> | 27 return g_browser_process->profile_manager()->GetDefaultProfile()-> |
| 30 GetOriginalProfile(); | 28 GetOriginalProfile(); |
| 31 } | 29 } |
| 32 | 30 |
| 33 } // namespace | 31 } // namespace |
| 34 | 32 |
| 35 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, | 33 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env, |
| 36 jobject obj) | 34 jobject obj) |
| 37 : weak_java_obj_(env, obj), | 35 : weak_java_obj_(env, obj), |
| 38 template_url_service_( | 36 template_url_service_( |
| 39 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { | 37 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) { |
| 40 registrar_.Add(this, | 38 if (template_url_service_) { |
| 41 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, | 39 template_url_subscription_ = |
| 42 content::Source<TemplateURLService>(template_url_service_)); | 40 template_url_service_->RegisterOnLoadedCallback( |
| 41 base::Bind(&TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded, | |
| 42 base::Unretained(this))); | |
| 43 } | |
| 43 } | 44 } |
| 44 | 45 |
| 45 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { | 46 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() { |
| 46 } | 47 } |
| 47 | 48 |
| 48 void TemplateUrlServiceAndroid::Observe( | |
| 49 int type, | |
| 50 const content::NotificationSource& source, | |
| 51 const content::NotificationDetails& details) { | |
| 52 DCHECK_EQ(chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, type); | |
| 53 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 54 if (weak_java_obj_.get(env).is_null()) | |
| 55 return; | |
| 56 | |
| 57 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
| 58 weak_java_obj_.get(env).obj()); | |
| 59 } | |
| 60 | |
| 61 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { | 49 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) { |
| 62 template_url_service_->Load(); | 50 template_url_service_->Load(); |
| 63 } | 51 } |
| 64 | 52 |
| 65 void TemplateUrlServiceAndroid::SetDefaultSearchProvider(JNIEnv* env, | 53 void TemplateUrlServiceAndroid::SetDefaultSearchProvider(JNIEnv* env, |
| 66 jobject obj, | 54 jobject obj, |
| 67 jint selected_index) { | 55 jint selected_index) { |
| 68 std::vector<TemplateURL*> template_urls = | 56 std::vector<TemplateURL*> template_urls = |
| 69 template_url_service_->GetTemplateURLs(); | 57 template_url_service_->GetTemplateURLs(); |
| 70 size_t selected_index_size_t = static_cast<size_t>(selected_index); | 58 size_t selected_index_size_t = static_cast<size_t>(selected_index); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 env, | 121 env, |
| 134 index, | 122 index, |
| 135 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), | 123 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), |
| 136 ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); | 124 ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); |
| 137 } | 125 } |
| 138 | 126 |
| 139 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) { | 127 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) { |
| 140 return url->prepopulate_id() > 0; | 128 return url->prepopulate_id() > 0; |
| 141 } | 129 } |
| 142 | 130 |
| 131 void TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded() { | |
| 132 template_url_subscription_.reset(); | |
| 133 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 134 if (weak_java_obj_.get(env).is_null()) | |
| 135 return; | |
| 136 | |
| 137 Java_TemplateUrlService_templateUrlServiceLoaded(env, | |
|
Peter Kasting
2013/10/02 22:22:04
Nit: Move this arg to the next line so we don't ha
Cait (Slow)
2013/10/03 15:36:45
Done.
| |
| 138 weak_java_obj_.get(env).obj()); | |
| 139 } | |
| 140 | |
| 143 base::android::ScopedJavaLocalRef<jstring> | 141 base::android::ScopedJavaLocalRef<jstring> |
| 144 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv* env, | 142 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv* env, |
| 145 jobject obj, | 143 jobject obj, |
| 146 jstring jquery) { | 144 jstring jquery) { |
| 147 const TemplateURL* default_provider = | 145 const TemplateURL* default_provider = |
| 148 template_url_service_->GetDefaultSearchProvider(); | 146 template_url_service_->GetDefaultSearchProvider(); |
| 149 | 147 |
| 150 string16 query(ConvertJavaStringToUTF16(env, jquery)); | 148 string16 query(ConvertJavaStringToUTF16(env, jquery)); |
| 151 | 149 |
| 152 std::string url; | 150 std::string url; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 182 static jint Init(JNIEnv* env, jobject obj) { | 180 static jint Init(JNIEnv* env, jobject obj) { |
| 183 TemplateUrlServiceAndroid* template_url_service_android = | 181 TemplateUrlServiceAndroid* template_url_service_android = |
| 184 new TemplateUrlServiceAndroid(env, obj); | 182 new TemplateUrlServiceAndroid(env, obj); |
| 185 return reinterpret_cast<jint>(template_url_service_android); | 183 return reinterpret_cast<jint>(template_url_service_android); |
| 186 } | 184 } |
| 187 | 185 |
| 188 // static | 186 // static |
| 189 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | 187 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { |
| 190 return RegisterNativesImpl(env); | 188 return RegisterNativesImpl(env); |
| 191 } | 189 } |
| OLD | NEW |