• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 #ifndef COMPONENTS_METRICS_FIELD_TRIALS_PROVIDER_H_
6 #define COMPONENTS_METRICS_FIELD_TRIALS_PROVIDER_H_
7 
8 #include <string_view>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/time/time.h"
12 #include "components/metrics/metrics_provider.h"
13 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
14 
15 // TODO(crbug.com/41187035): Once MetricsProvider/SystemProfileProto are moved
16 // into
17 // //services/metrics, then //components/variations can depend on them, and
18 // this should be moved there.
19 namespace variations {
20 
21 class SyntheticTrialRegistry;
22 struct ActiveGroupId;
23 
24 class FieldTrialsProvider : public metrics::MetricsProvider {
25  public:
26   // |registry| must outlive this metrics provider.
27   FieldTrialsProvider(SyntheticTrialRegistry* registry,
28                       std::string_view suffix);
29 
30   FieldTrialsProvider(const FieldTrialsProvider&) = delete;
31   FieldTrialsProvider& operator=(const FieldTrialsProvider&) = delete;
32 
33   ~FieldTrialsProvider() override;
34 
35   // metrics::MetricsProvider:
36   void ProvideSystemProfileMetrics(
37       metrics::SystemProfileProto* system_profile_proto) override;
38   void ProvideSystemProfileMetricsWithLogCreationTime(
39       base::TimeTicks log_creation_time,
40       metrics::SystemProfileProto* system_profile_proto) override;
41   void ProvideCurrentSessionData(
42       metrics::ChromeUserMetricsExtension* uma_proto) override;
43 
44   // Sets |log_creation_time_| to |time|.
45   void SetLogCreationTimeForTesting(base::TimeTicks time);
46 
47  private:
48   // Populates |field_trial_ids| with currently active field trials groups. The
49   // trial and group names are suffixed with |suffix_| before being hashed.
50   void GetFieldTrialIds(std::vector<ActiveGroupId>* field_trial_ids) const;
51 
52   // Gets active FieldTrials and SyntheticFieldTrials and populates
53   // |system_profile_proto| with them.
54   void GetAndWriteFieldTrials(
55       metrics::SystemProfileProto* system_profile_proto) const;
56 
57   // The most recent time passed to
58   // ProvideSystemProfileMetricsWithLogCreationTime().
59   base::TimeTicks log_creation_time_;
60 
61   raw_ptr<SyntheticTrialRegistry> registry_;
62 
63   // Suffix used for the field trial names before they are hashed for uploads.
64   std::string suffix_;
65 };
66 
67 }  // namespace variations
68 
69 #endif  // COMPONENTS_METRICS_FIELD_TRIALS_PROVIDER_H_
70