1 // Copyright 2012 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/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 "base/strings/string_number_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/google/google_url_tracker.h"
14 #include "chrome/browser/google/google_util.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/pref_names.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "sync/protocol/sync.pb.h"
26 #if defined(ENABLE_RLZ)
27 #include "chrome/browser/rlz/rlz.h"
30 using content::BrowserThread
;
32 SearchTermsData::SearchTermsData() {
35 SearchTermsData::~SearchTermsData() {
38 std::string
SearchTermsData::GoogleBaseURLValue() const {
39 return GoogleURLTracker::kDefaultGoogleHomepage
;
42 std::string
SearchTermsData::GoogleBaseSuggestURLValue() const {
43 // Start with the Google base URL.
44 const GURL
base_url(GoogleBaseURLValue());
45 DCHECK(base_url
.is_valid());
47 GURL::Replacements repl
;
49 // Replace any existing path with "/complete/".
50 // SetPathStr() requires its argument to stay in scope as long as |repl| is,
51 // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in
53 const std::string
suggest_path("/complete/");
54 repl
.SetPathStr(suggest_path
);
56 // Clear the query and ref.
59 return base_url
.ReplaceComponents(repl
).spec();
62 std::string
SearchTermsData::GetApplicationLocale() const {
66 base::string16
SearchTermsData::GetRlzParameterValue() const {
67 return base::string16();
70 std::string
SearchTermsData::GetSearchClient() const {
74 std::string
SearchTermsData::GetSuggestClient() const {
78 std::string
SearchTermsData::GetSuggestRequestIdentifier() const {
82 std::string
SearchTermsData::ForceInstantResultsParam(
83 bool for_prerender
) const {
87 std::string
SearchTermsData::InstantExtendedEnabledParam() const {
91 std::string
SearchTermsData::NTPIsThemedParam() const {
96 std::string
* UIThreadSearchTermsData::google_base_url_
= NULL
;
98 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile
* profile
)
100 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
101 BrowserThread::CurrentlyOn(BrowserThread::UI
));
104 std::string
UIThreadSearchTermsData::GoogleBaseURLValue() const {
105 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
106 BrowserThread::CurrentlyOn(BrowserThread::UI
));
107 if (google_base_url_
)
108 return *google_base_url_
;
109 std::string base_url
= CommandLine::ForCurrentProcess()->
110 GetSwitchValueASCII(switches::kGoogleBaseURL
);
111 if (!base_url
.empty())
113 return profile_
? GoogleURLTracker::GoogleURL(profile_
).spec() :
114 SearchTermsData::GoogleBaseURLValue();
117 std::string
UIThreadSearchTermsData::GetApplicationLocale() const {
118 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
119 BrowserThread::CurrentlyOn(BrowserThread::UI
));
120 return g_browser_process
->GetApplicationLocale();
123 // Android implementations are located in search_terms_data_android.cc.
124 #if !defined(OS_ANDROID)
125 base::string16
UIThreadSearchTermsData::GetRlzParameterValue() const {
126 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
127 BrowserThread::CurrentlyOn(BrowserThread::UI
));
128 base::string16 rlz_string
;
129 #if defined(ENABLE_RLZ)
130 // For organic brandcodes do not use rlz at all. Empty brandcode usually
131 // means a chromium install. This is ok.
133 if (google_util::GetBrand(&brand
) && !brand
.empty() &&
134 !google_util::IsOrganic(brand
)) {
135 // This call will return false the first time(s) it is called until the
136 // value has been cached. This normally would mean that at most one omnibox
137 // search might not send the RLZ data but this is not really a problem.
138 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX
, &rlz_string
);
144 // We can enable this on non-Android if other platforms ever want a non-empty
145 // search client string. There is already a unit test in place for Android
146 // called TemplateURLTest::SearchClient.
147 std::string
UIThreadSearchTermsData::GetSearchClient() const {
148 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
149 BrowserThread::CurrentlyOn(BrowserThread::UI
));
150 return std::string();
154 std::string
UIThreadSearchTermsData::GetSuggestClient() const {
155 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
156 BrowserThread::CurrentlyOn(BrowserThread::UI
));
157 #if defined(OS_ANDROID)
158 sync_pb::SyncEnums::DeviceType device_type
=
159 browser_sync::DeviceInfo::GetLocalDeviceType();
160 return device_type
== sync_pb::SyncEnums_DeviceType_TYPE_PHONE
?
161 "chrome" : "chrome-omni";
163 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
167 std::string
UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
168 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
169 BrowserThread::CurrentlyOn(BrowserThread::UI
));
170 #if defined(OS_ANDROID)
171 sync_pb::SyncEnums::DeviceType device_type
=
172 browser_sync::DeviceInfo::GetLocalDeviceType();
173 return device_type
== sync_pb::SyncEnums_DeviceType_TYPE_PHONE
?
174 "chrome-mobile-ext" : "chrome-ext";
180 std::string
UIThreadSearchTermsData::ForceInstantResultsParam(
181 bool for_prerender
) const {
182 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
183 BrowserThread::CurrentlyOn(BrowserThread::UI
));
184 return (for_prerender
|| !chrome::IsInstantExtendedAPIEnabled()) ? "ion=1&" :
188 std::string
UIThreadSearchTermsData::InstantExtendedEnabledParam() const {
189 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
190 BrowserThread::CurrentlyOn(BrowserThread::UI
));
191 uint64 instant_extended_api_version
= chrome::EmbeddedSearchPageVersion();
192 if (instant_extended_api_version
) {
193 return std::string(google_util::kInstantExtendedAPIParam
) + "=" +
194 base::Uint64ToString(instant_extended_api_version
) + "&";
196 return std::string();
199 std::string
UIThreadSearchTermsData::NTPIsThemedParam() const {
200 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
201 BrowserThread::CurrentlyOn(BrowserThread::UI
));
202 #if defined(ENABLE_THEMES)
203 if (!chrome::IsInstantExtendedAPIEnabled())
204 return std::string();
206 // TODO(dhollowa): Determine fraction of custom themes that don't affect the
207 // NTP background and/or color.
208 ThemeService
* theme_service
= ThemeServiceFactory::GetForProfile(profile_
);
209 // NTP is considered themed if the theme is not default and not native (GTK+).
210 if (theme_service
&& !theme_service
->UsingDefaultTheme() &&
211 !theme_service
->UsingNativeTheme())
213 #endif // defined(ENABLE_THEMES)
215 return std::string();
219 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string
& base_url
) {
220 delete google_base_url_
;
221 google_base_url_
= base_url
.empty() ? NULL
: new std::string(base_url
);