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"
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"
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()
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
);
59 void DriveMetricsProvider::QuerySeekPenalty(
61 DriveMetricsProvider::SeekPenaltyResponse
* response
) {
65 if (!PathService::Get(path_service_key
, &path
))
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",
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());
90 void DriveMetricsProvider::FillDriveMetrics(
91 const DriveMetricsProvider::SeekPenaltyResponse
& response
,
92 metrics::SystemProfileProto::Hardware::Drive
* drive
) {
94 drive
->set_has_seek_penalty(response
.has_seek_penalty
);
97 } // namespace metrics