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 const char kExtensionActionRedesignExperiment
[] = "ExtensionActionRedesign";
19 class CommonSwitches
{
22 : easy_off_store_install(nullptr, FeatureSwitch::DEFAULT_DISABLED
),
23 force_dev_mode_highlighting(switches::kForceDevModeHighlighting
,
24 FeatureSwitch::DEFAULT_DISABLED
),
25 prompt_for_external_extensions(
26 #if defined(CHROMIUM_BUILD)
27 switches::kPromptForExternalExtensions
,
32 FeatureSwitch::DEFAULT_ENABLED
),
34 FeatureSwitch::DEFAULT_DISABLED
),
36 error_console(switches::kErrorConsole
, FeatureSwitch::DEFAULT_DISABLED
),
37 enable_override_bookmarks_ui(switches::kEnableOverrideBookmarksUI
,
38 FeatureSwitch::DEFAULT_DISABLED
),
39 extension_action_redesign(switches::kExtensionActionRedesign
,
40 kExtensionActionRedesignExperiment
,
41 FeatureSwitch::DEFAULT_DISABLED
),
42 extension_action_redesign_override(switches::kExtensionActionRedesign
,
43 FeatureSwitch::DEFAULT_ENABLED
),
44 scripts_require_action(switches::kScriptsRequireAction
,
45 FeatureSwitch::DEFAULT_DISABLED
),
46 embedded_extension_options(switches::kEmbeddedExtensionOptions
,
47 FeatureSwitch::DEFAULT_DISABLED
),
48 trace_app_source(switches::kTraceAppSource
,
49 FeatureSwitch::DEFAULT_ENABLED
) {
52 // Enables extensions to be easily installed from sites other than the web
54 FeatureSwitch easy_off_store_install
;
56 FeatureSwitch force_dev_mode_highlighting
;
58 // Should we prompt the user before allowing external extensions to install?
60 FeatureSwitch prompt_for_external_extensions
;
62 FeatureSwitch error_console
;
63 FeatureSwitch enable_override_bookmarks_ui
;
64 FeatureSwitch extension_action_redesign
;
65 FeatureSwitch extension_action_redesign_override
;
66 FeatureSwitch scripts_require_action
;
67 FeatureSwitch embedded_extension_options
;
68 FeatureSwitch trace_app_source
;
71 base::LazyInstance
<CommonSwitches
> g_common_switches
=
72 LAZY_INSTANCE_INITIALIZER
;
76 FeatureSwitch
* FeatureSwitch::force_dev_mode_highlighting() {
77 return &g_common_switches
.Get().force_dev_mode_highlighting
;
79 FeatureSwitch
* FeatureSwitch::easy_off_store_install() {
80 return &g_common_switches
.Get().easy_off_store_install
;
82 FeatureSwitch
* FeatureSwitch::prompt_for_external_extensions() {
83 return &g_common_switches
.Get().prompt_for_external_extensions
;
85 FeatureSwitch
* FeatureSwitch::error_console() {
86 return &g_common_switches
.Get().error_console
;
88 FeatureSwitch
* FeatureSwitch::enable_override_bookmarks_ui() {
89 return &g_common_switches
.Get().enable_override_bookmarks_ui
;
91 FeatureSwitch
* FeatureSwitch::extension_action_redesign() {
92 #if defined(ENABLE_MEDIA_ROUTER)
93 // Force-enable the redesigned extension action toolbar when the Media Router
94 // is enabled. Should be removed when the toolbar redesign is used by default.
95 // See crbug.com/514694
96 // TODO(kmarshall): Remove this override.
97 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
98 "enable-media-router")) {
99 return &g_common_switches
.Get().extension_action_redesign_override
;
101 #endif // defined(ENABLE_MEDIA_ROUTER)
102 return &g_common_switches
.Get().extension_action_redesign
;
104 FeatureSwitch
* FeatureSwitch::scripts_require_action() {
105 return &g_common_switches
.Get().scripts_require_action
;
107 FeatureSwitch
* FeatureSwitch::embedded_extension_options() {
108 return &g_common_switches
.Get().embedded_extension_options
;
110 FeatureSwitch
* FeatureSwitch::trace_app_source() {
111 return &g_common_switches
.Get().trace_app_source
;
114 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch
* feature
,
117 previous_value_(feature
->GetOverrideValue()) {
118 feature_
->SetOverrideValue(
119 override_value
? OVERRIDE_ENABLED
: OVERRIDE_DISABLED
);
122 FeatureSwitch::ScopedOverride::~ScopedOverride() {
123 feature_
->SetOverrideValue(previous_value_
);
126 FeatureSwitch::FeatureSwitch(const char* switch_name
,
127 DefaultValue default_value
)
128 : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
132 FeatureSwitch::FeatureSwitch(const char* switch_name
,
133 const char* field_trial_name
,
134 DefaultValue default_value
)
135 : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
140 FeatureSwitch::FeatureSwitch(const base::CommandLine
* command_line
,
141 const char* switch_name
,
142 DefaultValue default_value
)
143 : FeatureSwitch(command_line
, switch_name
, nullptr, default_value
) {}
145 FeatureSwitch::FeatureSwitch(const base::CommandLine
* command_line
,
146 const char* switch_name
,
147 const char* field_trial_name
,
148 DefaultValue default_value
)
149 : command_line_(command_line
),
150 switch_name_(switch_name
),
151 field_trial_name_(field_trial_name
),
152 default_value_(default_value
== DEFAULT_ENABLED
),
153 override_value_(OVERRIDE_NONE
) {}
155 bool FeatureSwitch::IsEnabled() const {
156 if (override_value_
!= OVERRIDE_NONE
)
157 return override_value_
== OVERRIDE_ENABLED
;
160 return default_value_
;
162 std::string temp
= command_line_
->GetSwitchValueASCII(switch_name_
);
163 std::string switch_value
;
164 base::TrimWhitespaceASCII(temp
, base::TRIM_ALL
, &switch_value
);
166 if (switch_value
== "1")
169 if (switch_value
== "0")
172 if (!default_value_
&& command_line_
->HasSwitch(GetLegacyEnableFlag()))
175 if (default_value_
&& command_line_
->HasSwitch(GetLegacyDisableFlag()))
178 if (field_trial_name_
) {
179 std::string group_name
=
180 base::FieldTrialList::FindFullName(field_trial_name_
);
181 if (group_name
== "Enabled")
183 if (group_name
== "Disabled")
187 return default_value_
;
190 std::string
FeatureSwitch::GetLegacyEnableFlag() const {
191 DCHECK(switch_name_
);
192 return std::string("enable-") + switch_name_
;
195 std::string
FeatureSwitch::GetLegacyDisableFlag() const {
196 DCHECK(switch_name_
);
197 return std::string("disable-") + switch_name_
;
200 void FeatureSwitch::SetOverrideValue(OverrideValue override_value
) {
201 override_value_
= override_value
;
204 FeatureSwitch::OverrideValue
FeatureSwitch::GetOverrideValue() const {
205 return override_value_
;
208 } // namespace extensions