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 #ifndef BASE_FEATURE_LIST_H_ | 5 #ifndef BASE_FEATURE_LIST_H_ |
6 #define BASE_FEATURE_LIST_H_ | 6 #define BASE_FEATURE_LIST_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 class FieldTrial; | |
20 | |
19 // Specifies whether a given feature is enabled or disabled by default. | 21 // Specifies whether a given feature is enabled or disabled by default. |
20 enum FeatureState { | 22 enum FeatureState { |
21 FEATURE_DISABLED_BY_DEFAULT, | 23 FEATURE_DISABLED_BY_DEFAULT, |
22 FEATURE_ENABLED_BY_DEFAULT, | 24 FEATURE_ENABLED_BY_DEFAULT, |
23 }; | 25 }; |
24 | 26 |
25 // The Feature struct is used to define the default state for a feature. See | 27 // The Feature struct is used to define the default state for a feature. See |
26 // comment below for more details. There must only ever be one struct instance | 28 // comment below for more details. There must only ever be one struct instance |
27 // for a given feature name - generally defined as a constant global variable or | 29 // for a given feature name - generally defined as a constant global variable or |
28 // file static. | 30 // file static. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 ~FeatureList(); | 78 ~FeatureList(); |
77 | 79 |
78 // Initializes feature overrides via command-line flags |enable_features| and | 80 // Initializes feature overrides via command-line flags |enable_features| and |
79 // |disable_features|, each of which is a comma-separated list of features to | 81 // |disable_features|, each of which is a comma-separated list of features to |
80 // enable or disable, respectively. If a feature appears on both lists, then | 82 // enable or disable, respectively. If a feature appears on both lists, then |
81 // it will be disabled. Must only be invoked during the initialization phase | 83 // it will be disabled. Must only be invoked during the initialization phase |
82 // (before FinalizeInitialization() has been called). | 84 // (before FinalizeInitialization() has been called). |
83 void InitializeFromCommandLine(const std::string& enable_features, | 85 void InitializeFromCommandLine(const std::string& enable_features, |
84 const std::string& disable_features); | 86 const std::string& disable_features); |
85 | 87 |
88 // Specifies whether a feature override enables or disables the feature. | |
89 enum OverrideState { | |
90 OVERRIDE_DISABLE_FEATURE, | |
91 OVERRIDE_ENABLE_FEATURE, | |
92 }; | |
93 | |
94 // Registers a field trial to override the enabled state of the specified | |
95 // feature to |override_state|. Command-line overrides still take precedence | |
96 // over field trials, so this will have no effect if the feature is being | |
97 // overridden from the command-line. The associated field trial will be | |
98 // activated when the feature state for this feature is queried. This should | |
99 // be called during registration, after InitializeFromCommandLine() has been | |
100 // called but before the instance is registered via SetInstance(). | |
101 void RegisterFieldTrialOverride(const std::string& feature_name, | |
102 OverrideState override_state, | |
103 FieldTrial* field_trial); | |
104 | |
105 // Creates and associates a field trial for reporting purposes corresponding | |
106 // to the command-line setting the feature state to |for_overridden_state|. | |
107 // This is a no-op and returns null if the specified feature is not being | |
108 // affected by a command-line override to the given state. Otherwise, it | |
109 // creates a field trial with name |field_trial_name| and a single group named | |
110 // |group_name| and returns it. The trial will be activated when the state of | |
111 // the feature is queried. This should be called during registration, after | |
112 // InitializeFromCommandLine() has been called but before the instance is | |
113 // registered via SetInstance(). | |
114 FieldTrial* AssociateReportingFieldTrial(const std::string& feature_name, | |
115 OverrideState for_overridden_state, | |
116 const std::string& field_trial_name, | |
117 const std::string& group_name); | |
Ilya Sherman
2015/09/16 00:50:43
This API doesn't make it especially obvious that A
| |
118 | |
86 // Returns whether the given |feature| is enabled. Must only be called after | 119 // Returns whether the given |feature| is enabled. Must only be called after |
87 // the singleton instance has been registered via SetInstance(). Additionally, | 120 // the singleton instance has been registered via SetInstance(). Additionally, |
88 // a feature with a given name must only have a single corresponding Feature | 121 // a feature with a given name must only have a single corresponding Feature |
89 // struct, which is checked in builds with DCHECKs enabled. | 122 // struct, which is checked in builds with DCHECKs enabled. |
90 static bool IsEnabled(const Feature& feature); | 123 static bool IsEnabled(const Feature& feature); |
91 | 124 |
92 // Returns the singleton instance of FeatureList. Will return null until an | 125 // Returns the singleton instance of FeatureList. Will return null until an |
93 // instance is registered via SetInstance(). | 126 // instance is registered via SetInstance(). |
94 static FeatureList* GetInstance(); | 127 static FeatureList* GetInstance(); |
95 | 128 |
96 // Registers the given |instance| to be the singleton feature list for this | 129 // Registers the given |instance| to be the singleton feature list for this |
97 // process. This should only be called once and |instance| must not be null. | 130 // process. This should only be called once and |instance| must not be null. |
98 static void SetInstance(scoped_ptr<FeatureList> instance); | 131 static void SetInstance(scoped_ptr<FeatureList> instance); |
99 | 132 |
100 // Clears the previously-registered singleton instance for tests. | 133 // Clears the previously-registered singleton instance for tests. |
101 static void ClearInstanceForTesting(); | 134 static void ClearInstanceForTesting(); |
102 | 135 |
103 private: | 136 private: |
104 FRIEND_TEST_ALL_PREFIXES(FeatureListTest, CheckFeatureIdentity); | 137 FRIEND_TEST_ALL_PREFIXES(FeatureListTest, CheckFeatureIdentity); |
105 | 138 |
106 // Specifies whether a feature override enables or disables the feature. | |
107 enum OverrideState { | |
108 OVERRIDE_DISABLE_FEATURE, | |
109 OVERRIDE_ENABLE_FEATURE, | |
110 }; | |
111 | |
112 // Finalizes the initialization state of the FeatureList, so that no further | 139 // Finalizes the initialization state of the FeatureList, so that no further |
113 // overrides can be registered. This is called by SetInstance() on the | 140 // overrides can be registered. This is called by SetInstance() on the |
114 // singleton feature list that is being registered. | 141 // singleton feature list that is being registered. |
115 void FinalizeInitialization(); | 142 void FinalizeInitialization(); |
116 | 143 |
117 // Returns whether the given |feature| is enabled. This is invoked by the | 144 // Returns whether the given |feature| is enabled. This is invoked by the |
118 // public FeatureList::IsEnabled() static function on the global singleton. | 145 // public FeatureList::IsEnabled() static function on the global singleton. |
119 // Requires the FeatureList to have already been fully initialized. | 146 // Requires the FeatureList to have already been fully initialized. |
120 bool IsFeatureEnabled(const Feature& feature); | 147 bool IsFeatureEnabled(const Feature& feature); |
121 | 148 |
122 // Registers an override for feature |feature_name|. The override specifies | 149 // Registers an override for feature |feature_name|. The override specifies |
123 // whether the feature should be on or off (via |overridden_state|), which | 150 // whether the feature should be on or off (via |overridden_state|), which |
124 // will take precedence over the feature's default state. | 151 // will take precedence over the feature's default state. If |field_trial| is |
152 // not null, registers the specified field trial object to be associated with | |
153 // the feature, which will activate the field trial when the feature state is | |
154 // queried. | |
125 void RegisterOverride(const std::string& feature_name, | 155 void RegisterOverride(const std::string& feature_name, |
126 OverrideState overridden_state); | 156 OverrideState overridden_state, |
157 FieldTrial* field_trial); | |
127 | 158 |
128 // Verifies that there's only a single definition of a Feature struct for a | 159 // Verifies that there's only a single definition of a Feature struct for a |
129 // given feature name. Keeps track of the first seen Feature struct for each | 160 // given feature name. Keeps track of the first seen Feature struct for each |
130 // feature. Returns false when called on a Feature struct with a different | 161 // feature. Returns false when called on a Feature struct with a different |
131 // address than the first one it saw for that feature name. Used only from | 162 // address than the first one it saw for that feature name. Used only from |
132 // DCHECKs and tests. | 163 // DCHECKs and tests. |
133 bool CheckFeatureIdentity(const Feature& feature); | 164 bool CheckFeatureIdentity(const Feature& feature); |
134 | 165 |
135 struct OverrideEntry { | 166 struct OverrideEntry { |
136 // The overridden enable (on/off) state of the feature. | 167 // The overridden enable (on/off) state of the feature. |
137 const OverrideState overridden_state; | 168 const OverrideState overridden_state; |
138 | 169 |
170 // An optional associated field trial, which will be activated when the | |
171 // state of the feature is queried for the first time. | |
Ilya Sherman
2015/09/16 00:50:43
Please document ownership and lifetime expectation
Alexei Svitkine (slow)
2015/09/22 21:19:59
Done.
| |
172 base::FieldTrial* field_trial; | |
173 | |
139 // TODO(asvitkine): Expand this as more support is added. | 174 // TODO(asvitkine): Expand this as more support is added. |
Ilya Sherman
2015/09/16 00:50:43
nit: Ok to remove this? You removed a similar com
Alexei Svitkine (slow)
2015/09/22 21:19:59
There's still the kill-switch semantics that are p
| |
140 | 175 |
141 explicit OverrideEntry(OverrideState overridden_state); | 176 OverrideEntry(OverrideState overridden_state, FieldTrial* field_trial); |
142 }; | 177 }; |
143 // Map from feature name to an OverrideEntry struct for the feature, if it | 178 // Map from feature name to an OverrideEntry struct for the feature, if it |
144 // exists. | 179 // exists. |
145 std::map<std::string, OverrideEntry> overrides_; | 180 std::map<std::string, OverrideEntry> overrides_; |
146 | 181 |
147 // Locked map that keeps track of seen features, to ensure a single feature is | 182 // Locked map that keeps track of seen features, to ensure a single feature is |
148 // only defined once. This verification is only done in builds with DCHECKs | 183 // only defined once. This verification is only done in builds with DCHECKs |
149 // enabled. | 184 // enabled. |
150 Lock feature_identity_tracker_lock_; | 185 Lock feature_identity_tracker_lock_; |
151 std::map<std::string, const Feature*> feature_identity_tracker_; | 186 std::map<std::string, const Feature*> feature_identity_tracker_; |
152 | 187 |
153 // Whether this object has been fully initialized. This gets set to true as a | 188 // Whether this object has been fully initialized. This gets set to true as a |
154 // result of FinalizeInitialization(). | 189 // result of FinalizeInitialization(). |
155 bool initialized_; | 190 bool initialized_; |
156 | 191 |
157 DISALLOW_COPY_AND_ASSIGN(FeatureList); | 192 DISALLOW_COPY_AND_ASSIGN(FeatureList); |
158 }; | 193 }; |
159 | 194 |
160 } // namespace base | 195 } // namespace base |
161 | 196 |
162 #endif // BASE_FEATURE_LIST_H_ | 197 #endif // BASE_FEATURE_LIST_H_ |
OLD | NEW |