Upstream version 5.34.104.0
[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 "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"
23 #include "url/gurl.h"
24
25 #if defined(ENABLE_RLZ)
26 #include "chrome/browser/rlz/rlz.h"
27 #endif
28
29 using content::BrowserThread;
30
31 SearchTermsData::SearchTermsData() {
32 }
33
34 SearchTermsData::~SearchTermsData() {
35 }
36
37 std::string SearchTermsData::GoogleBaseURLValue() const {
38   return GoogleURLTracker::kDefaultGoogleHomepage;
39 }
40
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());
45
46   GURL::Replacements repl;
47
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
51   // a variable.
52   const std::string suggest_path("/complete/");
53   repl.SetPathStr(suggest_path);
54
55   // Clear the query and ref.
56   repl.ClearQuery();
57   repl.ClearRef();
58   return base_url.ReplaceComponents(repl).spec();
59 }
60
61 std::string SearchTermsData::GetApplicationLocale() const {
62   return "en";
63 }
64
65 base::string16 SearchTermsData::GetRlzParameterValue() const {
66   return base::string16();
67 }
68
69 std::string SearchTermsData::GetSearchClient() const {
70   return std::string();
71 }
72
73 std::string SearchTermsData::GetSuggestClient() const {
74   return std::string();
75 }
76
77 std::string SearchTermsData::GetSuggestRequestIdentifier() const {
78   return std::string();
79 }
80
81 std::string SearchTermsData::NTPIsThemedParam() const {
82   return std::string();
83 }
84
85 // static
86 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
87
88 UIThreadSearchTermsData::UIThreadSearchTermsData(Profile* profile)
89     : profile_(profile) {
90   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
91       BrowserThread::CurrentlyOn(BrowserThread::UI));
92 }
93
94 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
95   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
96       BrowserThread::CurrentlyOn(BrowserThread::UI));
97   if (google_base_url_)
98     return *google_base_url_;
99   std::string base_url = CommandLine::ForCurrentProcess()->
100       GetSwitchValueASCII(switches::kGoogleBaseURL);
101   if (!base_url.empty())
102     return base_url;
103   return profile_ ? GoogleURLTracker::GoogleURL(profile_).spec() :
104       SearchTermsData::GoogleBaseURLValue();
105 }
106
107 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
108   DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
109       BrowserThread::CurrentlyOn(BrowserThread::UI));
110   return g_browser_process->GetApplicationLocale();
111 }
112
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.
122   std::string brand;
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);
129   }
130 #endif
131   return rlz_string;
132 }
133
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();
141 }
142 #endif
143
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";
152 #else
153   return chrome::IsInstantExtendedAPIEnabled() ? "chrome-omni" : "chrome";
154 #endif
155 }
156
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";
165 #else
166   return "chrome-ext";
167 #endif
168 }
169
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();
176
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())
183     return "es_th=1&";
184 #endif  // defined(ENABLE_THEMES)
185
186   return std::string();
187 }
188
189 // static
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);
193 }