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