Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_android.cc
blobb28a3f3811875ee45e80c4707553187693eab68c
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.
5 #include "chrome/browser/search_engines/template_url_service_android.h"
7 #include "base/android/jni_string.h"
8 #include "base/bind.h"
9 #include "base/format_macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/google/google_util.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/search_engines/search_terms_data.h"
16 #include "chrome/browser/search_engines/template_url.h"
17 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/search_engines/util.h"
21 #include "jni/TemplateUrlService_jni.h"
22 #include "net/base/url_util.h"
24 using base::android::ConvertJavaStringToUTF16;
25 using base::android::ConvertUTF16ToJavaString;
26 using base::android::ConvertUTF8ToJavaString;
28 namespace {
30 Profile* GetOriginalProfile() {
31 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
34 } // namespace
36 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env,
37 jobject obj)
38 : weak_java_obj_(env, obj),
39 template_url_service_(
40 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) {
41 template_url_subscription_ =
42 template_url_service_->RegisterOnLoadedCallback(
43 base::Bind(&TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded,
44 base::Unretained(this)));
47 TemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() {
50 void TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) {
51 template_url_service_->Load();
54 void TemplateUrlServiceAndroid::SetDefaultSearchProvider(JNIEnv* env,
55 jobject obj,
56 jint selected_index) {
57 std::vector<TemplateURL*> template_urls =
58 template_url_service_->GetTemplateURLs();
59 size_t selected_index_size_t = static_cast<size_t>(selected_index);
60 DCHECK_LT(selected_index_size_t, template_urls.size()) <<
61 "Wrong index for search engine";
63 TemplateURL* template_url = template_urls[selected_index_size_t];
64 DCHECK_GT(template_url->prepopulate_id(), 0) <<
65 "Tried to select non-prepopulated search engine";
66 template_url_service_->SetDefaultSearchProvider(template_url);
69 jint TemplateUrlServiceAndroid::GetDefaultSearchProvider(JNIEnv* env,
70 jobject obj) {
71 std::vector<TemplateURL*> template_urls =
72 template_url_service_->GetTemplateURLs();
73 TemplateURL* default_search_provider =
74 template_url_service_->GetDefaultSearchProvider();
75 for (size_t i = 0; i < template_urls.size(); ++i) {
76 if (default_search_provider == template_urls[i])
77 return i;
79 return -1;
82 jboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) {
83 return template_url_service_->loaded();
86 jint TemplateUrlServiceAndroid::GetTemplateUrlCount(JNIEnv* env, jobject obj) {
87 return template_url_service_->GetTemplateURLs().size();
90 jboolean TemplateUrlServiceAndroid::IsSearchProviderManaged(JNIEnv* env,
91 jobject obj) {
92 return template_url_service_->is_default_search_managed();
95 jboolean TemplateUrlServiceAndroid::IsSearchByImageAvailable(JNIEnv* env,
96 jobject obj) {
97 const TemplateURL* default_search_provider =
98 template_url_service_->GetDefaultSearchProvider();
99 return default_search_provider &&
100 !default_search_provider->image_url().empty() &&
101 default_search_provider->image_url_ref().IsValid();
104 jboolean TemplateUrlServiceAndroid::IsDefaultSearchEngineGoogle(JNIEnv* env,
105 jobject obj) {
106 TemplateURL* default_search_provider =
107 template_url_service_->GetDefaultSearchProvider();
108 return default_search_provider &&
109 default_search_provider->url_ref().HasGoogleBaseURLs();
112 base::android::ScopedJavaLocalRef<jobject>
113 TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlAt(JNIEnv* env,
114 jobject obj,
115 jint index) {
116 TemplateURL* template_url = template_url_service_->GetTemplateURLs()[index];
117 if (!IsPrepopulatedTemplate(template_url) &&
118 !template_url->created_by_policy())
119 return ScopedJavaLocalRef<jobject>();
121 return Java_TemplateUrl_create(
122 env,
123 index,
124 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(),
125 ConvertUTF16ToJavaString(env, template_url->keyword()).obj());
128 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) {
129 return url->prepopulate_id() > 0;
132 void TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded() {
133 template_url_subscription_.reset();
134 JNIEnv* env = base::android::AttachCurrentThread();
135 if (weak_java_obj_.get(env).is_null())
136 return;
138 Java_TemplateUrlService_templateUrlServiceLoaded(
139 env, weak_java_obj_.get(env).obj());
142 base::android::ScopedJavaLocalRef<jstring>
143 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv* env,
144 jobject obj,
145 jstring jquery) {
146 const TemplateURL* default_provider =
147 template_url_service_->GetDefaultSearchProvider();
149 base::string16 query(ConvertJavaStringToUTF16(env, jquery));
151 std::string url;
152 if (default_provider &&
153 default_provider->url_ref().SupportsReplacement() && !query.empty()) {
154 url = default_provider->url_ref().ReplaceSearchTerms(
155 TemplateURLRef::SearchTermsArgs(query));
158 return ConvertUTF8ToJavaString(env, url);
161 base::android::ScopedJavaLocalRef<jstring>
162 TemplateUrlServiceAndroid::GetUrlForVoiceSearchQuery(JNIEnv* env,
163 jobject obj,
164 jstring jquery) {
165 base::string16 query(ConvertJavaStringToUTF16(env, jquery));
166 std::string url;
168 if (!query.empty()) {
169 GURL gurl = GetDefaultSearchURLForSearchTerms(GetOriginalProfile(), query);
170 if (google_util::IsGoogleSearchUrl(gurl))
171 gurl = net::AppendQueryParameter(gurl, "inm", "vs");
172 url = gurl.spec();
175 return ConvertUTF8ToJavaString(env, url);
178 base::android::ScopedJavaLocalRef<jstring>
179 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv* env,
180 jobject obj,
181 jstring jquery,
182 jstring jcurrent_url) {
183 TemplateURL* default_provider =
184 template_url_service_->GetDefaultSearchProvider();
186 base::string16 query(ConvertJavaStringToUTF16(env, jquery));
187 GURL current_url(ConvertJavaStringToUTF16(env, jcurrent_url));
188 GURL destination_url(current_url);
189 if (default_provider && !query.empty()) {
190 bool refined_query = default_provider->ReplaceSearchTermsInURL(current_url,
191 TemplateURLRef::SearchTermsArgs(query), &destination_url);
192 if (refined_query)
193 return ConvertUTF8ToJavaString(env, destination_url.spec());
195 return base::android::ScopedJavaLocalRef<jstring>(env, NULL);
198 static jlong Init(JNIEnv* env, jobject obj) {
199 TemplateUrlServiceAndroid* template_url_service_android =
200 new TemplateUrlServiceAndroid(env, obj);
201 return reinterpret_cast<intptr_t>(template_url_service_android);
204 // static
205 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) {
206 return RegisterNativesImpl(env);