Home | History | Annotate | Download | only in search_engines
      1 // Copyright 2012 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.
      4 
      5 #include "chrome/browser/search_engines/search_terms_data.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/logging.h"
      9 #include "base/metrics/field_trial.h"
     10 #include "base/prefs/pref_service.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "chrome/browser/browser_process.h"
     13 #include "chrome/browser/google/google_url_tracker.h"
     14 #include "chrome/browser/google/google_util.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 #include "chrome/browser/search/search.h"
     17 #include "chrome/browser/themes/theme_service.h"
     18 #include "chrome/browser/themes/theme_service_factory.h"
     19 #include "chrome/common/chrome_switches.h"
     20 #include "chrome/common/pref_names.h"
     21 #include "content/public/browser/browser_thread.h"
     22 #include "url/gurl.h"
     23 
     24 #if defined(ENABLE_RLZ)
     25 #include "chrome/browser/rlz/rlz.h"
     26 #endif
     27 
     28 using content::BrowserThread;
     29 
     30 SearchTermsData::SearchTermsData() {
     31 }
     32 
     33 SearchTermsData::~SearchTermsData() {
     34 }
     35 
     36 std::string SearchTermsData::GoogleBaseURLValue() const {
     37   return GoogleURLTracker::kDefaultGoogleHomepage;
     38 }
     39 
     40 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
     41   // Start with the Google base URL.
     42   const GURL base_url(GoogleBaseURLValue());
     43   DCHECK(base_url.is_valid());
     44 
     45   GURL::Replacements repl;
     46 
     47   // Replace any existing path with "/complete/".
     48   // SetPathStr() requires its argument to stay in scope as long as |repl| is,
     49   // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in
     50   // a variable.
     51   const std::string suggest_path("/complete/");
     52   repl.SetPathStr(suggest_path);
     53 
     54   // Clear the query and ref.
     55   repl.ClearQuery();
     56   repl.ClearRef();
     57   return base_url.ReplaceComponents(repl).spec();
     58 }
     59 
     60 std::string SearchTermsData::GetApplicationLocale() const {
     61   return "en";
     62 }
     63 
     64 string16 SearchTermsData::GetRlzParameterValue() const {
     65   return string16();
     66 }
     67 
     68 std::string SearchTermsData::GetSearchClient() const {
     69   return std::string();
     70 }
     71 
     72 std::string SearchTermsData::GetSuggestClient() const {
     73   return std::string();
     74 }
     75 
     76 std::string SearchTermsData::InstantEnabledParam() const {
     77   return std::string();
     78 }
     79 
     80 std::string SearchTermsData::InstantExtendedEnabledParam() const {
     81   return std::string();
     82 }
     83 
     84 std::string SearchTermsData::NTPIsThemedParam() const {
     85   return std::string();
     86 }
     87 
     88 // static
     89 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
     90 
     91 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
     92     : profile_(profile) {
     93   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
     94       BrowserThread::CurrentlyOn(BrowserThread::UI));
     95 }
     96 
     97 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
     98   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
     99       BrowserThread::CurrentlyOn(BrowserThread::UI));
    100   if (google_base_url_)
    101     return *google_base_url_;
    102   std::string base_url = CommandLine::ForCurrentProcess()->
    103       GetSwitchValueASCII(switches::kGoogleBaseURL);
    104   if (!base_url.empty())
    105     return base_url;
    106   return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
    107       SearchTermsData::GoogleBaseURLValue();
    108 }
    109 
    110 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
    111   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    112       BrowserThread::CurrentlyOn(BrowserThread::UI));
    113   return g_browser_process->GetApplicationLocale();
    114 }
    115 
    116 // Android implementations are located in search_terms_data_android.cc.
    117 #if !defined(OS_ANDROID)
    118 string16 UIThreadSearchTermsData::GetRlzParameterValue() const {
    119   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    120       BrowserThread::CurrentlyOn(BrowserThread::UI));
    121   string16 rlz_string;
    122 #if defined(ENABLE_RLZ)
    123   // For organic brandcodes do not use rlz at all. Empty brandcode usually
    124   // means a chromium install. This is ok.
    125   std::string brand;
    126   if (google_util::GetBrand(&brand) && !brand.empty() &&
    127       !google_util::IsOrganic(brand)) {
    128     // This call will return false the first time(s) it is called until the
    129     // value has been cached. This normally would mean that at most one omnibox
    130     // search might not send the RLZ data but this is not really a problem.
    131     RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
    132   }
    133 #endif
    134   return rlz_string;
    135 }
    136 
    137 // We can enable this on non-Android if other platforms ever want a non-empty
    138 // search client string.  There is already a unit test in place for Android
    139 // called TemplateURLTest::SearchClient.
    140 std::string UIThreadSearchTermsData::GetSearchClient() const {
    141   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    142       BrowserThread::CurrentlyOn(BrowserThread::UI));
    143   return std::string();
    144 }
    145 #endif
    146 
    147 std::string UIThreadSearchTermsData::GetSuggestClient() const {
    148   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    149       BrowserThread::CurrentlyOn(BrowserThread::UI));
    150   return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
    151 }
    152 
    153 std::string UIThreadSearchTermsData::InstantEnabledParam() const {
    154   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    155          BrowserThread::CurrentlyOn(BrowserThread::UI));
    156   return chrome::IsInstantExtendedAPIEnabled() ? std::string() : "ion=1&";
    157 }
    158 
    159 std::string UIThreadSearchTermsData::InstantExtendedEnabledParam() const {
    160   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    161          BrowserThread::CurrentlyOn(BrowserThread::UI));
    162   uint64 instant_extended_api_version = chrome::EmbeddedSearchPageVersion();
    163   if (instant_extended_api_version) {
    164     return std::string(google_util::kInstantExtendedAPIParam) + "=" +
    165         base::Uint64ToString(instant_extended_api_version) + "&";
    166   }
    167   return std::string();
    168 }
    169 
    170 std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
    171   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
    172          BrowserThread::CurrentlyOn(BrowserThread::UI));
    173 #if defined(ENABLE_THEMES)
    174   if (!chrome::IsInstantExtendedAPIEnabled())
    175     return std::string();
    176 
    177   // TODO(dhollowa): Determine fraction of custom themes that don't affect the
    178   // NTP background and/or color.
    179   ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
    180   if (theme_service && !theme_service->UsingDefaultTheme())
    181     return "es_th=1&";
    182 #endif  // defined(ENABLE_THEMES)
    183 
    184   return std::string();
    185 }
    186 
    187 // static
    188 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) {
    189   delete google_base_url_;
    190   google_base_url_ = base_url.empty() ? NULL : new std::string(base_url);
    191 }
    192