Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / metrics / drive_metrics_provider.cc
blob22ad08919194b2376b0061560f2c051f8fbbcc37
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/drive_metrics_provider.h"
7 #include "base/base_paths.h"
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/files/file_path.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/path_service.h"
15 #include "base/task_runner_util.h"
16 #include "base/time/time.h"
18 namespace metrics {
20 DriveMetricsProvider::DriveMetricsProvider(
21 scoped_refptr<base::SequencedTaskRunner> file_thread,
22 int local_state_path_key)
23 : file_thread_(file_thread),
24 local_state_path_key_(local_state_path_key),
25 weak_ptr_factory_(this) {}
27 DriveMetricsProvider::~DriveMetricsProvider() {}
29 void DriveMetricsProvider::ProvideSystemProfileMetrics(
30 metrics::SystemProfileProto* system_profile_proto) {
31 auto* hardware = system_profile_proto->mutable_hardware();
32 FillDriveMetrics(metrics_.app_drive, hardware->mutable_app_drive());
33 FillDriveMetrics(metrics_.user_data_drive,
34 hardware->mutable_user_data_drive());
37 void DriveMetricsProvider::GetDriveMetrics(const base::Closure& done_callback) {
38 base::PostTaskAndReplyWithResult(
39 file_thread_.get(), FROM_HERE,
40 base::Bind(&DriveMetricsProvider::GetDriveMetricsOnFileThread,
41 local_state_path_key_),
42 base::Bind(&DriveMetricsProvider::GotDriveMetrics,
43 weak_ptr_factory_.GetWeakPtr(), done_callback));
46 DriveMetricsProvider::SeekPenaltyResponse::SeekPenaltyResponse()
47 : success(false) {}
49 // static
50 DriveMetricsProvider::DriveMetrics
51 DriveMetricsProvider::GetDriveMetricsOnFileThread(int local_state_path_key) {
52 DriveMetricsProvider::DriveMetrics metrics;
53 QuerySeekPenalty(base::FILE_EXE, &metrics.app_drive);
54 QuerySeekPenalty(local_state_path_key, &metrics.user_data_drive);
55 return metrics;
58 // static
59 void DriveMetricsProvider::QuerySeekPenalty(
60 int path_service_key,
61 DriveMetricsProvider::SeekPenaltyResponse* response) {
62 DCHECK(response);
64 base::FilePath path;
65 if (!PathService::Get(path_service_key, &path))
66 return;
68 base::TimeTicks start = base::TimeTicks::Now();
70 response->success = HasSeekPenalty(path, &response->has_seek_penalty);
72 UMA_HISTOGRAM_TIMES("Hardware.Drive.HasSeekPenalty_Time",
73 base::TimeTicks::Now() - start);
74 UMA_HISTOGRAM_BOOLEAN("Hardware.Drive.HasSeekPenalty_Success",
75 response->success);
76 if (response->success) {
77 UMA_HISTOGRAM_BOOLEAN("Hardware.Drive.HasSeekPenalty",
78 response->has_seek_penalty);
82 void DriveMetricsProvider::GotDriveMetrics(
83 const base::Closure& done_callback,
84 const DriveMetricsProvider::DriveMetrics& metrics) {
85 DCHECK(thread_checker_.CalledOnValidThread());
86 metrics_ = metrics;
87 done_callback.Run();
90 void DriveMetricsProvider::FillDriveMetrics(
91 const DriveMetricsProvider::SeekPenaltyResponse& response,
92 metrics::SystemProfileProto::Hardware::Drive* drive) {
93 if (response.success)
94 drive->set_has_seek_penalty(response.has_seek_penalty);
97 } // namespace metrics