blob: 1e56a4dbf59b0b33fd7765a35009b35c64578994 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2017 The Chromium Authors
Steven Holte5c6dd632017-07-19 23:25:492// 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/field_trials_provider.h"
6
Yue Ru Sun1787d5a2020-02-25 20:00:507#include <string>
Helmut Januschkaff1c3ec2024-04-25 19:38:328#include <string_view>
Yue Ru Sun1787d5a2020-02-25 20:00:509#include <vector>
10
Steven Holte5c6dd632017-07-19 23:25:4911#include "components/variations/active_field_trials.h"
12#include "components/variations/synthetic_trial_registry.h"
Steven Holtef9d5ed62017-10-21 02:02:3013#include "third_party/metrics_proto/system_profile.pb.h"
Steven Holte5c6dd632017-07-19 23:25:4914
15namespace variations {
16
17namespace {
18
19void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
20 metrics::SystemProfileProto* system_profile) {
21 for (const ActiveGroupId& id : field_trial_ids) {
22 metrics::SystemProfileProto::FieldTrial* field_trial =
23 system_profile->add_field_trial();
24 field_trial->set_name_id(id.name);
25 field_trial->set_group_id(id.group);
26 }
27}
28
29} // namespace
30
Robert Kaplowd45b6202017-07-20 02:01:4631FieldTrialsProvider::FieldTrialsProvider(SyntheticTrialRegistry* registry,
Helmut Januschkaff1c3ec2024-04-25 19:38:3232 std::string_view suffix)
Robert Kaplowd45b6202017-07-20 02:01:4633 : registry_(registry), suffix_(suffix) {}
Steven Holte5c6dd632017-07-19 23:25:4934FieldTrialsProvider::~FieldTrialsProvider() = default;
35
36void FieldTrialsProvider::GetFieldTrialIds(
37 std::vector<ActiveGroupId>* field_trial_ids) const {
James Lee01bb2e92023-04-15 19:46:4038 // As the trial groups are included in metrics reports, we must not include
39 // the low anonymity trials.
James Lee514e2ef2023-04-18 15:33:3340 variations::GetFieldTrialActiveGroupIds(suffix_, field_trial_ids);
Steven Holte5c6dd632017-07-19 23:25:4941}
42
Alexei Svitkine70c95022019-08-21 18:13:2443void FieldTrialsProvider::ProvideSystemProfileMetrics(
44 metrics::SystemProfileProto* system_profile_proto) {
45 // ProvideSystemProfileMetricsWithLogCreationTime() should be called instead.
Peter Boström65b06922024-11-08 20:34:1246 NOTREACHED();
Steven Holte5c6dd632017-07-19 23:25:4947}
48
Alexei Svitkine70c95022019-08-21 18:13:2449void FieldTrialsProvider::ProvideSystemProfileMetricsWithLogCreationTime(
50 base::TimeTicks log_creation_time,
Steven Holte5c6dd632017-07-19 23:25:4951 metrics::SystemProfileProto* system_profile_proto) {
Alison Galeb8be9522024-04-16 00:00:3152 // TODO(crbug.com/40697205): Maybe call ProvideCurrentSessionData() instead
53 // from places in which ProvideSystemProfileMetricsWithLogCreationTime() is
54 // called, e.g. startup_data.cc and background_tracing_metrics_provider.cc.
Caitlin Fischerd0cd0b92020-06-03 12:10:4855
56 log_creation_time_ = log_creation_time;
57
Steven Holtea70ef7d2018-11-21 20:03:1658 const std::string& version = variations::GetSeedVersion();
59 if (!version.empty())
60 system_profile_proto->set_variations_seed_version(version);
Caitlin Fischerd0cd0b92020-06-03 12:10:4861
Alison Galeb8be9522024-04-16 00:00:3162 // TODO(crbug.com/40133600): Determine whether this can be deleted.
Caitlin Fischerd0cd0b92020-06-03 12:10:4863 GetAndWriteFieldTrials(system_profile_proto);
64}
65
66void FieldTrialsProvider::ProvideCurrentSessionData(
67 metrics::ChromeUserMetricsExtension* uma_proto) {
68 // This function is called from both
69 // ProvideSystemProfileMetricsWithLogCreationTime() and
70 // ProvideCurrentSessionData() so that field trials activated in other metrics
71 // providers are captured. We need both calls because in some scenarios in
72 // which this class is used, only the former function gets called.
73 DCHECK(!log_creation_time_.is_null());
74 GetAndWriteFieldTrials(uma_proto->mutable_system_profile());
75}
76
77void FieldTrialsProvider::SetLogCreationTimeForTesting(base::TimeTicks time) {
78 log_creation_time_ = time;
79}
80
81void FieldTrialsProvider::GetAndWriteFieldTrials(
82 metrics::SystemProfileProto* system_profile_proto) const {
83 system_profile_proto->clear_field_trial();
84
85 std::vector<ActiveGroupId> field_trials;
86 GetFieldTrialIds(&field_trials);
87 WriteFieldTrials(field_trials, system_profile_proto);
Robert Kaplowd45b6202017-07-20 02:01:4688
Mike Wittmancfd2f372022-03-09 02:30:1489 // May be null in tests.
Robert Kaplowd45b6202017-07-20 02:01:4690 if (registry_) {
Robert Kaplowd45b6202017-07-20 02:01:4691 std::vector<ActiveGroupId> synthetic_trials;
Caitlin Fischerd0cd0b92020-06-03 12:10:4892 registry_->GetSyntheticFieldTrialsOlderThan(log_creation_time_,
Luc Nguyend28c2022022-05-12 18:58:5793 &synthetic_trials, suffix_);
Robert Kaplowd45b6202017-07-20 02:01:4694 WriteFieldTrials(synthetic_trials, system_profile_proto);
95 }
Steven Holte5c6dd632017-07-19 23:25:4996}
97
98} // namespace variations