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.
5 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
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/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 "components/google/core/browser/google_util.h"
23 #include "components/omnibox/omnibox_field_trial.h"
24 #include "components/search/search.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "ui/base/device_form_factor.h"
29 #if defined(ENABLE_RLZ)
30 #include "chrome/browser/rlz/rlz.h"
33 using content::BrowserThread;
36 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
38 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
40 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
41 BrowserThread::CurrentlyOn(BrowserThread::UI));
44 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
45 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
46 BrowserThread::CurrentlyOn(BrowserThread::UI));
48 return *google_base_url_;
49 GURL base_url(google_util::CommandLineGoogleBaseURL());
50 if (base_url.is_valid())
51 return base_url.spec();
53 google_profile_helper::GetGoogleHomePageURL(profile_).spec() :
54 SearchTermsData::GoogleBaseURLValue();
57 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
58 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
59 BrowserThread::CurrentlyOn(BrowserThread::UI));
60 return g_browser_process->GetApplicationLocale();
63 // Android implementations are in ui_thread_search_terms_data_android.cc.
64 #if !defined(OS_ANDROID)
65 base::string16 UIThreadSearchTermsData::GetRlzParameterValue(
66 bool from_app_list) const {
67 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
68 BrowserThread::CurrentlyOn(BrowserThread::UI));
69 base::string16 rlz_string;
70 #if defined(ENABLE_RLZ)
71 // For organic brandcodes do not use rlz at all. Empty brandcode usually
72 // means a chromium install. This is ok.
74 if (google_brand::GetBrand(&brand) && !brand.empty() &&
75 !google_brand::IsOrganic(brand)) {
76 // This call will return false the first time(s) it is called until the
77 // value has been cached. This normally would mean that at most one omnibox
78 // search might not send the RLZ data but this is not really a problem.
79 rlz_lib::AccessPoint access_point = RLZTracker::ChromeOmnibox();
82 access_point = RLZTracker::ChromeAppList();
84 RLZTracker::GetAccessPointRlz(access_point, &rlz_string);
90 // We can enable this on non-Android if other platforms ever want a non-empty
91 // search client string. There is already a unit test in place for Android
92 // called TemplateURLTest::SearchClient.
93 std::string UIThreadSearchTermsData::GetSearchClient() const {
94 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
95 BrowserThread::CurrentlyOn(BrowserThread::UI));
100 std::string UIThreadSearchTermsData::GetSuggestClient() const {
101 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
102 BrowserThread::CurrentlyOn(BrowserThread::UI));
103 #if defined(OS_ANDROID)
104 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE ?
105 "chrome" : "chrome-omni";
107 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
111 std::string UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
112 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
113 BrowserThread::CurrentlyOn(BrowserThread::UI));
114 #if defined(OS_ANDROID)
115 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE) {
116 return OmniboxFieldTrial::EnableAnswersInSuggest() ?
117 "chrome-mobile-ext-ansg" : "chrome-mobile-ext";
119 return OmniboxFieldTrial::EnableAnswersInSuggest() ?
120 "chrome-ext-ansg" : "chrome-ext";
121 #elif defined(OS_IOS)
122 return OmniboxFieldTrial::EnableAnswersInSuggest() ?
123 "chrome-ext-ansg" : "chrome-ext";
129 bool UIThreadSearchTermsData::EnableAnswersInSuggest() const {
130 return OmniboxFieldTrial::EnableAnswersInSuggest();
133 bool UIThreadSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
134 return chrome::IsInstantExtendedAPIEnabled() &&
135 chrome::IsQueryExtractionEnabled();
138 std::string UIThreadSearchTermsData::InstantExtendedEnabledParam(
139 bool for_search) const {
140 return chrome::InstantExtendedEnabledParam(for_search);
143 std::string UIThreadSearchTermsData::ForceInstantResultsParam(
144 bool for_prerender) const {
145 return chrome::ForceInstantResultsParam(for_prerender);
148 int UIThreadSearchTermsData::OmniboxStartMargin() const {
149 InstantService* instant_service =
150 InstantServiceFactory::GetForProfile(profile_);
151 // Android and iOS have no InstantService.
152 return instant_service ?
153 instant_service->omnibox_start_margin() : chrome::kDisableStartMargin;
156 std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
157 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
158 BrowserThread::CurrentlyOn(BrowserThread::UI));
159 #if defined(ENABLE_THEMES)
160 if (!chrome::IsInstantExtendedAPIEnabled())
161 return std::string();
163 // TODO(dhollowa): Determine fraction of custom themes that don't affect the
164 // NTP background and/or color.
165 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
166 // NTP is considered themed if the theme is not default and not native (GTK+).
167 if (theme_service && !theme_service->UsingDefaultTheme() &&
168 !theme_service->UsingSystemTheme())
170 #endif // defined(ENABLE_THEMES)
172 return std::string();
175 // It's acutally OK to call this method on any thread, but it's currently placed
176 // in UIThreadSearchTermsData since SearchTermsData cannot depend on
178 std::string UIThreadSearchTermsData::GoogleImageSearchSource() const {
179 chrome::VersionInfo version_info;
180 std::string version(version_info.Name() + " " + version_info.Version());
181 if (version_info.IsOfficialBuild())
182 version += " (Official)";
183 version += " " + version_info.OSType();
184 std::string modifier(version_info.GetVersionStringModifier());
185 if (!modifier.empty())
186 version += " " + modifier;
191 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) {
192 delete google_base_url_;
193 google_base_url_ = base_url.empty() ? NULL : new std::string(base_url);