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 #include "base/feature_list.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/string_split.h" |
| 11 |
| 12 namespace base { |
| 13 |
| 14 namespace { |
| 15 |
| 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; |
| 20 |
| 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); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 FeatureList::FeatureList() : initialized_(false) {} |
| 29 |
| 30 FeatureList::~FeatureList() {} |
| 31 |
| 32 void FeatureList::InitializeFromCommandLine( |
| 33 const std::string& enable_features, |
| 34 const std::string& disable_features) { |
| 35 DCHECK(!initialized_); |
| 36 |
| 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); |
| 41 } |
| 42 for (const auto& feature_name : SplitFeatureListString(enable_features)) { |
| 43 RegisterOverride(feature_name, OVERRIDE_ENABLE_FEATURE); |
| 44 } |
| 45 } |
| 46 |
| 47 // static |
| 48 bool FeatureList::IsEnabled(const Feature& feature) { |
| 49 return GetInstance()->IsFeatureEnabled(feature); |
| 50 } |
| 51 |
| 52 // static |
| 53 FeatureList* FeatureList::GetInstance() { |
| 54 return g_instance; |
| 55 } |
| 56 |
| 57 // static |
| 58 void FeatureList::SetInstance(scoped_ptr<FeatureList> instance) { |
| 59 DCHECK(!g_instance); |
| 60 instance->FinalizeInitialization(); |
| 61 |
| 62 // Note: Intentional leak of global singleton. |
| 63 g_instance = instance.release(); |
| 64 } |
| 65 |
| 66 // static |
| 67 void FeatureList::ClearInstanceForTesting() { |
| 68 delete g_instance; |
| 69 g_instance = nullptr; |
| 70 } |
| 71 |
| 72 void FeatureList::FinalizeInitialization() { |
| 73 DCHECK(!initialized_); |
| 74 initialized_ = true; |
| 75 } |
| 76 |
| 77 bool FeatureList::IsFeatureEnabled(const Feature& feature) { |
| 78 DCHECK(initialized_); |
| 79 DCHECK(CheckFeatureIdentity(feature)) << feature.name; |
| 80 |
| 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; |
| 86 } |
| 87 // Otherwise, return the default state. |
| 88 return feature.default_state == FEATURE_ENABLED_BY_DEFAULT; |
| 89 } |
| 90 |
| 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))); |
| 95 } |
| 96 |
| 97 bool FeatureList::CheckFeatureIdentity(const Feature& feature) { |
| 98 AutoLock auto_lock(feature_identity_tracker_lock_); |
| 99 |
| 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; |
| 105 } |
| 106 // Compare address of |feature| to the existing tracked entry. |
| 107 return it->second == &feature; |
| 108 } |
| 109 |
| 110 FeatureList::OverrideEntry::OverrideEntry(OverrideState overridden_state) |
| 111 : overridden_state(overridden_state) {} |
| 112 |
| 113 } // namespace base |
OLD | NEW |