| 6 | #define BASE_FEATURE_LIST_H_ |
| 7 |
| 8 | #include <functional> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 |
| 14 | #include "base/base_export.h" |
| 15 | #include "base/gtest_prod_util.h" |
| 16 | #include "base/macros.h" |
| 17 | #include "base/metrics/persistent_memory_allocator.h" |
| 18 | #include "base/strings/string_piece.h" |
| 19 | #include "base/synchronization/lock.h" |
| 20 |
| 21 | namespace base { |
| 22 |
| 23 | class FieldTrial; |
| 24 | class FieldTrialList; |
| 25 |
| 26 | // Specifies whether a given feature is enabled or disabled by default. |
| 27 | // NOTE: The actual runtime state may be different, due to a field trial or a |
| 28 | // command line switch. |
| 29 | enum FeatureState { |
| 30 | FEATURE_DISABLED_BY_DEFAULT, |
| 31 | FEATURE_ENABLED_BY_DEFAULT, |
| 32 | }; |
| 33 |
| 34 | // The Feature struct is used to define the default state for a feature. See |
| 35 | // comment below for more details. There must only ever be one struct instance |
| 36 | // for a given feature name - generally defined as a constant global variable or |
| 37 | // file static. It should never be used as a constexpr as it breaks |
| 38 | // pointer-based identity lookup. |
| 39 | struct BASE_EXPORT Feature { |
| 40 | // The name of the feature. This should be unique to each feature and is used |
| 41 | // for enabling/disabling features via command line flags and experiments. |
| 42 | // It is strongly recommended to use CamelCase style for feature names, e.g. |
| 43 | // "MyGreatFeature". |
| 44 | const char* const name; |
| 45 |
| 46 | // The default state (i.e. enabled or disabled) for this feature. |
| 47 | // NOTE: The actual runtime state may be different, due to a field trial or a |
| 48 | // command line switch. |
| 49 | const FeatureState default_state; |
| 50 | }; |
| 51 |
| 52 | #if defined(DCHECK_IS_CONFIGURABLE) |
| 53 | // DCHECKs have been built-in, and are configurable at run-time to be fatal, or |
| 54 | // not, via a DcheckIsFatal feature. We define the Feature here since it is |
| 55 | // checked in FeatureList::SetInstance(). See https://crbug.com/596231. |
| 56 | extern BASE_EXPORT const Feature kDCheckIsFatalFeature; |
| 57 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
| 58 |
| 59 | // The FeatureList class is used to determine whether a given feature is on or |
| 60 | // off. It provides an authoritative answer, taking into account command-line |
| 61 | // overrides and experimental control. |
| 62 | // |
| 63 | // The basic use case is for any feature that can be toggled (e.g. through |
| 64 | // command-line or an experiment) to have a defined Feature struct, e.g.: |
| 65 | // |
| 66 | // const base::Feature kMyGreatFeature { |
| 67 | // "MyGreatFeature", base::FEATURE_ENABLED_BY_DEFAULT |
| 68 | // }; |
| 69 | // |
| 70 | // Then, client code that wishes to query the state of the feature would check: |
| 71 | // |
| 72 | // if (base::FeatureList::IsEnabled(kMyGreatFeature)) { |
| 73 | // // Feature code goes here. |
| 74 | // } |
| 75 | // |
| 76 | // Behind the scenes, the above call would take into account any command-line |
| 77 | // flags to enable or disable the feature, any experiments that may control it |
| 78 | // and finally its default state (in that order of priority), to determine |
| 79 | // whether the feature is on. |
| 80 | // |
| 81 | // Features can be explicitly forced on or off by specifying a list of comma- |
| 82 | // separated feature names via the following command-line flags: |
| 83 | // |
| 84 | // --enable-features=Feature5,Feature7 |
| 85 | // --disable-features=Feature1,Feature2,Feature3 |
| 86 | // |
| 87 | // To enable/disable features in a test, do NOT append --enable-features or |
| 88 | // --disable-features to the command-line directly. Instead, use |
| 89 | // ScopedFeatureList. See base/test/scoped_feature_list.h for details. |
| 90 | // |
| 91 | // After initialization (which should be done single-threaded), the FeatureList |
| 92 | // API is thread safe. |
| 93 | // |
| 94 | // Note: This class is a singleton, but does not use base/memory/singleton.h in |
| 95 | // order to have control over its initialization sequence. Specifically, the |
| 96 | // intended use is to create an instance of this class and fully initialize it, |
| 97 | // before setting it as the singleton for a process, via SetInstance(). |
| 98 | class BASE_EXPORT FeatureList { |
| 99 | public: |
| 100 | FeatureList(); |
| 101 | ~FeatureList(); |
| 102 |
| 103 | // Used by common test fixture classes to prevent abuse of ScopedFeatureList |
| 104 | // after multiple threads have started. |
| 105 | class BASE_EXPORT ScopedDisallowOverrides { |
| 106 | public: |
| 107 | explicit ScopedDisallowOverrides(const char* reason); |
| 108 | ~ScopedDisallowOverrides(); |
| 109 |
| 110 | private: |
| 111 | #if DCHECK_IS_ON() |
| 112 | const char* const previous_reason_; |
| 113 | #endif |
| 114 |
| 115 | DISALLOW_COPY_AND_ASSIGN(ScopedDisallowOverrides); |
| 116 | }; |
| 117 |
| 118 | // Specifies whether a feature override enables or disables the feature. |
| 119 | enum OverrideState { |
| 120 | OVERRIDE_USE_DEFAULT, |
| 121 | OVERRIDE_DISABLE_FEATURE, |
| 122 | OVERRIDE_ENABLE_FEATURE, |
| 123 | }; |
| 124 |
| 125 | // Describes a feature override. The first member is a Feature that will be |
| 126 | // overridden with the state given by the second member. |
| 127 | using FeatureOverrideInfo = |
| 128 | std::pair<const std::reference_wrapper<const Feature>, OverrideState>; |
| 129 |
| 130 | // Initializes feature overrides via command-line flags |enable_features| and |
| 131 | // |disable_features|, each of which is a comma-separated list of features to |
| 132 | // enable or disable, respectively. If a feature appears on both lists, then |
| 133 | // it will be disabled. If a list entry has the format "FeatureName<TrialName" |
| 134 | // then this initialization will also associate the feature state override |
| 135 | // with the named field trial, if it exists. If a feature name is prefixed |
| 136 | // with the '*' character, it will be created with OVERRIDE_USE_DEFAULT - |
| 137 | // which is useful for associating with a trial while using the default state. |
| 138 | // Must only be invoked during the initialization phase (before |
| 139 | // FinalizeInitialization() has been called). |
| 140 | void InitializeFromCommandLine(const std::string& enable_features, |
| 141 | const std::string& disable_features); |
| 142 |
| 143 | // Initializes feature overrides through the field trial allocator, which |
| 144 | // we're using to store the feature names, their override state, and the name |
| 145 | // of the associated field trial. |
| 146 | void InitializeFromSharedMemory(PersistentMemoryAllocator* allocator); |
| 147 |
| 148 | // Returns true if the state of |feature_name| has been overridden via |
| 149 | // |InitializeFromCommandLine()|. This includes features explicitly |
| 150 | // disabled/enabled with --disable-features and --enable-features, as well as |
| 151 | // any extra feature overrides that depend on command line switches. |
| 152 | bool IsFeatureOverriddenFromCommandLine(const std::string& feature_name, |
| 153 | OverrideState state) const; |
| 154 |
| 155 | // Associates a field trial for reporting purposes corresponding to the |
| 156 | // command-line setting the feature state to |for_overridden_state|. The trial |
| 157 | // will be activated when the state of the feature is first queried. This |
| 158 | // should be called during registration, after InitializeFromCommandLine() has |
| 159 | // been called but before the instance is registered via SetInstance(). |
| 160 | void AssociateReportingFieldTrial(const std::string& feature_name, |
| 161 | OverrideState for_overridden_state, |
| 162 | FieldTrial* field_trial); |
| 163 |
| 164 | // Registers a field trial to override the enabled state of the specified |
| 165 | // feature to |override_state|. Command-line overrides still take precedence |
| 166 | // over field trials, so this will have no effect if the feature is being |
| 167 | // overridden from the command-line. The associated field trial will be |
| 168 | // activated when the feature state for this feature is queried. This should |
| 169 | // be called during registration, after InitializeFromCommandLine() has been |
| 170 | // called but before the instance is registered via SetInstance(). |
| 171 | void RegisterFieldTrialOverride(const std::string& feature_name, |
| 172 | OverrideState override_state, |
| 173 | FieldTrial* field_trial); |
| 174 |
| 175 | // Adds extra overrides (not associated with a field trial). Should be called |
| 176 | // before SetInstance(). |
| 177 | // The ordering of calls with respect to InitializeFromCommandLine(), |
| 178 | // RegisterFieldTrialOverride(), etc. matters. The first call wins out, |
| 179 | // because the |overrides_| map uses insert(), which retains the first |
| 180 | // inserted entry and does not overwrite it on subsequent calls to insert(). |
| 181 | void RegisterExtraFeatureOverrides( |
| 182 | const std::vector<FeatureOverrideInfo>& extra_overrides); |
| 183 |
| 184 | // Loops through feature overrides and serializes them all into |allocator|. |
| 185 | void AddFeaturesToAllocator(PersistentMemoryAllocator* allocator); |
| 186 |
| 187 | // Returns comma-separated lists of feature names (in the same format that is |
| 188 | // accepted by InitializeFromCommandLine()) corresponding to features that |
| 189 | // have been overridden - either through command-line or via FieldTrials. For |
| 190 | // those features that have an associated FieldTrial, the output entry will be |
| 191 | // of the format "FeatureName<TrialName", where "TrialName" is the name of the |
| 192 | // FieldTrial. Features that have overrides with OVERRIDE_USE_DEFAULT will be |
| 193 | // added to |enable_overrides| with a '*' character prefix. Must be called |
| 194 | // only after the instance has been initialized and registered. |
| 195 | void GetFeatureOverrides(std::string* enable_overrides, |
| 196 | std::string* disable_overrides); |
| 197 |
| 198 | // Like GetFeatureOverrides(), but only returns overrides that were specified |
| 199 | // explicitly on the command-line, omitting the ones from field trials. |
| 200 | void GetCommandLineFeatureOverrides(std::string* enable_overrides, |
| 201 | std::string* disable_overrides); |
| 202 |
| 203 | // Returns whether the given |feature| is enabled. Must only be called after |
| 204 | // the singleton instance has been registered via SetInstance(). Additionally, |
| 205 | // a feature with a given name must only have a single corresponding Feature |
| 206 | // struct, which is checked in builds with DCHECKs enabled. |
| 207 | static bool IsEnabled(const Feature& feature); |
| 208 |
| 209 | // Returns the field trial associated with the given |feature|. Must only be |
| 210 | // called after the singleton instance has been registered via SetInstance(). |
| 211 | static FieldTrial* GetFieldTrial(const Feature& feature); |
| 212 |
| 213 | // Splits a comma-separated string containing feature names into a vector. The |
| 214 | // resulting pieces point to parts of |input|. |
| 215 | static std::vector<base::StringPiece> SplitFeatureListString( |
| 216 | base::StringPiece input); |
| 217 |
| 218 | // Initializes and sets an instance of FeatureList with feature overrides via |
| 219 | // command-line flags |enable_features| and |disable_features| if one has not |
| 220 | // already been set from command-line flags. Returns true if an instance did |
| 221 | // not previously exist. See InitializeFromCommandLine() for more details |
| 222 | // about |enable_features| and |disable_features| parameters. |
| 223 | static bool InitializeInstance(const std::string& enable_features, |
| 224 | const std::string& disable_features); |
| 225 |
| 226 | // Like the above, but also adds extra overrides. If a feature appears in |
| 227 | // |extra_overrides| and also |enable_features| or |disable_features|, the |
| 228 | // disable/enable will supersede the extra overrides. |
| 229 | static bool InitializeInstance( |
| 230 | const std::string& enable_features, |
| 231 | const std::string& disable_features, |
| 232 | const std::vector<FeatureOverrideInfo>& extra_overrides); |
| 233 |
| 234 | // Returns the singleton instance of FeatureList. Will return null until an |
| 235 | // instance is registered via SetInstance(). |
| 236 | static FeatureList* GetInstance(); |
| 237 |
| 238 | // Registers the given |instance| to be the singleton feature list for this |
| 239 | // process. This should only be called once and |instance| must not be null. |
| 240 | // Note: If you are considering using this for the purposes of testing, take |
| 241 | // a look at using base/test/scoped_feature_list.h instead. |
| 242 | static void SetInstance(std::unique_ptr<FeatureList> instance); |
| 243 |
| 244 | // Clears the previously-registered singleton instance for tests and returns |
| 245 | // the old instance. |
| 246 | // Note: Most tests should never call this directly. Instead consider using |
| 247 | // base::test::ScopedFeatureList. |
| 248 | static std::unique_ptr<FeatureList> ClearInstanceForTesting(); |
| 249 |
| 250 | // Sets a given (initialized) |instance| to be the singleton feature list, |
| 251 | // for testing. Existing instance must be null. This is primarily intended |
| 252 | // to support base::test::ScopedFeatureList helper class. |
| 253 | static void RestoreInstanceForTesting(std::unique_ptr<FeatureList> instance); |
| 254 |
| 255 | private: |
| 256 | FRIEND_TEST_ALL_PREFIXES(FeatureListTest, CheckFeatureIdentity); |
| 257 | FRIEND_TEST_ALL_PREFIXES(FeatureListTest, |
| 258 | StoreAndRetrieveFeaturesFromSharedMemory); |
| 259 | FRIEND_TEST_ALL_PREFIXES(FeatureListTest, |
| 260 | StoreAndRetrieveAssociatedFeaturesFromSharedMemory); |
| 261 |
| 262 | struct OverrideEntry { |
| 263 | // The overridden enable (on/off) state of the feature. |
| 264 | const OverrideState overridden_state; |
| 265 |
| 266 | // An optional associated field trial, which will be activated when the |
| 267 | // state of the feature is queried for the first time. Weak pointer to the |
| 268 | // FieldTrial object that is owned by the FieldTrialList singleton. |
| 269 | base::FieldTrial* field_trial; |
| 270 |
| 271 | // Specifies whether the feature's state is overridden by |field_trial|. |
| 272 | // If it's not, and |field_trial| is not null, it means it is simply an |
| 273 | // associated field trial for reporting purposes (and |overridden_state| |
| 274 | // came from the command-line). |
| 275 | const bool overridden_by_field_trial; |
| 276 |
| 277 | // TODO(asvitkine): Expand this as more support is added. |
| 278 |
| 279 | // Constructs an OverrideEntry for the given |overridden_state|. If |
| 280 | // |field_trial| is not null, it implies that |overridden_state| comes from |
| 281 | // the trial, so |overridden_by_field_trial| will be set to true. |
| 282 | OverrideEntry(OverrideState overridden_state, FieldTrial* field_trial); |
| 283 | }; |
| 284 |
| 285 | // Finalizes the initialization state of the FeatureList, so that no further |
| 286 | // overrides can be registered. This is called by SetInstance() on the |
| 287 | // singleton feature list that is being registered. |
| 288 | void FinalizeInitialization(); |
| 289 |
| 290 | // Returns whether the given |feature| is enabled. This is invoked by the |
| 291 | // public FeatureList::IsEnabled() static function on the global singleton. |
| 292 | // Requires the FeatureList to have already been fully initialized. |
| 293 | bool IsFeatureEnabled(const Feature& feature); |
| 294 |
| 295 | // Returns the field trial associated with the given |feature|. This is |
| 296 | // invoked by the public FeatureList::GetFieldTrial() static function on the |
| 297 | // global singleton. Requires the FeatureList to have already been fully |
| 298 | // initialized. |
| 299 | base::FieldTrial* GetAssociatedFieldTrial(const Feature& feature); |
| 300 |
| 301 | // For each feature name in comma-separated list of strings |feature_list|, |
| 302 | // registers an override with the specified |overridden_state|. Also, will |
| 303 | // associate an optional named field trial if the entry is of the format |
| 304 | // "FeatureName<TrialName". |
| 305 | void RegisterOverridesFromCommandLine(const std::string& feature_list, |
| 306 | OverrideState overridden_state); |
| 307 |
| 308 | // Registers an override for feature |feature_name|. The override specifies |
| 309 | // whether the feature should be on or off (via |overridden_state|), which |
| 310 | // will take precedence over the feature's default state. If |field_trial| is |
| 311 | // not null, registers the specified field trial object to be associated with |
| 312 | // the feature, which will activate the field trial when the feature state is |
| 313 | // queried. If an override is already registered for the given feature, it |
| 314 | // will not be changed. |
| 315 | void RegisterOverride(StringPiece feature_name, |
| 316 | OverrideState overridden_state, |
| 317 | FieldTrial* field_trial); |
| 318 |
| 319 | // Implementation of GetFeatureOverrides() with a parameter that specifies |
| 320 | // whether only command-line enabled overrides should be emitted. See that |
| 321 | // function's comments for more details. |
| 322 | void GetFeatureOverridesImpl(std::string* enable_overrides, |
| 323 | std::string* disable_overrides, |
| 324 | bool command_line_only); |
| 325 |
| 326 | // Verifies that there's only a single definition of a Feature struct for a |
| 327 | // given feature name. Keeps track of the first seen Feature struct for each |
| 328 | // feature. Returns false when called on a Feature struct with a different |
| 329 | // address than the first one it saw for that feature name. Used only from |
| 330 | // DCHECKs and tests. |
| 331 | bool CheckFeatureIdentity(const Feature& feature); |
| 332 |
| 333 | // Map from feature name to an OverrideEntry struct for the feature, if it |
| 334 | // exists. |
| 335 | std::map<std::string, OverrideEntry, std::less<>> overrides_; |
| 336 |
| 337 | // Locked map that keeps track of seen features, to ensure a single feature is |
| 338 | // only defined once. This verification is only done in builds with DCHECKs |
| 339 | // enabled. |
| 340 | Lock feature_identity_tracker_lock_; |
| 341 | std::map<std::string, const Feature*> feature_identity_tracker_; |
| 342 |
| 343 | // Tracks the associated FieldTrialList for DCHECKs. This is used to catch |
| 344 | // the scenario where multiple FieldTrialList are used with the same |
| 345 | // FeatureList - which can lead to overrides pointing to invalid FieldTrial |
| 346 | // objects. |
| 347 | base::FieldTrialList* field_trial_list_ = nullptr; |
| 348 |
| 349 | // Whether this object has been fully initialized. This gets set to true as a |
| 350 | // result of FinalizeInitialization(). |
| 351 | bool initialized_ = false; |
| 352 |
| 353 | // Whether this object has been initialized from command line. |
| 354 | bool initialized_from_command_line_ = false; |
| 355 |
| 356 | DISALLOW_COPY_AND_ASSIGN(FeatureList); |
| 357 | }; |
| 358 |
| 359 | } // namespace base |
| 360 |
| 361 | #endif // BASE_FEATURE_LIST_H_ |
| 362 |