Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / rlz / chrome_rlz_tracker_delegate.cc
blob7fc92a5aa38e60a1f39039fd1d4c5c18c427254d
1 // Copyright 2015 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/rlz/chrome_rlz_tracker_delegate.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/google/google_brand.h"
13 #include "chrome/browser/prefs/session_startup_pref.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/ui/startup/startup_browser_creator.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/google/core/browser/google_util.h"
20 #include "components/omnibox/browser/omnibox_log.h"
21 #include "components/search_engines/template_url.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/navigation_controller.h"
25 #include "content/public/browser/navigation_details.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h"
31 #if defined(OS_WIN)
32 #include "chrome/installer/util/google_update_settings.h"
33 #endif
35 ChromeRLZTrackerDelegate::ChromeRLZTrackerDelegate() {
38 ChromeRLZTrackerDelegate::~ChromeRLZTrackerDelegate() {
41 // static
42 bool ChromeRLZTrackerDelegate::IsGoogleDefaultSearch(Profile* profile) {
43 bool is_google_default_search = false;
44 TemplateURLService* template_url_service =
45 TemplateURLServiceFactory::GetForProfile(profile);
46 if (template_url_service) {
47 const TemplateURL* url_template =
48 template_url_service->GetDefaultSearchProvider();
49 is_google_default_search = url_template &&
50 url_template->url_ref().HasGoogleBaseURLs(
51 template_url_service->search_terms_data());
53 return is_google_default_search;
56 // static
57 bool ChromeRLZTrackerDelegate::IsGoogleHomepage(Profile* profile) {
58 return google_util::IsGoogleHomePageUrl(
59 GURL(profile->GetPrefs()->GetString(prefs::kHomePage)));
62 // static
63 bool ChromeRLZTrackerDelegate::IsGoogleInStartpages(Profile* profile) {
64 bool is_google_in_startpages = false;
65 SessionStartupPref session_startup_prefs =
66 StartupBrowserCreator::GetSessionStartupPref(
67 *base::CommandLine::ForCurrentProcess(), profile);
68 if (session_startup_prefs.type == SessionStartupPref::URLS) {
69 is_google_in_startpages =
70 std::count_if(session_startup_prefs.urls.begin(),
71 session_startup_prefs.urls.end(),
72 google_util::IsGoogleHomePageUrl) > 0;
74 return is_google_in_startpages;
77 void ChromeRLZTrackerDelegate::Cleanup() {
78 registrar_.RemoveAll();
79 on_omnibox_search_callback_.Reset();
80 on_homepage_search_callback_.Reset();
83 bool ChromeRLZTrackerDelegate::IsOnUIThread() {
84 return content::BrowserThread::CurrentlyOn(content::BrowserThread::UI);
87 base::SequencedWorkerPool* ChromeRLZTrackerDelegate::GetBlockingPool() {
88 return content::BrowserThread::GetBlockingPool();
91 net::URLRequestContextGetter* ChromeRLZTrackerDelegate::GetRequestContext() {
92 return g_browser_process->system_request_context();
95 bool ChromeRLZTrackerDelegate::GetBrand(std::string* brand) {
96 return google_brand::GetBrand(brand);
99 bool ChromeRLZTrackerDelegate::IsBrandOrganic(const std::string& brand) {
100 return brand.empty() || google_brand::IsOrganic(brand);
103 bool ChromeRLZTrackerDelegate::GetReactivationBrand(std::string* brand) {
104 return google_brand::GetReactivationBrand(brand);
107 bool ChromeRLZTrackerDelegate::ShouldEnableZeroDelayForTesting() {
108 return base::CommandLine::ForCurrentProcess()->HasSwitch(
109 ::switches::kTestType);
112 bool ChromeRLZTrackerDelegate::GetLanguage(base::string16* language) {
113 #if defined(OS_WIN)
114 return GoogleUpdateSettings::GetLanguage(language);
115 #else
116 // TODO(thakis): Implement.
117 NOTIMPLEMENTED();
118 return false;
119 #endif
122 bool ChromeRLZTrackerDelegate::GetReferral(base::string16* referral) {
123 #if defined(OS_WIN)
124 return GoogleUpdateSettings::GetReferral(referral);
125 #else
126 // The referral program is defunct and not used. No need to implement this
127 // function on non-Win platforms.
128 return true;
129 #endif
132 bool ChromeRLZTrackerDelegate::ClearReferral() {
133 #if defined(OS_WIN)
134 return GoogleUpdateSettings::ClearReferral();
135 #else
136 // The referral program is defunct and not used. No need to implement this
137 // function on non-Win platforms.
138 return true;
139 #endif
142 void ChromeRLZTrackerDelegate::SetOmniboxSearchCallback(
143 const base::Closure& callback) {
144 DCHECK(!callback.is_null());
145 omnibox_url_opened_subscription_ =
146 OmniboxEventGlobalTracker::GetInstance()->RegisterCallback(
147 base::Bind(&ChromeRLZTrackerDelegate::OnURLOpenedFromOmnibox,
148 base::Unretained(this)));
149 on_omnibox_search_callback_ = callback;
152 void ChromeRLZTrackerDelegate::SetHomepageSearchCallback(
153 const base::Closure& callback) {
154 DCHECK(!callback.is_null());
155 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
156 content::NotificationService::AllSources());
157 on_homepage_search_callback_ = callback;
160 void ChromeRLZTrackerDelegate::Observe(
161 int type,
162 const content::NotificationSource& source,
163 const content::NotificationDetails& details) {
164 using std::swap;
165 base::Closure callback_to_run;
166 switch (type) {
167 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
168 // Firstly check if it is a Google search.
169 content::LoadCommittedDetails* load_details =
170 content::Details<content::LoadCommittedDetails>(details).ptr();
171 if (load_details == nullptr)
172 break;
174 content::NavigationEntry* entry = load_details->entry;
175 if (entry == nullptr)
176 break;
178 if (google_util::IsGoogleSearchUrl(entry->GetURL())) {
179 // If it is a Google search, check if it originates from HOMEPAGE by
180 // getting the previous NavigationEntry.
181 content::NavigationController* controller =
182 content::Source<content::NavigationController>(source).ptr();
183 if (controller == nullptr)
184 break;
186 int entry_index = controller->GetLastCommittedEntryIndex();
187 if (entry_index < 1)
188 break;
190 const content::NavigationEntry* previous_entry =
191 controller->GetEntryAtIndex(entry_index - 1);
193 if (previous_entry == nullptr)
194 break;
196 // Make sure it is a Google web page originated from HOMEPAGE.
197 if (google_util::IsGoogleHomePageUrl(previous_entry->GetURL()) &&
198 ((previous_entry->GetTransitionType() &
199 ui::PAGE_TRANSITION_HOME_PAGE) != 0)) {
200 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
201 content::NotificationService::AllSources());
202 swap(callback_to_run, on_homepage_search_callback_);
205 break;
208 default:
209 NOTREACHED();
210 break;
213 if (!callback_to_run.is_null())
214 callback_to_run.Run();
217 void ChromeRLZTrackerDelegate::OnURLOpenedFromOmnibox(OmniboxLog* log) {
218 using std::swap;
220 // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than
221 // it did previously. The RLZ folks want RLZ's "first search" detection
222 // to remain as unaffected as possible by this change. This test is
223 // there to keep the old behavior.
224 if (!log->is_popup_open)
225 return;
227 omnibox_url_opened_subscription_.reset();
228 base::Closure omnibox_callback;
229 swap(omnibox_callback, on_omnibox_search_callback_);
230 omnibox_callback.Run();