- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / search_engines / search_terms_data.cc
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 "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"
22 #include "url/gurl.h"
23
24 #if defined(ENABLE_RLZ)
25 #include "chrome/browser/rlz/rlz.h"
26 #endif
27
28 using content::BrowserThread;
29
30 SearchTermsData::SearchTermsData() {
31 }
32
33 SearchTermsData::~SearchTermsData() {
34 }
35
36 std::string SearchTermsData::GoogleBaseURLValue() const {
37   return GoogleURLTracker::kDefaultGoogleHomepage;
38 }
39
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());
44
45   GURL::Replacements repl;
46
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
50   // a variable.
51   const std::string suggest_path("/complete/");
52   repl.SetPathStr(suggest_path);
53
54   // Clear the query and ref.
55   repl.ClearQuery();
56   repl.ClearRef();
57   return base_url.ReplaceComponents(repl).spec();
58 }
59
60 std::string SearchTermsData::GetApplicationLocale() const {
61   return "en";
62 }
63
64 string16 SearchTermsData::GetRlzParameterValue() const {
65   return string16();
66 }
67
68 std::string SearchTermsData::GetSearchClient() const {
69   return std::string();
70 }
71
72 std::string SearchTermsData::GetSuggestClient() const {
73   return std::string();
74 }
75
76 std::string SearchTermsData::ForceInstantResultsParam(
77     bool for_prerender) const {
78   return std::string();
79 }
80
81 std::string SearchTermsData::InstantExtendedEnabledParam() const {
82   return std::string();
83 }
84
85 std::string SearchTermsData::NTPIsThemedParam() const {
86   return std::string();
87 }
88
89 // static
90 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
91
92 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
93     : profile_(profile) {
94   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
95       BrowserThread::CurrentlyOn(BrowserThread::UI));
96 }
97
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())
106     return base_url;
107   return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
108       SearchTermsData::GoogleBaseURLValue();
109 }
110
111 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
112   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
113       BrowserThread::CurrentlyOn(BrowserThread::UI));
114   return g_browser_process->GetApplicationLocale();
115 }
116
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));
122   string16 rlz_string;
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.
126   std::string brand;
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);
133   }
134 #endif
135   return rlz_string;
136 }
137
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();
145 }
146 #endif
147
148 std::string UIThreadSearchTermsData::GetSuggestClient() const {
149   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
150       BrowserThread::CurrentlyOn(BrowserThread::UI));
151   return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
152 }
153
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&" :
159       std::string();
160 }
161
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) + "&";
169   }
170   return std::string();
171 }
172
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();
179
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)
188
189   return std::string();
190 }
191
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);
196 }