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/files/file_path.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/path_service.h"
14 #include "base/task_runner_util.h"
15 #include "base/time/time.h"
19 DriveMetricsProvider::DriveMetricsProvider(
20 scoped_refptr
<base::SequencedTaskRunner
> file_thread
,
21 int local_state_path_key
)
22 : file_thread_(file_thread
),
23 local_state_path_key_(local_state_path_key
),
24 weak_ptr_factory_(this) {}
26 DriveMetricsProvider::~DriveMetricsProvider() {}
28 void DriveMetricsProvider::ProvideSystemProfileMetrics(
29 metrics::SystemProfileProto
* system_profile_proto
) {
30 auto* hardware
= system_profile_proto
->mutable_hardware();
31 FillDriveMetrics(metrics_
.app_drive
, hardware
->mutable_app_drive());
32 FillDriveMetrics(metrics_
.user_data_drive
,
33 hardware
->mutable_user_data_drive());
36 void DriveMetricsProvider::GetDriveMetrics(const base::Closure
& done
) {
37 got_metrics_callback_
= done
;
39 base::PostTaskAndReplyWithResult(
40 file_thread_
.get(), FROM_HERE
,
41 base::Bind(&DriveMetricsProvider::GetDriveMetricsOnFileThread
,
42 local_state_path_key_
),
43 base::Bind(&DriveMetricsProvider::GotDriveMetrics
,
44 weak_ptr_factory_
.GetWeakPtr()));
47 DriveMetricsProvider::SeekPenaltyResponse::SeekPenaltyResponse()
51 DriveMetricsProvider::DriveMetrics
52 DriveMetricsProvider::GetDriveMetricsOnFileThread(int local_state_path_key
) {
53 DriveMetricsProvider::DriveMetrics metrics
;
54 QuerySeekPenalty(base::FILE_EXE
, &metrics
.app_drive
);
55 QuerySeekPenalty(local_state_path_key
, &metrics
.user_data_drive
);
60 void DriveMetricsProvider::QuerySeekPenalty(
62 DriveMetricsProvider::SeekPenaltyResponse
* response
) {
66 if (!PathService::Get(path_service_key
, &path
))
69 base::TimeTicks start
= base::TimeTicks::Now();
71 response
->success
= HasSeekPenalty(path
, &response
->has_seek_penalty
);
73 UMA_HISTOGRAM_TIMES("Hardware.Drive.HasSeekPenalty_Time",
74 base::TimeTicks::Now() - start
);
75 UMA_HISTOGRAM_BOOLEAN("Hardware.Drive.HasSeekPenalty_Success",
77 if (response
->success
) {
78 UMA_HISTOGRAM_BOOLEAN("Hardware.Drive.HasSeekPenalty",
79 response
->has_seek_penalty
);
83 void DriveMetricsProvider::GotDriveMetrics(
84 const DriveMetricsProvider::DriveMetrics
& metrics
) {
85 DCHECK(thread_checker_
.CalledOnValidThread());
87 got_metrics_callback_
.Run();
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