• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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/persistent_synthetic_trial_observer.h"
6 
7 #include "base/task/sequenced_task_runner.h"
8 #include "components/metrics/persistent_system_profile.h"
9 #include "components/variations/synthetic_trials.h"
10 
11 namespace metrics {
12 namespace {
13 
PersistSyntheticFieldTrials(const std::vector<variations::SyntheticTrialGroup> & trials_updated,const std::vector<variations::SyntheticTrialGroup> & trials_removed)14 void PersistSyntheticFieldTrials(
15     const std::vector<variations::SyntheticTrialGroup>& trials_updated,
16     const std::vector<variations::SyntheticTrialGroup>& trials_removed) {
17   // Mark all the updated synthetic field trials in the persistent profile,
18   // update the removed trials first, then add the updated trials.
19   // TODO: crbug.com/345445618 - Handle kNextLog annotation mode properly, and
20   // when the annotation mode changes.
21   for (const auto& removed : trials_removed) {
22     metrics::GlobalPersistentSystemProfile::GetInstance()->RemoveFieldTrial(
23         removed.trial_name());
24   }
25   for (const auto& updated : trials_updated) {
26     if (updated.annotation_mode() ==
27         variations::SyntheticTrialAnnotationMode::kCurrentLog) {
28       metrics::GlobalPersistentSystemProfile::GetInstance()->AddFieldTrial(
29           updated.trial_name(), updated.group_name());
30     }
31   }
32 }
33 
34 }  // namespace
35 
PersistentSyntheticTrialObserver()36 PersistentSyntheticTrialObserver::PersistentSyntheticTrialObserver()
37     : task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {}
38 
39 PersistentSyntheticTrialObserver::~PersistentSyntheticTrialObserver() = default;
40 
OnSyntheticTrialsChanged(const std::vector<variations::SyntheticTrialGroup> & trials_updated,const std::vector<variations::SyntheticTrialGroup> & trials_removed,const std::vector<variations::SyntheticTrialGroup> & groups)41 void PersistentSyntheticTrialObserver::OnSyntheticTrialsChanged(
42     const std::vector<variations::SyntheticTrialGroup>& trials_updated,
43     const std::vector<variations::SyntheticTrialGroup>& trials_removed,
44     const std::vector<variations::SyntheticTrialGroup>& groups) {
45   // Synthetic trials may be registered in any task runner, so switch to correct
46   // task runner before storing.
47   if (task_runner_->RunsTasksInCurrentSequence()) {
48     PersistSyntheticFieldTrials(trials_updated, trials_removed);
49   } else {
50     task_runner_->PostTask(
51         FROM_HERE, base::BindOnce(&PersistSyntheticFieldTrials, trials_updated,
52                                   trials_removed));
53   }
54 }
55 
56 }  // namespace metrics
57