Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "components/rlz/rlz_tracker.h" | 9 #include "components/rlz/rlz_tracker.h" |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/task_scheduler/post_task.h" | |
| 21 #include "base/task_scheduler/task_traits.h" | |
| 19 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 20 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 21 #include "components/rlz/rlz_tracker_delegate.h" | 24 #include "components/rlz/rlz_tracker_delegate.h" |
| 22 | 25 |
| 23 namespace rlz { | 26 namespace rlz { |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 // Maximum and minimum delay for financial ping we would allow to be set through | 29 // Maximum and minimum delay for financial ping we would allow to be set through |
| 27 // master preferences. Somewhat arbitrary, may need to be adjusted in future. | 30 // master preferences. Somewhat arbitrary, may need to be adjusted in future. |
| 28 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200); | 31 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 RLZTracker::RLZTracker() | 161 RLZTracker::RLZTracker() |
| 159 : first_run_(false), | 162 : first_run_(false), |
| 160 send_ping_immediately_(false), | 163 send_ping_immediately_(false), |
| 161 is_google_default_search_(false), | 164 is_google_default_search_(false), |
| 162 is_google_homepage_(false), | 165 is_google_homepage_(false), |
| 163 is_google_in_startpages_(false), | 166 is_google_in_startpages_(false), |
| 164 already_ran_(false), | 167 already_ran_(false), |
| 165 omnibox_used_(false), | 168 omnibox_used_(false), |
| 166 homepage_used_(false), | 169 homepage_used_(false), |
| 167 app_list_used_(false), | 170 app_list_used_(false), |
| 168 min_init_delay_(kMinInitDelay) { | 171 min_init_delay_(kMinInitDelay), |
| 169 } | 172 task_runner_(base::CreateSequencedTaskRunnerWithTraits( |
| 173 {base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN, | |
| 174 base::WithBaseSyncPrimitives(), base::TaskPriority::BACKGROUND})) {} | |
| 170 | 175 |
| 171 RLZTracker::~RLZTracker() { | 176 RLZTracker::~RLZTracker() { |
| 172 } | 177 } |
| 173 | 178 |
| 174 // static | 179 // static |
| 175 void RLZTracker::SetRlzDelegate(std::unique_ptr<RLZTrackerDelegate> delegate) { | 180 void RLZTracker::SetRlzDelegate(std::unique_ptr<RLZTrackerDelegate> delegate) { |
| 176 RLZTracker* tracker = GetInstance(); | 181 RLZTracker* tracker = GetInstance(); |
| 177 if (!tracker->delegate_) { | 182 if (!tracker->delegate_) { |
| 178 // RLZTracker::SetRlzDelegate is called at Profile creation time which can | 183 // RLZTracker::SetRlzDelegate is called at Profile creation time which can |
| 179 // happens multiple time on ChromeOS, so do nothing if the delegate already | 184 // happens multiple time on ChromeOS, so do nothing if the delegate already |
| 180 // exists. | 185 // exists. |
| 181 tracker->SetDelegate(std::move(delegate)); | 186 tracker->SetDelegate(std::move(delegate)); |
| 182 } | 187 } |
| 183 } | 188 } |
| 184 | 189 |
| 185 void RLZTracker::SetDelegate(std::unique_ptr<RLZTrackerDelegate> delegate) { | 190 void RLZTracker::SetDelegate(std::unique_ptr<RLZTrackerDelegate> delegate) { |
| 186 DCHECK(delegate); | 191 DCHECK(delegate); |
| 187 DCHECK(!delegate_); | 192 DCHECK(!delegate_); |
| 188 delegate_ = std::move(delegate); | 193 delegate_ = std::move(delegate); |
| 189 worker_pool_token_ = delegate_->GetBlockingPool()->GetSequenceToken(); | |
| 190 } | 194 } |
| 191 | 195 |
| 192 // static | 196 // static |
| 193 bool RLZTracker::InitRlzDelayed(bool first_run, | 197 bool RLZTracker::InitRlzDelayed(bool first_run, |
| 194 bool send_ping_immediately, | 198 bool send_ping_immediately, |
| 195 base::TimeDelta delay, | 199 base::TimeDelta delay, |
| 196 bool is_google_default_search, | 200 bool is_google_default_search, |
| 197 bool is_google_homepage, | 201 bool is_google_homepage, |
| 198 bool is_google_in_startpages) { | 202 bool is_google_in_startpages) { |
| 199 return GetInstance()->Init(first_run, send_ping_immediately, delay, | 203 return GetInstance()->Init(first_run, send_ping_immediately, delay, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 void RLZTracker::Cleanup() { | 261 void RLZTracker::Cleanup() { |
| 258 rlz_cache_.clear(); | 262 rlz_cache_.clear(); |
| 259 if (delegate_) | 263 if (delegate_) |
| 260 delegate_->Cleanup(); | 264 delegate_->Cleanup(); |
| 261 } | 265 } |
| 262 | 266 |
| 263 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) { | 267 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) { |
| 264 DCHECK(delegate_) << "RLZTracker used before initialization"; | 268 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 265 // The RLZTracker is a singleton object that outlives any runnable tasks | 269 // The RLZTracker is a singleton object that outlives any runnable tasks |
| 266 // that will be queued up. | 270 // that will be queued up. |
| 267 delegate_->GetBlockingPool()->PostDelayedSequencedWorkerTask( | 271 task_runner_->PostDelayedTask( |
| 268 worker_pool_token_, FROM_HERE, | 272 FROM_HERE, base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), |
| 269 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), delay); | 273 delay); |
| 270 } | 274 } |
| 271 | 275 |
| 272 void RLZTracker::DelayedInit() { | 276 void RLZTracker::DelayedInit() { |
| 273 DCHECK(delegate_) << "RLZTracker used before initialization"; | 277 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 274 bool schedule_ping = false; | 278 bool schedule_ping = false; |
| 275 | 279 |
| 276 // For organic brandcodes do not use rlz at all. Empty brandcode usually | 280 // For organic brandcodes do not use rlz at all. Empty brandcode usually |
| 277 // means a chromium install. This is ok. | 281 // means a chromium install. This is ok. |
| 278 if (!delegate_->IsBrandOrganic(brand_)) { | 282 if (!delegate_->IsBrandOrganic(brand_)) { |
| 279 RecordProductEvents(first_run_, is_google_default_search_, | 283 RecordProductEvents(first_run_, is_google_default_search_, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 295 } | 299 } |
| 296 | 300 |
| 297 already_ran_ = true; | 301 already_ran_ = true; |
| 298 | 302 |
| 299 if (schedule_ping) | 303 if (schedule_ping) |
| 300 ScheduleFinancialPing(); | 304 ScheduleFinancialPing(); |
| 301 } | 305 } |
| 302 | 306 |
| 303 void RLZTracker::ScheduleFinancialPing() { | 307 void RLZTracker::ScheduleFinancialPing() { |
| 304 DCHECK(delegate_) << "RLZTracker used before initialization"; | 308 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 305 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( | 309 task_runner_->PostTask( |
| 306 worker_pool_token_, FROM_HERE, | 310 FROM_HERE, base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this))); |
| 307 base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this)), | |
| 308 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 309 } | 311 } |
| 310 | 312 |
| 311 void RLZTracker::PingNowImpl() { | 313 void RLZTracker::PingNowImpl() { |
| 312 DCHECK(delegate_) << "RLZTracker used before initialization"; | 314 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 313 TRACE_EVENT0("RLZ", "RLZTracker::PingNowImpl"); | 315 TRACE_EVENT0("RLZ", "RLZTracker::PingNowImpl"); |
| 314 base::string16 lang; | 316 base::string16 lang; |
| 315 delegate_->GetLanguage(&lang); | 317 delegate_->GetLanguage(&lang); |
| 316 if (lang.empty()) | 318 if (lang.empty()) |
| 317 lang = base::ASCIIToUTF16("en"); | 319 lang = base::ASCIIToUTF16("en"); |
| 318 base::string16 referral; | 320 base::string16 referral; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 return ret; | 381 return ret; |
| 380 } | 382 } |
| 381 | 383 |
| 382 bool RLZTracker::ScheduleRecordProductEvent(rlz_lib::Product product, | 384 bool RLZTracker::ScheduleRecordProductEvent(rlz_lib::Product product, |
| 383 rlz_lib::AccessPoint point, | 385 rlz_lib::AccessPoint point, |
| 384 rlz_lib::Event event_id) { | 386 rlz_lib::Event event_id) { |
| 385 DCHECK(delegate_) << "RLZTracker used before initialization"; | 387 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 386 if (!delegate_->IsOnUIThread()) | 388 if (!delegate_->IsOnUIThread()) |
| 387 return false; | 389 return false; |
| 388 | 390 |
| 389 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( | 391 task_runner_->PostTask( |
| 390 worker_pool_token_, FROM_HERE, | 392 FROM_HERE, base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent), |
| 391 base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent), product, | 393 product, point, event_id)); |
| 392 point, event_id), | |
| 393 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 394 | |
| 395 return true; | 394 return true; |
| 396 } | 395 } |
| 397 | 396 |
| 398 void RLZTracker::RecordFirstSearch(rlz_lib::AccessPoint point) { | 397 void RLZTracker::RecordFirstSearch(rlz_lib::AccessPoint point) { |
| 399 DCHECK(delegate_) << "RLZTracker used before initialization"; | 398 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 400 // Make sure we don't access disk outside of the I/O thread. | 399 // Make sure we don't access disk outside of the I/O thread. |
| 401 // In such case we repost the task on the right thread and return error. | 400 // In such case we repost the task on the right thread and return error. |
| 402 if (ScheduleRecordFirstSearch(point)) | 401 if (ScheduleRecordFirstSearch(point)) |
| 403 return; | 402 return; |
| 404 | 403 |
| 405 bool* record_used = GetAccessPointRecord(point); | 404 bool* record_used = GetAccessPointRecord(point); |
| 406 | 405 |
| 407 // Try to record event now, else set the flag to try later when we | 406 // Try to record event now, else set the flag to try later when we |
| 408 // attempt the ping. | 407 // attempt the ping. |
| 409 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) | 408 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) { |
| 410 *record_used = true; | 409 *record_used = true; |
| 411 else if (send_ping_immediately_ && point == ChromeOmnibox()) | 410 } else if (send_ping_immediately_ && point == ChromeOmnibox()) { |
| 412 ScheduleDelayedInit(base::TimeDelta()); | 411 ScheduleDelayedInit(base::TimeDelta()); |
| 412 } | |
| 413 } | 413 } |
| 414 | 414 |
| 415 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { | 415 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { |
| 416 DCHECK(delegate_) << "RLZTracker used before initialization"; | 416 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 417 if (!delegate_->IsOnUIThread()) | 417 if (!delegate_->IsOnUIThread()) |
| 418 return false; | 418 return false; |
| 419 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( | 419 task_runner_->PostTask(FROM_HERE, base::Bind(&RLZTracker::RecordFirstSearch, |
| 420 worker_pool_token_, FROM_HERE, | 420 base::Unretained(this), point)); |
| 421 base::Bind(&RLZTracker::RecordFirstSearch, base::Unretained(this), point), | |
| 422 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 423 return true; | 421 return true; |
| 424 } | 422 } |
| 425 | 423 |
| 426 bool* RLZTracker::GetAccessPointRecord(rlz_lib::AccessPoint point) { | 424 bool* RLZTracker::GetAccessPointRecord(rlz_lib::AccessPoint point) { |
| 427 if (point == ChromeOmnibox()) | 425 if (point == ChromeOmnibox()) |
| 428 return &omnibox_used_; | 426 return &omnibox_used_; |
| 429 #if !defined(OS_IOS) | 427 #if !defined(OS_IOS) |
| 430 if (point == ChromeHomePage()) | 428 if (point == ChromeHomePage()) |
| 431 return &homepage_used_; | 429 return &homepage_used_; |
| 432 if (point == ChromeAppList()) | 430 if (point == ChromeAppList()) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 rlz_cache_[point] = rlz_local; | 495 rlz_cache_[point] = rlz_local; |
| 498 return true; | 496 return true; |
| 499 } | 497 } |
| 500 | 498 |
| 501 bool RLZTracker::ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) { | 499 bool RLZTracker::ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point) { |
| 502 DCHECK(delegate_) << "RLZTracker used before initialization"; | 500 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 503 if (!delegate_->IsOnUIThread()) | 501 if (!delegate_->IsOnUIThread()) |
| 504 return false; | 502 return false; |
| 505 | 503 |
| 506 base::string16* not_used = nullptr; | 504 base::string16* not_used = nullptr; |
| 507 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( | 505 task_runner_->PostTask( |
| 508 worker_pool_token_, FROM_HERE, | 506 FROM_HERE, base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), |
| 509 base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), point, | 507 point, not_used)); |
| 510 not_used), | |
| 511 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 512 return true; | 508 return true; |
| 513 } | 509 } |
| 514 | 510 |
| 515 #if defined(OS_CHROMEOS) | 511 #if defined(OS_CHROMEOS) |
| 516 // static | 512 // static |
| 517 void RLZTracker::ClearRlzState() { | 513 void RLZTracker::ClearRlzState() { |
| 518 RLZTracker* tracker = GetInstance(); | 514 RLZTracker* tracker = GetInstance(); |
| 519 if (tracker->delegate_) | 515 if (tracker->delegate_) |
| 520 tracker->ClearRlzStateImpl(); | 516 tracker->ClearRlzStateImpl(); |
| 521 } | 517 } |
| 522 | 518 |
| 523 void RLZTracker::ClearRlzStateImpl() { | 519 void RLZTracker::ClearRlzStateImpl() { |
|
gab
2017/07/27 18:29:37
DCHECK(impl_task_runner_->RunsTasksOnCurrentSequen
Roger Tawa OOO till Jul 10th
2017/07/28 18:43:06
Added where it makes sense.
| |
| 524 DCHECK(delegate_) << "RLZTracker used before initialization"; | 520 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 525 if (ScheduleClearRlzState()) | 521 if (ScheduleClearRlzState()) |
| 526 return; | 522 return; |
| 527 rlz_lib::ClearAllProductEvents(rlz_lib::CHROME); | 523 rlz_lib::ClearAllProductEvents(rlz_lib::CHROME); |
| 528 } | 524 } |
| 529 | 525 |
| 530 bool RLZTracker::ScheduleClearRlzState() { | 526 bool RLZTracker::ScheduleClearRlzState() { |
| 531 DCHECK(delegate_) << "RLZTracker used before initialization"; | 527 DCHECK(delegate_) << "RLZTracker used before initialization"; |
| 532 if (!delegate_->IsOnUIThread()) | 528 if (!delegate_->IsOnUIThread()) |
| 533 return false; | 529 return false; |
| 534 | 530 |
| 535 delegate_->GetBlockingPool()->PostSequencedWorkerTaskWithShutdownBehavior( | 531 task_runner_->PostTask(FROM_HERE, base::Bind(&RLZTracker::ClearRlzStateImpl, |
| 536 worker_pool_token_, FROM_HERE, | 532 base::Unretained(this))); |
| 537 base::Bind(&RLZTracker::ClearRlzStateImpl, base::Unretained(this)), | |
| 538 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 539 return true; | 533 return true; |
| 540 } | 534 } |
| 541 #endif | 535 #endif |
| 542 | 536 |
| 543 // static | 537 // static |
| 544 void RLZTracker::CleanupRlz() { | 538 void RLZTracker::CleanupRlz() { |
| 545 GetInstance()->Cleanup(); | 539 GetInstance()->Cleanup(); |
| 546 rlz_lib::SetURLRequestContext(nullptr); | 540 rlz_lib::SetURLRequestContext(nullptr); |
| 547 } | 541 } |
| 548 | 542 |
| 549 // static | 543 // static |
| 550 void RLZTracker::EnableZeroDelayForTesting() { | 544 void RLZTracker::EnableZeroDelayForTesting() { |
| 551 GetInstance()->min_init_delay_ = base::TimeDelta(); | 545 GetInstance()->min_init_delay_ = base::TimeDelta(); |
| 552 } | 546 } |
| 553 | 547 |
| 554 #if !defined(OS_IOS) | 548 #if !defined(OS_IOS) |
| 555 // static | 549 // static |
| 556 void RLZTracker::RecordAppListSearch() { | 550 void RLZTracker::RecordAppListSearch() { |
| 557 // This method is called during unit tests while the RLZTracker has not been | 551 // This method is called during unit tests while the RLZTracker has not been |
| 558 // initialized, so check for the presence of a delegate and exit if there is | 552 // initialized, so check for the presence of a delegate and exit if there is |
| 559 // none registered. | 553 // none registered. |
| 560 RLZTracker* tracker = GetInstance(); | 554 RLZTracker* tracker = GetInstance(); |
| 561 if (tracker->delegate_) | 555 if (tracker->delegate_) |
| 562 tracker->RecordFirstSearch(RLZTracker::ChromeAppList()); | 556 tracker->RecordFirstSearch(RLZTracker::ChromeAppList()); |
| 563 } | 557 } |
| 564 #endif | 558 #endif |
| 565 | 559 |
| 566 } // namespace rlz | 560 } // namespace rlz |
| OLD | NEW |