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 #include "chrome/browser/metrics/chrome_metrics_service_client.h" | 5 #include "chrome/browser/metrics/chrome_metrics_service_client.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
19 #include "base/metrics/persistent_histogram_allocator.h" | 19 #include "base/metrics/persistent_histogram_allocator.h" |
20 #include "base/metrics/persistent_metrics_file_util.h" | |
20 #include "base/path_service.h" | 21 #include "base/path_service.h" |
21 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
22 #include "base/strings/string16.h" | 23 #include "base/strings/string16.h" |
23 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
25 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
26 #include "chrome/browser/chrome_notification_types.h" | 27 #include "chrome/browser/chrome_notification_types.h" |
27 #include "chrome/browser/google/google_brand.h" | 28 #include "chrome/browser/google/google_brand.h" |
28 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | 29 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
29 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" | 30 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 #include "chrome/browser/signin/chrome_signin_status_metrics_provider_delegate.h " | 104 #include "chrome/browser/signin/chrome_signin_status_metrics_provider_delegate.h " |
104 #include "components/signin/core/browser/signin_status_metrics_provider.h" | 105 #include "components/signin/core/browser/signin_status_metrics_provider.h" |
105 #endif // !defined(OS_CHROMEOS) | 106 #endif // !defined(OS_CHROMEOS) |
106 | 107 |
107 namespace { | 108 namespace { |
108 | 109 |
109 // This specifies the amount of time to wait for all renderers to send their | 110 // This specifies the amount of time to wait for all renderers to send their |
110 // data. | 111 // data. |
111 const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. | 112 const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. |
112 | 113 |
114 // Needs to be kept in sync with the writer in | |
115 // third_party/crashpad/crashpad/handler/handler_main.cc. | |
116 const char kCrashpadHistogramAllocatorName[] = "CrashpadMetrics"; | |
117 | |
113 // Checks whether it is the first time that cellular uploads logic should be | 118 // Checks whether it is the first time that cellular uploads logic should be |
114 // enabled based on whether the the preference for that logic is initialized. | 119 // enabled based on whether the the preference for that logic is initialized. |
115 // This should happen only once as the used preference will be initialized | 120 // This should happen only once as the used preference will be initialized |
116 // afterwards in |UmaSessionStats.java|. | 121 // afterwards in |UmaSessionStats.java|. |
117 bool ShouldClearSavedMetrics() { | 122 bool ShouldClearSavedMetrics() { |
118 #if BUILDFLAG(ANDROID_JAVA_UI) | 123 #if BUILDFLAG(ANDROID_JAVA_UI) |
119 PrefService* local_state = g_browser_process->local_state(); | 124 PrefService* local_state = g_browser_process->local_state(); |
120 return !local_state->HasPrefPath(metrics::prefs::kMetricsReportingEnabled) && | 125 return !local_state->HasPrefPath(metrics::prefs::kMetricsReportingEnabled) && |
121 metrics::IsCellularLogicEnabled(); | 126 metrics::IsCellularLogicEnabled(); |
122 #else | 127 #else |
123 return false; | 128 return false; |
124 #endif | 129 #endif |
125 } | 130 } |
126 | 131 |
127 void RegisterInstallerFileMetricsPreferences(PrefRegistrySimple* registry) { | 132 void RegisterFileMetricsPreferences(PrefRegistrySimple* registry) { |
128 metrics::FileMetricsProvider::RegisterPrefs( | 133 metrics::FileMetricsProvider::RegisterPrefs( |
129 registry, ChromeMetricsServiceClient::kBrowserMetricsName); | 134 registry, ChromeMetricsServiceClient::kBrowserMetricsName); |
130 | 135 |
136 metrics::FileMetricsProvider::RegisterPrefs(registry, | |
137 kCrashpadHistogramAllocatorName); | |
138 | |
131 #if defined(OS_WIN) | 139 #if defined(OS_WIN) |
132 metrics::FileMetricsProvider::RegisterPrefs( | 140 metrics::FileMetricsProvider::RegisterPrefs( |
133 registry, installer::kSetupHistogramAllocatorName); | 141 registry, installer::kSetupHistogramAllocatorName); |
134 #endif | 142 #endif |
135 } | 143 } |
136 | 144 |
137 std::unique_ptr<metrics::FileMetricsProvider> | 145 std::unique_ptr<metrics::FileMetricsProvider> CreateFileMetricsProvider( |
138 CreateInstallerFileMetricsProvider(bool metrics_reporting_enabled) { | 146 bool metrics_reporting_enabled) { |
139 // Fetch a worker-pool for performing I/O tasks that are not allowed on | 147 // Fetch a worker-pool for performing I/O tasks that are not allowed on |
140 // the main UI thread. | 148 // the main UI thread. |
141 scoped_refptr<base::TaskRunner> task_runner = | 149 scoped_refptr<base::TaskRunner> task_runner = |
142 content::BrowserThread::GetBlockingPool() | 150 content::BrowserThread::GetBlockingPool() |
143 ->GetTaskRunnerWithShutdownBehavior( | 151 ->GetTaskRunnerWithShutdownBehavior( |
144 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 152 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
145 | 153 |
146 // Create an object to monitor files of metrics and include them in reports. | 154 // Create an object to monitor files of metrics and include them in reports. |
147 std::unique_ptr<metrics::FileMetricsProvider> file_metrics_provider( | 155 std::unique_ptr<metrics::FileMetricsProvider> file_metrics_provider( |
148 new metrics::FileMetricsProvider(task_runner, | 156 new metrics::FileMetricsProvider(task_runner, |
(...skipping 16 matching lines...) Expand all Loading... | |
165 ChromeMetricsServiceClient::kBrowserMetricsName); | 173 ChromeMetricsServiceClient::kBrowserMetricsName); |
166 } else { | 174 } else { |
167 // When metrics reporting is not enabled, any existing file should be | 175 // When metrics reporting is not enabled, any existing file should be |
168 // deleted in order to preserve user privacy. | 176 // deleted in order to preserve user privacy. |
169 task_runner->PostTask(FROM_HERE, | 177 task_runner->PostTask(FROM_HERE, |
170 base::Bind(base::IgnoreResult(&base::DeleteFile), | 178 base::Bind(base::IgnoreResult(&base::DeleteFile), |
171 metrics_file, /*recursive=*/false)); | 179 metrics_file, /*recursive=*/false)); |
172 } | 180 } |
173 } | 181 } |
174 | 182 |
183 // Read the Crashpad metrics files. | |
184 base::FilePath crash_dump_dir; | |
185 if (base::PathService::Get(chrome::DIR_USER_DATA, | |
186 &crash_dump_dir)) { | |
187 base::FilePath crashpad_metrics_file_prev = | |
188 crash_dump_dir.AppendASCII(kCrashpadHistogramAllocatorName) | |
189 .AddExtension(base::PersistentMemoryAllocator::kFileExtension); | |
190 if (metrics_reporting_enabled) { | |
191 // Register the data from the previous run if crashpad_handler didn't exit | |
192 // cleanly. | |
193 file_metrics_provider->RegisterSource( | |
194 crashpad_metrics_file_prev, | |
195 metrics::FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, | |
196 metrics::FileMetricsProvider::ASSOCIATE_PREVIOUS_RUN, | |
197 kCrashpadHistogramAllocatorName); | |
198 | |
199 // Register data that will be populated for the current run. | |
200 base::FilePath crashpad_metrics_file_cur = | |
201 crash_dump_dir.AppendASCII(kCrashpadHistogramAllocatorName + | |
202 std::string("-active")) | |
203 .AddExtension(base::PersistentMemoryAllocator::kFileExtension); | |
204 file_metrics_provider->RegisterSource( | |
205 crashpad_metrics_file_prev, | |
206 metrics::FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE, | |
207 metrics::FileMetricsProvider::ASSOCIATE_CURRENT_RUN, | |
208 kCrashpadHistogramAllocatorName); | |
209 } else { | |
210 task_runner->PostTask( | |
bcwhite
2016/09/14 13:16:12
Comment: This file must be deleted here so it isn'
scottmg
2016/09/14 20:37:01
Done.
| |
211 FROM_HERE, | |
212 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
213 crashpad_metrics_file_prev, /*recursive=*/false)); | |
214 } | |
215 } | |
216 | |
175 #if defined(OS_WIN) | 217 #if defined(OS_WIN) |
176 // Read metrics file from setup.exe. | 218 // Read metrics file from setup.exe. |
177 base::FilePath program_dir; | 219 base::FilePath program_dir; |
178 base::PathService::Get(base::DIR_EXE, &program_dir); | 220 base::PathService::Get(base::DIR_EXE, &program_dir); |
179 file_metrics_provider->RegisterSource( | 221 file_metrics_provider->RegisterSource( |
180 program_dir.AppendASCII(installer::kSetupHistogramAllocatorName), | 222 program_dir.AppendASCII(installer::kSetupHistogramAllocatorName), |
181 metrics::FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_DIR, | 223 metrics::FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_DIR, |
182 metrics::FileMetricsProvider::ASSOCIATE_CURRENT_RUN, | 224 metrics::FileMetricsProvider::ASSOCIATE_CURRENT_RUN, |
183 installer::kSetupHistogramAllocatorName); | 225 installer::kSetupHistogramAllocatorName); |
184 #endif | 226 #endif |
185 | 227 |
186 return file_metrics_provider; | 228 return file_metrics_provider; |
187 } | 229 } |
188 | 230 |
189 // If there is a global metrics file being updated on disk, mark it to be | |
190 // deleted when the process exits. A normal shutdown is almost complete | |
191 // so there is no benefit in keeping a file with no new data to be processed | |
192 // during the next startup sequence. Deleting the file during shutdown adds | |
193 // an extra disk-access or two to shutdown but eliminates the unnecessary | |
194 // processing of the contents during startup only to find nothing. | |
195 void CleanUpGlobalPersistentHistogramStorage() { | |
196 base::GlobalHistogramAllocator* allocator = | |
197 base::GlobalHistogramAllocator::Get(); | |
198 if (!allocator) | |
199 return; | |
200 | |
201 const base::FilePath& path = allocator->GetPersistentLocation(); | |
202 if (path.empty()) | |
203 return; | |
204 | |
205 // Open (with delete) and then immediately close the file by going out of | |
206 // scope. This is the only cross-platform safe way to delete a file that may | |
207 // be open elsewhere. Open handles will continue to operate normally but | |
208 // new opens will not be possible. | |
209 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ | | |
210 base::File::FLAG_DELETE_ON_CLOSE); | |
211 } | |
212 | |
213 } // namespace | 231 } // namespace |
214 | 232 |
215 | 233 |
216 const char ChromeMetricsServiceClient::kBrowserMetricsName[] = "BrowserMetrics"; | 234 const char ChromeMetricsServiceClient::kBrowserMetricsName[] = "BrowserMetrics"; |
217 | 235 |
218 ChromeMetricsServiceClient::ChromeMetricsServiceClient( | 236 ChromeMetricsServiceClient::ChromeMetricsServiceClient( |
219 metrics::MetricsStateManager* state_manager) | 237 metrics::MetricsStateManager* state_manager) |
220 : metrics_state_manager_(state_manager), | 238 : metrics_state_manager_(state_manager), |
221 #if defined(OS_CHROMEOS) | 239 #if defined(OS_CHROMEOS) |
222 chromeos_metrics_provider_(nullptr), | 240 chromeos_metrics_provider_(nullptr), |
(...skipping 11 matching lines...) Expand all Loading... | |
234 start_time_(base::TimeTicks::Now()), | 252 start_time_(base::TimeTicks::Now()), |
235 has_uploaded_profiler_data_(false), | 253 has_uploaded_profiler_data_(false), |
236 weak_ptr_factory_(this) { | 254 weak_ptr_factory_(this) { |
237 DCHECK(thread_checker_.CalledOnValidThread()); | 255 DCHECK(thread_checker_.CalledOnValidThread()); |
238 RecordCommandLineMetrics(); | 256 RecordCommandLineMetrics(); |
239 RegisterForNotifications(); | 257 RegisterForNotifications(); |
240 } | 258 } |
241 | 259 |
242 ChromeMetricsServiceClient::~ChromeMetricsServiceClient() { | 260 ChromeMetricsServiceClient::~ChromeMetricsServiceClient() { |
243 DCHECK(thread_checker_.CalledOnValidThread()); | 261 DCHECK(thread_checker_.CalledOnValidThread()); |
244 CleanUpGlobalPersistentHistogramStorage(); | 262 base::CleanUpGlobalPersistentHistogramStorage(); |
245 } | 263 } |
246 | 264 |
247 // static | 265 // static |
248 std::unique_ptr<ChromeMetricsServiceClient> ChromeMetricsServiceClient::Create( | 266 std::unique_ptr<ChromeMetricsServiceClient> ChromeMetricsServiceClient::Create( |
249 metrics::MetricsStateManager* state_manager) { | 267 metrics::MetricsStateManager* state_manager) { |
250 // Perform two-phase initialization so that |client->metrics_service_| only | 268 // Perform two-phase initialization so that |client->metrics_service_| only |
251 // receives pointers to fully constructed objects. | 269 // receives pointers to fully constructed objects. |
252 std::unique_ptr<ChromeMetricsServiceClient> client( | 270 std::unique_ptr<ChromeMetricsServiceClient> client( |
253 new ChromeMetricsServiceClient(state_manager)); | 271 new ChromeMetricsServiceClient(state_manager)); |
254 client->Initialize(); | 272 client->Initialize(); |
255 | 273 |
256 return client; | 274 return client; |
257 } | 275 } |
258 | 276 |
259 // static | 277 // static |
260 void ChromeMetricsServiceClient::RegisterPrefs(PrefRegistrySimple* registry) { | 278 void ChromeMetricsServiceClient::RegisterPrefs(PrefRegistrySimple* registry) { |
261 metrics::MetricsService::RegisterPrefs(registry); | 279 metrics::MetricsService::RegisterPrefs(registry); |
262 metrics::StabilityMetricsHelper::RegisterPrefs(registry); | 280 metrics::StabilityMetricsHelper::RegisterPrefs(registry); |
263 | 281 |
264 RegisterInstallerFileMetricsPreferences(registry); | 282 RegisterFileMetricsPreferences(registry); |
265 | 283 |
266 metrics::RegisterMetricsReportingStatePrefs(registry); | 284 metrics::RegisterMetricsReportingStatePrefs(registry); |
267 | 285 |
268 #if BUILDFLAG(ANDROID_JAVA_UI) | 286 #if BUILDFLAG(ANDROID_JAVA_UI) |
269 AndroidMetricsProvider::RegisterPrefs(registry); | 287 AndroidMetricsProvider::RegisterPrefs(registry); |
270 #endif // BUILDFLAG(ANDROID_JAVA_UI) | 288 #endif // BUILDFLAG(ANDROID_JAVA_UI) |
271 | 289 |
272 #if defined(ENABLE_PLUGINS) | 290 #if defined(ENABLE_PLUGINS) |
273 PluginMetricsProvider::RegisterPrefs(registry); | 291 PluginMetricsProvider::RegisterPrefs(registry); |
274 #endif // defined(ENABLE_PLUGINS) | 292 #endif // defined(ENABLE_PLUGINS) |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 std::unique_ptr<metrics::MetricsProvider>( | 443 std::unique_ptr<metrics::MetricsProvider>( |
426 new ChromeStabilityMetricsProvider( | 444 new ChromeStabilityMetricsProvider( |
427 g_browser_process->local_state()))); | 445 g_browser_process->local_state()))); |
428 metrics_service_->RegisterMetricsProvider( | 446 metrics_service_->RegisterMetricsProvider( |
429 std::unique_ptr<metrics::MetricsProvider>( | 447 std::unique_ptr<metrics::MetricsProvider>( |
430 new metrics::GPUMetricsProvider)); | 448 new metrics::GPUMetricsProvider)); |
431 metrics_service_->RegisterMetricsProvider( | 449 metrics_service_->RegisterMetricsProvider( |
432 std::unique_ptr<metrics::MetricsProvider>( | 450 std::unique_ptr<metrics::MetricsProvider>( |
433 new metrics::ScreenInfoMetricsProvider)); | 451 new metrics::ScreenInfoMetricsProvider)); |
434 | 452 |
435 metrics_service_->RegisterMetricsProvider(CreateInstallerFileMetricsProvider( | 453 metrics_service_->RegisterMetricsProvider(CreateFileMetricsProvider( |
436 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled())); | 454 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled())); |
437 | 455 |
438 drive_metrics_provider_ = new metrics::DriveMetricsProvider( | 456 drive_metrics_provider_ = new metrics::DriveMetricsProvider( |
439 content::BrowserThread::GetTaskRunnerForThread( | 457 content::BrowserThread::GetTaskRunnerForThread( |
440 content::BrowserThread::FILE), | 458 content::BrowserThread::FILE), |
441 chrome::FILE_LOCAL_STATE); | 459 chrome::FILE_LOCAL_STATE); |
442 metrics_service_->RegisterMetricsProvider( | 460 metrics_service_->RegisterMetricsProvider( |
443 std::unique_ptr<metrics::MetricsProvider>(drive_metrics_provider_)); | 461 std::unique_ptr<metrics::MetricsProvider>(drive_metrics_provider_)); |
444 | 462 |
445 profiler_metrics_provider_ = new metrics::ProfilerMetricsProvider( | 463 profiler_metrics_provider_ = new metrics::ProfilerMetricsProvider( |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 } | 803 } |
786 } | 804 } |
787 | 805 |
788 void ChromeMetricsServiceClient::OnURLOpenedFromOmnibox(OmniboxLog* log) { | 806 void ChromeMetricsServiceClient::OnURLOpenedFromOmnibox(OmniboxLog* log) { |
789 metrics_service_->OnApplicationNotIdle(); | 807 metrics_service_->OnApplicationNotIdle(); |
790 } | 808 } |
791 | 809 |
792 bool ChromeMetricsServiceClient::IsUMACellularUploadLogicEnabled() { | 810 bool ChromeMetricsServiceClient::IsUMACellularUploadLogicEnabled() { |
793 return metrics::IsCellularLogicEnabled(); | 811 return metrics::IsCellularLogicEnabled(); |
794 } | 812 } |
OLD | NEW |