summaryrefslogtreecommitdiffstats
path: root/chrome/common/metrics/experiments_helper.cc
blob: 3bc3aee3a907f8e1c0debbfc5d96af947397bc5f (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2012 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 "chrome/common/metrics/experiments_helper.h"

#include <vector>

#include "base/memory/singleton.h"
#include "base/sha1.h"
#include "base/string16.h"
#include "base/stringprintf.h"
#include "base/sys_byteorder.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/child_process_logging.h"
#include "chrome/common/metrics/variation_ids.h"

namespace {

// The internal singleton accessor for the map, used to keep it thread-safe.
class GroupMapAccessor {
 public:
  // Retrieve the singleton.
  static GroupMapAccessor* GetInstance() {
    return Singleton<GroupMapAccessor>::get();
  }

  GroupMapAccessor() {}
  ~GroupMapAccessor() {}

  // Note that this normally only sets the ID for a group the first time, unless
  // |force| is set to true, in which case it will always override it.
  void AssociateID(const experiments_helper::SelectedGroupId& group_identifier,
                   chrome_variations::ID id,
                   const bool force) {
    base::AutoLock scoped_lock(lock_);
    if (force ||
        group_to_id_map_.find(group_identifier) == group_to_id_map_.end())
      group_to_id_map_[group_identifier] = id;
  }

  chrome_variations::ID GetID(
      const experiments_helper::SelectedGroupId& group_identifier) {
    base::AutoLock scoped_lock(lock_);
    GroupToIDMap::const_iterator it = group_to_id_map_.find(group_identifier);
    if (it == group_to_id_map_.end())
      return chrome_variations::kEmptyID;
    return it->second;
  }

 private:
  typedef std::map<experiments_helper::SelectedGroupId,
      chrome_variations::ID,
      experiments_helper::SelectedGroupIdCompare> GroupToIDMap;

  base::Lock lock_;
  GroupToIDMap group_to_id_map_;
};

// Creates unique identifier for the trial by hashing a name string, whether
// it's for the field trial or the group name.
uint32 HashName(const std::string& name) {
  // SHA-1 is designed to produce a uniformly random spread in its output space,
  // even for nearly-identical inputs.
  unsigned char sha1_hash[base::kSHA1Length];
  base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()),
                      name.size(),
                      sha1_hash);

  COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data);
  uint32 bits;
  memcpy(&bits, sha1_hash, sizeof(bits));

  return base::ByteSwapToLE32(bits);
}

experiments_helper::SelectedGroupId MakeSelectedGroupId(
    const std::string& trial_name,
    const std::string& group_name) {
  experiments_helper::SelectedGroupId id;
  id.name = HashName(trial_name);
  id.group = HashName(group_name);
  return id;
}

// Populates |name_group_ids| based on |selected_groups|.
void GetFieldTrialSelectedGroupIdsForSelectedGroups(
    const base::FieldTrial::SelectedGroups& selected_groups,
    std::vector<experiments_helper::SelectedGroupId>* name_group_ids) {
  DCHECK(name_group_ids->empty());
  for (base::FieldTrial::SelectedGroups::const_iterator it =
       selected_groups.begin(); it != selected_groups.end(); ++it) {
    name_group_ids->push_back(MakeSelectedGroupId(it->trial, it->group));
  }
}

}  // namespace

namespace experiments_helper {

void GetFieldTrialSelectedGroupIds(
    std::vector<SelectedGroupId>* name_group_ids) {
  DCHECK(name_group_ids->empty());
  // A note on thread safety: Since GetFieldTrialSelectedGroups is thread
  // safe, and we operate on a separate list of that data, this function is
  // technically thread safe as well, with respect to the FieldTriaList data.
  base::FieldTrial::SelectedGroups selected_groups;
  base::FieldTrialList::GetFieldTrialSelectedGroups(&selected_groups);
  GetFieldTrialSelectedGroupIdsForSelectedGroups(selected_groups,
                                                 name_group_ids);
}

void AssociateGoogleVariationID(const std::string& trial_name,
                                const std::string& group_name,
                                chrome_variations::ID id) {
  GroupMapAccessor::GetInstance()->AssociateID(
      MakeSelectedGroupId(trial_name, group_name), id, false);
}

void AssociateGoogleVariationIDForce(const std::string& trial_name,
                                     const std::string& group_name,
                                     chrome_variations::ID id) {
  GroupMapAccessor::GetInstance()->AssociateID(
      MakeSelectedGroupId(trial_name, group_name), id, true);
}

chrome_variations::ID GetGoogleVariationID(const std::string& trial_name,
                                           const std::string& group_name) {
  return GroupMapAccessor::GetInstance()->GetID(
      MakeSelectedGroupId(trial_name, group_name));
}

void SetChildProcessLoggingExperimentList() {
  std::vector<SelectedGroupId> name_group_ids;
  GetFieldTrialSelectedGroupIds(&name_group_ids);
  std::vector<string16> experiment_strings(name_group_ids.size());
  for (size_t i = 0; i < name_group_ids.size(); ++i) {
    // Wish there was a StringPrintf for string16... :-(
    experiment_strings[i] = WideToUTF16(base::StringPrintf(
        L"%x-%x", name_group_ids[i].name, name_group_ids[i].group));
  }
  child_process_logging::SetExperimentList(experiment_strings);
}

}  // namespace experiments_helper

// Functions below are exposed for testing explicitly behind this namespace.
// They simply wrap existing functions in this file.
namespace testing {

void TestGetFieldTrialSelectedGroupIdsForSelectedGroups(
    const base::FieldTrial::SelectedGroups& selected_groups,
    std::vector<experiments_helper::SelectedGroupId>* name_group_ids) {
  ::GetFieldTrialSelectedGroupIdsForSelectedGroups(selected_groups,
                                                   name_group_ids);
}

uint32 TestHashName(const std::string& name) {
  return ::HashName(name);
}

}  // namespace testing