1 // Copyright 2013 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 "extensions/common/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 "extensions/common/switches.h"
13 namespace extensions
{
17 class CommonSwitches
{
20 : easy_off_store_install(
21 switches::kEasyOffStoreExtensionInstall
,
22 FeatureSwitch::DEFAULT_DISABLED
),
24 switches::kGlobalCommands
,
25 FeatureSwitch::DEFAULT_DISABLED
),
27 switches::kScriptBadges
,
28 FeatureSwitch::DEFAULT_DISABLED
),
30 switches::kScriptBubble
,
31 FeatureSwitch::DEFAULT_DISABLED
),
32 prompt_for_external_extensions(
33 switches::kPromptForExternalExtensions
,
35 FeatureSwitch::DEFAULT_ENABLED
),
37 FeatureSwitch::DEFAULT_DISABLED
),
40 switches::kErrorConsole
,
41 FeatureSwitch::DEFAULT_DISABLED
) {}
43 FeatureSwitch easy_off_store_install
;
44 FeatureSwitch global_commands
;
45 FeatureSwitch script_badges
;
46 FeatureSwitch script_bubble
;
47 FeatureSwitch prompt_for_external_extensions
;
48 FeatureSwitch error_console
;
51 base::LazyInstance
<CommonSwitches
> g_common_switches
=
52 LAZY_INSTANCE_INITIALIZER
;
56 FeatureSwitch
* FeatureSwitch::easy_off_store_install() {
57 return &g_common_switches
.Get().easy_off_store_install
;
59 FeatureSwitch
* FeatureSwitch::global_commands() {
60 return &g_common_switches
.Get().global_commands
;
62 FeatureSwitch
* FeatureSwitch::script_badges() {
63 return &g_common_switches
.Get().script_badges
;
65 FeatureSwitch
* FeatureSwitch::script_bubble() {
66 return &g_common_switches
.Get().script_bubble
;
68 FeatureSwitch
* FeatureSwitch::prompt_for_external_extensions() {
69 return &g_common_switches
.Get().prompt_for_external_extensions
;
71 FeatureSwitch
* FeatureSwitch::error_console() {
72 return &g_common_switches
.Get().error_console
;
75 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch
* feature
,
78 previous_value_(feature
->GetOverrideValue()) {
79 feature_
->SetOverrideValue(
80 override_value
? OVERRIDE_ENABLED
: OVERRIDE_DISABLED
);
83 FeatureSwitch::ScopedOverride::~ScopedOverride() {
84 feature_
->SetOverrideValue(previous_value_
);
87 FeatureSwitch::FeatureSwitch(const char* switch_name
,
88 DefaultValue default_value
) {
89 Init(CommandLine::ForCurrentProcess(), switch_name
, default_value
);
92 FeatureSwitch::FeatureSwitch(const CommandLine
* command_line
,
93 const char* switch_name
,
94 DefaultValue default_value
) {
95 Init(command_line
, switch_name
, default_value
);
98 void FeatureSwitch::Init(const CommandLine
* command_line
,
99 const char* switch_name
,
100 DefaultValue default_value
) {
101 command_line_
= command_line
;
102 switch_name_
= switch_name
;
103 default_value_
= default_value
== DEFAULT_ENABLED
;
104 override_value_
= OVERRIDE_NONE
;
107 bool FeatureSwitch::IsEnabled() const {
108 if (override_value_
!= OVERRIDE_NONE
)
109 return override_value_
== OVERRIDE_ENABLED
;
111 std::string temp
= command_line_
->GetSwitchValueASCII(switch_name_
);
112 std::string switch_value
;
113 TrimWhitespaceASCII(temp
, TRIM_ALL
, &switch_value
);
115 if (switch_value
== "1")
118 if (switch_value
== "0")
121 if (!default_value_
&& command_line_
->HasSwitch(GetLegacyEnableFlag()))
124 if (default_value_
&& command_line_
->HasSwitch(GetLegacyDisableFlag()))
127 return default_value_
;
130 std::string
FeatureSwitch::GetLegacyEnableFlag() const {
131 return std::string("enable-") + switch_name_
;
134 std::string
FeatureSwitch::GetLegacyDisableFlag() const {
135 return std::string("disable-") + switch_name_
;
138 void FeatureSwitch::SetOverrideValue(OverrideValue override_value
) {
139 override_value_
= override_value
;
142 FeatureSwitch::OverrideValue
FeatureSwitch::GetOverrideValue() const {
143 return override_value_
;
146 } // namespace extensions