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/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/chrome_notification_types.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" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
20 #include "jni/TemplateUrlService_jni.h" | 20 #include "jni/TemplateUrlService_jni.h" |
21 | 21 |
| 22 using base::android::ConvertJavaStringToUTF16; |
22 using base::android::ConvertUTF16ToJavaString; | 23 using base::android::ConvertUTF16ToJavaString; |
| 24 using base::android::ConvertUTF8ToJavaString; |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 Profile* GetOriginalProfile() { | 28 Profile* GetOriginalProfile() { |
27 return g_browser_process->profile_manager()->GetDefaultProfile()-> | 29 return g_browser_process->profile_manager()->GetDefaultProfile()-> |
28 GetOriginalProfile(); | 30 GetOriginalProfile(); |
29 } | 31 } |
30 | 32 |
31 } // namespace | 33 } // namespace |
32 | 34 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 env, | 133 env, |
132 index, | 134 index, |
133 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), | 135 ConvertUTF16ToJavaString(env, template_url->short_name()).obj(), |
134 ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); | 136 ConvertUTF16ToJavaString(env, template_url->keyword()).obj()); |
135 } | 137 } |
136 | 138 |
137 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) { | 139 bool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) { |
138 return url->prepopulate_id() > 0; | 140 return url->prepopulate_id() > 0; |
139 } | 141 } |
140 | 142 |
| 143 base::android::ScopedJavaLocalRef<jstring> |
| 144 TemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv* env, |
| 145 jobject obj, |
| 146 jstring jquery) { |
| 147 const TemplateURL* default_provider = |
| 148 template_url_service_->GetDefaultSearchProvider(); |
| 149 |
| 150 string16 query(ConvertJavaStringToUTF16(env, jquery)); |
| 151 |
| 152 std::string url; |
| 153 if (default_provider && |
| 154 default_provider->url_ref().SupportsReplacement() && !query.empty()) { |
| 155 url = default_provider->url_ref().ReplaceSearchTerms( |
| 156 TemplateURLRef::SearchTermsArgs(query)); |
| 157 } |
| 158 |
| 159 return ConvertUTF8ToJavaString(env, url); |
| 160 } |
| 161 |
| 162 base::android::ScopedJavaLocalRef<jstring> |
| 163 TemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv* env, |
| 164 jobject obj, |
| 165 jstring jquery, |
| 166 jstring jcurrent_url) { |
| 167 TemplateURL* default_provider = |
| 168 template_url_service_->GetDefaultSearchProvider(); |
| 169 |
| 170 string16 query(ConvertJavaStringToUTF16(env, jquery)); |
| 171 GURL current_url(ConvertJavaStringToUTF16(env, jcurrent_url)); |
| 172 GURL destination_url(current_url); |
| 173 if (default_provider && !query.empty()) { |
| 174 bool refined_query = default_provider->ReplaceSearchTermsInURL(current_url, |
| 175 TemplateURLRef::SearchTermsArgs(query), &destination_url); |
| 176 if (refined_query) |
| 177 return ConvertUTF8ToJavaString(env, destination_url.spec()); |
| 178 } |
| 179 return base::android::ScopedJavaLocalRef<jstring>(env, NULL); |
| 180 } |
| 181 |
141 static jint Init(JNIEnv* env, jobject obj) { | 182 static jint Init(JNIEnv* env, jobject obj) { |
142 TemplateUrlServiceAndroid* template_url_service_android = | 183 TemplateUrlServiceAndroid* template_url_service_android = |
143 new TemplateUrlServiceAndroid(env, obj); | 184 new TemplateUrlServiceAndroid(env, obj); |
144 return reinterpret_cast<jint>(template_url_service_android); | 185 return reinterpret_cast<jint>(template_url_service_android); |
145 } | 186 } |
146 | 187 |
147 // static | 188 // static |
148 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | 189 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { |
149 return RegisterNativesImpl(env); | 190 return RegisterNativesImpl(env); |
150 } | 191 } |
OLD | NEW |