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 //------------------------------------------------------------------------------ | 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 #include "chrome/browser/browser_process.h" | 184 #include "chrome/browser/browser_process.h" |
185 #include "chrome/browser/chrome_notification_types.h" | 185 #include "chrome/browser/chrome_notification_types.h" |
186 #include "chrome/browser/io_thread.h" | 186 #include "chrome/browser/io_thread.h" |
187 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" | 187 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" |
188 #include "chrome/browser/metrics/compression_utils.h" | 188 #include "chrome/browser/metrics/compression_utils.h" |
189 #include "chrome/browser/metrics/gpu_metrics_provider.h" | 189 #include "chrome/browser/metrics/gpu_metrics_provider.h" |
190 #include "chrome/browser/metrics/metrics_log.h" | 190 #include "chrome/browser/metrics/metrics_log.h" |
191 #include "chrome/browser/metrics/metrics_state_manager.h" | 191 #include "chrome/browser/metrics/metrics_state_manager.h" |
192 #include "chrome/browser/metrics/network_metrics_provider.h" | 192 #include "chrome/browser/metrics/network_metrics_provider.h" |
193 #include "chrome/browser/metrics/omnibox_metrics_provider.h" | 193 #include "chrome/browser/metrics/omnibox_metrics_provider.h" |
194 #include "chrome/browser/metrics/tracking_synchronizer.h" | |
195 #include "chrome/common/pref_names.h" | 194 #include "chrome/common/pref_names.h" |
196 #include "chrome/common/variations/variations_util.h" | 195 #include "chrome/common/variations/variations_util.h" |
197 #include "components/metrics/metrics_log_base.h" | 196 #include "components/metrics/metrics_log_base.h" |
198 #include "components/metrics/metrics_log_manager.h" | 197 #include "components/metrics/metrics_log_manager.h" |
199 #include "components/metrics/metrics_pref_names.h" | 198 #include "components/metrics/metrics_pref_names.h" |
200 #include "components/metrics/metrics_reporting_scheduler.h" | 199 #include "components/metrics/metrics_reporting_scheduler.h" |
201 #include "components/metrics/metrics_service_client.h" | 200 #include "components/metrics/metrics_service_client.h" |
202 #include "components/variations/entropy_provider.h" | 201 #include "components/variations/entropy_provider.h" |
203 #include "net/base/load_flags.h" | 202 #include "net/base/load_flags.h" |
204 #include "net/url_request/url_fetcher.h" | 203 #include "net/url_request/url_fetcher.h" |
205 | 204 |
206 #if defined(ENABLE_PLUGINS) | |
207 // TODO(asvitkine): Move this out of MetricsService. | |
208 #include "chrome/browser/metrics/plugin_metrics_provider.h" | |
209 #endif | |
210 | |
211 #if defined(OS_WIN) | |
212 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" | |
213 #endif | |
214 | |
215 #if defined(OS_ANDROID) | |
216 // TODO(asvitkine): Move this out of MetricsService. | |
217 #include "chrome/browser/metrics/android_metrics_provider.h" | |
218 #endif | |
219 | |
220 using base::Time; | 205 using base::Time; |
221 using metrics::MetricsLogManager; | 206 using metrics::MetricsLogManager; |
222 | 207 |
223 namespace { | 208 namespace { |
224 | 209 |
225 // Check to see that we're being called on only one thread. | 210 // Check to see that we're being called on only one thread. |
226 bool IsSingleThreaded() { | 211 bool IsSingleThreaded() { |
227 static base::PlatformThreadId thread_id = 0; | 212 static base::PlatformThreadId thread_id = 0; |
228 if (!thread_id) | 213 if (!thread_id) |
229 thread_id = base::PlatformThread::CurrentId(); | 214 thread_id = base::PlatformThread::CurrentId(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 std::string()); | 309 std::string()); |
325 | 310 |
326 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); | 311 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); |
327 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); | 312 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); |
328 | 313 |
329 registry->RegisterInt64Pref(prefs::kInstallDate, 0); | 314 registry->RegisterInt64Pref(prefs::kInstallDate, 0); |
330 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); | 315 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
331 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); | 316 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
332 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); | 317 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
333 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); | 318 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
334 | |
335 // TODO(asvitkine): Move this out of here. | |
336 ChromeStabilityMetricsProvider::RegisterPrefs(registry); | |
337 | |
338 #if defined(OS_ANDROID) | |
339 // TODO(asvitkine): Move this out of here. | |
340 AndroidMetricsProvider::RegisterPrefs(registry); | |
341 #endif // defined(OS_ANDROID) | |
342 | |
343 #if defined(ENABLE_PLUGINS) | |
344 // TODO(asvitkine): Move this out of here. | |
345 PluginMetricsProvider::RegisterPrefs(registry); | |
346 #endif | |
347 } | 319 } |
348 | 320 |
349 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, | 321 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
350 metrics::MetricsServiceClient* client, | 322 metrics::MetricsServiceClient* client, |
351 PrefService* local_state) | 323 PrefService* local_state) |
352 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), | 324 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
353 histogram_snapshot_manager_(this), | 325 histogram_snapshot_manager_(this), |
354 state_manager_(state_manager), | 326 state_manager_(state_manager), |
355 client_(client), | 327 client_(client), |
356 local_state_(local_state), | 328 local_state_(local_state), |
357 recording_active_(false), | 329 recording_active_(false), |
358 reporting_active_(false), | 330 reporting_active_(false), |
359 test_mode_active_(false), | 331 test_mode_active_(false), |
360 state_(INITIALIZED), | 332 state_(INITIALIZED), |
361 has_initial_stability_log_(false), | 333 has_initial_stability_log_(false), |
362 idle_since_last_transmission_(false), | 334 idle_since_last_transmission_(false), |
363 session_id_(-1), | 335 session_id_(-1), |
364 next_window_id_(0), | 336 next_window_id_(0), |
365 self_ptr_factory_(this), | 337 self_ptr_factory_(this), |
366 state_saver_factory_(this), | 338 state_saver_factory_(this), |
367 waiting_for_asynchronous_reporting_step_(false) { | 339 waiting_for_asynchronous_reporting_step_(false) { |
368 DCHECK(IsSingleThreaded()); | 340 DCHECK(IsSingleThreaded()); |
369 DCHECK(state_manager_); | 341 DCHECK(state_manager_); |
370 DCHECK(client_); | 342 DCHECK(client_); |
371 DCHECK(local_state_); | 343 DCHECK(local_state_); |
372 | |
373 #if defined(OS_ANDROID) | |
374 // TODO(asvitkine): Move this out of MetricsService. | |
375 RegisterMetricsProvider( | |
376 scoped_ptr<metrics::MetricsProvider>(new AndroidMetricsProvider( | |
377 local_state_))); | |
378 #endif // defined(OS_ANDROID) | |
379 | |
380 // TODO(asvitkine): Move these out of MetricsService. | |
381 RegisterMetricsProvider( | |
382 scoped_ptr<metrics::MetricsProvider>(new NetworkMetricsProvider)); | |
383 RegisterMetricsProvider( | |
384 scoped_ptr<metrics::MetricsProvider>(new OmniboxMetricsProvider)); | |
385 RegisterMetricsProvider( | |
386 scoped_ptr<metrics::MetricsProvider>(new ChromeStabilityMetricsProvider)); | |
387 RegisterMetricsProvider( | |
388 scoped_ptr<metrics::MetricsProvider>(new GPUMetricsProvider())); | |
389 | |
390 #if defined(OS_WIN) | |
391 google_update_metrics_provider_ = new GoogleUpdateMetricsProviderWin; | |
392 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
393 google_update_metrics_provider_)); | |
394 #endif | |
395 | |
396 #if defined(ENABLE_PLUGINS) | |
397 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | |
398 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
399 plugin_metrics_provider_)); | |
400 #endif | |
401 | |
402 } | 344 } |
403 | 345 |
404 MetricsService::~MetricsService() { | 346 MetricsService::~MetricsService() { |
405 DisableRecording(); | 347 DisableRecording(); |
406 } | 348 } |
407 | 349 |
408 void MetricsService::InitializeMetricsRecordingState() { | 350 void MetricsService::InitializeMetricsRecordingState() { |
409 InitializeMetricsState(); | 351 InitializeMetricsState(); |
410 | 352 |
411 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 353 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 611 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
670 | 612 |
671 // Bookkeeping for the uninstall metrics. | 613 // Bookkeeping for the uninstall metrics. |
672 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 614 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
673 | 615 |
674 // Kick off the process of saving the state (so the uptime numbers keep | 616 // Kick off the process of saving the state (so the uptime numbers keep |
675 // getting updated) every n minutes. | 617 // getting updated) every n minutes. |
676 ScheduleNextStateSave(); | 618 ScheduleNextStateSave(); |
677 } | 619 } |
678 | 620 |
679 void MetricsService::OnInitTaskGotHardwareClass() { | |
680 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
681 | |
682 const base::Closure got_plugin_info_callback = | |
683 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | |
684 self_ptr_factory_.GetWeakPtr()); | |
685 | |
686 #if defined(ENABLE_PLUGINS) | |
687 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | |
688 #else | |
689 got_plugin_info_callback.Run(); | |
690 #endif | |
691 } | |
692 | |
693 void MetricsService::OnInitTaskGotPluginInfo() { | |
694 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
695 | |
696 const base::Closure got_metrics_callback = | |
697 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData, | |
698 self_ptr_factory_.GetWeakPtr()); | |
699 | |
700 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
701 google_update_metrics_provider_->GetGoogleUpdateData(got_metrics_callback); | |
702 #else | |
703 got_metrics_callback.Run(); | |
704 #endif | |
705 } | |
706 | |
707 void MetricsService::OnInitTaskGotGoogleUpdateData() { | |
708 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
709 | |
710 // Start the next part of the init task: fetching performance data. This will | |
711 // call into |FinishedReceivingProfilerData()| when the task completes. | |
712 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously( | |
713 self_ptr_factory_.GetWeakPtr()); | |
714 } | |
715 | |
716 void MetricsService::OnUserAction(const std::string& action) { | 621 void MetricsService::OnUserAction(const std::string& action) { |
717 if (!ShouldLogEvents()) | 622 if (!ShouldLogEvents()) |
718 return; | 623 return; |
719 | 624 |
720 log_manager_.current_log()->RecordUserAction(action); | 625 log_manager_.current_log()->RecordUserAction(action); |
721 HandleIdleSinceLastTransmission(false); | 626 HandleIdleSinceLastTransmission(false); |
722 } | 627 } |
723 | 628 |
724 void MetricsService::ReceivedProfilerData( | 629 void MetricsService::ReceivedProfilerData( |
725 const tracked_objects::ProcessDataSnapshot& process_data, | 630 const tracked_objects::ProcessDataSnapshot& process_data, |
726 int process_type) { | 631 int process_type) { |
727 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 632 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
728 | 633 |
729 // Upon the first callback, create the initial log so that we can immediately | 634 // Upon the first callback, create the initial log so that we can immediately |
730 // save the profiler data. | 635 // save the profiler data. |
731 if (!initial_metrics_log_.get()) { | 636 if (!initial_metrics_log_.get()) { |
732 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); | 637 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
733 NotifyOnDidCreateMetricsLog(); | 638 NotifyOnDidCreateMetricsLog(); |
734 } | 639 } |
735 | 640 |
736 initial_metrics_log_->RecordProfilerData(process_data, process_type); | 641 initial_metrics_log_->RecordProfilerData(process_data, process_type); |
737 } | 642 } |
738 | 643 |
739 void MetricsService::FinishedReceivingProfilerData() { | 644 void MetricsService::FinishedGatheringInitialMetrics() { |
740 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 645 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
741 state_ = INIT_TASK_DONE; | 646 state_ = INIT_TASK_DONE; |
742 scheduler_->InitTaskComplete(); | 647 scheduler_->InitTaskComplete(); |
743 } | 648 } |
744 | 649 |
745 void MetricsService::GetUptimes(PrefService* pref, | 650 void MetricsService::GetUptimes(PrefService* pref, |
746 base::TimeDelta* incremental_uptime, | 651 base::TimeDelta* incremental_uptime, |
747 base::TimeDelta* uptime) { | 652 base::TimeDelta* uptime) { |
748 base::TimeTicks now = base::TimeTicks::Now(); | 653 base::TimeTicks now = base::TimeTicks::Now(); |
749 // If this is the first call, init |first_updated_time_| and | 654 // If this is the first call, init |first_updated_time_| and |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 void MetricsService::OpenNewLog() { | 713 void MetricsService::OpenNewLog() { |
809 DCHECK(!log_manager_.current_log()); | 714 DCHECK(!log_manager_.current_log()); |
810 | 715 |
811 log_manager_.BeginLoggingWithLog( | 716 log_manager_.BeginLoggingWithLog( |
812 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); | 717 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); |
813 NotifyOnDidCreateMetricsLog(); | 718 NotifyOnDidCreateMetricsLog(); |
814 if (state_ == INITIALIZED) { | 719 if (state_ == INITIALIZED) { |
815 // We only need to schedule that run once. | 720 // We only need to schedule that run once. |
816 state_ = INIT_TASK_SCHEDULED; | 721 state_ = INIT_TASK_SCHEDULED; |
817 | 722 |
818 // TODO(blundell): Move all initial metrics gathering to | |
819 // ChromeMetricsServiceClient. | |
820 client_->StartGatheringMetrics( | 723 client_->StartGatheringMetrics( |
821 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | 724 base::Bind(&MetricsService::FinishedGatheringInitialMetrics, |
822 self_ptr_factory_.GetWeakPtr())); | 725 self_ptr_factory_.GetWeakPtr())); |
823 } | 726 } |
824 } | 727 } |
825 | 728 |
826 void MetricsService::CloseCurrentLog() { | 729 void MetricsService::CloseCurrentLog() { |
827 if (!log_manager_.current_log()) | 730 if (!log_manager_.current_log()) |
828 return; | 731 return; |
829 | 732 |
830 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 733 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
831 // can stop recording logs that are too big much sooner. | 734 // can stop recording logs that are too big much sooner. |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1327 | 1230 |
1328 // Redundant setting to assure that we always reset this value at shutdown | 1231 // Redundant setting to assure that we always reset this value at shutdown |
1329 // (and that we don't use some alternate path, and not call LogCleanShutdown). | 1232 // (and that we don't use some alternate path, and not call LogCleanShutdown). |
1330 clean_shutdown_status_ = CLEANLY_SHUTDOWN; | 1233 clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
1331 | 1234 |
1332 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); | 1235 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
1333 local_state_->SetInteger(prefs::kStabilityExecutionPhase, | 1236 local_state_->SetInteger(prefs::kStabilityExecutionPhase, |
1334 MetricsService::SHUTDOWN_COMPLETE); | 1237 MetricsService::SHUTDOWN_COMPLETE); |
1335 } | 1238 } |
1336 | 1239 |
1337 void MetricsService::LogPluginLoadingError(const base::FilePath& plugin_path) { | |
1338 #if defined(ENABLE_PLUGINS) | |
1339 // TODO(asvitkine): Move this out of here. | |
1340 plugin_metrics_provider_->LogPluginLoadingError(plugin_path); | |
1341 #endif | |
1342 } | |
1343 | |
1344 bool MetricsService::ShouldLogEvents() { | 1240 bool MetricsService::ShouldLogEvents() { |
1345 // We simply don't log events to UMA if there is a single incognito | 1241 // We simply don't log events to UMA if there is a single incognito |
1346 // session visible. The problem is that we always notify using the orginal | 1242 // session visible. The problem is that we always notify using the orginal |
1347 // profile in order to simplify notification processing. | 1243 // profile in order to simplify notification processing. |
1348 return !client_->IsOffTheRecordSessionActive(); | 1244 return !client_->IsOffTheRecordSessionActive(); |
1349 } | 1245 } |
1350 | 1246 |
1351 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { | 1247 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
1352 DCHECK(IsSingleThreaded()); | 1248 DCHECK(IsSingleThreaded()); |
1353 local_state_->SetBoolean(path, value); | 1249 local_state_->SetBoolean(path, value); |
1354 RecordCurrentState(local_state_); | 1250 RecordCurrentState(local_state_); |
1355 } | 1251 } |
1356 | 1252 |
1357 void MetricsService::RecordCurrentState(PrefService* pref) { | 1253 void MetricsService::RecordCurrentState(PrefService* pref) { |
1358 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1254 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
1359 | 1255 |
1360 #if defined(ENABLE_PLUGINS) | 1256 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
1361 plugin_metrics_provider_->RecordPluginChanges(); | 1257 metrics_providers_[i]->RecordCurrentState(); |
1362 #endif | |
1363 } | 1258 } |
OLD | NEW |