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(NULL
, FeatureSwitch::DEFAULT_DISABLED
),
21 force_dev_mode_highlighting(switches::kForceDevModeHighlighting
,
22 FeatureSwitch::DEFAULT_DISABLED
),
23 prompt_for_external_extensions(
24 #if defined(CHROMIUM_BUILD)
25 switches::kPromptForExternalExtensions
,
30 FeatureSwitch::DEFAULT_ENABLED
),
32 FeatureSwitch::DEFAULT_DISABLED
),
34 error_console(switches::kErrorConsole
, FeatureSwitch::DEFAULT_DISABLED
),
35 enable_override_bookmarks_ui(switches::kEnableOverrideBookmarksUI
,
36 FeatureSwitch::DEFAULT_DISABLED
),
37 extension_action_redesign(switches::kExtensionActionRedesign
,
38 FeatureSwitch::DEFAULT_DISABLED
),
39 extension_action_redesign_override(switches::kExtensionActionRedesign
,
40 FeatureSwitch::DEFAULT_ENABLED
),
41 scripts_require_action(switches::kScriptsRequireAction
,
42 FeatureSwitch::DEFAULT_DISABLED
),
43 embedded_extension_options(switches::kEmbeddedExtensionOptions
,
44 FeatureSwitch::DEFAULT_DISABLED
),
45 trace_app_source(switches::kTraceAppSource
,
46 FeatureSwitch::DEFAULT_ENABLED
) {
49 // Enables extensions to be easily installed from sites other than the web
51 FeatureSwitch easy_off_store_install
;
53 FeatureSwitch force_dev_mode_highlighting
;
55 // Should we prompt the user before allowing external extensions to install?
57 FeatureSwitch prompt_for_external_extensions
;
59 FeatureSwitch error_console
;
60 FeatureSwitch enable_override_bookmarks_ui
;
61 FeatureSwitch extension_action_redesign
;
62 FeatureSwitch extension_action_redesign_override
;
63 FeatureSwitch scripts_require_action
;
64 FeatureSwitch embedded_extension_options
;
65 FeatureSwitch trace_app_source
;
68 base::LazyInstance
<CommonSwitches
> g_common_switches
=
69 LAZY_INSTANCE_INITIALIZER
;
73 FeatureSwitch
* FeatureSwitch::force_dev_mode_highlighting() {
74 return &g_common_switches
.Get().force_dev_mode_highlighting
;
76 FeatureSwitch
* FeatureSwitch::easy_off_store_install() {
77 return &g_common_switches
.Get().easy_off_store_install
;
79 FeatureSwitch
* FeatureSwitch::prompt_for_external_extensions() {
80 return &g_common_switches
.Get().prompt_for_external_extensions
;
82 FeatureSwitch
* FeatureSwitch::error_console() {
83 return &g_common_switches
.Get().error_console
;
85 FeatureSwitch
* FeatureSwitch::enable_override_bookmarks_ui() {
86 return &g_common_switches
.Get().enable_override_bookmarks_ui
;
88 FeatureSwitch
* FeatureSwitch::extension_action_redesign() {
89 #if defined(ENABLE_MEDIA_ROUTER)
90 // Force-enable the redesigned extension action toolbar when the Media Router
91 // is enabled. Should be removed when the toolbar redesign is used by default.
92 // See crbug.com/514694
93 // TODO(kmarshall): Remove this override.
94 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
95 "enable-media-router")) {
96 return &g_common_switches
.Get().extension_action_redesign_override
;
98 #endif // defined(ENABLE_MEDIA_ROUTER)
99 return &g_common_switches
.Get().extension_action_redesign
;
101 FeatureSwitch
* FeatureSwitch::scripts_require_action() {
102 return &g_common_switches
.Get().scripts_require_action
;
104 FeatureSwitch
* FeatureSwitch::embedded_extension_options() {
105 return &g_common_switches
.Get().embedded_extension_options
;
107 FeatureSwitch
* FeatureSwitch::trace_app_source() {
108 return &g_common_switches
.Get().trace_app_source
;
111 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch
* feature
,
114 previous_value_(feature
->GetOverrideValue()) {
115 feature_
->SetOverrideValue(
116 override_value
? OVERRIDE_ENABLED
: OVERRIDE_DISABLED
);
119 FeatureSwitch::ScopedOverride::~ScopedOverride() {
120 feature_
->SetOverrideValue(previous_value_
);
123 FeatureSwitch::FeatureSwitch(const char* switch_name
,
124 DefaultValue default_value
) {
125 Init(base::CommandLine::ForCurrentProcess(), switch_name
, default_value
);
128 FeatureSwitch::FeatureSwitch(const base::CommandLine
* command_line
,
129 const char* switch_name
,
130 DefaultValue default_value
) {
131 Init(command_line
, switch_name
, default_value
);
134 void FeatureSwitch::Init(const base::CommandLine
* command_line
,
135 const char* switch_name
,
136 DefaultValue default_value
) {
137 command_line_
= command_line
;
138 switch_name_
= switch_name
;
139 default_value_
= default_value
== DEFAULT_ENABLED
;
140 override_value_
= OVERRIDE_NONE
;
143 bool FeatureSwitch::IsEnabled() const {
144 if (override_value_
!= OVERRIDE_NONE
)
145 return override_value_
== OVERRIDE_ENABLED
;
148 return default_value_
;
150 std::string temp
= command_line_
->GetSwitchValueASCII(switch_name_
);
151 std::string switch_value
;
152 base::TrimWhitespaceASCII(temp
, base::TRIM_ALL
, &switch_value
);
154 if (switch_value
== "1")
157 if (switch_value
== "0")
160 if (!default_value_
&& command_line_
->HasSwitch(GetLegacyEnableFlag()))
163 if (default_value_
&& command_line_
->HasSwitch(GetLegacyDisableFlag()))
166 return default_value_
;
169 std::string
FeatureSwitch::GetLegacyEnableFlag() const {
170 DCHECK(switch_name_
);
171 return std::string("enable-") + switch_name_
;
174 std::string
FeatureSwitch::GetLegacyDisableFlag() const {
175 DCHECK(switch_name_
);
176 return std::string("disable-") + switch_name_
;
179 void FeatureSwitch::SetOverrideValue(OverrideValue override_value
) {
180 override_value_
= override_value
;
183 FeatureSwitch::OverrideValue
FeatureSwitch::GetOverrideValue() const {
184 return override_value_
;
187 } // namespace extensions