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_url_tracker_factory.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/channel_info.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "components/google/core/browser/google_url_tracker.h"
24 #include "components/google/core/browser/google_util.h"
25 #include "components/omnibox/browser/omnibox_field_trial.h"
26 #include "components/search/search.h"
27 #include "components/version_info/version_info.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "ui/base/device_form_factor.h"
32 #if defined(ENABLE_RLZ)
33 #include "components/rlz/rlz_tracker.h"
36 using content::BrowserThread
;
39 std::string
* UIThreadSearchTermsData::google_base_url_
= NULL
;
41 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile
* profile
)
43 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
44 BrowserThread::CurrentlyOn(BrowserThread::UI
));
47 std::string
UIThreadSearchTermsData::GoogleBaseURLValue() const {
48 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
49 BrowserThread::CurrentlyOn(BrowserThread::UI
));
51 return *google_base_url_
;
52 GURL
base_url(google_util::CommandLineGoogleBaseURL());
53 if (base_url
.is_valid())
54 return base_url
.spec();
57 return SearchTermsData::GoogleBaseURLValue();
59 const GoogleURLTracker
* tracker
=
60 GoogleURLTrackerFactory::GetForProfile(profile_
);
62 tracker
->google_url().spec() : GoogleURLTracker::kDefaultGoogleHomepage
;
65 std::string
UIThreadSearchTermsData::GetApplicationLocale() const {
66 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
67 BrowserThread::CurrentlyOn(BrowserThread::UI
));
68 return g_browser_process
->GetApplicationLocale();
71 // Android implementations are in ui_thread_search_terms_data_android.cc.
72 #if !defined(OS_ANDROID)
73 base::string16
UIThreadSearchTermsData::GetRlzParameterValue(
74 bool from_app_list
) const {
75 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
76 BrowserThread::CurrentlyOn(BrowserThread::UI
));
77 base::string16 rlz_string
;
78 #if defined(ENABLE_RLZ)
79 // For organic brandcodes do not use rlz at all. Empty brandcode usually
80 // means a chromium install. This is ok.
82 if (google_brand::GetBrand(&brand
) && !brand
.empty() &&
83 !google_brand::IsOrganic(brand
)) {
84 // This call will return false the first time(s) it is called until the
85 // value has been cached. This normally would mean that at most one omnibox
86 // search might not send the RLZ data but this is not really a problem.
87 rlz_lib::AccessPoint access_point
= rlz::RLZTracker::ChromeOmnibox();
90 access_point
= rlz::RLZTracker::ChromeAppList();
92 rlz::RLZTracker::GetAccessPointRlz(access_point
, &rlz_string
);
98 // We can enable this on non-Android if other platforms ever want a non-empty
99 // search client string. There is already a unit test in place for Android
100 // called TemplateURLTest::SearchClient.
101 std::string
UIThreadSearchTermsData::GetSearchClient() const {
102 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
103 BrowserThread::CurrentlyOn(BrowserThread::UI
));
104 return std::string();
108 std::string
UIThreadSearchTermsData::GetSuggestClient() const {
109 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
110 BrowserThread::CurrentlyOn(BrowserThread::UI
));
111 #if defined(OS_ANDROID)
112 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE
?
113 "chrome" : "chrome-omni";
115 return search::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
119 std::string
UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
120 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
121 BrowserThread::CurrentlyOn(BrowserThread::UI
));
122 #if defined(OS_ANDROID)
123 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE
)
124 return "chrome-mobile-ext-ansg";
126 return "chrome-ext-ansg";
129 bool UIThreadSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
130 return search::IsInstantExtendedAPIEnabled() &&
131 search::IsQueryExtractionEnabled();
134 std::string
UIThreadSearchTermsData::InstantExtendedEnabledParam(
135 bool for_search
) const {
136 return search::InstantExtendedEnabledParam(for_search
);
139 std::string
UIThreadSearchTermsData::ForceInstantResultsParam(
140 bool for_prerender
) const {
141 return search::ForceInstantResultsParam(for_prerender
);
144 int UIThreadSearchTermsData::OmniboxStartMargin() const {
145 InstantService
* instant_service
=
146 InstantServiceFactory::GetForProfile(profile_
);
147 // Android and iOS have no InstantService.
148 return instant_service
? instant_service
->omnibox_start_margin()
149 : search::kDisableStartMargin
;
152 std::string
UIThreadSearchTermsData::NTPIsThemedParam() const {
153 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
154 BrowserThread::CurrentlyOn(BrowserThread::UI
));
155 #if defined(ENABLE_THEMES)
156 if (!search::IsInstantExtendedAPIEnabled())
157 return std::string();
159 // TODO(dhollowa): Determine fraction of custom themes that don't affect the
160 // NTP background and/or color.
161 ThemeService
* theme_service
= ThemeServiceFactory::GetForProfile(profile_
);
162 // NTP is considered themed if the theme is not default and not native (GTK+).
163 if (theme_service
&& !theme_service
->UsingDefaultTheme() &&
164 !theme_service
->UsingSystemTheme())
166 #endif // defined(ENABLE_THEMES)
168 return std::string();
171 // It's acutally OK to call this method on any thread, but it's currently placed
172 // in UIThreadSearchTermsData since SearchTermsData cannot depend on src/chrome
173 // as it is shared with iOS.
174 std::string
UIThreadSearchTermsData::GoogleImageSearchSource() const {
175 std::string
version(version_info::GetProductName() + " " +
176 version_info::GetVersionNumber());
177 if (version_info::IsOfficialBuild())
178 version
+= " (Official)";
179 version
+= " " + version_info::GetOSType();
180 std::string
modifier(chrome::GetChannelString());
181 if (!modifier
.empty())
182 version
+= " " + modifier
;
186 std::string
UIThreadSearchTermsData::GetAcceptLanguages() const {
187 return profile_
? profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
)
192 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string
& base_url
) {
193 delete google_base_url_
;
194 google_base_url_
= base_url
.empty() ? NULL
: new std::string(base_url
);