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