summaryrefslogtreecommitdiff
path: root/chromium/components/variations/variations_id_collection.cc
blob: e48bcfabc2212c0abee2599f6e4cc3a3a755b2e6 (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
// 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/variations_id_collection.h"

#include "base/callback_helpers.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/stl_util.h"
#include "base/time/time.h"
#include "components/variations/active_field_trials.h"

namespace variations {

VariationsIdCollection::VariationsIdCollection(
    IDCollectionKey collection_key,
    base::RepeatingCallback<void(VariationID)> new_id_callback)
    : collection_key_(collection_key) {
  base::FieldTrialList::AddObserver(this);

  base::FieldTrial::ActiveGroups initial_groups;
  base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
  for (const base::FieldTrial::ActiveGroup& group : initial_groups) {
    OnFieldTrialGroupFinalized(group.trial_name, group.group_name);
  }

  // Delay setting |new_id_callback_| until initialization is over.
  new_id_callback_ = new_id_callback;
}

VariationsIdCollection::~VariationsIdCollection() {
  base::FieldTrialList::RemoveObserver(this);
}

void VariationsIdCollection::OnFieldTrialGroupFinalized(
    const std::string& trial_name,
    const std::string& group_name) {
  const VariationID id =
      GetGoogleVariationID(collection_key_, trial_name, group_name);
  if (id != EMPTY_ID) {
    bool modified = id_set_.insert(id).second;
    if (modified && new_id_callback_)
      new_id_callback_.Run(id);
  }
}

const std::set<VariationID>& VariationsIdCollection::GetIds() {
  return id_set_;
}

}  // namespace variations