OLD | NEW |
---|---|
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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/field_trial.h" | |
10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // Pointer to the FeatureList instance singleton that was set via | 17 // Pointer to the FeatureList instance singleton that was set via |
17 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to | 18 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to |
18 // have more control over initialization timing. Leaky. | 19 // have more control over initialization timing. Leaky. |
19 FeatureList* g_instance = nullptr; | 20 FeatureList* g_instance = nullptr; |
(...skipping 10 matching lines...) Expand all Loading... | |
30 FeatureList::~FeatureList() {} | 31 FeatureList::~FeatureList() {} |
31 | 32 |
32 void FeatureList::InitializeFromCommandLine( | 33 void FeatureList::InitializeFromCommandLine( |
33 const std::string& enable_features, | 34 const std::string& enable_features, |
34 const std::string& disable_features) { | 35 const std::string& disable_features) { |
35 DCHECK(!initialized_); | 36 DCHECK(!initialized_); |
36 | 37 |
37 // Process disabled features first, so that disabled ones take precedence over | 38 // Process disabled features first, so that disabled ones take precedence over |
38 // enabled ones (since RegisterOverride() uses insert()). | 39 // enabled ones (since RegisterOverride() uses insert()). |
39 for (const auto& feature_name : SplitFeatureListString(disable_features)) { | 40 for (const auto& feature_name : SplitFeatureListString(disable_features)) { |
40 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE); | 41 RegisterOverride(feature_name, OVERRIDE_DISABLE_FEATURE, nullptr); |
41 } | 42 } |
42 for (const auto& feature_name : SplitFeatureListString(enable_features)) { | 43 for (const auto& feature_name : SplitFeatureListString(enable_features)) { |
43 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE); | 44 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE, nullptr); |
44 } | 45 } |
45 } | 46 } |
46 | 47 |
48 void FeatureList::RegisterFieldTrialOverride(const std::string& feature_name, | |
49 OverrideState override_state, | |
50 FieldTrial* field_trial) { | |
51 DCHECK(field_trial); | |
52 const auto& result = | |
53 RegisterOverride(feature_name, override_state, field_trial); | |
54 // The call above should either succeed or if it failed, it should be because | |
55 // of an override that doesn't have a field trial (i.e. a command-line based | |
56 // one). It's not valid for two field trials to try to override the state of | |
57 // the same feature. | |
58 if (!result.second && result.first.field_trial) { | |
59 NOTREACHED() << "Feature " << feature_name | |
60 << " has conflicting field trial overrides: " | |
61 << result.first.field_trial->trial_name() << " / " | |
62 << field_trial->trial_name(); | |
63 } | |
Ilya Sherman
2015/09/24 01:11:32
I think that rather than returning a pair from Reg
Alexei Svitkine (slow)
2015/09/24 20:49:21
Thanks, that does simplify things a lot! Done.
| |
64 } | |
65 | |
66 FieldTrial* FeatureList::AssociateReportingFieldTrial( | |
67 const std::string& feature_name, | |
68 OverrideState for_overridden_state, | |
69 const std::string& field_trial_name, | |
70 const std::string& group_name) { | |
71 auto it = overrides_.find(feature_name); | |
72 // No association if there's no override entry or its state doesn't match | |
73 // |for_overridden_state|. | |
74 if (it == overrides_.end() || | |
75 it->second.overridden_state != for_overridden_state) { | |
76 return nullptr; | |
77 } | |
78 | |
79 // Only one associated field trial is supported per feature. This is generally | |
80 // enforced server-side. | |
81 if (it->second.field_trial) { | |
82 NOTREACHED() << "Feature = " << feature_name | |
83 << ", has trial = " << it->second.field_trial->trial_name() | |
84 << ", associating trial = " << field_trial_name; | |
85 return nullptr; | |
86 } | |
87 it->second.field_trial = | |
88 FieldTrialList::CreateFieldTrial(field_trial_name, group_name); | |
89 return it->second.field_trial; | |
90 } | |
91 | |
47 // static | 92 // static |
48 bool FeatureList::IsEnabled(const Feature& feature) { | 93 bool FeatureList::IsEnabled(const Feature& feature) { |
49 return GetInstance()->IsFeatureEnabled(feature); | 94 return GetInstance()->IsFeatureEnabled(feature); |
50 } | 95 } |
51 | 96 |
52 // static | 97 // static |
53 FeatureList* FeatureList::GetInstance() { | 98 FeatureList* FeatureList::GetInstance() { |
54 return g_instance; | 99 return g_instance; |
55 } | 100 } |
56 | 101 |
(...skipping 17 matching lines...) Expand all Loading... | |
74 initialized_ = true; | 119 initialized_ = true; |
75 } | 120 } |
76 | 121 |
77 bool FeatureList::IsFeatureEnabled(const Feature& feature) { | 122 bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
78 DCHECK(initialized_); | 123 DCHECK(initialized_); |
79 DCHECK(CheckFeatureIdentity(feature)) << feature.name; | 124 DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
80 | 125 |
81 auto it = overrides_.find(feature.name); | 126 auto it = overrides_.find(feature.name); |
82 if (it != overrides_.end()) { | 127 if (it != overrides_.end()) { |
83 const OverrideEntry& entry = it->second; | 128 const OverrideEntry& entry = it->second; |
129 | |
130 // Activate the corresponding field trial, if necessary. | |
131 if (entry.field_trial) | |
132 entry.field_trial->group(); | |
133 | |
84 // TODO(asvitkine) Expand this section as more support is added. | 134 // TODO(asvitkine) Expand this section as more support is added. |
85 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; | 135 return entry.overridden_state == OVERRIDE_ENABLE_FEATURE; |
86 } | 136 } |
87 // Otherwise, return the default state. | 137 // Otherwise, return the default state. |
88 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; | 138 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
89 } | 139 } |
90 | 140 |
91 void FeatureList::RegisterOverride(const std::string& feature_name, | 141 std::pair<const FeatureList::OverrideEntry&, bool> |
92 OverrideState overridden_state) { | 142 FeatureList::RegisterOverride(const std::string& feature_name, |
143 OverrideState overridden_state, | |
144 FieldTrial* field_trial) { | |
93 DCHECK(!initialized_); | 145 DCHECK(!initialized_); |
94 overrides_.insert(make_pair(feature_name, OverrideEntry(overridden_state))); | 146 // Note: The semantics of insert() is that it does not overwrite the entry if |
147 // one already exists for the key. Thus, only the first override for a given | |
148 // feature name takes effect. | |
149 const auto& result = overrides_.insert(std::make_pair( | |
150 feature_name, OverrideEntry(overridden_state, field_trial))); | |
151 return std::make_pair(result.first->second, result.second); | |
95 } | 152 } |
96 | 153 |
97 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { | 154 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { |
98 AutoLock auto_lock(feature_identity_tracker_lock_); | 155 AutoLock auto_lock(feature_identity_tracker_lock_); |
99 | 156 |
100 auto it = feature_identity_tracker_.find(feature.name); | 157 auto it = feature_identity_tracker_.find(feature.name); |
101 if (it == feature_identity_tracker_.end()) { | 158 if (it == feature_identity_tracker_.end()) { |
102 // If it's not tracked yet, register it. | 159 // If it's not tracked yet, register it. |
103 feature_identity_tracker_[feature.name] = &feature; | 160 feature_identity_tracker_[feature.name] = &feature; |
104 return true; | 161 return true; |
105 } | 162 } |
106 // Compare address of |feature| to the existing tracked entry. | 163 // Compare address of |feature| to the existing tracked entry. |
107 return it->second == &feature; | 164 return it->second == &feature; |
108 } | 165 } |
109 | 166 |
110 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state) | 167 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state, |
111 : overridden_state(overridden_state) {} | 168 FieldTrial* field_trial) |
169 : overridden_state(overridden_state), field_trial(field_trial) {} | |
112 | 170 |
113 } // namespace base | 171 } // namespace base |
OLD | NEW |