Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / search_engines / search_terms_data.cc
blob7c6e0d7257efa1eca4053b732a21fe20e4878625
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/logging.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/google/google_url_tracker.h"
12 #include "chrome/browser/google/google_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/sync/glue/device_info.h"
16 #include "chrome/browser/themes/theme_service.h"
17 #include "chrome/browser/themes/theme_service_factory.h"
18 #include "chrome/common/pref_names.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "sync/protocol/sync.pb.h"
21 #include "url/gurl.h"
23 #if defined(ENABLE_RLZ)
24 #include "chrome/browser/rlz/rlz.h"
25 #endif
27 using content::BrowserThread;
29 SearchTermsData::SearchTermsData() {
32 SearchTermsData::~SearchTermsData() {
35 std::string SearchTermsData::GoogleBaseURLValue() const {
36 return GoogleURLTracker::kDefaultGoogleHomepage;
39 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
40 // Start with the Google base URL.
41 const GURL base_url(GoogleBaseURLValue());
42 DCHECK(base_url.is_valid());
44 GURL::Replacements repl;
46 // Replace any existing path with "/complete/".
47 // SetPathStr() requires its argument to stay in scope as long as |repl| is,
48 // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in
49 // a variable.
50 const std::string suggest_path("/complete/");
51 repl.SetPathStr(suggest_path);
53 // Clear the query and ref.
54 repl.ClearQuery();
55 repl.ClearRef();
56 return base_url.ReplaceComponents(repl).spec();
59 std::string SearchTermsData::GetApplicationLocale() const {
60 return "en";
63 base::string16 SearchTermsData::GetRlzParameterValue(bool from_app_list) const {
64 return base::string16();
67 std::string SearchTermsData::GetSearchClient() const {
68 return std::string();
71 std::string SearchTermsData::GetSuggestClient() const {
72 return std::string();
75 std::string SearchTermsData::GetSuggestRequestIdentifier() const {
76 return std::string();
79 std::string SearchTermsData::NTPIsThemedParam() const {
80 return std::string();
83 // static
84 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
86 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
87 : profile_(profile) {
88 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
89 BrowserThread::CurrentlyOn(BrowserThread::UI));
92 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
93 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
94 BrowserThread::CurrentlyOn(BrowserThread::UI));
95 if (google_base_url_)
96 return *google_base_url_;
97 GURL base_url(google_util::CommandLineGoogleBaseURL());
98 if (base_url.is_valid())
99 return base_url.spec();
100 return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
101 SearchTermsData::GoogleBaseURLValue();
104 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
105 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
106 BrowserThread::CurrentlyOn(BrowserThread::UI));
107 return g_browser_process->GetApplicationLocale();
110 // Android implementations are located in search_terms_data_android.cc.
111 #if !defined(OS_ANDROID)
112 base::string16 UIThreadSearchTermsData::GetRlzParameterValue(
113 bool from_app_list) const {
114 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
115 BrowserThread::CurrentlyOn(BrowserThread::UI));
116 base::string16 rlz_string;
117 #if defined(ENABLE_RLZ)
118 // For organic brandcodes do not use rlz at all. Empty brandcode usually
119 // means a chromium install. This is ok.
120 std::string brand;
121 if (google_util::GetBrand(&brand) && !brand.empty() &&
122 !google_util::IsOrganic(brand)) {
123 // This call will return false the first time(s) it is called until the
124 // value has been cached. This normally would mean that at most one omnibox
125 // search might not send the RLZ data but this is not really a problem.
126 rlz_lib::AccessPoint access_point = RLZTracker::CHROME_OMNIBOX;
127 #if !defined(OS_IOS)
128 if (from_app_list)
129 access_point = RLZTracker::CHROME_APP_LIST;
130 #endif
131 RLZTracker::GetAccessPointRlz(access_point, &rlz_string);
133 #endif
134 return rlz_string;
137 // We can enable this on non-Android if other platforms ever want a non-empty
138 // search client string. There is already a unit test in place for Android
139 // called TemplateURLTest::SearchClient.
140 std::string UIThreadSearchTermsData::GetSearchClient() const {
141 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
142 BrowserThread::CurrentlyOn(BrowserThread::UI));
143 return std::string();
145 #endif
147 std::string UIThreadSearchTermsData::GetSuggestClient() const {
148 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
149 BrowserThread::CurrentlyOn(BrowserThread::UI));
150 #if defined(OS_ANDROID)
151 sync_pb::SyncEnums::DeviceType device_type =
152 browser_sync::DeviceInfo::GetLocalDeviceType();
153 return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ?
154 "chrome" : "chrome-omni";
155 #else
156 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
157 #endif
160 std::string UIThreadSearchTermsData::GetSuggestRequestIdentifier() const {
161 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
162 BrowserThread::CurrentlyOn(BrowserThread::UI));
163 #if defined(OS_ANDROID)
164 sync_pb::SyncEnums::DeviceType device_type =
165 browser_sync::DeviceInfo::GetLocalDeviceType();
166 return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ?
167 "chrome-mobile-ext" : "chrome-ext";
168 #else
169 return "chrome-ext";
170 #endif
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())
186 return "es_th=1&";
187 #endif // defined(ENABLE_THEMES)
189 return std::string();
192 // static
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);