• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/drive_metrics_provider.h"
6 
7 #include "base/base_paths.h"
8 #include "base/check_op.h"
9 #include "base/files/file_path.h"
10 #include "base/functional/bind.h"
11 #include "base/functional/callback.h"
12 #include "base/location.h"
13 #include "base/path_service.h"
14 #include "base/task/task_traits.h"
15 #include "base/task/thread_pool.h"
16 #include "base/threading/scoped_blocking_call.h"
17 
18 namespace metrics {
19 
DriveMetricsProvider(int local_state_path_key)20 DriveMetricsProvider::DriveMetricsProvider(int local_state_path_key)
21     : local_state_path_key_(local_state_path_key) {}
22 
~DriveMetricsProvider()23 DriveMetricsProvider::~DriveMetricsProvider() {}
24 
ProvideSystemProfileMetrics(metrics::SystemProfileProto * system_profile_proto)25 void DriveMetricsProvider::ProvideSystemProfileMetrics(
26     metrics::SystemProfileProto* system_profile_proto) {
27   auto* hardware = system_profile_proto->mutable_hardware();
28   FillDriveMetrics(metrics_.app_drive, hardware->mutable_app_drive());
29   FillDriveMetrics(metrics_.user_data_drive,
30                    hardware->mutable_user_data_drive());
31 }
32 
AsyncInit(base::OnceClosure done_callback)33 void DriveMetricsProvider::AsyncInit(base::OnceClosure done_callback) {
34   base::ThreadPool::PostTaskAndReplyWithResult(
35       FROM_HERE,
36       {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
37        base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
38       base::BindOnce(&DriveMetricsProvider::GetDriveMetricsOnBackgroundThread,
39                      local_state_path_key_),
40       base::BindOnce(&DriveMetricsProvider::GotDriveMetrics,
41                      weak_ptr_factory_.GetWeakPtr(), std::move(done_callback)));
42 }
43 
SeekPenaltyResponse()44 DriveMetricsProvider::SeekPenaltyResponse::SeekPenaltyResponse()
45     : success(false) {}
46 
47 // static
48 DriveMetricsProvider::DriveMetrics
GetDriveMetricsOnBackgroundThread(int local_state_path_key)49 DriveMetricsProvider::GetDriveMetricsOnBackgroundThread(
50     int local_state_path_key) {
51   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
52                                                 base::BlockingType::WILL_BLOCK);
53 
54   DriveMetricsProvider::DriveMetrics metrics;
55   QuerySeekPenalty(base::FILE_EXE, &metrics.app_drive);
56   QuerySeekPenalty(local_state_path_key, &metrics.user_data_drive);
57   return metrics;
58 }
59 
60 // static
QuerySeekPenalty(int path_service_key,DriveMetricsProvider::SeekPenaltyResponse * response)61 void DriveMetricsProvider::QuerySeekPenalty(
62     int path_service_key,
63     DriveMetricsProvider::SeekPenaltyResponse* response) {
64   DCHECK(response);
65 
66   base::FilePath path;
67   if (!base::PathService::Get(path_service_key, &path))
68     return;
69 
70   response->success = HasSeekPenalty(path, &response->has_seek_penalty);
71 }
72 
GotDriveMetrics(base::OnceClosure done_callback,const DriveMetricsProvider::DriveMetrics & metrics)73 void DriveMetricsProvider::GotDriveMetrics(
74     base::OnceClosure done_callback,
75     const DriveMetricsProvider::DriveMetrics& metrics) {
76   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
77   metrics_ = metrics;
78   std::move(done_callback).Run();
79 }
80 
FillDriveMetrics(const DriveMetricsProvider::SeekPenaltyResponse & response,metrics::SystemProfileProto::Hardware::Drive * drive)81 void DriveMetricsProvider::FillDriveMetrics(
82     const DriveMetricsProvider::SeekPenaltyResponse& response,
83     metrics::SystemProfileProto::Hardware::Drive* drive) {
84   if (response.success)
85     drive->set_has_seek_penalty(response.has_seek_penalty);
86 }
87 
88 }  // namespace metrics
89