| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 //------------------------------------------------------------------------------ | 5 //------------------------------------------------------------------------------ |
| 6 // Description of the life cycle of a instance of MetricsService. | 6 // Description of the life cycle of a instance of MetricsService. |
| 7 // | 7 // |
| 8 // OVERVIEW | 8 // OVERVIEW |
| 9 // | 9 // |
| 10 // A MetricsService instance is typically created at application startup. It is | 10 // A MetricsService instance is typically created at application startup. It is |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 if (install_date == 0) | 311 if (install_date == 0) |
| 312 local_state_->SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT()); | 312 local_state_->SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT()); |
| 313 } | 313 } |
| 314 | 314 |
| 315 MetricsService::~MetricsService() { | 315 MetricsService::~MetricsService() { |
| 316 DisableRecording(); | 316 DisableRecording(); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void MetricsService::InitializeMetricsRecordingState() { | 319 void MetricsService::InitializeMetricsRecordingState() { |
| 320 InitializeMetricsState(); | 320 InitializeMetricsState(); |
| 321 | 321 LOG(WARNING) << "metrix InitializeMetricsRecordingState"; |
| 322 base::Closure upload_callback = | 322 base::Closure upload_callback = |
| 323 base::Bind(&MetricsService::StartScheduledUpload, | 323 base::Bind(&MetricsService::StartScheduledUpload, |
| 324 self_ptr_factory_.GetWeakPtr()); | 324 self_ptr_factory_.GetWeakPtr()); |
| 325 scheduler_.reset( | 325 scheduler_.reset( |
| 326 new MetricsReportingScheduler( | 326 new MetricsReportingScheduler( |
| 327 upload_callback, | 327 upload_callback, |
| 328 // MetricsServiceClient outlives MetricsService, and | 328 // MetricsServiceClient outlives MetricsService, and |
| 329 // MetricsReportingScheduler is tied to the lifetime of |this|. | 329 // MetricsReportingScheduler is tied to the lifetime of |this|. |
| 330 base::Bind(&MetricsServiceClient::GetStandardUploadInterval, | 330 base::Bind(&MetricsServiceClient::GetStandardUploadInterval, |
| 331 base::Unretained(client_)))); | 331 base::Unretained(client_)))); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void MetricsService::Start() { | 334 void MetricsService::Start() { |
| 335 LOG(WARNING) << "metrix starting metrics service"; |
| 335 HandleIdleSinceLastTransmission(false); | 336 HandleIdleSinceLastTransmission(false); |
| 336 EnableRecording(); | 337 EnableRecording(); |
| 337 EnableReporting(); | 338 EnableReporting(); |
| 338 } | 339 } |
| 339 | 340 |
| 340 void MetricsService::StartRecordingForTests() { | 341 void MetricsService::StartRecordingForTests() { |
| 341 test_mode_active_ = true; | 342 test_mode_active_ = true; |
| 342 EnableRecording(); | 343 EnableRecording(); |
| 343 DisableReporting(); | 344 DisableReporting(); |
| 344 } | 345 } |
| 345 | 346 |
| 346 void MetricsService::Stop() { | 347 void MetricsService::Stop() { |
| 348 LOG(WARNING) << "metrix stopping metrics service"; |
| 347 HandleIdleSinceLastTransmission(false); | 349 HandleIdleSinceLastTransmission(false); |
| 348 DisableReporting(); | 350 DisableReporting(); |
| 349 DisableRecording(); | 351 DisableRecording(); |
| 350 } | 352 } |
| 351 | 353 |
| 352 void MetricsService::EnableReporting() { | 354 void MetricsService::EnableReporting() { |
| 355 LOG(WARNING) << "metrix enablereporting"; |
| 353 if (reporting_active_) | 356 if (reporting_active_) |
| 354 return; | 357 return; |
| 355 reporting_active_ = true; | 358 reporting_active_ = true; |
| 356 StartSchedulerIfNecessary(); | 359 StartSchedulerIfNecessary(); |
| 357 } | 360 } |
| 358 | 361 |
| 359 void MetricsService::DisableReporting() { | 362 void MetricsService::DisableReporting() { |
| 360 reporting_active_ = false; | 363 reporting_active_ = false; |
| 361 } | 364 } |
| 362 | 365 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 378 | 381 |
| 379 scoped_ptr<const base::FieldTrial::EntropyProvider> | 382 scoped_ptr<const base::FieldTrial::EntropyProvider> |
| 380 MetricsService::CreateEntropyProvider() { | 383 MetricsService::CreateEntropyProvider() { |
| 381 // TODO(asvitkine): Refactor the code so that MetricsService does not expose | 384 // TODO(asvitkine): Refactor the code so that MetricsService does not expose |
| 382 // this method. | 385 // this method. |
| 383 return state_manager_->CreateEntropyProvider(); | 386 return state_manager_->CreateEntropyProvider(); |
| 384 } | 387 } |
| 385 | 388 |
| 386 void MetricsService::EnableRecording() { | 389 void MetricsService::EnableRecording() { |
| 387 DCHECK(IsSingleThreaded()); | 390 DCHECK(IsSingleThreaded()); |
| 388 | 391 LOG(WARNING) << "metrix EnableRecording " << recording_state_; |
| 389 if (recording_state_ == ACTIVE) | 392 if (recording_state_ == ACTIVE) |
| 390 return; | 393 return; |
| 391 recording_state_ = ACTIVE; | 394 recording_state_ = ACTIVE; |
| 392 | 395 |
| 393 state_manager_->ForceClientIdCreation(); | 396 state_manager_->ForceClientIdCreation(); |
| 394 client_->SetMetricsClientId(state_manager_->client_id()); | 397 client_->SetMetricsClientId(state_manager_->client_id()); |
| 395 if (!log_manager_.current_log()) | 398 if (!log_manager_.current_log()) |
| 396 OpenNewLog(); | 399 OpenNewLog(); |
| 397 | 400 |
| 398 for (size_t i = 0; i < metrics_providers_.size(); ++i) | 401 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 | 636 |
| 634 // Bookkeeping for the uninstall metrics. | 637 // Bookkeeping for the uninstall metrics. |
| 635 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 638 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| 636 | 639 |
| 637 // Kick off the process of saving the state (so the uptime numbers keep | 640 // Kick off the process of saving the state (so the uptime numbers keep |
| 638 // getting updated) every n minutes. | 641 // getting updated) every n minutes. |
| 639 ScheduleNextStateSave(); | 642 ScheduleNextStateSave(); |
| 640 } | 643 } |
| 641 | 644 |
| 642 void MetricsService::OnUserAction(const std::string& action) { | 645 void MetricsService::OnUserAction(const std::string& action) { |
| 646 LOG(INFO) << "metrix metrics::MetricsService::OnUserAction action=" << action; |
| 647 |
| 643 if (!ShouldLogEvents()) | 648 if (!ShouldLogEvents()) |
| 644 return; | 649 return; |
| 645 | 650 |
| 646 log_manager_.current_log()->RecordUserAction(action); | 651 log_manager_.current_log()->RecordUserAction(action); |
| 647 HandleIdleSinceLastTransmission(false); | 652 HandleIdleSinceLastTransmission(false); |
| 648 } | 653 } |
| 649 | 654 |
| 650 void MetricsService::FinishedInitTask() { | 655 void MetricsService::FinishedInitTask() { |
| 656 LOG(WARNING) << "metrix FinishedInitTask"; |
| 651 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 657 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 652 state_ = INIT_TASK_DONE; | 658 state_ = INIT_TASK_DONE; |
| 653 | 659 |
| 654 // Create the initial log. | 660 // Create the initial log. |
| 655 if (!initial_metrics_log_.get()) { | 661 if (!initial_metrics_log_.get()) { |
| 656 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); | 662 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
| 657 NotifyOnDidCreateMetricsLog(); | 663 NotifyOnDidCreateMetricsLog(); |
| 658 } | 664 } |
| 659 | 665 |
| 660 scheduler_->InitTaskComplete(); | 666 scheduler_->InitTaskComplete(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 state_ = INIT_TASK_SCHEDULED; | 727 state_ = INIT_TASK_SCHEDULED; |
| 722 | 728 |
| 723 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 729 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 724 FROM_HERE, base::Bind(&MetricsService::StartInitTask, | 730 FROM_HERE, base::Bind(&MetricsService::StartInitTask, |
| 725 self_ptr_factory_.GetWeakPtr()), | 731 self_ptr_factory_.GetWeakPtr()), |
| 726 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 732 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
| 727 } | 733 } |
| 728 } | 734 } |
| 729 | 735 |
| 730 void MetricsService::StartInitTask() { | 736 void MetricsService::StartInitTask() { |
| 737 LOG(WARNING) << "metrix StartInitTask"; |
| 731 client_->InitializeSystemProfileMetrics( | 738 client_->InitializeSystemProfileMetrics( |
| 732 base::Bind(&MetricsService::FinishedInitTask, | 739 base::Bind(&MetricsService::FinishedInitTask, |
| 733 self_ptr_factory_.GetWeakPtr())); | 740 self_ptr_factory_.GetWeakPtr())); |
| 734 } | 741 } |
| 735 | 742 |
| 736 void MetricsService::CloseCurrentLog() { | 743 void MetricsService::CloseCurrentLog() { |
| 737 if (!log_manager_.current_log()) | 744 if (!log_manager_.current_log()) |
| 738 return; | 745 return; |
| 739 | 746 |
| 740 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 747 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| (...skipping 29 matching lines...) Expand all Loading... |
| 770 return; // We didn't and still don't have time to get plugin list etc. | 777 return; // We didn't and still don't have time to get plugin list etc. |
| 771 | 778 |
| 772 CloseCurrentLog(); | 779 CloseCurrentLog(); |
| 773 log_manager_.PersistUnsentLogs(); | 780 log_manager_.PersistUnsentLogs(); |
| 774 } | 781 } |
| 775 | 782 |
| 776 //------------------------------------------------------------------------------ | 783 //------------------------------------------------------------------------------ |
| 777 // Transmission of logs methods | 784 // Transmission of logs methods |
| 778 | 785 |
| 779 void MetricsService::StartSchedulerIfNecessary() { | 786 void MetricsService::StartSchedulerIfNecessary() { |
| 787 LOG(WARNING) << "metrix StartSchedulerIfNecessary"; |
| 780 // Never schedule cutting or uploading of logs in test mode. | 788 // Never schedule cutting or uploading of logs in test mode. |
| 781 if (test_mode_active_) | 789 if (test_mode_active_) |
| 782 return; | 790 return; |
| 783 | 791 |
| 784 // Even if reporting is disabled, the scheduler is needed to trigger the | 792 // Even if reporting is disabled, the scheduler is needed to trigger the |
| 785 // creation of the initial log, which must be done in order for any logs to be | 793 // creation of the initial log, which must be done in order for any logs to be |
| 786 // persisted on shutdown or backgrounding. | 794 // persisted on shutdown or backgrounding. |
| 787 if (recording_active() && | 795 if (recording_active() && |
| 788 (reporting_active() || state_ < SENDING_LOGS)) { | 796 (reporting_active() || state_ < SENDING_LOGS)) { |
| 789 scheduler_->Start(); | 797 scheduler_->Start(); |
| 790 } | 798 } |
| 791 } | 799 } |
| 792 | 800 |
| 793 void MetricsService::StartScheduledUpload() { | 801 void MetricsService::StartScheduledUpload() { |
| 794 DCHECK(state_ >= INIT_TASK_DONE); | 802 DCHECK(state_ >= INIT_TASK_DONE); |
| 803 LOG(WARNING) << " metrix StartScheduledUpload " << idle_since_last_transmissi
on_ |
| 804 << " " << recording_active() << " " << state_; |
| 795 // If we're getting no notifications, then the log won't have much in it, and | 805 // If we're getting no notifications, then the log won't have much in it, and |
| 796 // it's possible the computer is about to go to sleep, so don't upload and | 806 // it's possible the computer is about to go to sleep, so don't upload and |
| 797 // stop the scheduler. | 807 // stop the scheduler. |
| 798 // If recording has been turned off, the scheduler doesn't need to run. | 808 // If recording has been turned off, the scheduler doesn't need to run. |
| 799 // If reporting is off, proceed if the initial log hasn't been created, since | 809 // If reporting is off, proceed if the initial log hasn't been created, since |
| 800 // that has to happen in order for logs to be cut and stored when persisting. | 810 // that has to happen in order for logs to be cut and stored when persisting. |
| 801 // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or | 811 // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or |
| 802 // recording are turned off instead of letting it fire and then aborting. | 812 // recording are turned off instead of letting it fire and then aborting. |
| 803 if (idle_since_last_transmission_ || | 813 if (idle_since_last_transmission_ || |
| 804 !recording_active() || | 814 !recording_active() || |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 } else { | 850 } else { |
| 841 DCHECK_EQ(SENDING_LOGS, state_); | 851 DCHECK_EQ(SENDING_LOGS, state_); |
| 842 CloseCurrentLog(); | 852 CloseCurrentLog(); |
| 843 OpenNewLog(); | 853 OpenNewLog(); |
| 844 } | 854 } |
| 845 SendNextLog(); | 855 SendNextLog(); |
| 846 } | 856 } |
| 847 | 857 |
| 848 void MetricsService::SendNextLog() { | 858 void MetricsService::SendNextLog() { |
| 849 DCHECK_EQ(SENDING_LOGS, state_); | 859 DCHECK_EQ(SENDING_LOGS, state_); |
| 860 LOG(WARNING) << "metrix SendNextLog"; |
| 850 if (!reporting_active()) { | 861 if (!reporting_active()) { |
| 851 scheduler_->Stop(); | 862 scheduler_->Stop(); |
| 852 scheduler_->UploadCancelled(); | 863 scheduler_->UploadCancelled(); |
| 853 return; | 864 return; |
| 854 } | 865 } |
| 855 if (!log_manager_.has_unsent_logs()) { | 866 if (!log_manager_.has_unsent_logs()) { |
| 856 // Should only get here if serializing the log failed somehow. | 867 // Should only get here if serializing the log failed somehow. |
| 857 // Just tell the scheduler it was uploaded and wait for the next log | 868 // Just tell the scheduler it was uploaded and wait for the next log |
| 858 // interval. | 869 // interval. |
| 859 scheduler_->UploadFinished(true, log_manager_.has_unsent_logs()); | 870 scheduler_->UploadFinished(true, log_manager_.has_unsent_logs()); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 933 log_manager_.ResumePausedLog(); | 944 log_manager_.ResumePausedLog(); |
| 934 | 945 |
| 935 // Store unsent logs, including the initial log that was just saved, so | 946 // Store unsent logs, including the initial log that was just saved, so |
| 936 // that they're not lost in case of a crash before upload time. | 947 // that they're not lost in case of a crash before upload time. |
| 937 log_manager_.PersistUnsentLogs(); | 948 log_manager_.PersistUnsentLogs(); |
| 938 | 949 |
| 939 state_ = SENDING_LOGS; | 950 state_ = SENDING_LOGS; |
| 940 } | 951 } |
| 941 | 952 |
| 942 void MetricsService::SendStagedLog() { | 953 void MetricsService::SendStagedLog() { |
| 954 LOG(WARNING) << "metrix SendStagedLog"; |
| 943 DCHECK(log_manager_.has_staged_log()); | 955 DCHECK(log_manager_.has_staged_log()); |
| 944 if (!log_manager_.has_staged_log()) | 956 if (!log_manager_.has_staged_log()) |
| 945 return; | 957 return; |
| 946 | 958 |
| 947 DCHECK(!log_upload_in_progress_); | 959 DCHECK(!log_upload_in_progress_); |
| 948 log_upload_in_progress_ = true; | 960 log_upload_in_progress_ = true; |
| 949 | 961 |
| 950 if (!ShouldUploadLog()) { | 962 if (!ShouldUploadLog()) { |
| 951 SkipAndDiscardUpload(); | 963 SkipAndDiscardUpload(); |
| 952 return; | 964 return; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 base::Time::Now().ToTimeT()); | 1155 base::Time::Now().ToTimeT()); |
| 1144 } | 1156 } |
| 1145 | 1157 |
| 1146 void MetricsService::SkipAndDiscardUpload() { | 1158 void MetricsService::SkipAndDiscardUpload() { |
| 1147 log_manager_.DiscardStagedLog(); | 1159 log_manager_.DiscardStagedLog(); |
| 1148 scheduler_->UploadCancelled(); | 1160 scheduler_->UploadCancelled(); |
| 1149 log_upload_in_progress_ = false; | 1161 log_upload_in_progress_ = false; |
| 1150 } | 1162 } |
| 1151 | 1163 |
| 1152 } // namespace metrics | 1164 } // namespace metrics |
| OLD | NEW |