OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/search_engines/search_terms_data.h" | |
6 | |
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/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 "components/google/core/browser/google_url_tracker.h" | |
23 #include "content/public/browser/browser_thread.h" | |
24 #include "sync/protocol/sync.pb.h" | |
25 #include "url/gurl.h" | |
26 | |
27 #if defined(ENABLE_RLZ) | |
28 #include "chrome/browser/rlz/rlz.h" | |
29 #endif | |
30 | |
31 using content::BrowserThread; | |
32 | |
33 SearchTermsData::SearchTermsData() { | |
34 } | |
35 | |
36 SearchTermsData::~SearchTermsData() { | |
37 } | |
38 | |
39 std::string SearchTermsData::GoogleBaseURLValue() const { | |
40 return GoogleURLTracker::kDefaultGoogleHomepage; | |
41 } | |
42 | |
43 std::string SearchTermsData::GoogleBaseSuggestURLValue() const { | |
44 // Start with the Google base URL. | |
45 const GURL base_url(GoogleBaseURLValue()); | |
46 DCHECK(base_url.is_valid()); | |
47 | |
48 GURL::Replacements repl; | |
49 | |
50 // Replace any existing path with "/complete/". | |
51 // SetPathStr() requires its argument to stay in scope as long as |repl| is, | |
52 // so "/complete/" can't be passed to SetPathStr() directly, it needs to be in | |
53 // a variable. | |
54 const std::string suggest_path("/complete/"); | |
55 repl.SetPathStr(suggest_path); | |
56 | |
57 // Clear the query and ref. | |
58 repl.ClearQuery(); | |
59 repl.ClearRef(); | |
60 return base_url.ReplaceComponents(repl).spec(); | |
61 } | |
62 | |
63 std::string SearchTermsData::GetApplicationLocale() const { | |
64 return "en"; | |
65 } | |
66 | |
67 base::string16 SearchTermsData::GetRlzParameterValue(bool from_app_list) const { | |
68 return base::string16(); | |
69 } | |
70 | |
71 std::string SearchTermsData::GetSearchClient() const { | |
72 return std::string(); | |
73 } | |
74 | |
75 std::string SearchTermsData::GetSuggestClient() const { | |
76 return std::string(); | |
77 } | |
78 | |
79 std::string SearchTermsData::GetSuggestRequestIdentifier() const { | |
80 return std::string(); | |
81 } | |
82 | |
83 std::string SearchTermsData::NTPIsThemedParam() const { | |
84 return std::string(); | |
85 } | |
86 | |
87 // static | |
88 std::string* UIThreadSearchTermsData::google_base_url_ = NULL; | |
89 | |
90 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile) | |
91 : profile_(profile) { | |
92 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
93 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
94 } | |
95 | |
96 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const { | |
97 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
98 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
99 if (google_base_url_) | |
100 return *google_base_url_; | |
101 GURL base_url(google_util::CommandLineGoogleBaseURL()); | |
102 if (base_url.is_valid()) | |
103 return base_url.spec(); | |
104 return profile_ ? | |
105 google_profile_helper::GetGoogleHomePageURL(profile_).spec() : | |
106 SearchTermsData::GoogleBaseURLValue(); | |
107 } | |
108 | |
109 std::string UIThreadSearchTermsData::GetApplicationLocale() const { | |
110 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
111 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
112 return g_browser_process->GetApplicationLocale(); | |
113 } | |
114 | |
115 // Android implementations are located in search_terms_data_android.cc. | |
116 #if !defined(OS_ANDROID) | |
117 base::string16 UIThreadSearchTermsData::GetRlzParameterValue( | |
118 bool from_app_list) const { | |
119 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
120 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
121 base::string16 rlz_string; | |
122 #if defined(ENABLE_RLZ) | |
123 // For organic brandcodes do not use rlz at all. Empty brandcode usually | |
124 // means a chromium install. This is ok. | |
125 std::string brand; | |
126 if (google_brand::GetBrand(&brand) && !brand.empty() && | |
127 !google_brand::IsOrganic(brand)) { | |
128 // This call will return false the first time(s) it is called until the | |
129 // value has been cached. This normally would mean that at most one omnibox | |
130 // search might not send the RLZ data but this is not really a problem. | |
131 rlz_lib::AccessPoint access_point = RLZTracker::ChromeOmnibox(); | |
132 #if !defined(OS_IOS) | |
133 if (from_app_list) | |
134 access_point = RLZTracker::ChromeAppList(); | |
135 #endif | |
136 RLZTracker::GetAccessPointRlz(access_point, &rlz_string); | |
137 } | |
138 #endif | |
139 return rlz_string; | |
140 } | |
141 | |
142 // We can enable this on non-Android if other platforms ever want a non-empty | |
143 // search client string. There is already a unit test in place for Android | |
144 // called TemplateURLTest::SearchClient. | |
145 std::string UIThreadSearchTermsData::GetSearchClient() const { | |
146 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
147 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
148 return std::string(); | |
149 } | |
150 #endif | |
151 | |
152 std::string UIThreadSearchTermsData::GetSuggestClient() const { | |
153 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
154 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
155 #if defined(OS_ANDROID) | |
156 sync_pb::SyncEnums::DeviceType device_type = | |
157 browser_sync::DeviceInfo::GetLocalDeviceType(); | |
158 return device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE ? | |
159 "chrome" : "chrome-omni"; | |
160 #else | |
161 return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome"; | |
162 #endif | |
163 } | |
164 | |
165 std::string UIThreadSearchTermsData::GetSuggestRequestIdentifier() const { | |
166 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
167 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
168 #if defined(OS_ANDROID) | |
169 sync_pb::SyncEnums::DeviceType device_type = | |
170 browser_sync::DeviceInfo::GetLocalDeviceType(); | |
171 if (device_type == sync_pb::SyncEnums_DeviceType_TYPE_PHONE) { | |
172 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
173 switches::kEnableAnswersInSuggest)) { | |
174 return "chrome-mobile-ext-ansg"; | |
175 } else { | |
176 return "chrome-mobile-ext"; | |
177 } | |
178 } | |
179 return "chrome-ext"; | |
180 #else | |
181 return "chrome-ext"; | |
182 #endif | |
183 } | |
184 | |
185 std::string UIThreadSearchTermsData::NTPIsThemedParam() const { | |
186 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | |
187 BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
188 #if defined(ENABLE_THEMES) | |
189 if (!chrome::IsInstantExtendedAPIEnabled()) | |
190 return std::string(); | |
191 | |
192 // TODO(dhollowa): Determine fraction of custom themes that don't affect the | |
193 // NTP background and/or color. | |
194 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_); | |
195 // NTP is considered themed if the theme is not default and not native (GTK+). | |
196 if (theme_service && !theme_service->UsingDefaultTheme() && | |
197 !theme_service->UsingSystemTheme()) | |
198 return "es_th=1&"; | |
199 #endif // defined(ENABLE_THEMES) | |
200 | |
201 return std::string(); | |
202 } | |
203 | |
204 // static | |
205 void UIThreadSearchTermsData::SetGoogleBaseURL(const std::string& base_url) { | |
206 delete google_base_url_; | |
207 google_base_url_ = base_url.empty() ? NULL : new std::string(base_url); | |
208 } | |
OLD | NEW |