summaryrefslogtreecommitdiff
path: root/chromium/components/variations/synthetic_trial_registry.cc
blob: 28e162cd1a3c4e6d81aa9db5da90a642122fe55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/synthetic_trial_registry.h"

namespace variations {

SyntheticTrialRegistry::SyntheticTrialRegistry() = default;
SyntheticTrialRegistry::~SyntheticTrialRegistry() = default;

void SyntheticTrialRegistry::AddSyntheticTrialObserver(
    SyntheticTrialObserver* observer) {
  synthetic_trial_observer_list_.AddObserver(observer);
  if (!synthetic_trial_groups_.empty())
    observer->OnSyntheticTrialsChanged(synthetic_trial_groups_);
}

void SyntheticTrialRegistry::RemoveSyntheticTrialObserver(
    SyntheticTrialObserver* observer) {
  synthetic_trial_observer_list_.RemoveObserver(observer);
}

void SyntheticTrialRegistry::RegisterSyntheticFieldTrial(
    const SyntheticTrialGroup& trial) {
  for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
    if (synthetic_trial_groups_[i].id.name == trial.id.name) {
      if (synthetic_trial_groups_[i].id.group != trial.id.group) {
        synthetic_trial_groups_[i].id.group = trial.id.group;
        synthetic_trial_groups_[i].start_time = base::TimeTicks::Now();
        NotifySyntheticTrialObservers();
      }
      return;
    }
  }

  SyntheticTrialGroup trial_group = trial;
  trial_group.start_time = base::TimeTicks::Now();
  synthetic_trial_groups_.push_back(trial_group);
  NotifySyntheticTrialObservers();
}

void SyntheticTrialRegistry::RegisterSyntheticMultiGroupFieldTrial(
    uint32_t trial_name_hash,
    const std::vector<uint32_t>& group_name_hashes) {
  auto has_same_trial_name = [trial_name_hash](const SyntheticTrialGroup& x) {
    return x.id.name == trial_name_hash;
  };
  synthetic_trial_groups_.erase(
      std::remove_if(synthetic_trial_groups_.begin(),
                     synthetic_trial_groups_.end(), has_same_trial_name),
      synthetic_trial_groups_.end());

  if (group_name_hashes.empty())
    return;

  SyntheticTrialGroup trial_group(trial_name_hash, group_name_hashes[0]);
  trial_group.start_time = base::TimeTicks::Now();
  for (uint32_t group_name_hash : group_name_hashes) {
    // Note: Adding the trial group will copy it, so this re-uses the same
    // |trial_group| struct for convenience (e.g. so start_time's all match).
    trial_group.id.group = group_name_hash;
    synthetic_trial_groups_.push_back(trial_group);
  }
  NotifySyntheticTrialObservers();
}

void SyntheticTrialRegistry::NotifySyntheticTrialObservers() {
  for (SyntheticTrialObserver& observer : synthetic_trial_observer_list_) {
    observer.OnSyntheticTrialsChanged(synthetic_trial_groups_);
  }
}

void SyntheticTrialRegistry::GetSyntheticFieldTrialsOlderThan(
    base::TimeTicks time,
    std::vector<ActiveGroupId>* synthetic_trials) {
  DCHECK(synthetic_trials);
  synthetic_trials->clear();
  for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
    if (synthetic_trial_groups_[i].start_time <= time)
      synthetic_trials->push_back(synthetic_trial_groups_[i].id);
  }
}

}  // namespace variations