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 "chrome/browser/browser_process.h"
12 #include "chrome/browser/google/google_url_tracker.h"
13 #include "chrome/browser/google/google_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/sync/glue/device_info.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"
22 #include "sync/protocol/sync.pb.h"
25 #if defined(ENABLE_RLZ)
26 #include "chrome/browser/rlz/rlz.h"
29 using content::BrowserThread;
31 SearchTermsData::SearchTermsData() {
34 SearchTermsData::~SearchTermsData() {
37 std::string SearchTermsData::GoogleBaseURLValue() const {
38 return GoogleURLTracker::kDefaultGoogleHomepage;
41 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
42 // Start with the Google base URL.
43 const GURL base_url(GoogleBaseURLValue());
44 DCHECK(base_url.is_valid());
46 GURL::Replacements repl;
48 // Replace any existing path with "/complete/".
49 // SetPathStr() requires its argument to stay in scope as long as |repl| is,
50 // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in
52 const std::string suggest_path("/complete/");
53 repl.SetPathStr(suggest_path);
55 // Clear the query and ref.
58 return base_url.ReplaceComponents(repl).spec();
61 std::string SearchTermsData::GetApplicationLocale() const {
65 base::string16 SearchTermsData::GetRlzParameterValue() const {
66 return base::string16();
69 std::string SearchTermsData::GetSearchClient() const {
73 std::string SearchTermsData::GetSuggestClient() const {
77 std::string SearchTermsData::GetSuggestRequestIdentifier() const {
81 std::string SearchTermsData::NTPIsThemedParam() const {
86 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
88 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
90 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
91 BrowserThread::CurrentlyOn(BrowserThread::UI));
94 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
95 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
96 BrowserThread::CurrentlyOn(BrowserThread::UI));
98 return *google_base_url_;
99 std::string base_url = CommandLine::ForCurrentProcess()->
100 GetSwitchValueASCII(switches::kGoogleBaseURL);
101 if (!base_url.empty())
103 return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
104 SearchTermsData::GoogleBaseURLValue();
107 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
108 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
109 BrowserThread::CurrentlyOn(BrowserThread::UI));
110 return g_browser_process->GetApplicationLocale();
113 // Android implementations are located in search_terms_data_android.cc.
114 #if !defined(OS_ANDROID)
115 base::string16 UIThreadSearchTermsData::GetRlzParameterValue() const {
116 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
117 BrowserThread::CurrentlyOn(BrowserThread::UI));
118 base::string16 rlz_string;
119 #if defined(ENABLE_RLZ)
120 // For organic brandcodes do not use rlz at all. Empty brandcode usually
121 // means a chromium install. This is ok.
123 if (google_util::GetBrand(&brand) && !brand.empty() &&
124 !google_util::IsOrganic(brand)) {
125 // This call will return false the first time(s) it is called until the
126 // value has been cached. This normally would mean that at most one omnibox
127 // search might not send the RLZ data but this is not really a problem.
128 RLZTracker::GetAccessPointRlz(RLZTracker::CHROME_OMNIBOX, &rlz_string);
134 // We can enable this on non-Android if other platforms ever want a non-empty
135 // search client string. There is already a unit test in place for Android
136 // called TemplateURLTest::SearchClient.
137 std::string UIThreadSearchTermsData::GetSearchClient() const {
138 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
139 BrowserThread::CurrentlyOn(BrowserThread::UI));
140 return std::string();
144 std::string UIThreadSearchTermsData::GetSuggestClient() const {
145 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
146 BrowserThread::CurrentlyOn(BrowserThread::UI));
147 #if defined(OS_ANDROID)
148 sync_pb::SyncEnums::DeviceType device_type =
149 browser_sync::DeviceInfo::GetLocalDeviceType();
150 return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ?
151 "chrome" : "chrome-omni";
153 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
157 std::string UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
158 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
159 BrowserThread::CurrentlyOn(BrowserThread::UI));
160 #if defined(OS_ANDROID)
161 sync_pb::SyncEnums::DeviceType device_type =
162 browser_sync::DeviceInfo::GetLocalDeviceType();
163 return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ?
164 "chrome-mobile-ext" : "chrome-ext";
170 std::string UIThreadSearchTermsData::NTPIsThemedParam() const {
171 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
172 BrowserThread::CurrentlyOn(BrowserThread::UI));
173 #if defined(ENABLE_THEMES)
174 if (!chrome::IsInstantExtendedAPIEnabled())
175 return std::string();
177 // TODO(dhollowa): Determine fraction of custom themes that don't affect the
178 // NTP background and/or color.
179 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
180 // NTP is considered themed if the theme is not default and not native (GTK+).
181 if (theme_service && !theme_service->UsingDefaultTheme() &&
182 !theme_service->UsingNativeTheme())
184 #endif // defined(ENABLE_THEMES)
186 return std::string();
190 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) {
191 delete google_base_url_;
192 google_base_url_ = base_url.empty() ? NULL : new std::string(base_url);