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 | 10 // A MetricsService instance is typically created at application startup. It |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 #include "chrome/browser/metrics/metrics_service.h" | 145 #include "chrome/browser/metrics/metrics_service.h" |
146 | 146 |
147 #include "base/bind.h" | 147 #include "base/bind.h" |
148 #include "base/callback.h" | 148 #include "base/callback.h" |
149 #include "base/command_line.h" | 149 #include "base/command_line.h" |
150 #include "base/md5.h" | 150 #include "base/md5.h" |
151 #include "base/metrics/histogram.h" | 151 #include "base/metrics/histogram.h" |
152 #include "base/string_number_conversions.h" | 152 #include "base/string_number_conversions.h" |
153 #include "base/threading/platform_thread.h" | 153 #include "base/threading/platform_thread.h" |
154 #include "base/threading/thread.h" | 154 #include "base/threading/thread.h" |
| 155 #include "base/tracked_objects.h" |
155 #include "base/utf_string_conversions.h" | 156 #include "base/utf_string_conversions.h" |
156 #include "base/values.h" | 157 #include "base/values.h" |
157 #include "chrome/browser/bookmarks/bookmark_model.h" | 158 #include "chrome/browser/bookmarks/bookmark_model.h" |
158 #include "chrome/browser/browser_process.h" | 159 #include "chrome/browser/browser_process.h" |
159 #include "chrome/browser/extensions/extension_service.h" | 160 #include "chrome/browser/extensions/extension_service.h" |
160 #include "chrome/browser/extensions/process_map.h" | 161 #include "chrome/browser/extensions/process_map.h" |
161 #include "chrome/browser/io_thread.h" | 162 #include "chrome/browser/io_thread.h" |
162 #include "chrome/browser/memory_details.h" | 163 #include "chrome/browser/memory_details.h" |
163 #include "chrome/browser/metrics/histogram_synchronizer.h" | 164 #include "chrome/browser/metrics/histogram_synchronizer.h" |
164 #include "chrome/browser/metrics/metrics_log.h" | 165 #include "chrome/browser/metrics/metrics_log.h" |
165 #include "chrome/browser/metrics/metrics_log_serializer.h" | 166 #include "chrome/browser/metrics/metrics_log_serializer.h" |
166 #include "chrome/browser/metrics/metrics_reporting_scheduler.h" | 167 #include "chrome/browser/metrics/metrics_reporting_scheduler.h" |
| 168 #include "chrome/browser/metrics/tracking_synchronizer.h" |
167 #include "chrome/browser/net/http_pipelining_compatibility_client.h" | 169 #include "chrome/browser/net/http_pipelining_compatibility_client.h" |
168 #include "chrome/browser/net/network_stats.h" | 170 #include "chrome/browser/net/network_stats.h" |
169 #include "chrome/browser/prefs/pref_service.h" | 171 #include "chrome/browser/prefs/pref_service.h" |
170 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 172 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
171 #include "chrome/browser/profiles/profile.h" | 173 #include "chrome/browser/profiles/profile.h" |
172 #include "chrome/browser/search_engines/template_url_service.h" | 174 #include "chrome/browser/search_engines/template_url_service.h" |
173 #include "chrome/browser/ui/browser_list.h" | 175 #include "chrome/browser/ui/browser_list.h" |
174 #include "chrome/common/child_process_logging.h" | 176 #include "chrome/common/child_process_logging.h" |
175 #include "chrome/common/chrome_notification_types.h" | 177 #include "chrome/common/chrome_notification_types.h" |
176 #include "chrome/common/chrome_switches.h" | 178 #include "chrome/common/chrome_switches.h" |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 "hardware_class", &hardware_class); | 768 "hardware_class", &hardware_class); |
767 #endif // OS_CHROMEOS | 769 #endif // OS_CHROMEOS |
768 | 770 |
769 target_loop->PostTask(FROM_HERE, | 771 target_loop->PostTask(FROM_HERE, |
770 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | 772 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, |
771 self, hardware_class)); | 773 self, hardware_class)); |
772 } | 774 } |
773 | 775 |
774 void MetricsService::OnInitTaskGotHardwareClass( | 776 void MetricsService::OnInitTaskGotHardwareClass( |
775 const std::string& hardware_class) { | 777 const std::string& hardware_class) { |
776 DCHECK_EQ(state_, INIT_TASK_SCHEDULED); | 778 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
777 hardware_class_ = hardware_class; | 779 hardware_class_ = hardware_class; |
778 | 780 |
779 // Start the next part of the init task: loading plugin information. | 781 // Start the next part of the init task: loading plugin information. |
780 PluginService::GetInstance()->GetPlugins( | 782 PluginService::GetInstance()->GetPlugins( |
781 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | 783 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, |
782 self_ptr_factory_.GetWeakPtr())); | 784 self_ptr_factory_.GetWeakPtr())); |
783 } | 785 } |
784 | 786 |
785 void MetricsService::OnInitTaskGotPluginInfo( | 787 void MetricsService::OnInitTaskGotPluginInfo( |
786 const std::vector<webkit::WebPluginInfo>& plugins) { | 788 const std::vector<webkit::WebPluginInfo>& plugins) { |
787 DCHECK_EQ(state_, INIT_TASK_SCHEDULED); | 789 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
788 plugins_ = plugins; | 790 plugins_ = plugins; |
789 | 791 |
790 if (state_ == INIT_TASK_SCHEDULED) | 792 // Start the next part of the init task: fetching performance data. This will |
791 state_ = INIT_TASK_DONE; | 793 // call into |FinishedReceivingProfilerData()| when the task completes. |
| 794 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously( |
| 795 self_ptr_factory_.GetWeakPtr()); |
| 796 } |
| 797 |
| 798 void MetricsService::ReceivedProfilerData( |
| 799 const tracked_objects::ProcessDataSnapshot& process_data, |
| 800 content::ProcessType process_type) { |
| 801 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 802 |
| 803 // Upon the first callback, create the initial log so that we can immediately |
| 804 // save the profiler data. |
| 805 if (!initial_log_.get()) |
| 806 initial_log_.reset(new MetricsLog(client_id_, session_id_)); |
| 807 |
| 808 initial_log_->RecordProfilerData(process_data, process_type); |
| 809 } |
| 810 |
| 811 void MetricsService::FinishedReceivingProfilerData() { |
| 812 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 813 state_ = INIT_TASK_DONE; |
792 } | 814 } |
793 | 815 |
794 std::string MetricsService::GenerateClientID() { | 816 std::string MetricsService::GenerateClientID() { |
795 return guid::GenerateGUID(); | 817 return guid::GenerateGUID(); |
796 } | 818 } |
797 | 819 |
798 //------------------------------------------------------------------------------ | 820 //------------------------------------------------------------------------------ |
799 // State save methods | 821 // State save methods |
800 | 822 |
801 void MetricsService::ScheduleNextStateSave() { | 823 void MetricsService::ScheduleNextStateSave() { |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 case INITIALIZED: | 1029 case INITIALIZED: |
1008 case INIT_TASK_SCHEDULED: // We should be further along by now. | 1030 case INIT_TASK_SCHEDULED: // We should be further along by now. |
1009 DCHECK(false); | 1031 DCHECK(false); |
1010 return; | 1032 return; |
1011 | 1033 |
1012 case INIT_TASK_DONE: | 1034 case INIT_TASK_DONE: |
1013 // We need to wait for the initial log to be ready before sending | 1035 // We need to wait for the initial log to be ready before sending |
1014 // anything, because the server will tell us whether it wants to hear | 1036 // anything, because the server will tell us whether it wants to hear |
1015 // from us. | 1037 // from us. |
1016 PrepareInitialLog(); | 1038 PrepareInitialLog(); |
1017 DCHECK(state_ == INIT_TASK_DONE); | 1039 DCHECK_EQ(INIT_TASK_DONE, state_); |
1018 log_manager_.LoadPersistedUnsentLogs(); | 1040 log_manager_.LoadPersistedUnsentLogs(); |
1019 state_ = INITIAL_LOG_READY; | 1041 state_ = INITIAL_LOG_READY; |
1020 break; | 1042 break; |
1021 | 1043 |
1022 case SENDING_OLD_LOGS: | 1044 case SENDING_OLD_LOGS: |
1023 if (log_manager_.has_unsent_logs()) { | 1045 if (log_manager_.has_unsent_logs()) { |
1024 log_manager_.StageNextLogForUpload(); | 1046 log_manager_.StageNextLogForUpload(); |
1025 break; | 1047 break; |
1026 } | 1048 } |
1027 state_ = SENDING_CURRENT_LOGS; | 1049 state_ = SENDING_CURRENT_LOGS; |
1028 // Fall through. | 1050 // Fall through. |
1029 | 1051 |
1030 case SENDING_CURRENT_LOGS: | 1052 case SENDING_CURRENT_LOGS: |
1031 StopRecording(); | 1053 StopRecording(); |
1032 StartRecording(); | 1054 StartRecording(); |
1033 log_manager_.StageNextLogForUpload(); | 1055 log_manager_.StageNextLogForUpload(); |
1034 break; | 1056 break; |
1035 | 1057 |
1036 default: | 1058 default: |
1037 NOTREACHED(); | 1059 NOTREACHED(); |
1038 return; | 1060 return; |
1039 } | 1061 } |
1040 | 1062 |
1041 DCHECK(log_manager_.has_staged_log()); | 1063 DCHECK(log_manager_.has_staged_log()); |
1042 } | 1064 } |
1043 | 1065 |
1044 void MetricsService::PrepareInitialLog() { | 1066 void MetricsService::PrepareInitialLog() { |
1045 DCHECK(state_ == INIT_TASK_DONE); | 1067 DCHECK_EQ(INIT_TASK_DONE, state_); |
1046 | 1068 |
1047 MetricsLog* log = new MetricsLog(client_id_, session_id_); | 1069 DCHECK(initial_log_.get()); |
1048 log->set_hardware_class(hardware_class_); // Adds to initial log. | 1070 initial_log_->set_hardware_class(hardware_class_); |
1049 log->RecordEnvironment(plugins_, profile_dictionary_.get()); | 1071 initial_log_->RecordEnvironment(plugins_, profile_dictionary_.get()); |
1050 | 1072 |
1051 // Histograms only get written to the current log, so make the new log current | 1073 // Histograms only get written to the current log, so make the new log current |
1052 // before writing them. | 1074 // before writing them. |
1053 log_manager_.PauseCurrentLog(); | 1075 log_manager_.PauseCurrentLog(); |
1054 log_manager_.BeginLoggingWithLog(log, MetricsLogManager::INITIAL_LOG); | 1076 log_manager_.BeginLoggingWithLog(initial_log_.release(), |
| 1077 MetricsLogManager::INITIAL_LOG); |
1055 RecordCurrentHistograms(); | 1078 RecordCurrentHistograms(); |
1056 log_manager_.FinishCurrentLog(); | 1079 log_manager_.FinishCurrentLog(); |
1057 log_manager_.ResumePausedLog(); | 1080 log_manager_.ResumePausedLog(); |
1058 | 1081 |
1059 DCHECK(!log_manager_.has_staged_log()); | 1082 DCHECK(!log_manager_.has_staged_log()); |
1060 log_manager_.StageNextLogForUpload(); | 1083 log_manager_.StageNextLogForUpload(); |
1061 } | 1084 } |
1062 | 1085 |
1063 void MetricsService::StoreUnsentLogs() { | 1086 void MetricsService::StoreUnsentLogs() { |
1064 if (state_ < INITIAL_LOG_READY) | 1087 if (state_ < INITIAL_LOG_READY) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1249 break; | 1272 break; |
1250 | 1273 |
1251 default: | 1274 default: |
1252 NOTREACHED(); | 1275 NOTREACHED(); |
1253 break; | 1276 break; |
1254 } | 1277 } |
1255 | 1278 |
1256 log_manager_.DiscardStagedLog(); | 1279 log_manager_.DiscardStagedLog(); |
1257 | 1280 |
1258 if (log_manager_.has_unsent_logs()) | 1281 if (log_manager_.has_unsent_logs()) |
1259 DCHECK(state_ < SENDING_CURRENT_LOGS); | 1282 DCHECK_LT(state_, SENDING_CURRENT_LOGS); |
1260 } | 1283 } |
1261 | 1284 |
1262 // Error 400 indicates a problem with the log, not with the server, so | 1285 // Error 400 indicates a problem with the log, not with the server, so |
1263 // don't consider that a sign that the server is in trouble. | 1286 // don't consider that a sign that the server is in trouble. |
1264 bool server_is_healthy = upload_succeeded || response_code_ == 400; | 1287 bool server_is_healthy = upload_succeeded || response_code_ == 400; |
1265 | 1288 |
1266 scheduler_->UploadFinished(server_is_healthy, | 1289 scheduler_->UploadFinished(server_is_healthy, |
1267 log_manager_.has_unsent_logs()); | 1290 log_manager_.has_unsent_logs()); |
1268 | 1291 |
1269 // Collect network stats if UMA upload succeeded. | 1292 // Collect network stats if UMA upload succeeded. |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1670 if (local_state) { | 1693 if (local_state) { |
1671 const PrefService::Preference* uma_pref = | 1694 const PrefService::Preference* uma_pref = |
1672 local_state->FindPreference(prefs::kMetricsReportingEnabled); | 1695 local_state->FindPreference(prefs::kMetricsReportingEnabled); |
1673 if (uma_pref) { | 1696 if (uma_pref) { |
1674 bool success = uma_pref->GetValue()->GetAsBoolean(&result); | 1697 bool success = uma_pref->GetValue()->GetAsBoolean(&result); |
1675 DCHECK(success); | 1698 DCHECK(success); |
1676 } | 1699 } |
1677 } | 1700 } |
1678 return result; | 1701 return result; |
1679 } | 1702 } |
OLD | NEW |