Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: base/metrics/feature_list.cc

Issue 1278403003: Initial implementation of FeatureList in base/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More comments addressed. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/metrics/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) {
Ilya Sherman 2015/09/01 03:55:25 Currently, there is no guard preventing this metho
Alexei Svitkine (slow) 2015/09/01 15:53:43 I don't think it's really worth guarding against -
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, false);
41 }
42 for (const auto& feature_name : SplitFeatureListString(enable_features)) {
43 RegisterOverride(feature_name, true);
44 }
45 }
46
47 void FeatureList::FinalizeInitialization() {
48 DCHECK(!initialized_);
49 initialized_ = true;
50 }
51
52 bool FeatureList::IsFeatureEnabled(const Feature& feature) {
53 DCHECK(initialized_);
54 DCHECK(CheckFeatureIdentity(feature)) << feature.name;
55
56 auto it = overrides_.find(feature.name);
57 if (it != overrides_.end()) {
58 auto entry = &*it->second;
Ilya Sherman 2015/09/01 03:55:25 nit: Boy, this line makes me do a double-take. I
Alexei Svitkine (slow) 2015/09/01 15:53:43 Done. In the future it won't be able to be a const
59 // TODO(asvitkine) Expand this section as more support is added.
60 return entry->overriden_state;
61 }
62 // Otherwise, return the default state.
63 return feature.default_state;
64 }
65
66 // static
67 bool FeatureList::IsEnabled(const Feature& feature) {
68 DCHECK(GetInstance());
Ilya Sherman 2015/09/01 03:55:25 nit: This line seems a little silly, since the fol
Alexei Svitkine (slow) 2015/09/01 15:53:43 Done.
69 return GetInstance()->IsFeatureEnabled(feature);
70 }
71
72 // static
73 FeatureList* FeatureList::GetInstance() {
74 return g_instance;
75 }
76
77 // static
78 void FeatureList::SetInstance(scoped_ptr<FeatureList> instance) {
79 DCHECK(!g_instance);
80 instance->FinalizeInitialization();
81
82 // Note: Intentional leak of global singleton.
83 g_instance = instance.release();
84 }
85
86 // static
87 void FeatureList::ClearInstanceForTesting() {
88 delete g_instance;
89 g_instance = nullptr;
90 }
91
92 void FeatureList::RegisterOverride(const std::string& feature_name,
93 bool overriden_state) {
94 DCHECK(!initialized_);
95 overrides_.insert(feature_name,
96 make_scoped_ptr(new OverrideEntry(overriden_state)));
97 }
98
99 bool FeatureList::CheckFeatureIdentity(const Feature& feature) {
100 AutoLock auto_lock(feature_identity_tracker_lock_);
101
102 auto it = feature_identity_tracker_.find(feature.name);
103 if (it == feature_identity_tracker_.end()) {
104 // If it's not tracked yet, register it.
105 feature_identity_tracker_[feature.name] = &feature;
106 return true;
107 }
108 // Compare address of |feature| to the existing tracked entry.
109 return it->second == &feature;
110 }
111
112 FeatureList::OverrideEntry::OverrideEntry(bool overriden_state)
113 : overriden_state(overriden_state) {}
114
115 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698