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/bind.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/google/google_util.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/browser/search_engines/search_terms_data.h" | 15 #include "chrome/browser/search_engines/search_terms_data.h" |
15 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 17 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 18 #include "chrome/browser/search_engines/template_url_service.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/browser/search_engines/util.h" |
18 #include "jni/TemplateUrlService_jni.h" | 21 #include "jni/TemplateUrlService_jni.h" |
| 22 #include "net/base/url_util.h" |
19 | 23 |
20 using base::android::ConvertJavaStringToUTF16; | 24 using base::android::ConvertJavaStringToUTF16; |
21 using base::android::ConvertUTF16ToJavaString; | 25 using base::android::ConvertUTF16ToJavaString; |
22 using base::android::ConvertUTF8ToJavaString; | 26 using base::android::ConvertUTF8ToJavaString; |
23 | 27 |
24 namespace { | 28 namespace { |
25 | 29 |
26 Profile* GetOriginalProfile() { | 30 Profile* GetOriginalProfile() { |
27 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); | 31 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); |
28 } | 32 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 if (default_provider && | 152 if (default_provider && |
149 default_provider->url_ref().SupportsReplacement() && !query.empty()) { | 153 default_provider->url_ref().SupportsReplacement() && !query.empty()) { |
150 url = default_provider->url_ref().ReplaceSearchTerms( | 154 url = default_provider->url_ref().ReplaceSearchTerms( |
151 TemplateURLRef::SearchTermsArgs(query)); | 155 TemplateURLRef::SearchTermsArgs(query)); |
152 } | 156 } |
153 | 157 |
154 return ConvertUTF8ToJavaString(env, url); | 158 return ConvertUTF8ToJavaString(env, url); |
155 } | 159 } |
156 | 160 |
157 base::android::ScopedJavaLocalRef<jstring> | 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; |
| 167 |
| 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(); |
| 173 } |
| 174 |
| 175 return ConvertUTF8ToJavaString(env, url); |
| 176 } |
| 177 |
| 178 base::android::ScopedJavaLocalRef<jstring> |
158 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv* env, | 179 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv* env, |
159 jobject obj, | 180 jobject obj, |
160 jstring jquery, | 181 jstring jquery, |
161 jstring jcurrent_url) { | 182 jstring jcurrent_url) { |
162 TemplateURL* default_provider = | 183 TemplateURL* default_provider = |
163 template_url_service_->GetDefaultSearchProvider(); | 184 template_url_service_->GetDefaultSearchProvider(); |
164 | 185 |
165 base::string16 query(ConvertJavaStringToUTF16(env, jquery)); | 186 base::string16 query(ConvertJavaStringToUTF16(env, jquery)); |
166 GURL current_url(ConvertJavaStringToUTF16(env, jcurrent_url)); | 187 GURL current_url(ConvertJavaStringToUTF16(env, jcurrent_url)); |
167 GURL destination_url(current_url); | 188 GURL destination_url(current_url); |
168 if (default_provider && !query.empty()) { | 189 if (default_provider && !query.empty()) { |
169 bool refined_query = default_provider->ReplaceSearchTermsInURL(current_url, | 190 bool refined_query = default_provider->ReplaceSearchTermsInURL(current_url, |
170 TemplateURLRef::SearchTermsArgs(query), &destination_url); | 191 TemplateURLRef::SearchTermsArgs(query), &destination_url); |
171 if (refined_query) | 192 if (refined_query) |
172 return ConvertUTF8ToJavaString(env, destination_url.spec()); | 193 return ConvertUTF8ToJavaString(env, destination_url.spec()); |
173 } | 194 } |
174 return base::android::ScopedJavaLocalRef<jstring>(env, NULL); | 195 return base::android::ScopedJavaLocalRef<jstring>(env, NULL); |
175 } | 196 } |
176 | 197 |
177 static jlong Init(JNIEnv* env, jobject obj) { | 198 static jlong Init(JNIEnv* env, jobject obj) { |
178 TemplateUrlServiceAndroid* template_url_service_android = | 199 TemplateUrlServiceAndroid* template_url_service_android = |
179 new TemplateUrlServiceAndroid(env, obj); | 200 new TemplateUrlServiceAndroid(env, obj); |
180 return reinterpret_cast<intptr_t>(template_url_service_android); | 201 return reinterpret_cast<intptr_t>(template_url_service_android); |
181 } | 202 } |
182 | 203 |
183 // static | 204 // static |
184 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | 205 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { |
185 return RegisterNativesImpl(env); | 206 return RegisterNativesImpl(env); |
186 } | 207 } |
OLD | NEW |