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

Side by Side Diff: base/feature_list.cc

Issue 1306653004: Expand FeatureList to support FieldTrial association. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 5 years, 2 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
« no previous file with comments | « base/feature_list.h ('k') | base/feature_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/feature_list.h" 5 #include "base/feature_list.h"
6 6
7 #include <utility>
7 #include <vector> 8 #include <vector>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
11 13
12 namespace base { 14 namespace base {
13 15
14 namespace { 16 namespace {
15 17
16 // Pointer to the FeatureList instance singleton that was set via 18 // Pointer to the FeatureList instance singleton that was set via
17 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to 19 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to
18 // have more control over initialization timing. Leaky. 20 // have more control over initialization timing. Leaky.
19 FeatureList* g_instance = nullptr; 21 FeatureList* g_instance = nullptr;
(...skipping 10 matching lines...) Expand all
30 FeatureList::~FeatureList() {} 32 FeatureList::~FeatureList() {}
31 33
32 void FeatureList::InitializeFromCommandLine( 34 void FeatureList::InitializeFromCommandLine(
33 const std::string& enable_features, 35 const std::string& enable_features,
34 const std::string& disable_features) { 36 const std::string& disable_features) {
35 DCHECK(!initialized_); 37 DCHECK(!initialized_);
36 38
37 // Process disabled features first, so that disabled ones take precedence over 39 // Process disabled features first, so that disabled ones take precedence over
38 // enabled ones (since RegisterOverride() uses insert()). 40 // enabled ones (since RegisterOverride() uses insert()).
39 for (const auto& feature_name : SplitFeatureListString(disable_features)) { 41 for (const auto& feature_name : SplitFeatureListString(disable_features)) {
40 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE); 42 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE, nullptr);
41 } 43 }
42 for (const auto& feature_name : SplitFeatureListString(enable_features)) { 44 for (const auto& feature_name : SplitFeatureListString(enable_features)) {
43 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE); 45 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE, nullptr);
44 } 46 }
45 } 47 }
46 48
49 bool FeatureList::IsFeatureOverriddenFromCommandLine(
50 const std::string& feature_name,
51 OverrideState state) const {
52 auto it = overrides_.find(feature_name);
53 return it != overrides_.end() && it->second.overridden_state == state &&
54 !it->second.overridden_by_field_trial;
55 }
56
57 void FeatureList::RegisterFieldTrialOverride(const std::string& feature_name,
58 OverrideState override_state,
59 FieldTrial* field_trial) {
60 DCHECK(field_trial);
61 DCHECK(!ContainsKey(overrides_, feature_name) ||
62 !overrides_.find(feature_name)->second.field_trial)
63 << "Feature " << feature_name
64 << " has conflicting field trial overrides: "
65 << overrides_.find(feature_name)->second.field_trial->trial_name()
66 << " / " << field_trial->trial_name();
67
68 RegisterOverride(feature_name, override_state, field_trial);
69 }
70
71 void FeatureList::AssociateReportingFieldTrial(
72 const std::string& feature_name,
73 OverrideState for_overridden_state,
74 FieldTrial* field_trial) {
75 DCHECK(
76 IsFeatureOverriddenFromCommandLine(feature_name, for_overridden_state));
77
78 // Only one associated field trial is supported per feature. This is generally
79 // enforced server-side.
80 OverrideEntry* entry = &overrides_.find(feature_name)->second;
81 if (entry->field_trial) {
82 NOTREACHED() << "Feature " << feature_name
83 << " already has trial: " << entry->field_trial->trial_name()
84 << ", associating trial: " << field_trial->trial_name();
85 return;
86 }
87
88 entry->field_trial = field_trial;
89 }
90
47 // static 91 // static
48 bool FeatureList::IsEnabled(const Feature& feature) { 92 bool FeatureList::IsEnabled(const Feature& feature) {
49 return GetInstance()->IsFeatureEnabled(feature); 93 return GetInstance()->IsFeatureEnabled(feature);
50 } 94 }
51 95
52 // static 96 // static
53 FeatureList* FeatureList::GetInstance() { 97 FeatureList* FeatureList::GetInstance() {
54 return g_instance; 98 return g_instance;
55 } 99 }
56 100
(...skipping 17 matching lines...) Expand all
74 initialized_ = true; 118 initialized_ = true;
75 } 119 }
76 120
77 bool FeatureList::IsFeatureEnabled(const Feature& feature) { 121 bool FeatureList::IsFeatureEnabled(const Feature& feature) {
78 DCHECK(initialized_); 122 DCHECK(initialized_);
79 DCHECK(CheckFeatureIdentity(feature)) << feature.name; 123 DCHECK(CheckFeatureIdentity(feature)) << feature.name;
80 124
81 auto it = overrides_.find(feature.name); 125 auto it = overrides_.find(feature.name);
82 if (it != overrides_.end()) { 126 if (it != overrides_.end()) {
83 const OverrideEntry& entry = it->second; 127 const OverrideEntry& entry = it->second;
128
129 // Activate the corresponding field trial, if necessary.
130 if (entry.field_trial)
131 entry.field_trial->group();
132
84 // TODO(asvitkine) Expand this section as more support is added. 133 // TODO(asvitkine) Expand this section as more support is added.
85 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; 134 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE;
86 } 135 }
87 // Otherwise, return the default state. 136 // Otherwise, return the default state.
88 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; 137 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT;
89 } 138 }
90 139
91 void FeatureList::RegisterOverride(const std::string& feature_name, 140 void FeatureList::RegisterOverride(const std::string& feature_name,
92 OverrideState overridden_state) { 141 OverrideState overridden_state,
142 FieldTrial* field_trial) {
93 DCHECK(!initialized_); 143 DCHECK(!initialized_);
94 overrides_.insert(make_pair(feature_name, OverrideEntry(overridden_state))); 144 // Note: The semantics of insert() is that it does not overwrite the entry if
145 // one already exists for the key. Thus, only the first override for a given
146 // feature name takes effect.
147 overrides_.insert(std::make_pair(
148 feature_name, OverrideEntry(overridden_state, field_trial)));
95 } 149 }
96 150
97 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { 151 bool FeatureList::CheckFeatureIdentity(const Feature& feature) {
98 AutoLock auto_lock(feature_identity_tracker_lock_); 152 AutoLock auto_lock(feature_identity_tracker_lock_);
99 153
100 auto it = feature_identity_tracker_.find(feature.name); 154 auto it = feature_identity_tracker_.find(feature.name);
101 if (it == feature_identity_tracker_.end()) { 155 if (it == feature_identity_tracker_.end()) {
102 // If it's not tracked yet, register it. 156 // If it's not tracked yet, register it.
103 feature_identity_tracker_[feature.name] = &feature; 157 feature_identity_tracker_[feature.name] = &feature;
104 return true; 158 return true;
105 } 159 }
106 // Compare address of |feature| to the existing tracked entry. 160 // Compare address of |feature| to the existing tracked entry.
107 return it->second == &feature; 161 return it->second == &feature;
108 } 162 }
109 163
110 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state) 164 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state,
111 : overridden_state(overridden_state) {} 165 FieldTrial* field_trial)
166 : overridden_state(overridden_state),
167 field_trial(field_trial),
168 overridden_by_field_trial(field_trial != nullptr) {}
112 169
113 } // namespace base 170 } // namespace base
OLDNEW
« no previous file with comments | « base/feature_list.h ('k') | base/feature_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698