Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: chrome/common/metrics/experiments_helper.cc

Issue 10165014: Add and implement API to associate Google experiment IDs with FieldTrials. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: added time variable Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/metrics/experiments_helper.h" 5 #include "chrome/common/metrics/experiments_helper.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/metrics/field_trial.h" 9 #include "base/memory/singleton.h"
10 #include "base/string16.h" 10 #include "base/string16.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/common/child_process_logging.h" 13 #include "chrome/common/child_process_logging.h"
14 14
15 namespace ExperimentsHelper { 15 namespace {
16
17 // We need to pass a Compare class to the std::map template since NameGroupId
18 // is a user-defined type.
19 struct NameGroupIdCompare {
20 bool operator() (const base::FieldTrial::NameGroupId& lhs,
21 const base::FieldTrial::NameGroupId& rhs) const {
22 // The group and name fields are just SHA-1 Hashes, so we just need to treat
23 // them as IDs and do a less-than comparison. We test group first, since
24 // name is more likely to collide.
25 return lhs.group == rhs.group ? lhs.name < rhs.name :
26 lhs.group < rhs.group;
27 }
28 };
29
30 // The internal singleton accessor for the map, used to keep it thread-safe.
31 class GroupMapAccessor {
32 public:
33 // Retrieve the singleton.
34 static GroupMapAccessor* GetInstance() {
35 return Singleton<GroupMapAccessor>::get();
36 }
37
38 GroupMapAccessor() {}
39 ~GroupMapAccessor() {}
40
41 void AssociateID(const base::FieldTrial::NameGroupId& group_identifier,
42 experiments_helper::GoogleExperimentID id) {
43 base::AutoLock scoped_lock(lock_);
44 DCHECK(group_to_id_map_.find(group_identifier) == group_to_id_map_.end()) <<
45 "You can associate a group with a GoogleExperimentID only once.";
46 group_to_id_map_[group_identifier] = id;
47 }
48
49 experiments_helper::GoogleExperimentID GetID(
50 const base::FieldTrial::NameGroupId& group_identifier) {
51 base::AutoLock scoped_lock(lock_);
52 GroupToIDMap::const_iterator it = group_to_id_map_.find(group_identifier);
53 if (it == group_to_id_map_.end())
54 return experiments_helper::kEmptyGoogleExperimentID;
55 return it->second;
56 }
57
58 private:
59 typedef std::map<base::FieldTrial::NameGroupId,
60 experiments_helper::GoogleExperimentID, NameGroupIdCompare> GroupToIDMap;
61
62 base::Lock lock_;
63 GroupToIDMap group_to_id_map_;
64 };
65
66 } // namespace
67
68 namespace experiments_helper {
69
70 const GoogleExperimentID kEmptyGoogleExperimentID = 0;
71
72 void AssociateGoogleExperimentID(
73 const base::FieldTrial::NameGroupId& group_identifier,
74 GoogleExperimentID id) {
75 GroupMapAccessor::GetInstance()->AssociateID(group_identifier, id);
76 }
77
78 GoogleExperimentID GetGoogleExperimentID(
79 const base::FieldTrial::NameGroupId& group_identifier) {
80 return GroupMapAccessor::GetInstance()->GetID(group_identifier);
81 }
16 82
17 void SetChildProcessLoggingExperimentList() { 83 void SetChildProcessLoggingExperimentList() {
18 std::vector<base::FieldTrial::NameGroupId> name_group_ids; 84 std::vector<base::FieldTrial::NameGroupId> name_group_ids;
19 base::FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids); 85 base::FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids);
20 std::vector<string16> experiment_strings(name_group_ids.size()); 86 std::vector<string16> experiment_strings(name_group_ids.size());
21 for (size_t i = 0; i < name_group_ids.size(); ++i) { 87 for (size_t i = 0; i < name_group_ids.size(); ++i) {
22 // Wish there was a StringPrintf for string16... :-( 88 // Wish there was a StringPrintf for string16... :-(
23 experiment_strings[i] = WideToUTF16(base::StringPrintf( 89 experiment_strings[i] = WideToUTF16(base::StringPrintf(
24 L"%x-%x", name_group_ids[i].name, name_group_ids[i].group)); 90 L"%x-%x", name_group_ids[i].name, name_group_ids[i].group));
25 } 91 }
26 child_process_logging::SetExperimentList(experiment_strings); 92 child_process_logging::SetExperimentList(experiment_strings);
27 } 93 }
28 94
29 } // namespace ExperimentsHelper 95 } // namespace experiments_helper
OLDNEW
« no previous file with comments | « chrome/common/metrics/experiments_helper.h ('k') | chrome/common/metrics/experiments_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698