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/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"
31 #if defined(ENABLE_RLZ)
32 #include "chrome/browser/rlz/rlz.h"
35 using content::BrowserThread;
38 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
40 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
42 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
43 BrowserThread::CurrentlyOn(BrowserThread::UI));
46 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
47 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
48 BrowserThread::CurrentlyOn(BrowserThread::UI));
50 return *google_base_url_;
51 GURL base_url(google_util::CommandLineGoogleBaseURL());
52 if (base_url.is_valid())
53 return base_url.spec();
55 google_profile_helper::GetGoogleHomePageURL(profile_).spec() :
56 SearchTermsData::GoogleBaseURLValue();
59 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
60 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
61 BrowserThread::CurrentlyOn(BrowserThread::UI));
62 return g_browser_process->GetApplicationLocale();
65 // Android implementations are in ui_thread_search_terms_data_android.cc.
66 #if !defined(OS_ANDROID)
67 base::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.
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();
84 access_point = RLZTracker::ChromeAppList();
86 RLZTracker::GetAccessPointRlz(access_point, &rlz_string);
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.
95 std::string UIThreadSearchTermsData::GetSearchClient() const {
96 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
97 BrowserThread::CurrentlyOn(BrowserThread::UI));
102 std::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";
111 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
115 std::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";
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";
135 bool UIThreadSearchTermsData::EnableAnswersInSuggest() const {
136 return OmniboxFieldTrial::EnableAnswersInSuggest();
139 bool UIThreadSearchTermsData::IsShowingSearchTermsOnSearchResultsPages() const {
140 return chrome::IsInstantExtendedAPIEnabled() &&
141 chrome::IsQueryExtractionEnabled();
144 std::string UIThreadSearchTermsData::InstantExtendedEnabledParam(
145 bool for_search) const {
146 return chrome::InstantExtendedEnabledParam(for_search);
149 std::string UIThreadSearchTermsData::ForceInstantResultsParam(
150 bool for_prerender) const {
151 return chrome::ForceInstantResultsParam(for_prerender);
154 int 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;
162 std::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();
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())
176 #endif // defined(ENABLE_THEMES)
178 return std::string();
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
184 std::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;
200 void 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);