Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: components/rlz/rlz_tracker.cc

Issue 1212163011: Componentize chrome/browser/rlz (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/rlz/rlz_tracker.h ('k') | components/rlz/rlz_tracker_chromeos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This code glues the RLZ library DLL with Chrome. It allows Chrome to work 5 // This code glues the RLZ library DLL with Chrome. It allows Chrome to work
6 // with or without the DLL being present. If the DLL is not present the 6 // with or without the DLL being present. If the DLL is not present the
7 // functions do nothing and just return false. 7 // functions do nothing and just return false.
8 8
9 #include "chrome/browser/rlz/rlz.h" 9 #include "components/rlz/rlz_tracker.h"
10 10
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h"
15 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
19 #include "base/trace_event/trace_event.h" 17 #include "base/trace_event/trace_event.h"
20 #include "chrome/browser/browser_process.h" 18 #include "components/rlz/rlz_tracker_delegate.h"
21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/google/google_brand.h"
23 #include "chrome/browser/prefs/session_startup_pref.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h"
25 #include "chrome/browser/ui/startup/startup_browser_creator.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h"
28 #include "components/google/core/browser/google_util.h"
29 #include "components/omnibox/browser/omnibox_log.h"
30 #include "components/search_engines/template_url.h"
31 #include "components/search_engines/template_url_service.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/navigation_details.h"
35 #include "content/public/browser/navigation_entry.h"
36 #include "content/public/browser/notification_service.h"
37 #include "net/http/http_util.h" 19 #include "net/http/http_util.h"
38 20
39 #if defined(OS_WIN) 21 namespace rlz {
40 #include "chrome/installer/util/google_update_settings.h"
41 #else
42 namespace GoogleUpdateSettings {
43 static bool GetLanguage(base::string16* language) {
44 // TODO(thakis): Implement.
45 NOTIMPLEMENTED();
46 return false;
47 }
48
49 // The referral program is defunct and not used. No need to implement these
50 // functions on non-Win platforms.
51 static bool GetReferral(base::string16* referral) {
52 return true;
53 }
54 static bool ClearReferral() {
55 return true;
56 }
57 } // namespace GoogleUpdateSettings
58 #endif
59
60 using content::BrowserThread;
61 using content::NavigationEntry;
62 using content::NavigationController;
63
64 namespace { 22 namespace {
65 23
66 // Maximum and minimum delay for financial ping we would allow to be set through 24 // Maximum and minimum delay for financial ping we would allow to be set through
67 // master preferences. Somewhat arbitrary, may need to be adjusted in future. 25 // master preferences. Somewhat arbitrary, may need to be adjusted in future.
68 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200); 26 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200);
69 const base::TimeDelta kMinInitDelay = base::TimeDelta::FromSeconds(20); 27 const base::TimeDelta kMinInitDelay = base::TimeDelta::FromSeconds(20);
70 28
71 bool IsBrandOrganic(const std::string& brand) {
72 return brand.empty() || google_brand::IsOrganic(brand);
73 }
74
75 void RecordProductEvents(bool first_run, 29 void RecordProductEvents(bool first_run,
76 bool is_google_default_search, 30 bool is_google_default_search,
77 bool is_google_homepage, 31 bool is_google_homepage,
78 bool is_google_in_startpages, 32 bool is_google_in_startpages,
79 bool already_ran, 33 bool already_ran,
80 bool omnibox_used, 34 bool omnibox_used,
81 bool homepage_used, 35 bool homepage_used,
82 bool app_list_used) { 36 bool app_list_used) {
83 TRACE_EVENT0("RLZ", "RecordProductEvents"); 37 TRACE_EVENT0("RLZ", "RecordProductEvents");
84 // Record the installation of chrome. We call this all the time but the rlz 38 // Record the installation of chrome. We call this all the time but the rlz
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 product_signature = "chrome"; 139 product_signature = "chrome";
186 #endif 140 #endif
187 return rlz_lib::SendFinancialPing(rlz_lib::CHROME, points, 141 return rlz_lib::SendFinancialPing(rlz_lib::CHROME, points,
188 product_signature.c_str(), 142 product_signature.c_str(),
189 brand.c_str(), referral_ascii.c_str(), 143 brand.c_str(), referral_ascii.c_str(),
190 lang_ascii.c_str(), false, true); 144 lang_ascii.c_str(), false, true);
191 } 145 }
192 146
193 } // namespace 147 } // namespace
194 148
195 RLZTracker* RLZTracker::tracker_ = NULL; 149 RLZTracker* RLZTracker::tracker_ = nullptr;
196 150
197 // static 151 // static
198 RLZTracker* RLZTracker::GetInstance() { 152 RLZTracker* RLZTracker::GetInstance() {
199 return tracker_ ? tracker_ : Singleton<RLZTracker>::get(); 153 return tracker_ ? tracker_ : Singleton<RLZTracker>::get();
200 } 154 }
201 155
202 RLZTracker::RLZTracker() 156 RLZTracker::RLZTracker()
203 : first_run_(false), 157 : first_run_(false),
204 send_ping_immediately_(false), 158 send_ping_immediately_(false),
205 is_google_default_search_(false), 159 is_google_default_search_(false),
206 is_google_homepage_(false), 160 is_google_homepage_(false),
207 is_google_in_startpages_(false), 161 is_google_in_startpages_(false),
208 worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()),
209 already_ran_(false), 162 already_ran_(false),
210 omnibox_used_(false), 163 omnibox_used_(false),
211 homepage_used_(false), 164 homepage_used_(false),
212 app_list_used_(false), 165 app_list_used_(false),
213 min_init_delay_(kMinInitDelay) { 166 min_init_delay_(kMinInitDelay) {
214 } 167 }
215 168
216 RLZTracker::~RLZTracker() { 169 RLZTracker::~RLZTracker() {
217 } 170 }
218 171
219 // static 172 // static
173 void RLZTracker::SetRlzDelegate(scoped_ptr<RLZTrackerDelegate> delegate) {
174 RLZTracker* tracker = GetInstance();
175 if (!tracker->delegate_) {
176 // RLZTracker::SetRlzDelegate is called at Profile creation time which can
177 // happens multiple time on ChromeOS, so do nothing if the delegate already
178 // exists.
179 tracker->SetDelegate(delegate.Pass());
180 }
181 }
182
183 void RLZTracker::SetDelegate(scoped_ptr<RLZTrackerDelegate> delegate) {
184 DCHECK(delegate);
185 DCHECK(!delegate_);
186 delegate_ = delegate.Pass();
187 worker_pool_token_ = delegate_->GetBlockingPool()->GetSequenceToken();
188 }
189
190 // static
220 bool RLZTracker::InitRlzDelayed(bool first_run, 191 bool RLZTracker::InitRlzDelayed(bool first_run,
221 bool send_ping_immediately, 192 bool send_ping_immediately,
222 base::TimeDelta delay, 193 base::TimeDelta delay,
223 bool is_google_default_search, 194 bool is_google_default_search,
224 bool is_google_homepage, 195 bool is_google_homepage,
225 bool is_google_in_startpages) { 196 bool is_google_in_startpages) {
226 return GetInstance()->Init(first_run, send_ping_immediately, delay, 197 return GetInstance()->Init(first_run, send_ping_immediately, delay,
227 is_google_default_search, is_google_homepage, 198 is_google_default_search, is_google_homepage,
228 is_google_in_startpages); 199 is_google_in_startpages);
229 } 200 }
230 201
231 // static
232 bool RLZTracker::InitRlzFromProfileDelayed(Profile* profile,
233 bool first_run,
234 bool send_ping_immediately,
235 base::TimeDelta delay) {
236 bool is_google_default_search = false;
237 TemplateURLService* template_url_service =
238 TemplateURLServiceFactory::GetForProfile(profile);
239 if (template_url_service) {
240 const TemplateURL* url_template =
241 template_url_service->GetDefaultSearchProvider();
242 is_google_default_search =
243 url_template && url_template->url_ref().HasGoogleBaseURLs(
244 template_url_service->search_terms_data());
245 }
246
247 PrefService* pref_service = profile->GetPrefs();
248 bool is_google_homepage = google_util::IsGoogleHomePageUrl(
249 GURL(pref_service->GetString(prefs::kHomePage)));
250
251 bool is_google_in_startpages = false;
252 #if !defined(OS_IOS)
253 // iOS does not have a notion of startpages.
254 SessionStartupPref session_startup_prefs =
255 StartupBrowserCreator::GetSessionStartupPref(
256 *base::CommandLine::ForCurrentProcess(), profile);
257 if (session_startup_prefs.type == SessionStartupPref::URLS) {
258 is_google_in_startpages =
259 std::count_if(session_startup_prefs.urls.begin(),
260 session_startup_prefs.urls.end(),
261 google_util::IsGoogleHomePageUrl) > 0;
262 }
263 #endif
264
265 if (!InitRlzDelayed(first_run, send_ping_immediately, delay,
266 is_google_default_search, is_google_homepage,
267 is_google_in_startpages)) {
268 return false;
269 }
270
271 #if !defined(OS_IOS)
272 // Prime the RLZ cache for the home page access point so that its avaiable
273 // for the startup page if needed (i.e., when the startup page is set to
274 // the home page).
275 GetAccessPointRlz(ChromeHomePage(), NULL);
276 #endif // !defined(OS_IOS)
277
278 return true;
279 }
280
281 bool RLZTracker::Init(bool first_run, 202 bool RLZTracker::Init(bool first_run,
282 bool send_ping_immediately, 203 bool send_ping_immediately,
283 base::TimeDelta delay, 204 base::TimeDelta delay,
284 bool is_google_default_search, 205 bool is_google_default_search,
285 bool is_google_homepage, 206 bool is_google_homepage,
286 bool is_google_in_startpages) { 207 bool is_google_in_startpages) {
208 DCHECK(delegate_) << "RLZTracker used before initialization";
287 first_run_ = first_run; 209 first_run_ = first_run;
288 is_google_default_search_ = is_google_default_search; 210 is_google_default_search_ = is_google_default_search;
289 is_google_homepage_ = is_google_homepage; 211 is_google_homepage_ = is_google_homepage;
290 is_google_in_startpages_ = is_google_in_startpages; 212 is_google_in_startpages_ = is_google_in_startpages;
291 send_ping_immediately_ = send_ping_immediately; 213 send_ping_immediately_ = send_ping_immediately;
292 214
293 // Enable zero delays for testing. 215 // Enable zero delays for testing.
294 if (base::CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType)) 216 if (delegate_->ShouldEnableZeroDelayForTesting())
295 EnableZeroDelayForTesting(); 217 EnableZeroDelayForTesting();
296 218
297 delay = std::min(kMaxInitDelay, std::max(min_init_delay_, delay)); 219 delay = std::min(kMaxInitDelay, std::max(min_init_delay_, delay));
298 220
299 if (google_brand::GetBrand(&brand_) && !IsBrandOrganic(brand_)) { 221 if (delegate_->GetBrand(&brand_) && !delegate_->IsBrandOrganic(brand_)) {
300 // Register for notifications from the omnibox so that we can record when 222 // Register for notifications from the omnibox so that we can record when
301 // the user performs a first search. 223 // the user performs a first search.
302 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, 224 delegate_->SetOmniboxSearchCallback(
303 content::NotificationService::AllSources()); 225 base::Bind(&RLZTracker::RecordFirstSearch, base::Unretained(this),
226 ChromeOmnibox()));
304 227
305 #if !defined(OS_IOS) 228 #if !defined(OS_IOS)
306 // Register for notifications from navigations, to see if the user has used 229 // Register for notifications from navigations, to see if the user has used
307 // the home page. 230 // the home page.
308 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 231 delegate_->SetHomepageSearchCallback(
309 content::NotificationService::AllSources()); 232 base::Bind(&RLZTracker::RecordFirstSearch, base::Unretained(this),
310 #endif // !defined(OS_IOS) 233 ChromeHomePage()));
234 #endif
311 } 235 }
312 google_brand::GetReactivationBrand(&reactivation_brand_); 236 delegate_->GetReactivationBrand(&reactivation_brand_);
313 237
314 net::URLRequestContextGetter* context_getter = 238 // Could be null; don't run if so. RLZ will try again next restart.
315 g_browser_process->system_request_context(); 239 net::URLRequestContextGetter* context_getter = delegate_->GetRequestContext();
316
317 // Could be NULL; don't run if so. RLZ will try again next restart.
318 if (context_getter) { 240 if (context_getter) {
319 rlz_lib::SetURLRequestContext(context_getter); 241 rlz_lib::SetURLRequestContext(context_getter);
320 ScheduleDelayedInit(delay); 242 ScheduleDelayedInit(delay);
321 } 243 }
322 244
245 #if !defined(OS_IOS)
246 // Prime the RLZ cache for the home page access point so that its avaiable
247 // for the startup page if needed (i.e., when the startup page is set to
248 // the home page).
249 GetAccessPointRlz(ChromeHomePage(), nullptr);
250 #endif // !defined(OS_IOS)
251
323 return true; 252 return true;
324 } 253 }
325 254
255 void RLZTracker::Cleanup() {
256 rlz_cache_.clear();
257 if (delegate_)
258 delegate_->Cleanup();
259 }
260
326 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) { 261 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) {
262 DCHECK(delegate_) << "RLZTracker used before initialization";
327 // The RLZTracker is a singleton object that outlives any runnable tasks 263 // The RLZTracker is a singleton object that outlives any runnable tasks
328 // that will be queued up. 264 // that will be queued up.
329 BrowserThread::GetBlockingPool()->PostDelayedSequencedWorkerTask( 265 delegate_->GetBlockingPool()->PostDelayedSequencedWorkerTask(
330 worker_pool_token_, 266 worker_pool_token_, FROM_HERE,
331 FROM_HERE, 267 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), delay);
332 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)),
333 delay);
334 } 268 }
335 269
336 void RLZTracker::DelayedInit() { 270 void RLZTracker::DelayedInit() {
271 DCHECK(delegate_) << "RLZTracker used before initialization";
337 bool schedule_ping = false; 272 bool schedule_ping = false;
338 273
339 // For organic brandcodes do not use rlz at all. Empty brandcode usually 274 // For organic brandcodes do not use rlz at all. Empty brandcode usually
340 // means a chromium install. This is ok. 275 // means a chromium install. This is ok.
341 if (!IsBrandOrganic(brand_)) { 276 if (!delegate_->IsBrandOrganic(brand_)) {
342 RecordProductEvents(first_run_, is_google_default_search_, 277 RecordProductEvents(first_run_, is_google_default_search_,
343 is_google_homepage_, is_google_in_startpages_, 278 is_google_homepage_, is_google_in_startpages_,
344 already_ran_, omnibox_used_, homepage_used_, 279 already_ran_, omnibox_used_, homepage_used_,
345 app_list_used_); 280 app_list_used_);
346 schedule_ping = true; 281 schedule_ping = true;
347 } 282 }
348 283
349 // If chrome has been reactivated, record the events for this brand 284 // If chrome has been reactivated, record the events for this brand
350 // as well. 285 // as well.
351 if (!IsBrandOrganic(reactivation_brand_)) { 286 if (!delegate_->IsBrandOrganic(reactivation_brand_)) {
352 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str()); 287 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str());
353 RecordProductEvents(first_run_, is_google_default_search_, 288 RecordProductEvents(first_run_, is_google_default_search_,
354 is_google_homepage_, is_google_in_startpages_, 289 is_google_homepage_, is_google_in_startpages_,
355 already_ran_, omnibox_used_, homepage_used_, 290 already_ran_, omnibox_used_, homepage_used_,
356 app_list_used_); 291 app_list_used_);
357 schedule_ping = true; 292 schedule_ping = true;
358 } 293 }
359 294
360 already_ran_ = true; 295 already_ran_ = true;
361 296
362 if (schedule_ping) 297 if (schedule_ping)
363 ScheduleFinancialPing(); 298 ScheduleFinancialPing();
364 } 299 }
365 300
366 void RLZTracker::ScheduleFinancialPing() { 301 void RLZTracker::ScheduleFinancialPing() {
367 BrowserThread::GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( 302 DCHECK(delegate_) << "RLZTracker used before initialization";
368 worker_pool_token_, 303 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior(
369 FROM_HERE, 304 worker_pool_token_, FROM_HERE,
370 base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this)), 305 base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this)),
371 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 306 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
372 } 307 }
373 308
374 void RLZTracker::PingNowImpl() { 309 void RLZTracker::PingNowImpl() {
310 DCHECK(delegate_) << "RLZTracker used before initialization";
375 TRACE_EVENT0("RLZ", "RLZTracker::PingNowImpl"); 311 TRACE_EVENT0("RLZ", "RLZTracker::PingNowImpl");
376 base::string16 lang; 312 base::string16 lang;
377 GoogleUpdateSettings::GetLanguage(&lang); 313 delegate_->GetLanguage(&lang);
378 if (lang.empty()) 314 if (lang.empty())
379 lang = base::ASCIIToUTF16("en"); 315 lang = base::ASCIIToUTF16("en");
380 base::string16 referral; 316 base::string16 referral;
381 GoogleUpdateSettings::GetReferral(&referral); 317 delegate_->GetReferral(&referral);
382 318
383 if (!IsBrandOrganic(brand_) && SendFinancialPing(brand_, lang, referral)) { 319 if (!delegate_->IsBrandOrganic(brand_) &&
384 GoogleUpdateSettings::ClearReferral(); 320 SendFinancialPing(brand_, lang, referral)) {
321 delegate_->ClearReferral();
385 322
386 { 323 {
387 base::AutoLock lock(cache_lock_); 324 base::AutoLock lock(cache_lock_);
388 rlz_cache_.clear(); 325 rlz_cache_.clear();
389 } 326 }
390 327
391 // Prime the RLZ cache for the access points we are interested in. 328 // Prime the RLZ cache for the access points we are interested in.
392 GetAccessPointRlz(RLZTracker::ChromeOmnibox(), NULL); 329 GetAccessPointRlz(RLZTracker::ChromeOmnibox(), nullptr);
393 #if !defined(OS_IOS) 330 #if !defined(OS_IOS)
394 GetAccessPointRlz(RLZTracker::ChromeHomePage(), NULL); 331 GetAccessPointRlz(RLZTracker::ChromeHomePage(), nullptr);
395 GetAccessPointRlz(RLZTracker::ChromeAppList(), NULL); 332 GetAccessPointRlz(RLZTracker::ChromeAppList(), nullptr);
396 #endif // !defined(OS_IOS) 333 #endif // !defined(OS_IOS)
397 } 334 }
398 335
399 if (!IsBrandOrganic(reactivation_brand_)) { 336 if (!delegate_->IsBrandOrganic(reactivation_brand_)) {
400 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str()); 337 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str());
401 SendFinancialPing(reactivation_brand_, lang, referral); 338 SendFinancialPing(reactivation_brand_, lang, referral);
402 } 339 }
403 } 340 }
404 341
405 bool RLZTracker::SendFinancialPing(const std::string& brand, 342 bool RLZTracker::SendFinancialPing(const std::string& brand,
406 const base::string16& lang, 343 const base::string16& lang,
407 const base::string16& referral) { 344 const base::string16& referral) {
408 return ::SendFinancialPing(brand, lang, referral); 345 return ::rlz::SendFinancialPing(brand, lang, referral);
409 }
410
411 void RLZTracker::Observe(int type,
412 const content::NotificationSource& source,
413 const content::NotificationDetails& details) {
414 switch (type) {
415 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL:
416 // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than
417 // it did previously. The RLZ folks want RLZ's "first search" detection
418 // to remain as unaffected as possible by this change. This test is
419 // there to keep the old behavior.
420 if (!content::Details<OmniboxLog>(details).ptr()->is_popup_open)
421 break;
422 RecordFirstSearch(ChromeOmnibox());
423 registrar_.Remove(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
424 content::NotificationService::AllSources());
425 break;
426 #if !defined(OS_IOS)
427 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
428 // Firstly check if it is a Google search.
429 content::LoadCommittedDetails* load_details =
430 content::Details<content::LoadCommittedDetails>(details).ptr();
431 if (load_details == NULL)
432 break;
433
434 NavigationEntry* entry = load_details->entry;
435 if (entry == NULL)
436 break;
437
438 if (google_util::IsGoogleSearchUrl(entry->GetURL())) {
439 // If it is a Google search, check if it originates from HOMEPAGE by
440 // getting the previous NavigationEntry.
441 NavigationController* controller =
442 content::Source<NavigationController>(source).ptr();
443 if (controller == NULL)
444 break;
445
446 int entry_index = controller->GetLastCommittedEntryIndex();
447 if (entry_index < 1)
448 break;
449
450 const NavigationEntry* previous_entry = controller->GetEntryAtIndex(
451 entry_index - 1);
452
453 if (previous_entry == NULL)
454 break;
455
456 // Make sure it is a Google web page originated from HOMEPAGE.
457 if (google_util::IsGoogleHomePageUrl(previous_entry->GetURL()) &&
458 ((previous_entry->GetTransitionType() &
459 ui::PAGE_TRANSITION_HOME_PAGE) != 0)) {
460 RecordFirstSearch(ChromeHomePage());
461 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
462 content::NotificationService::AllSources());
463 }
464 }
465 break;
466 }
467 #endif // !defined(OS_IOS)
468 default:
469 NOTREACHED();
470 break;
471 }
472 } 346 }
473 347
474 // static 348 // static
475 bool RLZTracker::RecordProductEvent(rlz_lib::Product product, 349 bool RLZTracker::RecordProductEvent(rlz_lib::Product product,
476 rlz_lib::AccessPoint point, 350 rlz_lib::AccessPoint point,
477 rlz_lib::Event event_id) { 351 rlz_lib::Event event_id) {
478 return GetInstance()->RecordProductEventImpl(product, point, event_id); 352 // This method is called during unit tests while the RLZTracker has not been
353 // initialized, so check for the presence of a delegate and exit if there is
354 // none registered.
355 RLZTracker* tracker = GetInstance();
356 return !tracker->delegate_ ? false : tracker->RecordProductEventImpl(
357 product, point, event_id);
479 } 358 }
480 359
481 bool RLZTracker::RecordProductEventImpl(rlz_lib::Product product, 360 bool RLZTracker::RecordProductEventImpl(rlz_lib::Product product,
482 rlz_lib::AccessPoint point, 361 rlz_lib::AccessPoint point,
483 rlz_lib::Event event_id) { 362 rlz_lib::Event event_id) {
363 DCHECK(delegate_) << "RLZTracker used before initialization";
484 // Make sure we don't access disk outside of the I/O thread. 364 // Make sure we don't access disk outside of the I/O thread.
485 // In such case we repost the task on the right thread and return error. 365 // In such case we repost the task on the right thread and return error.
486 if (ScheduleRecordProductEvent(product, point, event_id)) 366 if (ScheduleRecordProductEvent(product, point, event_id))
487 return true; 367 return true;
488 368
489 bool ret = rlz_lib::RecordProductEvent(product, point, event_id); 369 bool ret = rlz_lib::RecordProductEvent(product, point, event_id);
490 370
491 // If chrome has been reactivated, record the event for this brand as well. 371 // If chrome has been reactivated, record the event for this brand as well.
492 if (!reactivation_brand_.empty()) { 372 if (!reactivation_brand_.empty()) {
493 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str()); 373 rlz_lib::SupplementaryBranding branding(reactivation_brand_.c_str());
494 ret &= rlz_lib::RecordProductEvent(product, point, event_id); 374 ret &= rlz_lib::RecordProductEvent(product, point, event_id);
495 } 375 }
496 376
497 return ret; 377 return ret;
498 } 378 }
499 379
500 bool RLZTracker::ScheduleRecordProductEvent(rlz_lib::Product product, 380 bool RLZTracker::ScheduleRecordProductEvent(rlz_lib::Product product,
501 rlz_lib::AccessPoint point, 381 rlz_lib::AccessPoint point,
502 rlz_lib::Event event_id) { 382 rlz_lib::Event event_id) {
503 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) 383 DCHECK(delegate_) << "RLZTracker used before initialization";
384 if (!delegate_->IsOnUIThread())
504 return false; 385 return false;
505 386
506 BrowserThread::GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( 387 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior(
507 worker_pool_token_, 388 worker_pool_token_, FROM_HERE,
508 FROM_HERE, 389 base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent), product,
509 base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent), 390 point, event_id),
510 product, point, event_id),
511 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 391 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
512 392
513 return true; 393 return true;
514 } 394 }
515 395
516 void RLZTracker::RecordFirstSearch(rlz_lib::AccessPoint point) { 396 void RLZTracker::RecordFirstSearch(rlz_lib::AccessPoint point) {
397 DCHECK(delegate_) << "RLZTracker used before initialization";
517 // Make sure we don't access disk outside of the I/O thread. 398 // Make sure we don't access disk outside of the I/O thread.
518 // In such case we repost the task on the right thread and return error. 399 // In such case we repost the task on the right thread and return error.
519 if (ScheduleRecordFirstSearch(point)) 400 if (ScheduleRecordFirstSearch(point))
520 return; 401 return;
521 402
522 bool* record_used = GetAccessPointRecord(point); 403 bool* record_used = GetAccessPointRecord(point);
523 404
524 // Try to record event now, else set the flag to try later when we 405 // Try to record event now, else set the flag to try later when we
525 // attempt the ping. 406 // attempt the ping.
526 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) 407 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH))
527 *record_used = true; 408 *record_used = true;
528 else if (send_ping_immediately_ && point == ChromeOmnibox()) 409 else if (send_ping_immediately_ && point == ChromeOmnibox())
529 ScheduleDelayedInit(base::TimeDelta()); 410 ScheduleDelayedInit(base::TimeDelta());
530 } 411 }
531 412
532 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { 413 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) {
533 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) 414 DCHECK(delegate_) << "RLZTracker used before initialization";
415 if (!delegate_->IsOnUIThread())
534 return false; 416 return false;
535 BrowserThread::GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( 417 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior(
536 worker_pool_token_, 418 worker_pool_token_, FROM_HERE,
537 FROM_HERE, 419 base::Bind(&RLZTracker::RecordFirstSearch, base::Unretained(this), point),
538 base::Bind(&RLZTracker::RecordFirstSearch,
539 base::Unretained(this), point),
540 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 420 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
541 return true; 421 return true;
542 } 422 }
543 423
544 bool* RLZTracker::GetAccessPointRecord(rlz_lib::AccessPoint point) { 424 bool* RLZTracker::GetAccessPointRecord(rlz_lib::AccessPoint point) {
545 if (point == ChromeOmnibox()) 425 if (point == ChromeOmnibox())
546 return &omnibox_used_; 426 return &omnibox_used_;
547 #if !defined(OS_IOS) 427 #if !defined(OS_IOS)
548 if (point == ChromeHomePage()) 428 if (point == ChromeHomePage())
549 return &homepage_used_; 429 return &homepage_used_;
550 if (point == ChromeAppList()) 430 if (point == ChromeAppList())
551 return &app_list_used_; 431 return &app_list_used_;
552 #endif // !defined(OS_IOS) 432 #endif // !defined(OS_IOS)
553 NOTREACHED(); 433 NOTREACHED();
554 return NULL; 434 return nullptr;
555 } 435 }
556 436
557 // static 437 // static
558 std::string RLZTracker::GetAccessPointHttpHeader(rlz_lib::AccessPoint point) { 438 std::string RLZTracker::GetAccessPointHttpHeader(rlz_lib::AccessPoint point) {
559 TRACE_EVENT0("RLZ", "RLZTracker::GetAccessPointHttpHeader"); 439 TRACE_EVENT0("RLZ", "RLZTracker::GetAccessPointHttpHeader");
560 std::string extra_headers; 440 std::string extra_headers;
561 base::string16 rlz_string; 441 base::string16 rlz_string;
562 RLZTracker::GetAccessPointRlz(point, &rlz_string); 442 RLZTracker::GetAccessPointRlz(point, &rlz_string);
563 if (!rlz_string.empty()) { 443 if (!rlz_string.empty()) {
564 net::HttpUtil::AppendHeaderIfMissing("X-Rlz-String", 444 net::HttpUtil::AppendHeaderIfMissing("X-Rlz-String",
565 base::UTF16ToUTF8(rlz_string), 445 base::UTF16ToUTF8(rlz_string),
566 &extra_headers); 446 &extra_headers);
567 } 447 }
568 448
569 return extra_headers; 449 return extra_headers;
570 } 450 }
571 451
572 // GetAccessPointRlz() caches RLZ strings for all access points. If we had 452 // GetAccessPointRlz() caches RLZ strings for all access points. If we had
573 // a successful ping, then we update the cached value. 453 // a successful ping, then we update the cached value.
454 // static
574 bool RLZTracker::GetAccessPointRlz(rlz_lib::AccessPoint point, 455 bool RLZTracker::GetAccessPointRlz(rlz_lib::AccessPoint point,
575 base::string16* rlz) { 456 base::string16* rlz) {
457 // This method is called during unit tests while the RLZTracker has not been
458 // initialized, so check for the presence of a delegate and exit if there is
459 // none registered.
576 TRACE_EVENT0("RLZ", "RLZTracker::GetAccessPointRlz"); 460 TRACE_EVENT0("RLZ", "RLZTracker::GetAccessPointRlz");
577 return GetInstance()->GetAccessPointRlzImpl(point, rlz); 461 RLZTracker* tracker = GetInstance();
462 return !tracker->delegate_ ? false
463 : tracker->GetAccessPointRlzImpl(point, rlz);
578 } 464 }
579 465
580 // GetAccessPointRlz() caches RLZ strings for all access points. If we had 466 // GetAccessPointRlz() caches RLZ strings for all access points. If we had
581 // a successful ping, then we update the cached value. 467 // a successful ping, then we update the cached value.
582 bool RLZTracker::GetAccessPointRlzImpl(rlz_lib::AccessPoint point, 468 bool RLZTracker::GetAccessPointRlzImpl(rlz_lib::AccessPoint point,
583 base::string16* rlz) { 469 base::string16* rlz) {
470 DCHECK(delegate_) << "RLZTracker used before initialization";
584 // If the RLZ string for the specified access point is already cached, 471 // If the RLZ string for the specified access point is already cached,
585 // simply return its value. 472 // simply return its value.
586 { 473 {
587 base::AutoLock lock(cache_lock_); 474 base::AutoLock lock(cache_lock_);
588 if (rlz_cache_.find(point) != rlz_cache_.end()) { 475 if (rlz_cache_.find(point) != rlz_cache_.end()) {
589 if (rlz) 476 if (rlz)
590 *rlz = rlz_cache_[point]; 477 *rlz = rlz_cache_[point];
591 return true; 478 return true;
592 } 479 }
593 } 480 }
(...skipping 10 matching lines...) Expand all
604 base::string16 rlz_local(base::ASCIIToUTF16(str_rlz)); 491 base::string16 rlz_local(base::ASCIIToUTF16(str_rlz));
605 if (rlz) 492 if (rlz)
606 *rlz = rlz_local; 493 *rlz = rlz_local;
607 494
608 base::AutoLock lock(cache_lock_); 495 base::AutoLock lock(cache_lock_);
609 rlz_cache_[point] = rlz_local; 496 rlz_cache_[point] = rlz_local;
610 return true; 497 return true;
611 } 498 }
612 499
613 bool RLZTracker::ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) { 500 bool RLZTracker::ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) {
614 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) 501 DCHECK(delegate_) << "RLZTracker used before initialization";
502 if (!delegate_->IsOnUIThread())
615 return false; 503 return false;
616 504
617 base::string16* not_used = NULL; 505 base::string16* not_used = nullptr;
618 BrowserThread::GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( 506 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior(
619 worker_pool_token_, 507 worker_pool_token_, FROM_HERE,
620 FROM_HERE,
621 base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), point, 508 base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), point,
622 not_used), 509 not_used),
623 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 510 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
624 return true; 511 return true;
625 } 512 }
626 513
627 #if defined(OS_CHROMEOS) 514 #if defined(OS_CHROMEOS)
628 // static 515 // static
629 void RLZTracker::ClearRlzState() { 516 void RLZTracker::ClearRlzState() {
630 GetInstance()->ClearRlzStateImpl(); 517 RLZTracker* tracker = GetInstance();
518 if (tracker->delegate_)
519 tracker->ClearRlzStateImpl();
631 } 520 }
632 521
633 void RLZTracker::ClearRlzStateImpl() { 522 void RLZTracker::ClearRlzStateImpl() {
523 DCHECK(delegate_) << "RLZTracker used before initialization";
634 if (ScheduleClearRlzState()) 524 if (ScheduleClearRlzState())
635 return; 525 return;
636 rlz_lib::ClearAllProductEvents(rlz_lib::CHROME); 526 rlz_lib::ClearAllProductEvents(rlz_lib::CHROME);
637 } 527 }
638 528
639 bool RLZTracker::ScheduleClearRlzState() { 529 bool RLZTracker::ScheduleClearRlzState() {
640 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) 530 DCHECK(delegate_) << "RLZTracker used before initialization";
531 if (!delegate_->IsOnUIThread())
641 return false; 532 return false;
642 533
643 BrowserThread::GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( 534 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior(
644 worker_pool_token_, 535 worker_pool_token_, FROM_HERE,
645 FROM_HERE, 536 base::Bind(&RLZTracker::ClearRlzStateImpl, base::Unretained(this)),
646 base::Bind(&RLZTracker::ClearRlzStateImpl,
647 base::Unretained(this)),
648 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 537 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
649 return true; 538 return true;
650 } 539 }
651 #endif 540 #endif
652 541
653 // static 542 // static
654 void RLZTracker::CleanupRlz() { 543 void RLZTracker::CleanupRlz() {
655 GetInstance()->rlz_cache_.clear(); 544 GetInstance()->Cleanup();
656 GetInstance()->registrar_.RemoveAll(); 545 rlz_lib::SetURLRequestContext(nullptr);
657 rlz_lib::SetURLRequestContext(NULL);
658 } 546 }
659 547
660 // static 548 // static
661 void RLZTracker::EnableZeroDelayForTesting() { 549 void RLZTracker::EnableZeroDelayForTesting() {
662 GetInstance()->min_init_delay_ = base::TimeDelta(); 550 GetInstance()->min_init_delay_ = base::TimeDelta();
663 } 551 }
664 552
665 #if !defined(OS_IOS) 553 #if !defined(OS_IOS)
666 // static 554 // static
667 void RLZTracker::RecordAppListSearch() { 555 void RLZTracker::RecordAppListSearch() {
668 GetInstance()->RecordFirstSearch(RLZTracker::ChromeAppList()); 556 // This method is called during unit tests while the RLZTracker has not been
557 // initialized, so check for the presence of a delegate and exit if there is
558 // none registered.
559 RLZTracker* tracker = GetInstance();
560 if (tracker->delegate_)
561 tracker->RecordFirstSearch(RLZTracker::ChromeAppList());
669 } 562 }
670 #endif 563 #endif
564
565 } // namespace rlz
OLDNEW
« no previous file with comments | « components/rlz/rlz_tracker.h ('k') | components/rlz/rlz_tracker_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698