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.
5 #ifndef COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_
6 #define COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_
8 #include "base/functional/callback_forward.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/sequence_checker.h"
12 #include "components/metrics/metrics_provider.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
21 // Provides metrics about the local drives on a user's computer. Currently only
22 // checks to see if they incur a seek-time penalty (e.g. if they're SSDs).
23 class DriveMetricsProvider : public metrics::MetricsProvider {
25 explicit DriveMetricsProvider(int local_state_path_key);
27 DriveMetricsProvider(const DriveMetricsProvider&) = delete;
28 DriveMetricsProvider& operator=(const DriveMetricsProvider&) = delete;
30 ~DriveMetricsProvider() override;
32 // metrics::MetricsProvider:
33 void AsyncInit(base::OnceClosure done_callback) override;
34 void ProvideSystemProfileMetrics(
35 metrics::SystemProfileProto* system_profile_proto) override;
38 FRIEND_TEST_ALL_PREFIXES(DriveMetricsProviderTest, HasSeekPenalty);
40 // A response to querying a drive as to whether it incurs a seek penalty.
41 // |has_seek_penalty| is set if |success| is true.
42 struct SeekPenaltyResponse {
43 SeekPenaltyResponse();
45 bool has_seek_penalty;
49 SeekPenaltyResponse app_drive;
50 SeekPenaltyResponse user_data_drive;
53 // Determine whether the device that services |path| has a seek penalty.
54 // Returns false if it couldn't be determined (e.g., |path| doesn't exist).
55 static bool HasSeekPenalty(const base::FilePath& path,
56 bool* has_seek_penalty);
58 // Gather metrics about various drives. Should be run on a background thread.
59 static DriveMetrics GetDriveMetricsOnBackgroundThread(
60 int local_state_path_key);
62 // Tries to determine whether there is a penalty for seeking on the drive that
63 // hosts |path_service_key| (for example: the drive that holds "Local State").
64 static void QuerySeekPenalty(int path_service_key,
65 SeekPenaltyResponse* response);
67 // Called when metrics are done being gathered asynchronously.
68 // |done_callback| is the callback that should be called once all metrics are
70 void GotDriveMetrics(base::OnceClosure done_callback,
71 const DriveMetrics& metrics);
73 // Fills |drive| with information from successful |response|s.
74 void FillDriveMetrics(const SeekPenaltyResponse& response,
75 metrics::SystemProfileProto::Hardware::Drive* drive);
77 // The key to give to base::PathService to obtain the path to local state
78 // (supplied by the embedder).
79 int local_state_path_key_;
81 // Information gathered about various important drives.
82 DriveMetrics metrics_;
84 SEQUENCE_CHECKER(sequence_checker_);
85 base::WeakPtrFactory<DriveMetricsProvider> weak_ptr_factory_{this};
88 } // namespace metrics
90 #endif // COMPONENTS_METRICS_DRIVE_METRICS_PROVIDER_H_