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/themes/theme_service.h"
18 #include "chrome/browser/themes/theme_service_factory.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "content/public/browser/browser_thread.h"
24 #if defined(ENABLE_RLZ)
25 #include "chrome/browser/rlz/rlz.h"
28 using content::BrowserThread;
30 SearchTermsData::SearchTermsData() {
33 SearchTermsData::~SearchTermsData() {
36 std::string SearchTermsData::GoogleBaseURLValue() const {
37 return GoogleURLTracker::kDefaultGoogleHomepage;
40 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
41 // Start with the Google base URL.
42 const GURL base_url(GoogleBaseURLValue());
43 DCHECK(base_url.is_valid());
45 GURL::Replacements repl;
47 // Replace any existing path with "/complete/".
48 // SetPathStr() requires its argument to stay in scope as long as |repl| is,
49 // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in
51 const std::string suggest_path("/complete/");
52 repl.SetPathStr(suggest_path);
54 // Clear the query and ref.
57 return base_url.ReplaceComponents(repl).spec();
60 std::string SearchTermsData::GetApplicationLocale() const {
64 string16 SearchTermsData::GetRlzParameterValue() const {
68 std::string SearchTermsData::GetSearchClient() const {
72 std::string SearchTermsData::GetSuggestClient() const {
76 std::string SearchTermsData::ForceInstantResultsParam(
77 bool for_prerender) const {
81 std::string SearchTermsData::InstantExtendedEnabledParam() const {
85 std::string SearchTermsData::NTPIsThemedParam() const {
90 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
92 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
94 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
95 BrowserThread::CurrentlyOn(BrowserThread::UI));
98 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
99 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
100 BrowserThread::CurrentlyOn(BrowserThread::UI));
101 if (google_base_url_)
102 return *google_base_url_;
103 std::string base_url = CommandLine::ForCurrentProcess()->
104 GetSwitchValueASCII(switches::kGoogleBaseURL);
105 if (!base_url.empty())
107 return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
108 SearchTermsData::GoogleBaseURLValue();
111 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
112 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
113 BrowserThread::CurrentlyOn(BrowserThread::UI));
114 return g_browser_process->GetApplicationLocale();
117 // Android implementations are located in search_terms_data_android.cc.
118 #if !defined(OS_ANDROID)
119 string16 UIThreadSearchTermsData::GetRlzParameterValue() const {
120 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
121 BrowserThread::CurrentlyOn(BrowserThread::UI));
123 #if defined(ENABLE_RLZ)
124 // For organic brandcodes do not use rlz at all. Empty brandcode usually
125 // means a chromium install. This is ok.
127 if (google_util::GetBrand(&brand) && !brand.empty() &&
128 !google_util::IsOrganic(brand)) {
129 // This call will return false the first time(s) it is called until the
130 // value has been cached. This normally would mean that at most one omnibox
131 // search might not send the RLZ data but this is not really a problem.
132 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
138 // We can enable this on non-Android if other platforms ever want a non-empty
139 // search client string. There is already a unit test in place for Android
140 // called TemplateURLTest::SearchClient.
141 std::string UIThreadSearchTermsData::GetSearchClient() const {
142 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
143 BrowserThread::CurrentlyOn(BrowserThread::UI));
144 return std::string();
148 std::string UIThreadSearchTermsData::GetSuggestClient() const {
149 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
150 BrowserThread::CurrentlyOn(BrowserThread::UI));
151 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
154 std::string UIThreadSearchTermsData::ForceInstantResultsParam(
155 bool for_prerender) const {
156 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
157 BrowserThread::CurrentlyOn(BrowserThread::UI));
158 return (for_prerender || !chrome::IsInstantExtendedAPIEnabled()) ? "ion=1&" :
162 std::string UIThreadSearchTermsData::InstantExtendedEnabledParam() const {
163 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
164 BrowserThread::CurrentlyOn(BrowserThread::UI));
165 uint64 instant_extended_api_version = chrome::EmbeddedSearchPageVersion();
166 if (instant_extended_api_version) {
167 return std::string(google_util::kInstantExtendedAPIParam) + "=" +
168 base::Uint64ToString(instant_extended_api_version) + "&";
170 return std::string();
173 std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
174 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
175 BrowserThread::CurrentlyOn(BrowserThread::UI));
176 #if defined(ENABLE_THEMES)
177 if (!chrome::IsInstantExtendedAPIEnabled())
178 return std::string();
180 // TODO(dhollowa): Determine fraction of custom themes that don't affect the
181 // NTP background and/or color.
182 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
183 // NTP is considered themed if the theme is not default and not native (GTK+).
184 if (theme_service && !theme_service->UsingDefaultTheme() &&
185 !theme_service->UsingNativeTheme())
187 #endif // defined(ENABLE_THEMES)
189 return std::string();
193 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) {
194 delete google_base_url_;
195 google_base_url_ = base_url.empty() ? NULL : new std::string(base_url);