ui_thread_search_terms_data.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2014 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/ui_thread_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 "chrome/browser/browser_process.h"
12#include "chrome/browser/google/google_brand.h"
13#include "chrome/browser/google/google_profile_helper.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/search/instant_service.h"
16#include "chrome/browser/search/instant_service_factory.h"
17#include "chrome/browser/search/search.h"
18#include "chrome/browser/sync/glue/device_info.h"
19#include "chrome/browser/themes/theme_service.h"
20#include "chrome/browser/themes/theme_service_factory.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/chrome_version_info.h"
23#include "chrome/common/pref_names.h"
24#include "components/google/core/browser/google_util.h"
25#include "components/omnibox/omnibox_field_trial.h"
26#include "components/search/search.h"
27#include "content/public/browser/browser_thread.h"
28#include "sync/protocol/sync.pb.h"
29#include "url/gurl.h"
30
31#if defined(ENABLE_RLZ)
32#include "chrome/browser/rlz/rlz.h"
33#endif
34
35using content::BrowserThread;
36
37// static
38std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
39
40UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
41    : profile_(profile) {
42  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
43      BrowserThread::CurrentlyOn(BrowserThread::UI));
44}
45
46std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
47  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
48      BrowserThread::CurrentlyOn(BrowserThread::UI));
49  if (google_base_url_)
50    return *google_base_url_;
51  GURL base_url(google_util::CommandLineGoogleBaseURL());
52  if (base_url.is_valid())
53    return base_url.spec();
54  return profile_ ?
55      google_profile_helper::GetGoogleHomePageURL(profile_).spec() :
56      SearchTermsData::GoogleBaseURLValue();
57}
58
59std::string UIThreadSearchTermsData::GetApplicationLocale() const {
60  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
61      BrowserThread::CurrentlyOn(BrowserThread::UI));
62  return g_browser_process->GetApplicationLocale();
63}
64
65// Android implementations are in ui_thread_search_terms_data_android.cc.
66#if !defined(OS_ANDROID)
67base::string16 UIThreadSearchTermsData::GetRlzParameterValue(
68    bool from_app_list) const {
69  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
70      BrowserThread::CurrentlyOn(BrowserThread::UI));
71  base::string16 rlz_string;
72#if defined(ENABLE_RLZ)
73  // For organic brandcodes do not use rlz at all. Empty brandcode usually
74  // means a chromium install. This is ok.
75  std::string brand;
76  if (google_brand::GetBrand(&brand) && !brand.empty() &&
77      !google_brand::IsOrganic(brand)) {
78    // This call will return false the first time(s) it is called until the
79    // value has been cached. This normally would mean that at most one omnibox
80    // search might not send the RLZ data but this is not really a problem.
81    rlz_lib::AccessPoint access_point = RLZTracker::ChromeOmnibox();
82#if !defined(OS_IOS)
83    if (from_app_list)
84      access_point = RLZTracker::ChromeAppList();
85#endif
86    RLZTracker::GetAccessPointRlz(access_point, &rlz_string);
87  }
88#endif
89  return rlz_string;
90}
91
92// We can enable this on non-Android if other platforms ever want a non-empty
93// search client string.  There is already a unit test in place for Android
94// called TemplateURLTest::SearchClient.
95std::string UIThreadSearchTermsData::GetSearchClient() const {
96  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
97      BrowserThread::CurrentlyOn(BrowserThread::UI));
98  return std::string();
99}
100#endif
101
102std::string UIThreadSearchTermsData::GetSuggestClient() const {
103  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
104      BrowserThread::CurrentlyOn(BrowserThread::UI));
105#if defined(OS_ANDROID)
106  sync_pb::SyncEnums::DeviceType device_type =
107      browser_sync::DeviceInfo::GetLocalDeviceType();
108  return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ?
109    "chrome" : "chrome-omni";
110#else
111  return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
112#endif
113}
114
115std::string UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
116  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
117      BrowserThread::CurrentlyOn(BrowserThread::UI));
118#if defined(OS_ANDROID)
119  sync_pb::SyncEnums::DeviceType device_type =
120      browser_sync::DeviceInfo::GetLocalDeviceType();
121  if (device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE) {
122    return OmniboxFieldTrial::EnableAnswersInSuggest() ?
123        "chrome-mobile-ext-ansg" : "chrome-mobile-ext";
124  }
125  return OmniboxFieldTrial::EnableAnswersInSuggest() ?
126      "chrome-ext-ansg" : "chrome-ext";
127#elif defined(OS_IOS)
128  return OmniboxFieldTrial::EnableAnswersInSuggest() ?
129      "chrome-ext-ansg" : "chrome-ext";
130#else
131  return "chrome-ext";
132#endif
133}
134
135bool UIThreadSearchTermsData::EnableAnswersInSuggest() const {
136  return OmniboxFieldTrial::EnableAnswersInSuggest();
137}
138
139bool UIThreadSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
140  return chrome::IsInstantExtendedAPIEnabled() &&
141      chrome::IsQueryExtractionEnabled();
142}
143
144std::string UIThreadSearchTermsData::InstantExtendedEnabledParam(
145    bool for_search) const {
146  return chrome::InstantExtendedEnabledParam(for_search);
147}
148
149std::string UIThreadSearchTermsData::ForceInstantResultsParam(
150    bool for_prerender) const {
151  return chrome::ForceInstantResultsParam(for_prerender);
152}
153
154int UIThreadSearchTermsData::OmniboxStartMargin() const {
155  InstantService* instant_service =
156      InstantServiceFactory::GetForProfile(profile_);
157  // Android and iOS have no InstantService.
158  return instant_service ?
159      instant_service->omnibox_start_margin() : chrome::kDisableStartMargin;
160}
161
162std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
163  DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
164         BrowserThread::CurrentlyOn(BrowserThread::UI));
165#if defined(ENABLE_THEMES)
166  if (!chrome::IsInstantExtendedAPIEnabled())
167    return std::string();
168
169  // TODO(dhollowa): Determine fraction of custom themes that don't affect the
170  // NTP background and/or color.
171  ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
172  // NTP is considered themed if the theme is not default and not native (GTK+).
173  if (theme_service && !theme_service->UsingDefaultTheme() &&
174      !theme_service->UsingSystemTheme())
175    return "es_th=1&";
176#endif  // defined(ENABLE_THEMES)
177
178  return std::string();
179}
180
181// It's acutally OK to call this method on any thread, but it's currently placed
182// in UIThreadSearchTermsData since SearchTermsData cannot depend on
183// VersionInfo.
184std::string UIThreadSearchTermsData::GoogleImageSearchSource() const {
185  chrome::VersionInfo version_info;
186  if (version_info.is_valid()) {
187    std::string version(version_info.Name() + " " + version_info.Version());
188    if (version_info.IsOfficialBuild())
189      version += " (Official)";
190    version += " " + version_info.OSType();
191    std::string modifier(version_info.GetVersionStringModifier());
192    if (!modifier.empty())
193      version += " " + modifier;
194    return version;
195  }
196  return "unknown";
197}
198
199// static
200void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) {
201  delete google_base_url_;
202  google_base_url_ = base_url.empty() ? NULL : new std::string(base_url);
203}
204