12cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// Use of this source code is governed by a BSD-style license that can be
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// found in the LICENSE file.
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "chrome/browser/search_engines/template_url_service_android.h"
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/android/jni_string.h"
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/bind.h"
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/format_macros.h"
102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/strings/stringprintf.h"
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/strings/utf_string_conversions.h"
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "chrome/browser/profiles/profile_manager.h"
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "chrome/browser/search_engines/template_url_service_factory.h"
142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "components/google/core/browser/google_util.h"
152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "components/search_engines/template_url.h"
162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "components/search_engines/template_url_prepopulate_data.h"
172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "components/search_engines/template_url_service.h"
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "components/search_engines/util.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "jni/TemplateUrlService_jni.h"
207c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "net/base/url_util.h"
217c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
2214f79002e58556798e86168c63e48d533287eda5Douglas Gregornamespace {
2314f79002e58556798e86168c63e48d533287eda5Douglas Gregor
24bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas GregorProfile* GetOriginalProfile() {
252bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor  return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2814f79002e58556798e86168c63e48d533287eda5Douglas Gregor}  // namespace
293c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner
302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::TemplateUrlServiceAndroid(JNIEnv* env,
312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                     jobject obj)
322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    : weak_java_obj_(env, obj),
332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_(
342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) {
352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  template_url_subscription_ =
362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->RegisterOnLoadedCallback(
372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          base::Bind(&TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded,
382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                     base::Unretained(this)));
392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::~TemplateUrlServiceAndroid() {
422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid TemplateUrlServiceAndroid::Load(JNIEnv* env, jobject obj) {
452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  template_url_service_->Load();
462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid TemplateUrlServiceAndroid::SetUserSelectedDefaultSearchProvider(
492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    JNIEnv* env,
502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jobject obj,
512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jint selected_index) {
522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  std::vector<TemplateURL*> template_urls =
532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetTemplateURLs();
542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  size_t selected_index_size_t = static_cast<size_t>(selected_index);
552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DCHECK_LT(selected_index_size_t, template_urls.size()) <<
562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      "Wrong index for search engine";
572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateURL* template_url = template_urls[selected_index_size_t];
592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DCHECK_GT(template_url->prepopulate_id(), 0) <<
602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      "Tried to select non-prepopulated search engine";
612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjint TemplateUrlServiceAndroid::GetDefaultSearchProvider(JNIEnv* env,
652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                         jobject obj) {
662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  std::vector<TemplateURL*> template_urls =
672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetTemplateURLs();
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateURL* default_search_provider =
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetDefaultSearchProvider();
702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (size_t i = 0; i < template_urls.size(); ++i) {
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (default_search_provider == template_urls[i])
722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      return i;
732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return -1;
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjboolean TemplateUrlServiceAndroid::IsLoaded(JNIEnv* env, jobject obj) {
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return template_url_service_->loaded();
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjint TemplateUrlServiceAndroid::GetTemplateUrlCount(JNIEnv* env, jobject obj) {
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return template_url_service_->GetTemplateURLs().size();
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjboolean TemplateUrlServiceAndroid::IsSearchProviderManaged(JNIEnv* env,
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                            jobject obj) {
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return template_url_service_->is_default_search_managed();
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjboolean TemplateUrlServiceAndroid::IsSearchByImageAvailable(JNIEnv* env,
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                             jobject obj) {
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const TemplateURL* default_search_provider =
932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetDefaultSearchProvider();
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return default_search_provider &&
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      !default_search_provider->image_url().empty() &&
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      default_search_provider->image_url_ref().IsValid(
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          template_url_service_->search_terms_data());
982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorjboolean TemplateUrlServiceAndroid::IsDefaultSearchEngineGoogle(JNIEnv* env,
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                                jobject obj) {
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateURL* default_search_provider =
1032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetDefaultSearchProvider();
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return default_search_provider &&
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      default_search_provider->url_ref().HasGoogleBaseURLs(
1062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          template_url_service_->search_terms_data());
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbase::android::ScopedJavaLocalRef<jobject>
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::GetPrepopulatedTemplateUrlAt(JNIEnv* env,
1112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                        jobject obj,
1122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                        jint index) {
1132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateURL* template_url = template_url_service_->GetTemplateURLs()[index];
1142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!IsPrepopulatedTemplate(template_url) &&
1152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      !template_url->created_by_policy())
1162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor   return ScopedJavaLocalRef<jobject>();
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return Java_TemplateUrl_create(
1192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      env,
1202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      index,
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      base::android::ConvertUTF16ToJavaString(
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          env, template_url->short_name()).obj(),
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      base::android::ConvertUTF16ToJavaString(
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          env, template_url->keyword()).obj());
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbool TemplateUrlServiceAndroid::IsPrepopulatedTemplate(TemplateURL* url) {
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return url->prepopulate_id() > 0;
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded() {
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  template_url_subscription_.reset();
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  JNIEnv* env = base::android::AttachCurrentThread();
1342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (weak_java_obj_.get(env).is_null())
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return;
1362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Java_TemplateUrlService_templateUrlServiceLoaded(
1382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      env, weak_java_obj_.get(env).obj());
1392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbase::android::ScopedJavaLocalRef<jstring>
1422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::GetUrlForSearchQuery(JNIEnv* env,
1432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                jobject obj,
1442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                jstring jquery) {
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const TemplateURL* default_provider =
1462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetDefaultSearchProvider();
1472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  base::string16 query(base::android::ConvertJavaStringToUTF16(env, jquery));
1492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  std::string url;
1512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (default_provider &&
1522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      default_provider->url_ref().SupportsReplacement(
1532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          template_url_service_->search_terms_data()) &&
1542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      !query.empty()) {
1552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    url = default_provider->url_ref().ReplaceSearchTerms(
1562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        TemplateURLRef::SearchTermsArgs(query),
1572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        template_url_service_->search_terms_data());
1582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return base::android::ConvertUTF8ToJavaString(env, url);
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbase::android::ScopedJavaLocalRef<jstring>
1642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::GetUrlForVoiceSearchQuery(JNIEnv* env,
1652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                     jobject obj,
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                     jstring jquery) {
1672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  base::string16 query(base::android::ConvertJavaStringToUTF16(env, jquery));
1682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  std::string url;
1692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!query.empty()) {
1712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    GURL gurl(GetDefaultSearchURLForSearchTerms(template_url_service_, query));
1722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (google_util::IsGoogleSearchUrl(gurl))
1732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      gurl = net::AppendQueryParameter(gurl, "inm", "vs");
1742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    url = gurl.spec();
1752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return base::android::ConvertUTF8ToJavaString(env, url);
1782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbase::android::ScopedJavaLocalRef<jstring>
1812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::ReplaceSearchTermsInUrl(JNIEnv* env,
1822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                   jobject obj,
1832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                   jstring jquery,
1842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                   jstring jcurrent_url) {
1852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateURL* default_provider =
1862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      template_url_service_->GetDefaultSearchProvider();
1872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  base::string16 query(base::android::ConvertJavaStringToUTF16(env, jquery));
1892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  GURL current_url(base::android::ConvertJavaStringToUTF16(env, jcurrent_url));
1902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  GURL destination_url(current_url);
1912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (default_provider && !query.empty()) {
1922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    bool refined_query = default_provider->ReplaceSearchTermsInURL(
1932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        current_url, TemplateURLRef::SearchTermsArgs(query),
1942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        template_url_service_->search_terms_data(), &destination_url);
1952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (refined_query)
1962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      return base::android::ConvertUTF8ToJavaString(
1972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          env, destination_url.spec());
1982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return base::android::ScopedJavaLocalRef<jstring>(env, NULL);
2002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbase::android::ScopedJavaLocalRef<jstring>
2032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorTemplateUrlServiceAndroid::GetUrlForContextualSearchQuery(
2042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    JNIEnv* env,
2052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jobject obj,
2062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jstring jquery,
2072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jstring jalternate_term,
2082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    jboolean jshould_prefetch) {
2092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  base::string16 query(base::android::ConvertJavaStringToUTF16(env, jquery));
2102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  std::string url;
2112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!query.empty()) {
2132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    GURL gurl(GetDefaultSearchURLForSearchTerms(template_url_service_, query));
2142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (google_util::IsGoogleSearchUrl(gurl)) {
2152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      gurl = net::AppendQueryParameter(gurl, "ctxs", "2");
2162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (jshould_prefetch) {
2172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        // Indicate that the search page is being prefetched.
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        gurl = net::AppendQueryParameter(gurl, "pf", "c");
2192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      }
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (jalternate_term) {
2222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        std::string alternate_term(
2232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor            base::android::ConvertJavaStringToUTF8(env, jalternate_term));
2242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        if (!alternate_term.empty()) {
2252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          gurl = net::AppendQueryParameter(
2262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor              gurl, "ctxsl_alternate_term", alternate_term);
2272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        }
2282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      }
2292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
2302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    url = gurl.spec();
2312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
2322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return base::android::ConvertUTF8ToJavaString(env, url);
2342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorstatic jlong Init(JNIEnv* env, jobject obj) {
2372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  TemplateUrlServiceAndroid* template_url_service_android =
2382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      new TemplateUrlServiceAndroid(env, obj);
2392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return reinterpret_cast<intptr_t>(template_url_service_android);
2402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// static
2432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbool TemplateUrlServiceAndroid::Register(JNIEnv* env) {
2442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return RegisterNativesImpl(env);
2452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor