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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 #include "base/strings/string_number_conversions.h" | 177 #include "base/strings/string_number_conversions.h" |
178 #include "base/strings/utf_string_conversions.h" | 178 #include "base/strings/utf_string_conversions.h" |
179 #include "base/threading/platform_thread.h" | 179 #include "base/threading/platform_thread.h" |
180 #include "base/threading/thread.h" | 180 #include "base/threading/thread.h" |
181 #include "base/threading/thread_restrictions.h" | 181 #include "base/threading/thread_restrictions.h" |
182 #include "base/tracked_objects.h" | 182 #include "base/tracked_objects.h" |
183 #include "base/values.h" | 183 #include "base/values.h" |
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" | |
188 #include "chrome/browser/metrics/gpu_metrics_provider.h" | |
189 #include "chrome/browser/metrics/network_metrics_provider.h" | |
190 #include "chrome/browser/metrics/omnibox_metrics_provider.h" | |
191 #include "chrome/browser/metrics/profiler_metrics_provider.h" | |
192 #include "chrome/browser/metrics/tracking_synchronizer.h" | |
193 #include "chrome/common/pref_names.h" | 187 #include "chrome/common/pref_names.h" |
194 #include "chrome/common/variations/variations_util.h" | 188 #include "chrome/common/variations/variations_util.h" |
195 #include "components/metrics/metrics_log.h" | 189 #include "components/metrics/metrics_log.h" |
196 #include "components/metrics/metrics_log_base.h" | 190 #include "components/metrics/metrics_log_base.h" |
197 #include "components/metrics/metrics_log_manager.h" | 191 #include "components/metrics/metrics_log_manager.h" |
198 #include "components/metrics/metrics_log_uploader.h" | 192 #include "components/metrics/metrics_log_uploader.h" |
199 #include "components/metrics/metrics_pref_names.h" | 193 #include "components/metrics/metrics_pref_names.h" |
200 #include "components/metrics/metrics_reporting_scheduler.h" | 194 #include "components/metrics/metrics_reporting_scheduler.h" |
201 #include "components/metrics/metrics_service_client.h" | 195 #include "components/metrics/metrics_service_client.h" |
202 #include "components/metrics/metrics_state_manager.h" | 196 #include "components/metrics/metrics_state_manager.h" |
203 #include "components/variations/entropy_provider.h" | 197 #include "components/variations/entropy_provider.h" |
204 | 198 |
205 #if defined(ENABLE_PLUGINS) | |
206 // TODO(asvitkine): Move this out of MetricsService. | |
207 #include "chrome/browser/metrics/plugin_metrics_provider.h" | |
208 #endif | |
209 | |
210 #if defined(OS_WIN) | |
211 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" | |
212 #endif | |
213 | |
214 #if defined(OS_ANDROID) | |
215 // TODO(asvitkine): Move this out of MetricsService. | |
216 #include "chrome/browser/metrics/android_metrics_provider.h" | |
217 #endif | |
218 | |
219 using base::Time; | 199 using base::Time; |
220 using metrics::MetricsLogManager; | 200 using metrics::MetricsLogManager; |
221 | 201 |
222 namespace { | 202 namespace { |
223 | 203 |
224 // Check to see that we're being called on only one thread. | 204 // Check to see that we're being called on only one thread. |
225 bool IsSingleThreaded() { | 205 bool IsSingleThreaded() { |
226 static base::PlatformThreadId thread_id = 0; | 206 static base::PlatformThreadId thread_id = 0; |
227 if (!thread_id) | 207 if (!thread_id) |
228 thread_id = base::PlatformThread::CurrentId(); | 208 thread_id = base::PlatformThread::CurrentId(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1); | 303 registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1); |
324 | 304 |
325 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); | 305 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); |
326 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); | 306 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); |
327 | 307 |
328 registry->RegisterInt64Pref(prefs::kInstallDate, 0); | 308 registry->RegisterInt64Pref(prefs::kInstallDate, 0); |
329 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); | 309 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
330 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); | 310 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
331 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); | 311 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
332 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); | 312 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
333 | |
334 // TODO(asvitkine): Move this out of here. | |
335 ChromeStabilityMetricsProvider::RegisterPrefs(registry); | |
336 | |
337 #if defined(OS_ANDROID) | |
338 // TODO(asvitkine): Move this out of here. | |
339 AndroidMetricsProvider::RegisterPrefs(registry); | |
340 #endif // defined(OS_ANDROID) | |
341 | |
342 #if defined(ENABLE_PLUGINS) | |
343 // TODO(asvitkine): Move this out of here. | |
344 PluginMetricsProvider::RegisterPrefs(registry); | |
345 #endif | |
346 } | 313 } |
347 | 314 |
348 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, | 315 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
349 metrics::MetricsServiceClient* client, | 316 metrics::MetricsServiceClient* client, |
350 PrefService* local_state) | 317 PrefService* local_state) |
351 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), | 318 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
352 histogram_snapshot_manager_(this), | 319 histogram_snapshot_manager_(this), |
353 state_manager_(state_manager), | 320 state_manager_(state_manager), |
354 client_(client), | 321 client_(client), |
355 local_state_(local_state), | 322 local_state_(local_state), |
356 recording_active_(false), | 323 recording_active_(false), |
357 reporting_active_(false), | 324 reporting_active_(false), |
358 test_mode_active_(false), | 325 test_mode_active_(false), |
359 state_(INITIALIZED), | 326 state_(INITIALIZED), |
360 has_initial_stability_log_(false), | 327 has_initial_stability_log_(false), |
361 log_upload_in_progress_(false), | 328 log_upload_in_progress_(false), |
362 idle_since_last_transmission_(false), | 329 idle_since_last_transmission_(false), |
363 session_id_(-1), | 330 session_id_(-1), |
364 self_ptr_factory_(this), | 331 self_ptr_factory_(this), |
365 state_saver_factory_(this) { | 332 state_saver_factory_(this) { |
366 DCHECK(IsSingleThreaded()); | 333 DCHECK(IsSingleThreaded()); |
367 DCHECK(state_manager_); | 334 DCHECK(state_manager_); |
368 DCHECK(client_); | 335 DCHECK(client_); |
369 DCHECK(local_state_); | 336 DCHECK(local_state_); |
370 | |
371 #if defined(OS_ANDROID) | |
372 // TODO(asvitkine): Move this out of MetricsService. | |
373 RegisterMetricsProvider( | |
374 scoped_ptr<metrics::MetricsProvider>(new AndroidMetricsProvider( | |
375 local_state_))); | |
376 #endif // defined(OS_ANDROID) | |
377 | |
378 // TODO(asvitkine): Move these out of MetricsService. | |
379 RegisterMetricsProvider( | |
380 scoped_ptr<metrics::MetricsProvider>(new NetworkMetricsProvider)); | |
381 RegisterMetricsProvider( | |
382 scoped_ptr<metrics::MetricsProvider>(new OmniboxMetricsProvider)); | |
383 RegisterMetricsProvider( | |
384 scoped_ptr<metrics::MetricsProvider>(new ChromeStabilityMetricsProvider)); | |
385 RegisterMetricsProvider( | |
386 scoped_ptr<metrics::MetricsProvider>(new GPUMetricsProvider())); | |
387 profiler_metrics_provider_ = new ProfilerMetricsProvider; | |
388 RegisterMetricsProvider( | |
389 scoped_ptr<metrics::MetricsProvider>(profiler_metrics_provider_)); | |
390 | |
391 #if defined(OS_WIN) | |
392 google_update_metrics_provider_ = new GoogleUpdateMetricsProviderWin; | |
393 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
394 google_update_metrics_provider_)); | |
395 #endif | |
396 | |
397 #if defined(ENABLE_PLUGINS) | |
398 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | |
399 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
400 plugin_metrics_provider_)); | |
401 #endif | |
402 | |
403 } | 337 } |
404 | 338 |
405 MetricsService::~MetricsService() { | 339 MetricsService::~MetricsService() { |
406 DisableRecording(); | 340 DisableRecording(); |
407 } | 341 } |
408 | 342 |
409 void MetricsService::InitializeMetricsRecordingState() { | 343 void MetricsService::InitializeMetricsRecordingState() { |
410 InitializeMetricsState(); | 344 InitializeMetricsState(); |
411 | 345 |
412 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 346 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 604 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
671 | 605 |
672 // Bookkeeping for the uninstall metrics. | 606 // Bookkeeping for the uninstall metrics. |
673 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 607 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
674 | 608 |
675 // Kick off the process of saving the state (so the uptime numbers keep | 609 // Kick off the process of saving the state (so the uptime numbers keep |
676 // getting updated) every n minutes. | 610 // getting updated) every n minutes. |
677 ScheduleNextStateSave(); | 611 ScheduleNextStateSave(); |
678 } | 612 } |
679 | 613 |
680 void MetricsService::OnInitTaskGotHardwareClass() { | |
681 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
682 | |
683 const base::Closure got_plugin_info_callback = | |
684 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | |
685 self_ptr_factory_.GetWeakPtr()); | |
686 | |
687 #if defined(ENABLE_PLUGINS) | |
688 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | |
689 #else | |
690 got_plugin_info_callback.Run(); | |
691 #endif | |
692 } | |
693 | |
694 void MetricsService::OnInitTaskGotPluginInfo() { | |
695 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
696 | |
697 const base::Closure got_metrics_callback = | |
698 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData, | |
699 self_ptr_factory_.GetWeakPtr()); | |
700 | |
701 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
702 google_update_metrics_provider_->GetGoogleUpdateData(got_metrics_callback); | |
703 #else | |
704 got_metrics_callback.Run(); | |
705 #endif | |
706 } | |
707 | |
708 void MetricsService::OnInitTaskGotGoogleUpdateData() { | |
709 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
710 | |
711 // Start the next part of the init task: fetching performance data. This will | |
712 // call into |FinishedReceivingProfilerData()| when the task completes. | |
713 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously( | |
714 self_ptr_factory_.GetWeakPtr()); | |
715 } | |
716 | |
717 void MetricsService::OnUserAction(const std::string& action) { | 614 void MetricsService::OnUserAction(const std::string& action) { |
718 if (!ShouldLogEvents()) | 615 if (!ShouldLogEvents()) |
719 return; | 616 return; |
720 | 617 |
721 log_manager_.current_log()->RecordUserAction(action); | 618 log_manager_.current_log()->RecordUserAction(action); |
722 HandleIdleSinceLastTransmission(false); | 619 HandleIdleSinceLastTransmission(false); |
723 } | 620 } |
724 | 621 |
725 void MetricsService::ReceivedProfilerData( | 622 void MetricsService::FinishedGatheringInitialMetrics() { |
726 const tracked_objects::ProcessDataSnapshot& process_data, | |
727 int process_type) { | |
728 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
729 | |
730 profiler_metrics_provider_->RecordProfilerData(process_data, process_type); | |
731 } | |
732 | |
733 void MetricsService::FinishedReceivingProfilerData() { | |
734 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 623 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
735 state_ = INIT_TASK_DONE; | 624 state_ = INIT_TASK_DONE; |
736 | 625 |
737 // Create the initial log. | 626 // Create the initial log. |
738 if (!initial_metrics_log_.get()) { | 627 if (!initial_metrics_log_.get()) { |
739 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); | 628 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
740 NotifyOnDidCreateMetricsLog(); | 629 NotifyOnDidCreateMetricsLog(); |
741 } | 630 } |
742 | 631 |
743 scheduler_->InitTaskComplete(); | 632 scheduler_->InitTaskComplete(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 content::BrowserThread::PostDelayedTask( | 708 content::BrowserThread::PostDelayedTask( |
820 content::BrowserThread::UI, | 709 content::BrowserThread::UI, |
821 FROM_HERE, | 710 FROM_HERE, |
822 base::Bind(&MetricsService::StartGatheringMetrics, | 711 base::Bind(&MetricsService::StartGatheringMetrics, |
823 self_ptr_factory_.GetWeakPtr()), | 712 self_ptr_factory_.GetWeakPtr()), |
824 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 713 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
825 } | 714 } |
826 } | 715 } |
827 | 716 |
828 void MetricsService::StartGatheringMetrics() { | 717 void MetricsService::StartGatheringMetrics() { |
829 // TODO(blundell): Move all initial metrics gathering to | |
830 // ChromeMetricsServiceClient. | |
831 client_->StartGatheringMetrics( | 718 client_->StartGatheringMetrics( |
832 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | 719 base::Bind(&MetricsService::FinishedGatheringInitialMetrics, |
833 self_ptr_factory_.GetWeakPtr())); | 720 self_ptr_factory_.GetWeakPtr())); |
834 } | 721 } |
835 | 722 |
836 void MetricsService::CloseCurrentLog() { | 723 void MetricsService::CloseCurrentLog() { |
837 if (!log_manager_.current_log()) | 724 if (!log_manager_.current_log()) |
838 return; | 725 return; |
839 | 726 |
840 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 727 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
841 // can stop recording logs that are too big much sooner. | 728 // can stop recording logs that are too big much sooner. |
842 if (log_manager_.current_log()->num_events() > kEventLimit) { | 729 if (log_manager_.current_log()->num_events() > kEventLimit) { |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 | 1183 |
1297 // Redundant setting to assure that we always reset this value at shutdown | 1184 // Redundant setting to assure that we always reset this value at shutdown |
1298 // (and that we don't use some alternate path, and not call LogCleanShutdown). | 1185 // (and that we don't use some alternate path, and not call LogCleanShutdown). |
1299 clean_shutdown_status_ = CLEANLY_SHUTDOWN; | 1186 clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
1300 | 1187 |
1301 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); | 1188 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
1302 local_state_->SetInteger(prefs::kStabilityExecutionPhase, | 1189 local_state_->SetInteger(prefs::kStabilityExecutionPhase, |
1303 MetricsService::SHUTDOWN_COMPLETE); | 1190 MetricsService::SHUTDOWN_COMPLETE); |
1304 } | 1191 } |
1305 | 1192 |
1306 void MetricsService::LogPluginLoadingError(const base::FilePath& plugin_path) { | |
1307 #if defined(ENABLE_PLUGINS) | |
1308 plugin_metrics_provider_->LogPluginLoadingError(plugin_path); | |
1309 #endif | |
1310 } | |
1311 | |
1312 bool MetricsService::ShouldLogEvents() { | 1193 bool MetricsService::ShouldLogEvents() { |
1313 // We simply don't log events to UMA if there is a single incognito | 1194 // We simply don't log events to UMA if there is a single incognito |
1314 // session visible. The problem is that we always notify using the orginal | 1195 // session visible. The problem is that we always notify using the orginal |
1315 // profile in order to simplify notification processing. | 1196 // profile in order to simplify notification processing. |
1316 return !client_->IsOffTheRecordSessionActive(); | 1197 return !client_->IsOffTheRecordSessionActive(); |
1317 } | 1198 } |
1318 | 1199 |
1319 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { | 1200 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
1320 DCHECK(IsSingleThreaded()); | 1201 DCHECK(IsSingleThreaded()); |
1321 local_state_->SetBoolean(path, value); | 1202 local_state_->SetBoolean(path, value); |
1322 RecordCurrentState(local_state_); | 1203 RecordCurrentState(local_state_); |
1323 } | 1204 } |
1324 | 1205 |
1325 void MetricsService::RecordCurrentState(PrefService* pref) { | 1206 void MetricsService::RecordCurrentState(PrefService* pref) { |
1326 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1207 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
1327 | 1208 |
1328 for (size_t i = 0; i < metrics_providers_.size(); ++i) | 1209 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
1329 metrics_providers_[i]->RecordCurrentState(); | 1210 metrics_providers_[i]->RecordCurrentState(); |
1330 } | 1211 } |
OLD | NEW |