asvitkine | bccbb86 | 2015-09-04 17:17:45 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_FEATURE_LIST_H_ |
| 6 | #define BASE_FEATURE_LIST_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/gtest_prod_util.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/synchronization/lock.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // Specifies whether a given feature is enabled or disabled by default. |
| 20 | enum FeatureState { |
| 21 | FEATURE_DISABLED_BY_DEFAULT, |
| 22 | FEATURE_ENABLED_BY_DEFAULT, |
| 23 | }; |
| 24 | |
| 25 | // 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 |
| 27 | // for a given feature name - generally defined as a constant global variable or |
| 28 | // file static. |
| 29 | struct BASE_EXPORT Feature { |
| 30 | // The name of the feature. This should be unique to each feature and is used |
| 31 | // for enabling/disabling features via command line flags and experiments. |
| 32 | const char* const name; |
| 33 | |
| 34 | // The default state (i.e. enabled or disabled) for this feature. |
| 35 | const FeatureState default_state; |
| 36 | }; |
| 37 | |
| 38 | // The FeatureList class is used to determine whether a given feature is on or |
| 39 | // off. It provides an authoritative answer, taking into account command-line |
| 40 | // overrides and experimental control. |
| 41 | // |
| 42 | // The basic use case is for any feature that can be toggled (e.g. through |
| 43 | // command-line or an experiment) to have a defined Feature struct, e.g.: |
| 44 | // |
| 45 | // struct base::Feature kMyGreatFeature { |
| 46 | // "MyGreatFeature", base::FEATURE_ENABLED_BY_DEFAULT |
| 47 | // }; |
| 48 | // |
| 49 | // Then, client code that wishes to query the state of the feature would check: |
| 50 | // |
| 51 | // if (base::FeatureList::IsEnabled(kMyGreatFeature)) { |
| 52 | // // Feature code goes here. |
| 53 | // } |
| 54 | // |
| 55 | // Behind the scenes, the above call would take into account any command-line |
| 56 | // flags to enable or disable the feature, any experiments that may control it |
| 57 | // and finally its default state (in that order of priority), to determine |
| 58 | // whether the feature is on. |
| 59 | // |
| 60 | // Features can be explicitly forced on or off by specifying a list of comma- |
| 61 | // separated feature names via the following command-line flags: |
| 62 | // |
| 63 | // --enable-features=Feature5,Feature7 |
| 64 | // --disable-features=Feature1,Feature2,Feature3 |
| 65 | // |
| 66 | // After initialization (which should be done single-threaded), the FeatureList |
| 67 | // API is thread safe. |
| 68 | // |
| 69 | // Note: This class is a singleton, but does not use base/memory/singleton.h in |
| 70 | // order to have control over its initialization sequence. Specifically, the |
| 71 | // intended use is to create an instance of this class and fully initialize it, |
| 72 | // before setting it as the singleton for a process, via SetInstance(). |
| 73 | class BASE_EXPORT FeatureList { |
| 74 | public: |
| 75 | FeatureList(); |
| 76 | ~FeatureList(); |
| 77 | |
| 78 | // Initializes feature overrides via command-line flags |enable_features| and |
| 79 | // |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 |
| 81 | // it will be disabled. Must only be invoked during the initialization phase |
| 82 | // (before FinalizeInitialization() has been called). |
| 83 | void InitializeFromCommandLine(const std::string& enable_features, |
| 84 | const std::string& disable_features); |
| 85 | |
| 86 | // Returns whether the given |feature| is enabled. Must only be called after |
| 87 | // the singleton instance has been registered via SetInstance(). Additionally, |
| 88 | // a feature with a given name must only have a single corresponding Feature |
| 89 | // struct, which is checked in builds with DCHECKs enabled. |
| 90 | static bool IsEnabled(const Feature& feature); |
| 91 | |
| 92 | // Returns the singleton instance of FeatureList. Will return null until an |
| 93 | // instance is registered via SetInstance(). |
| 94 | static FeatureList* GetInstance(); |
| 95 | |
| 96 | // 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. |
| 98 | static void SetInstance(scoped_ptr<FeatureList> instance); |
| 99 | |
| 100 | // Clears the previously-registered singleton instance for tests. |
| 101 | static void ClearInstanceForTesting(); |
| 102 | |
| 103 | private: |
| 104 | FRIEND_TEST_ALL_PREFIXES(FeatureListTest, CheckFeatureIdentity); |
| 105 | |
| 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 |
| 113 | // overrides can be registered. This is called by SetInstance() on the |
| 114 | // singleton feature list that is being registered. |
| 115 | void FinalizeInitialization(); |
| 116 | |
| 117 | // Returns whether the given |feature| is enabled. This is invoked by the |
| 118 | // public FeatureList::IsEnabled() static function on the global singleton. |
| 119 | // Requires the FeatureList to have already been fully initialized. |
| 120 | bool IsFeatureEnabled(const Feature& feature); |
| 121 | |
| 122 | // Registers an override for feature |feature_name|. The override specifies |
| 123 | // whether the feature should be on or off (via |overridden_state|), which |
| 124 | // will take precedence over the feature's default state. |
| 125 | void RegisterOverride(const std::string& feature_name, |
| 126 | OverrideState overridden_state); |
| 127 | |
| 128 | // 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 |
| 130 | // 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 |
| 132 | // DCHECKs and tests. |
| 133 | bool CheckFeatureIdentity(const Feature& feature); |
| 134 | |
| 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 |
| 144 | // exists. |
| 145 | std::map<std::string, OverrideEntry> overrides_; |
| 146 | |
| 147 | // 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 |
| 149 | // enabled. |
| 150 | Lock feature_identity_tracker_lock_; |
| 151 | std::map<std::string, const Feature*> feature_identity_tracker_; |
| 152 | |
| 153 | // Whether this object has been fully initialized. This gets set to true as a |
| 154 | // result of FinalizeInitialization(). |
| 155 | bool initialized_; |
| 156 | |
| 157 | DISALLOW_COPY_AND_ASSIGN(FeatureList); |
| 158 | }; |
| 159 | |
| 160 | } // namespace base |
| 161 | |
| 162 | #endif // BASE_FEATURE_LIST_H_ |