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"
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
;
30 Profile
* GetOriginalProfile() {
31 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
36 TemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv
* env
,
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::SetUserSelectedDefaultSearchProvider(
57 jint selected_index
) {
58 std::vector
<TemplateURL
*> template_urls
=
59 template_url_service_
->GetTemplateURLs();
60 size_t selected_index_size_t
= static_cast<size_t>(selected_index
);
61 DCHECK_LT(selected_index_size_t
, template_urls
.size()) <<
62 "Wrong index for search engine";
64 TemplateURL
* template_url
= template_urls
[selected_index_size_t
];
65 DCHECK_GT(template_url
->prepopulate_id(), 0) <<
66 "Tried to select non-prepopulated search engine";
67 template_url_service_
->SetUserSelectedDefaultSearchProvider(template_url
);
70 jint
TemplateUrlServiceAndroid::GetDefaultSearchProvider(JNIEnv
* env
,
72 std::vector
<TemplateURL
*> template_urls
=
73 template_url_service_
->GetTemplateURLs();
74 TemplateURL
* default_search_provider
=
75 template_url_service_
->GetDefaultSearchProvider();
76 for (size_t i
= 0; i
< template_urls
.size(); ++i
) {
77 if (default_search_provider
== template_urls
[i
])
83 jboolean
TemplateUrlServiceAndroid::IsLoaded(JNIEnv
* env
, jobject obj
) {
84 return template_url_service_
->loaded();
87 jint
TemplateUrlServiceAndroid::GetTemplateUrlCount(JNIEnv
* env
, jobject obj
) {
88 return template_url_service_
->GetTemplateURLs().size();
91 jboolean
TemplateUrlServiceAndroid::IsSearchProviderManaged(JNIEnv
* env
,
93 return template_url_service_
->is_default_search_managed();
96 jboolean
TemplateUrlServiceAndroid::IsSearchByImageAvailable(JNIEnv
* env
,
98 const TemplateURL
* default_search_provider
=
99 template_url_service_
->GetDefaultSearchProvider();
100 return default_search_provider
&&
101 !default_search_provider
->image_url().empty() &&
102 default_search_provider
->image_url_ref().IsValid();
105 jboolean
TemplateUrlServiceAndroid::IsDefaultSearchEngineGoogle(JNIEnv
* env
,
107 TemplateURL
* default_search_provider
=
108 template_url_service_
->GetDefaultSearchProvider();
109 return default_search_provider
&&
110 default_search_provider
->url_ref().HasGoogleBaseURLs();
113 base::android::ScopedJavaLocalRef
<jobject
>
114 TemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlAt(JNIEnv
* env
,
117 TemplateURL
* template_url
= template_url_service_
->GetTemplateURLs()[index
];
118 if (!IsPrepopulatedTemplate(template_url
) &&
119 !template_url
->created_by_policy())
120 return ScopedJavaLocalRef
<jobject
>();
122 return Java_TemplateUrl_create(
125 ConvertUTF16ToJavaString(env
, template_url
->short_name()).obj(),
126 ConvertUTF16ToJavaString(env
, template_url
->keyword()).obj());
129 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL
* url
) {
130 return url
->prepopulate_id() > 0;
133 void TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded() {
134 template_url_subscription_
.reset();
135 JNIEnv
* env
= base::android::AttachCurrentThread();
136 if (weak_java_obj_
.get(env
).is_null())
139 Java_TemplateUrlService_templateUrlServiceLoaded(
140 env
, weak_java_obj_
.get(env
).obj());
143 base::android::ScopedJavaLocalRef
<jstring
>
144 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv
* env
,
147 const TemplateURL
* default_provider
=
148 template_url_service_
->GetDefaultSearchProvider();
150 base::string16
query(ConvertJavaStringToUTF16(env
, jquery
));
153 if (default_provider
&&
154 default_provider
->url_ref().SupportsReplacement() && !query
.empty()) {
155 url
= default_provider
->url_ref().ReplaceSearchTerms(
156 TemplateURLRef::SearchTermsArgs(query
));
159 return ConvertUTF8ToJavaString(env
, url
);
162 base::android::ScopedJavaLocalRef
<jstring
>
163 TemplateUrlServiceAndroid::GetUrlForVoiceSearchQuery(JNIEnv
* env
,
166 base::string16
query(ConvertJavaStringToUTF16(env
, jquery
));
169 if (!query
.empty()) {
170 GURL gurl
= GetDefaultSearchURLForSearchTerms(GetOriginalProfile(), query
);
171 if (google_util::IsGoogleSearchUrl(gurl
))
172 gurl
= net::AppendQueryParameter(gurl
, "inm", "vs");
176 return ConvertUTF8ToJavaString(env
, url
);
179 base::android::ScopedJavaLocalRef
<jstring
>
180 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv
* env
,
183 jstring jcurrent_url
) {
184 TemplateURL
* default_provider
=
185 template_url_service_
->GetDefaultSearchProvider();
187 base::string16
query(ConvertJavaStringToUTF16(env
, jquery
));
188 GURL
current_url(ConvertJavaStringToUTF16(env
, jcurrent_url
));
189 GURL
destination_url(current_url
);
190 if (default_provider
&& !query
.empty()) {
191 bool refined_query
= default_provider
->ReplaceSearchTermsInURL(current_url
,
192 TemplateURLRef::SearchTermsArgs(query
), &destination_url
);
194 return ConvertUTF8ToJavaString(env
, destination_url
.spec());
196 return base::android::ScopedJavaLocalRef
<jstring
>(env
, NULL
);
199 static jlong
Init(JNIEnv
* env
, jobject obj
) {
200 TemplateUrlServiceAndroid
* template_url_service_android
=
201 new TemplateUrlServiceAndroid(env
, obj
);
202 return reinterpret_cast<intptr_t>(template_url_service_android
);
206 bool TemplateUrlServiceAndroid::Register(JNIEnv
* env
) {
207 return RegisterNativesImpl(env
);