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

Unified Diff: base/feature_list.cc

Issue 2138923002: Updating FeatureList default initialization pattern (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2785
Patch Set: Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/feature_list.h ('k') | base/feature_list_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/feature_list.cc
diff --git a/base/feature_list.cc b/base/feature_list.cc
index 46732108dd6fcf0004e14b7ad6da06faf3561718..435165e10caa004c6485f569610427cdc1938be3 100644
--- a/base/feature_list.cc
+++ b/base/feature_list.cc
@@ -23,6 +23,9 @@ namespace {
// have more control over initialization timing. Leaky.
FeatureList* g_instance = nullptr;
+// Tracks whether the FeatureList instance was initialized via an accessor.
+bool g_initialized_from_accessor = false;
+
// Some characters are not allowed to appear in feature names or the associated
// field trial names, as they are used as special characters for command-line
// serialization. This function checks that the strings are ASCII (since they
@@ -35,10 +38,7 @@ bool IsValidFeatureOrFieldTrialName(const std::string& name) {
} // namespace
-FeatureList::FeatureList()
- : initialized_(false),
- initialized_from_command_line_(false) {
-}
+FeatureList::FeatureList() {}
FeatureList::~FeatureList() {}
@@ -133,7 +133,11 @@ void FeatureList::GetFeatureOverrides(std::string* enable_overrides,
// static
bool FeatureList::IsEnabled(const Feature& feature) {
- return GetInstance()->IsFeatureEnabled(feature);
+ if (!g_instance) {
+ g_initialized_from_accessor = true;
+ return feature.default_state == FEATURE_ENABLED_BY_DEFAULT;
+ }
+ return g_instance->IsFeatureEnabled(feature);
}
// static
@@ -158,6 +162,10 @@ bool FeatureList::InitializeInstance(const std::string& enable_features,
// For example, we initialize an instance in chrome/browser/
// chrome_browser_main.cc and do not override it in content/browser/
// browser_main_loop.cc.
+ // If the singleton was previously initialized from within an accessor, we
+ // want to prevent callers from reinitializing the singleton and masking the
+ // accessor call(s) which likely returned incorrect information.
+ CHECK(!g_initialized_from_accessor);
bool instance_existed_before = false;
if (g_instance) {
if (g_instance->initialized_from_command_line_)
@@ -192,6 +200,7 @@ void FeatureList::SetInstance(std::unique_ptr<FeatureList> instance) {
void FeatureList::ClearInstanceForTesting() {
delete g_instance;
g_instance = nullptr;
+ g_initialized_from_accessor = false;
}
void FeatureList::FinalizeInitialization() {
« no previous file with comments | « base/feature_list.h ('k') | base/feature_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698