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::SetDefaultSearchProvider(JNIEnv
* env
,
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
,
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
])
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
,
92 return template_url_service_
->is_default_search_managed();
95 jboolean
TemplateUrlServiceAndroid::IsSearchByImageAvailable(JNIEnv
* env
,
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
,
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
,
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(
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())
138 Java_TemplateUrlService_templateUrlServiceLoaded(
139 env
, weak_java_obj_
.get(env
).obj());
142 base::android::ScopedJavaLocalRef
<jstring
>
143 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv
* env
,
146 const TemplateURL
* default_provider
=
147 template_url_service_
->GetDefaultSearchProvider();
149 base::string16
query(ConvertJavaStringToUTF16(env
, jquery
));
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
,
165 base::string16
query(ConvertJavaStringToUTF16(env
, jquery
));
168 if (!query
.empty()) {
169 GURL gurl
= GetDefaultSearchURLForSearchTerms(GetOriginalProfile(), query
);
170 if (google_util::IsGoogleSearchUrl(gurl
))
171 gurl
= net::AppendQueryParameter(gurl
, "inm", "vs");
175 return ConvertUTF8ToJavaString(env
, url
);
178 base::android::ScopedJavaLocalRef
<jstring
>
179 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv
* env
,
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
);
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
);
205 bool TemplateUrlServiceAndroid::Register(JNIEnv
* env
) {
206 return RegisterNativesImpl(env
);