Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_METRICS_FEATURE_LIST_H_ | |
| 6 #define BASE_METRICS_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. | |
| 20 enum FeatureState { | |
|
Ilya Sherman
2015/09/01 20:17:17
Optional nit: I tend to prefer enum class, because
Alexei Svitkine (slow)
2015/09/01 21:18:39
Started the discussion on this on your other comme
| |
| 21 FEATURE_DISABLED, | |
| 22 FEATURE_ENABLED, | |
| 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; | |
|
Ilya Sherman
2015/09/01 20:17:17
Optional nit: I think it would be clearer to have
Alexei Svitkine (slow)
2015/09/01 21:18:39
Hmm, so if combining with your other comment above
Ilya Sherman
2015/09/01 21:20:37
Well, if the enum is named DefaultFeatureState, th
Alexei Svitkine (slow)
2015/09/01 21:36:00
True. It won't be as obvious as ENABLED_BY_DEFAULT
Alexei Svitkine (slow)
2015/09/02 15:52:59
Thinking about this more, I liked the verbosity /
| |
| 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 | |
| 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 // Finalizes the initialization state of the FeatureList instance, so that no | |
| 107 // further overrides can be registered. For the singleton feature list, this | |
| 108 // gets called when it is registered via SetInstance(). | |
|
Ilya Sherman
2015/09/01 20:17:17
nit: Please update this comment.
Alexei Svitkine (slow)
2015/09/01 21:18:39
Done.
| |
| 109 void FinalizeInitialization(); | |
| 110 | |
| 111 // Returns whether the given |feature| is enabled. This member function is | |
| 112 // normally called through the public static IsEnabled() function, which | |
| 113 // corresponds to GetInstance()->IsFeatureEnabled(). Must only be called on a | |
| 114 // FeatureList that has been initialized (FinalizeInitialization() called). | |
|
Ilya Sherman
2015/09/01 20:17:17
nit: Please update this comment.
Alexei Svitkine (slow)
2015/09/01 21:18:39
Done.
| |
| 115 bool IsFeatureEnabled(const Feature& feature); | |
| 116 | |
| 117 // Registers an override for feature |feature_name|. The override specifies | |
| 118 // whether the feature should be on or off (via |overridden_state|), which | |
| 119 // will take precedence over the feature's default state. | |
| 120 void RegisterOverride(const std::string& feature_name, | |
| 121 FeatureState overridden_state); | |
| 122 | |
| 123 // Verifies that there's only a single definition of a Feature struct for a | |
| 124 // given feature name. Keeps track of the first seen Feature struct for each | |
| 125 // feature. Returns false when called on a Feature struct with a different | |
| 126 // address than the first one it saw for that feature name. Used only from | |
| 127 // DCHECKs and tests. | |
| 128 bool CheckFeatureIdentity(const Feature& feature); | |
| 129 | |
| 130 struct OverrideEntry { | |
| 131 // The overridden enable (on/off) state of the feature. | |
| 132 const FeatureState overridden_state; | |
| 133 | |
| 134 // TODO(asvitkine): Expand this as more support is added. | |
| 135 | |
| 136 explicit OverrideEntry(FeatureState overridden_state); | |
| 137 }; | |
| 138 // Map from feature name to an OverrideEntry struct for the feature, if it | |
| 139 // exists. | |
| 140 std::map<std::string, OverrideEntry> overrides_; | |
| 141 | |
| 142 // Locked map that keeps track of seen features, to ensure a single feature is | |
| 143 // only defined once. This verification is only done in builds with DCHECKs | |
| 144 // enabled. | |
| 145 Lock feature_identity_tracker_lock_; | |
| 146 std::map<std::string, const Feature*> feature_identity_tracker_; | |
| 147 | |
| 148 // Whether this object has been fully initialized. This gets set to true as a | |
| 149 // result of FinalizeInitialization(). | |
| 150 bool initialized_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(FeatureList); | |
| 153 }; | |
| 154 | |
| 155 } // namespace base | |
| 156 | |
| 157 #endif // BASE_METRICS_FEATURE_LIST_H_ | |
| OLD | NEW |