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 // Returns true if the state of |feature_name| has been overridden via |
| 95 // |InitializeFromCommandLine()|. |
| 96 bool IsFeatureOverriddenFromCommandLine(const std::string& feature_name, |
| 97 OverrideState state) const; |
| 98 |
| 99 // Associates a field trial for reporting purposes corresponding to the |
| 100 // command-line setting the feature state to |for_overridden_state|. The trial |
| 101 // will be activated when the state of the feature is first queried. This |
| 102 // should be called during registration, after InitializeFromCommandLine() has |
| 103 // been called but before the instance is registered via SetInstance(). |
| 104 void AssociateReportingFieldTrial(const std::string& feature_name, |
| 105 OverrideState for_overridden_state, |
| 106 FieldTrial* field_trial); |
| 107 |
| 108 // Registers a field trial to override the enabled state of the specified |
| 109 // feature to |override_state|. Command-line overrides still take precedence |
| 110 // over field trials, so this will have no effect if the feature is being |
| 111 // overridden from the command-line. The associated field trial will be |
| 112 // activated when the feature state for this feature is queried. This should |
| 113 // be called during registration, after InitializeFromCommandLine() has been |
| 114 // called but before the instance is registered via SetInstance(). |
| 115 void RegisterFieldTrialOverride(const std::string& feature_name, |
| 116 OverrideState override_state, |
| 117 FieldTrial* field_trial); |
| 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. | 139 struct OverrideEntry { |
107 enum OverrideState { | 140 // The overridden enable (on/off) state of the feature. |
108 OVERRIDE_DISABLE_FEATURE, | 141 const OverrideState overridden_state; |
109 OVERRIDE_ENABLE_FEATURE, | 142 |
| 143 // An optional associated field trial, which will be activated when the |
| 144 // state of the feature is queried for the first time. Weak pointer to the |
| 145 // FieldTrial object that is owned by the FieldTrialList singleton. |
| 146 base::FieldTrial* field_trial; |
| 147 |
| 148 // Specifies whether the feature's state is overridden by |field_trial|. |
| 149 // If it's not, and |field_trial| is not null, it means it is simply an |
| 150 // associated field trial for reporting purposes (and |overridden_state| |
| 151 // came from the command-line). |
| 152 const bool overridden_by_field_trial; |
| 153 |
| 154 // TODO(asvitkine): Expand this as more support is added. |
| 155 |
| 156 // Constructs an OverrideEntry for the given |overridden_state|. If |
| 157 // |field_trial| is not null, it implies that |overridden_state| comes from |
| 158 // the trial, so |overridden_by_field_trial| will be set to true. |
| 159 OverrideEntry(OverrideState overridden_state, FieldTrial* field_trial); |
110 }; | 160 }; |
111 | 161 |
112 // Finalizes the initialization state of the FeatureList, so that no further | 162 // Finalizes the initialization state of the FeatureList, so that no further |
113 // overrides can be registered. This is called by SetInstance() on the | 163 // overrides can be registered. This is called by SetInstance() on the |
114 // singleton feature list that is being registered. | 164 // singleton feature list that is being registered. |
115 void FinalizeInitialization(); | 165 void FinalizeInitialization(); |
116 | 166 |
117 // Returns whether the given |feature| is enabled. This is invoked by the | 167 // Returns whether the given |feature| is enabled. This is invoked by the |
118 // public FeatureList::IsEnabled() static function on the global singleton. | 168 // public FeatureList::IsEnabled() static function on the global singleton. |
119 // Requires the FeatureList to have already been fully initialized. | 169 // Requires the FeatureList to have already been fully initialized. |
120 bool IsFeatureEnabled(const Feature& feature); | 170 bool IsFeatureEnabled(const Feature& feature); |
121 | 171 |
122 // Registers an override for feature |feature_name|. The override specifies | 172 // Registers an override for feature |feature_name|. The override specifies |
123 // whether the feature should be on or off (via |overridden_state|), which | 173 // whether the feature should be on or off (via |overridden_state|), which |
124 // will take precedence over the feature's default state. | 174 // will take precedence over the feature's default state. If |field_trial| is |
| 175 // not null, registers the specified field trial object to be associated with |
| 176 // the feature, which will activate the field trial when the feature state is |
| 177 // queried. If an override is already registered for the given feature, it |
| 178 // will not be changed. |
125 void RegisterOverride(const std::string& feature_name, | 179 void RegisterOverride(const std::string& feature_name, |
126 OverrideState overridden_state); | 180 OverrideState overridden_state, |
| 181 FieldTrial* field_trial); |
127 | 182 |
128 // Verifies that there's only a single definition of a Feature struct for a | 183 // 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 | 184 // 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 | 185 // 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 | 186 // address than the first one it saw for that feature name. Used only from |
132 // DCHECKs and tests. | 187 // DCHECKs and tests. |
133 bool CheckFeatureIdentity(const Feature& feature); | 188 bool CheckFeatureIdentity(const Feature& feature); |
134 | 189 |
135 struct OverrideEntry { | |
136 // The overridden enable (on/off) state of the feature. | |
137 const OverrideState overridden_state; | |
138 | |
139 // TODO(asvitkine): Expand this as more support is added. | |
140 | |
141 explicit OverrideEntry(OverrideState overridden_state); | |
142 }; | |
143 // Map from feature name to an OverrideEntry struct for the feature, if it | 190 // Map from feature name to an OverrideEntry struct for the feature, if it |
144 // exists. | 191 // exists. |
145 std::map<std::string, OverrideEntry> overrides_; | 192 std::map<std::string, OverrideEntry> overrides_; |
146 | 193 |
147 // Locked map that keeps track of seen features, to ensure a single feature is | 194 // 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 | 195 // only defined once. This verification is only done in builds with DCHECKs |
149 // enabled. | 196 // enabled. |
150 Lock feature_identity_tracker_lock_; | 197 Lock feature_identity_tracker_lock_; |
151 std::map<std::string, const Feature*> feature_identity_tracker_; | 198 std::map<std::string, const Feature*> feature_identity_tracker_; |
152 | 199 |
153 // Whether this object has been fully initialized. This gets set to true as a | 200 // Whether this object has been fully initialized. This gets set to true as a |
154 // result of FinalizeInitialization(). | 201 // result of FinalizeInitialization(). |
155 bool initialized_; | 202 bool initialized_; |
156 | 203 |
157 DISALLOW_COPY_AND_ASSIGN(FeatureList); | 204 DISALLOW_COPY_AND_ASSIGN(FeatureList); |
158 }; | 205 }; |
159 | 206 |
160 } // namespace base | 207 } // namespace base |
161 | 208 |
162 #endif // BASE_FEATURE_LIST_H_ | 209 #endif // BASE_FEATURE_LIST_H_ |
OLD | NEW |