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"
32 #include "chrome/installer/util/google_update_settings.h"
35 ChromeRLZTrackerDelegate::ChromeRLZTrackerDelegate() {
38 ChromeRLZTrackerDelegate::~ChromeRLZTrackerDelegate() {
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
;
57 bool ChromeRLZTrackerDelegate::IsGoogleHomepage(Profile
* profile
) {
58 return google_util::IsGoogleHomePageUrl(
59 GURL(profile
->GetPrefs()->GetString(prefs::kHomePage
)));
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
) {
114 return GoogleUpdateSettings::GetLanguage(language
);
116 // TODO(thakis): Implement.
122 bool ChromeRLZTrackerDelegate::GetReferral(base::string16
* referral
) {
124 return GoogleUpdateSettings::GetReferral(referral
);
126 // The referral program is defunct and not used. No need to implement this
127 // function on non-Win platforms.
132 bool ChromeRLZTrackerDelegate::ClearReferral() {
134 return GoogleUpdateSettings::ClearReferral();
136 // The referral program is defunct and not used. No need to implement this
137 // function on non-Win platforms.
142 void ChromeRLZTrackerDelegate::SetOmniboxSearchCallback(
143 const base::Closure
& callback
) {
144 DCHECK(!callback
.is_null());
145 registrar_
.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
146 content::NotificationService::AllSources());
147 on_omnibox_search_callback_
= callback
;
150 void ChromeRLZTrackerDelegate::SetHomepageSearchCallback(
151 const base::Closure
& callback
) {
152 DCHECK(!callback
.is_null());
153 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
154 content::NotificationService::AllSources());
155 on_homepage_search_callback_
= callback
;
158 void ChromeRLZTrackerDelegate::Observe(
160 const content::NotificationSource
& source
,
161 const content::NotificationDetails
& details
) {
163 base::Closure callback_to_run
;
165 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL
:
166 // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than
167 // it did previously. The RLZ folks want RLZ's "first search" detection
168 // to remain as unaffected as possible by this change. This test is
169 // there to keep the old behavior.
170 if (!content::Details
<OmniboxLog
>(details
).ptr()->is_popup_open
)
172 registrar_
.Remove(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
173 content::NotificationService::AllSources());
174 swap(callback_to_run
, on_omnibox_search_callback_
);
177 case content::NOTIFICATION_NAV_ENTRY_COMMITTED
: {
178 // Firstly check if it is a Google search.
179 content::LoadCommittedDetails
* load_details
=
180 content::Details
<content::LoadCommittedDetails
>(details
).ptr();
181 if (load_details
== nullptr)
184 content::NavigationEntry
* entry
= load_details
->entry
;
185 if (entry
== nullptr)
188 if (google_util::IsGoogleSearchUrl(entry
->GetURL())) {
189 // If it is a Google search, check if it originates from HOMEPAGE by
190 // getting the previous NavigationEntry.
191 content::NavigationController
* controller
=
192 content::Source
<content::NavigationController
>(source
).ptr();
193 if (controller
== nullptr)
196 int entry_index
= controller
->GetLastCommittedEntryIndex();
200 const content::NavigationEntry
* previous_entry
=
201 controller
->GetEntryAtIndex(entry_index
- 1);
203 if (previous_entry
== nullptr)
206 // Make sure it is a Google web page originated from HOMEPAGE.
207 if (google_util::IsGoogleHomePageUrl(previous_entry
->GetURL()) &&
208 ((previous_entry
->GetTransitionType() &
209 ui::PAGE_TRANSITION_HOME_PAGE
) != 0)) {
210 registrar_
.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
211 content::NotificationService::AllSources());
212 swap(callback_to_run
, on_homepage_search_callback_
);
223 if (!callback_to_run
.is_null())
224 callback_to_run
.Run();