Roll src/third_party/WebKit 3c1b001:57aef96 (svn 201977:201978)
[chromium-blink-merge.git] / base / feature_list.cc
blob16eba67e99fd1ecd467cc1b0f20fde6c7f9b9149
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"
7 #include <vector>
9 #include "base/logging.h"
10 #include "base/strings/string_split.h"
12 namespace base {
14 namespace {
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);
26 } // namespace
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);
47 // static
48 bool FeatureList::IsEnabled(const Feature& feature) {
49 return GetInstance()->IsFeatureEnabled(feature);
52 // static
53 FeatureList* FeatureList::GetInstance() {
54 return g_instance;
57 // static
58 void FeatureList::SetInstance(scoped_ptr<FeatureList> instance) {
59 DCHECK(!g_instance);
60 instance->FinalizeInitialization();
62 // Note: Intentional leak of global singleton.
63 g_instance = instance.release();
66 // static
67 void FeatureList::ClearInstanceForTesting() {
68 delete g_instance;
69 g_instance = nullptr;
72 void FeatureList::FinalizeInitialization() {
73 DCHECK(!initialized_);
74 initialized_ = true;
77 bool FeatureList::IsFeatureEnabled(const Feature& feature) {
78 DCHECK(initialized_);
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;
104 return true;
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) {}
113 } // namespace base