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 "ios/chrome/browser/rlz/rlz_tracker_delegate_impl.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "components/google/core/browser/google_util.h"
11 #include "components/omnibox/browser/omnibox_event_global_tracker.h"
12 #include "components/omnibox/browser/omnibox_log.h"
13 #include "components/search_engines/template_url.h"
14 #include "components/search_engines/template_url_service.h"
15 #include "ios/chrome/browser/application_context.h"
16 #include "ios/chrome/browser/google/google_brand.h"
17 #include "ios/chrome/browser/pref_names.h"
18 #include "ios/chrome/browser/search_engines/template_url_service_factory.h"
19 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
20 #include "ios/web/public/web_thread.h"
22 RLZTrackerDelegateImpl::RLZTrackerDelegateImpl() {}
24 RLZTrackerDelegateImpl::~RLZTrackerDelegateImpl() {}
27 bool RLZTrackerDelegateImpl::IsGoogleDefaultSearch(
28 ios::ChromeBrowserState
* browser_state
) {
29 bool is_google_default_search
= false;
30 TemplateURLService
* template_url_service
=
31 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state
);
32 if (template_url_service
) {
33 const TemplateURL
* url_template
=
34 template_url_service
->GetDefaultSearchProvider();
35 is_google_default_search
= url_template
&&
36 url_template
->url_ref().HasGoogleBaseURLs(
37 template_url_service
->search_terms_data());
39 return is_google_default_search
;
43 bool RLZTrackerDelegateImpl::IsGoogleHomepage(
44 ios::ChromeBrowserState
* browser_state
) {
45 return google_util::IsGoogleHomePageUrl(
46 GURL(browser_state
->GetPrefs()->GetString(ios::prefs::kHomePage
)));
50 bool RLZTrackerDelegateImpl::IsGoogleInStartpages(
51 ios::ChromeBrowserState
* browser_state
) {
52 // iOS does not have a notion of startpages.
56 void RLZTrackerDelegateImpl::Cleanup() {
57 on_omnibox_search_callback_
.Reset();
60 bool RLZTrackerDelegateImpl::IsOnUIThread() {
61 return web::WebThread::CurrentlyOn(web::WebThread::UI
);
64 base::SequencedWorkerPool
* RLZTrackerDelegateImpl::GetBlockingPool() {
65 return web::WebThread::GetBlockingPool();
68 net::URLRequestContextGetter
* RLZTrackerDelegateImpl::GetRequestContext() {
69 return GetApplicationContext()->GetSystemURLRequestContext();
72 bool RLZTrackerDelegateImpl::GetBrand(std::string
* brand
) {
73 return ios::google_brand::GetBrand(brand
);
76 bool RLZTrackerDelegateImpl::IsBrandOrganic(const std::string
& brand
) {
77 return brand
.empty() || ios::google_brand::IsOrganic(brand
);
80 bool RLZTrackerDelegateImpl::GetReactivationBrand(std::string
* brand
) {
81 // iOS does not have reactivation brand.
85 bool RLZTrackerDelegateImpl::ShouldEnableZeroDelayForTesting() {
89 bool RLZTrackerDelegateImpl::GetLanguage(base::string16
* language
) {
90 // TODO(thakis): Implement.
95 bool RLZTrackerDelegateImpl::GetReferral(base::string16
* referral
) {
96 // The referral program is defunct and not used. No need to implement this
97 // function on non-Win platforms.
101 bool RLZTrackerDelegateImpl::ClearReferral() {
102 // The referral program is defunct and not used. No need to implement this
103 // function on non-Win platforms.
107 void RLZTrackerDelegateImpl::SetOmniboxSearchCallback(
108 const base::Closure
& callback
) {
109 DCHECK(!callback
.is_null());
110 on_omnibox_search_callback_
= callback
;
111 on_omnibox_url_opened_subscription_
=
112 OmniboxEventGlobalTracker::GetInstance()->RegisterCallback(
113 base::Bind(&RLZTrackerDelegateImpl::OnURLOpenedFromOmnibox
,
114 base::Unretained(this)));
117 void RLZTrackerDelegateImpl::SetHomepageSearchCallback(
118 const base::Closure
& callback
) {
122 void RLZTrackerDelegateImpl::OnURLOpenedFromOmnibox(OmniboxLog
* log
) {
123 // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than
124 // it did previously. The RLZ folks want RLZ's "first search" detection
125 // to remain as unaffected as possible by this change. This test is
126 // there to keep the old behavior.
127 if (!log
->is_popup_open
)
130 on_omnibox_url_opened_subscription_
.reset();
133 base::Closure callback_to_run
;
134 swap(callback_to_run
, on_omnibox_search_callback_
);
135 if (!callback_to_run
.is_null())
136 callback_to_run
.Run();