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