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.
5 #include "base/feature_list.h"
9 #include "base/logging.h"
10 #include "base/strings/string_split.h"
16 // Pointer to the FeatureList instance singleton that was set via
17 // FeatureList::SetInstance(). Does not use base/memory/singleton.h in order to
18 // have more control over initialization timing. Leaky.
19 FeatureList
* g_instance
= nullptr;
21 // Splits a comma-separated string containing feature names into a vector.
22 std::vector
<std::string
> SplitFeatureListString(const std::string
& input
) {
23 return SplitString(input
, ",", TRIM_WHITESPACE
, SPLIT_WANT_NONEMPTY
);
28 FeatureList::FeatureList() : initialized_(false) {}
30 FeatureList::~FeatureList() {}
32 void FeatureList::InitializeFromCommandLine(
33 const std::string
& enable_features
,
34 const std::string
& disable_features
) {
35 DCHECK(!initialized_
);
37 // Process disabled features first, so that disabled ones take precedence over
38 // enabled ones (since RegisterOverride() uses insert()).
39 for (const auto& feature_name
: SplitFeatureListString(disable_features
)) {
40 RegisterOverride(feature_name
, OVERRIDE_DISABLE_FEATURE
);
42 for (const auto& feature_name
: SplitFeatureListString(enable_features
)) {
43 RegisterOverride(feature_name
, OVERRIDE_ENABLE_FEATURE
);
48 bool FeatureList::IsEnabled(const Feature
& feature
) {
49 return GetInstance()->IsFeatureEnabled(feature
);
53 FeatureList
* FeatureList::GetInstance() {
58 void FeatureList::SetInstance(scoped_ptr
<FeatureList
> instance
) {
60 instance
->FinalizeInitialization();
62 // Note: Intentional leak of global singleton.
63 g_instance
= instance
.release();
67 void FeatureList::ClearInstanceForTesting() {
72 void FeatureList::FinalizeInitialization() {
73 DCHECK(!initialized_
);
77 bool FeatureList::IsFeatureEnabled(const Feature
& feature
) {
79 DCHECK(CheckFeatureIdentity(feature
)) << feature
.name
;
81 auto it
= overrides_
.find(feature
.name
);
82 if (it
!= overrides_
.end()) {
83 const OverrideEntry
& entry
= it
->second
;
84 // TODO(asvitkine) Expand this section as more support is added.
85 return entry
.overridden_state
== OVERRIDE_ENABLE_FEATURE
;
87 // Otherwise, return the default state.
88 return feature
.default_state
== FEATURE_ENABLED_BY_DEFAULT
;
91 void FeatureList::RegisterOverride(const std::string
& feature_name
,
92 OverrideState overridden_state
) {
93 DCHECK(!initialized_
);
94 overrides_
.insert(make_pair(feature_name
, OverrideEntry(overridden_state
)));
97 bool FeatureList::CheckFeatureIdentity(const Feature
& feature
) {
98 AutoLock
auto_lock(feature_identity_tracker_lock_
);
100 auto it
= feature_identity_tracker_
.find(feature
.name
);
101 if (it
== feature_identity_tracker_
.end()) {
102 // If it's not tracked yet, register it.
103 feature_identity_tracker_
[feature
.name
] = &feature
;
106 // Compare address of |feature| to the existing tracked entry.
107 return it
->second
== &feature
;
110 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state
)
111 : overridden_state(overridden_state
) {}