1 // Copyright (c) 2012 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 "chrome/common/extensions/feature_switch.h"
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_util.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "extensions/common/switches.h"
14 namespace extensions {
18 class CommonSwitches {
21 : easy_off_store_install(
22 ::switches::kEasyOffStoreExtensionInstall,
23 FeatureSwitch::DEFAULT_DISABLED),
25 ::switches::kGlobalCommands,
26 FeatureSwitch::DEFAULT_DISABLED),
28 ::switches::kScriptBadges,
29 FeatureSwitch::DEFAULT_DISABLED),
31 ::switches::kScriptBubble,
32 FeatureSwitch::DEFAULT_DISABLED),
33 prompt_for_external_extensions(
34 ::switches::kPromptForExternalExtensions,
36 FeatureSwitch::DEFAULT_ENABLED),
38 FeatureSwitch::DEFAULT_DISABLED),
41 switches::kErrorConsole,
42 FeatureSwitch::DEFAULT_DISABLED) {}
44 FeatureSwitch easy_off_store_install;
45 FeatureSwitch global_commands;
46 FeatureSwitch script_badges;
47 FeatureSwitch script_bubble;
48 FeatureSwitch prompt_for_external_extensions;
49 FeatureSwitch error_console;
52 base::LazyInstance<CommonSwitches> g_common_switches =
53 LAZY_INSTANCE_INITIALIZER;
57 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
58 return &g_common_switches.Get().easy_off_store_install;
60 FeatureSwitch* FeatureSwitch::global_commands() {
61 return &g_common_switches.Get().global_commands;
63 FeatureSwitch* FeatureSwitch::script_badges() {
64 return &g_common_switches.Get().script_badges;
66 FeatureSwitch* FeatureSwitch::script_bubble() {
67 return &g_common_switches.Get().script_bubble;
69 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
70 return &g_common_switches.Get().prompt_for_external_extensions;
72 FeatureSwitch* FeatureSwitch::error_console() {
73 return &g_common_switches.Get().error_console;
76 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
79 previous_value_(feature->GetOverrideValue()) {
80 feature_->SetOverrideValue(
81 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
84 FeatureSwitch::ScopedOverride::~ScopedOverride() {
85 feature_->SetOverrideValue(previous_value_);
88 FeatureSwitch::FeatureSwitch(const char* switch_name,
89 DefaultValue default_value) {
90 Init(CommandLine::ForCurrentProcess(), switch_name, default_value);
93 FeatureSwitch::FeatureSwitch(const CommandLine* command_line,
94 const char* switch_name,
95 DefaultValue default_value) {
96 Init(command_line, switch_name, default_value);
99 void FeatureSwitch::Init(const CommandLine* command_line,
100 const char* switch_name,
101 DefaultValue default_value) {
102 command_line_ = command_line;
103 switch_name_ = switch_name;
104 default_value_ = default_value == DEFAULT_ENABLED;
105 override_value_ = OVERRIDE_NONE;
108 bool FeatureSwitch::IsEnabled() const {
109 if (override_value_ != OVERRIDE_NONE)
110 return override_value_ == OVERRIDE_ENABLED;
112 std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
113 std::string switch_value;
114 TrimWhitespaceASCII(temp, TRIM_ALL, &switch_value);
116 if (switch_value == "1")
119 if (switch_value == "0")
122 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
125 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
128 return default_value_;
131 std::string FeatureSwitch::GetLegacyEnableFlag() const {
132 return std::string("enable-") + switch_name_;
135 std::string FeatureSwitch::GetLegacyDisableFlag() const {
136 return std::string("disable-") + switch_name_;
139 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
140 override_value_ = override_value;
143 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
144 return override_value_;
147 } // namespace extensions