OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 10 // A MetricsService instance is typically created at application startup. It |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 #include "chrome/browser/profiles/profile.h" | 180 #include "chrome/browser/profiles/profile.h" |
181 #include "chrome/browser/search_engines/template_url_service.h" | 181 #include "chrome/browser/search_engines/template_url_service.h" |
182 #include "chrome/browser/ui/browser_list.h" | 182 #include "chrome/browser/ui/browser_list.h" |
183 #include "chrome/common/child_process_logging.h" | 183 #include "chrome/common/child_process_logging.h" |
184 #include "chrome/common/chrome_notification_types.h" | 184 #include "chrome/common/chrome_notification_types.h" |
185 #include "chrome/common/chrome_switches.h" | 185 #include "chrome/common/chrome_switches.h" |
186 #include "chrome/common/guid.h" | 186 #include "chrome/common/guid.h" |
187 #include "chrome/common/pref_names.h" | 187 #include "chrome/common/pref_names.h" |
188 #include "chrome/common/render_messages.h" | 188 #include "chrome/common/render_messages.h" |
189 #include "content/browser/load_notification_details.h" | 189 #include "content/browser/load_notification_details.h" |
| 190 #include "content/browser/plugin_service.h" |
190 #include "content/browser/renderer_host/render_process_host.h" | 191 #include "content/browser/renderer_host/render_process_host.h" |
191 #include "content/common/child_process_info.h" | 192 #include "content/common/child_process_info.h" |
192 #include "content/common/notification_service.h" | 193 #include "content/common/notification_service.h" |
193 #include "webkit/plugins/npapi/plugin_list.h" | 194 #include "webkit/plugins/npapi/plugin_list.h" |
194 #include "webkit/plugins/webplugininfo.h" | 195 #include "webkit/plugins/webplugininfo.h" |
195 | 196 |
196 // TODO(port): port browser_distribution.h. | 197 // TODO(port): port browser_distribution.h. |
197 #if !defined(OS_POSIX) | 198 #if !defined(OS_POSIX) |
198 #include "chrome/installer/util/browser_distribution.h" | 199 #include "chrome/installer/util/browser_distribution.h" |
199 #endif | 200 #endif |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 MessageLoop::current()->PostTask(FROM_HERE, completion_); | 317 MessageLoop::current()->PostTask(FROM_HERE, completion_); |
317 } | 318 } |
318 | 319 |
319 private: | 320 private: |
320 ~MetricsMemoryDetails() {} | 321 ~MetricsMemoryDetails() {} |
321 | 322 |
322 Task* completion_; | 323 Task* completion_; |
323 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); | 324 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); |
324 }; | 325 }; |
325 | 326 |
326 class MetricsService::InitTaskComplete : public Task { | |
327 public: | |
328 explicit InitTaskComplete( | |
329 const std::string& hardware_class, | |
330 const std::vector<webkit::WebPluginInfo>& plugins) | |
331 : hardware_class_(hardware_class), plugins_(plugins) {} | |
332 | |
333 virtual void Run() { | |
334 g_browser_process->metrics_service()->OnInitTaskComplete( | |
335 hardware_class_, plugins_); | |
336 } | |
337 | |
338 private: | |
339 std::string hardware_class_; | |
340 std::vector<webkit::WebPluginInfo> plugins_; | |
341 }; | |
342 | |
343 class MetricsService::InitTask : public Task { | |
344 public: | |
345 explicit InitTask(MessageLoop* callback_loop) | |
346 : callback_loop_(callback_loop) {} | |
347 | |
348 virtual void Run() { | |
349 std::vector<webkit::WebPluginInfo> plugins; | |
350 webkit::npapi::PluginList::Singleton()->GetPlugins(&plugins); | |
351 std::string hardware_class; // Empty string by default. | |
352 #if defined(OS_CHROMEOS) | |
353 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic( | |
354 "hardware_class", &hardware_class); | |
355 #endif // OS_CHROMEOS | |
356 callback_loop_->PostTask(FROM_HERE, new InitTaskComplete( | |
357 hardware_class, plugins)); | |
358 } | |
359 | |
360 private: | |
361 MessageLoop* callback_loop_; | |
362 }; | |
363 | |
364 // static | 327 // static |
365 void MetricsService::RegisterPrefs(PrefService* local_state) { | 328 void MetricsService::RegisterPrefs(PrefService* local_state) { |
366 DCHECK(IsSingleThreaded()); | 329 DCHECK(IsSingleThreaded()); |
367 local_state->RegisterStringPref(prefs::kMetricsClientID, ""); | 330 local_state->RegisterStringPref(prefs::kMetricsClientID, ""); |
368 local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); | 331 local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); |
369 local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); | 332 local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
370 local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); | 333 local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
371 local_state->RegisterStringPref(prefs::kStabilityStatsVersion, ""); | 334 local_state->RegisterStringPref(prefs::kStabilityStatsVersion, ""); |
372 local_state->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); | 335 local_state->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); |
373 local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); | 336 local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 local_state->ClearPref(prefs::kStabilityPluginStats); | 401 local_state->ClearPref(prefs::kStabilityPluginStats); |
439 | 402 |
440 local_state->ClearPref(prefs::kMetricsInitialLogs); | 403 local_state->ClearPref(prefs::kMetricsInitialLogs); |
441 local_state->ClearPref(prefs::kMetricsOngoingLogs); | 404 local_state->ClearPref(prefs::kMetricsOngoingLogs); |
442 } | 405 } |
443 | 406 |
444 MetricsService::MetricsService() | 407 MetricsService::MetricsService() |
445 : recording_active_(false), | 408 : recording_active_(false), |
446 reporting_active_(false), | 409 reporting_active_(false), |
447 state_(INITIALIZED), | 410 state_(INITIALIZED), |
| 411 init_state_(INIT_SUBTASK_UNSTARTED), |
448 current_fetch_(NULL), | 412 current_fetch_(NULL), |
449 io_thread_(NULL), | 413 io_thread_(NULL), |
450 idle_since_last_transmission_(false), | 414 idle_since_last_transmission_(false), |
451 next_window_id_(0), | 415 next_window_id_(0), |
452 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), | 416 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), |
453 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), | 417 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), |
454 waiting_for_asynchronus_reporting_step_(false) { | 418 waiting_for_asynchronus_reporting_step_(false) { |
455 DCHECK(IsSingleThreaded()); | 419 DCHECK(IsSingleThreaded()); |
456 InitializeMetricsState(); | 420 InitializeMetricsState(); |
457 | 421 |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 size_t switch_count = command_line->GetSwitches().size(); | 755 size_t switch_count = command_line->GetSwitches().size(); |
792 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", switch_count); | 756 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", switch_count); |
793 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount", | 757 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount", |
794 switch_count - common_commands); | 758 switch_count - common_commands); |
795 | 759 |
796 // Kick off the process of saving the state (so the uptime numbers keep | 760 // Kick off the process of saving the state (so the uptime numbers keep |
797 // getting updated) every n minutes. | 761 // getting updated) every n minutes. |
798 ScheduleNextStateSave(); | 762 ScheduleNextStateSave(); |
799 } | 763 } |
800 | 764 |
801 void MetricsService::OnInitTaskComplete( | 765 void MetricsService::RunInitTasks() { |
802 const std::string& hardware_class, | 766 // Schedules a task on the file thread for execution of slower |
803 const std::vector<webkit::WebPluginInfo>& plugins) { | 767 // initialization steps (such as plugin list generation) necessary |
| 768 // for sending the initial log. This avoids blocking the main UI |
| 769 // thread. |
| 770 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 771 base::Bind(&MetricsService::InitTaskGetHardwareClass, |
| 772 base::Unretained(this), |
| 773 MessageLoop::current()->message_loop_proxy())); |
| 774 |
| 775 PluginService::GetInstance()->GetPlugins( |
| 776 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, |
| 777 base::Unretained(this))); |
| 778 } |
| 779 |
| 780 void MetricsService::InitTaskGetHardwareClass( |
| 781 base::MessageLoopProxy* target_loop) { |
| 782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 783 |
| 784 std::string hardware_class; |
| 785 #if defined(OS_CHROMEOS) |
| 786 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic( |
| 787 "hardware_class", &hardware_class); |
| 788 #endif // OS_CHROMEOS |
| 789 |
| 790 target_loop->PostTask(FROM_HERE, |
| 791 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, |
| 792 base::Unretained(this), hardware_class)); |
| 793 } |
| 794 |
| 795 void MetricsService::OnInitTaskGotHardwareClass( |
| 796 const std::string& hardware_class) { |
| 797 hardware_class_ = hardware_class; |
| 798 init_state_ |= INIT_SUBTASK_HWCLASS; |
| 799 OnInitTaskMaybeDone(); |
| 800 } |
| 801 |
| 802 void MetricsService::OnInitTaskGotPluginInfo( |
| 803 std::vector<webkit::WebPluginInfo> plugins) { |
| 804 plugins_ = plugins; |
| 805 init_state_ |= INIT_SUBTASK_PLUGINS; |
| 806 OnInitTaskMaybeDone(); |
| 807 } |
| 808 |
| 809 void MetricsService::OnInitTaskMaybeDone() { |
804 DCHECK(state_ == INIT_TASK_SCHEDULED); | 810 DCHECK(state_ == INIT_TASK_SCHEDULED); |
805 hardware_class_ = hardware_class; | 811 if (state_ != INIT_TASK_SCHEDULED || |
806 plugins_ = plugins; | 812 (init_state_ & INIT_SUBTASK_ALL) != INIT_SUBTASK_ALL) { |
| 813 return; |
| 814 } |
| 815 |
807 io_thread_ = g_browser_process->io_thread(); | 816 io_thread_ = g_browser_process->io_thread(); |
808 if (state_ == INIT_TASK_SCHEDULED) | 817 state_ = INIT_TASK_DONE; |
809 state_ = INIT_TASK_DONE; | |
810 } | 818 } |
811 | 819 |
812 std::string MetricsService::GenerateClientID() { | 820 std::string MetricsService::GenerateClientID() { |
813 return guid::GenerateGUID(); | 821 return guid::GenerateGUID(); |
814 } | 822 } |
815 | 823 |
816 //------------------------------------------------------------------------------ | 824 //------------------------------------------------------------------------------ |
817 // State save methods | 825 // State save methods |
818 | 826 |
819 void MetricsService::ScheduleNextStateSave() { | 827 void MetricsService::ScheduleNextStateSave() { |
(...skipping 24 matching lines...) Expand all Loading... |
844 | 852 |
845 void MetricsService::StartRecording() { | 853 void MetricsService::StartRecording() { |
846 if (current_log_) | 854 if (current_log_) |
847 return; | 855 return; |
848 | 856 |
849 current_log_ = new MetricsLog(client_id_, session_id_); | 857 current_log_ = new MetricsLog(client_id_, session_id_); |
850 if (state_ == INITIALIZED) { | 858 if (state_ == INITIALIZED) { |
851 // We only need to schedule that run once. | 859 // We only need to schedule that run once. |
852 state_ = INIT_TASK_SCHEDULED; | 860 state_ = INIT_TASK_SCHEDULED; |
853 | 861 |
854 // Schedules a task on the file thread for execution of slower | 862 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
855 // initialization steps (such as plugin list generation) necessary | 863 base::Bind(&MetricsService::RunInitTasks, base::Unretained(this)), |
856 // for sending the initial log. This avoids blocking the main UI | |
857 // thread. | |
858 g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE, | |
859 new InitTask(MessageLoop::current()), | |
860 kInitializationDelaySeconds * 1000); | 864 kInitializationDelaySeconds * 1000); |
861 } | 865 } |
862 } | 866 } |
863 | 867 |
864 void MetricsService::StopRecording() { | 868 void MetricsService::StopRecording() { |
865 if (!current_log_) | 869 if (!current_log_) |
866 return; | 870 return; |
867 | 871 |
868 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 872 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
869 // can stop recording logs that are too big much sooner. | 873 // can stop recording logs that are too big much sooner. |
(...skipping 880 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1750 if (local_state) { | 1754 if (local_state) { |
1751 const PrefService::Preference* uma_pref = | 1755 const PrefService::Preference* uma_pref = |
1752 local_state->FindPreference(prefs::kMetricsReportingEnabled); | 1756 local_state->FindPreference(prefs::kMetricsReportingEnabled); |
1753 if (uma_pref) { | 1757 if (uma_pref) { |
1754 bool success = uma_pref->GetValue()->GetAsBoolean(&result); | 1758 bool success = uma_pref->GetValue()->GetAsBoolean(&result); |
1755 DCHECK(success); | 1759 DCHECK(success); |
1756 } | 1760 } |
1757 } | 1761 } |
1758 return result; | 1762 return result; |
1759 } | 1763 } |
OLD | NEW |