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(
22 FeatureSwitch::DEFAULT_DISABLED
),
23 force_dev_mode_highlighting(
24 switches::kForceDevModeHighlighting
,
25 FeatureSwitch::DEFAULT_DISABLED
),
26 prompt_for_external_extensions(
29 FeatureSwitch::DEFAULT_ENABLED
),
31 FeatureSwitch::DEFAULT_DISABLED
),
34 switches::kErrorConsole
,
35 FeatureSwitch::DEFAULT_DISABLED
),
36 enable_override_bookmarks_ui(
37 switches::kEnableOverrideBookmarksUI
,
38 FeatureSwitch::DEFAULT_DISABLED
),
39 extension_action_redesign(
40 switches::kExtensionActionRedesign
,
41 FeatureSwitch::DEFAULT_DISABLED
),
42 scripts_require_action(switches::kScriptsRequireAction
,
43 FeatureSwitch::DEFAULT_DISABLED
),
44 embedded_extension_options(
45 switches::kEmbeddedExtensionOptions
,
46 FeatureSwitch::DEFAULT_DISABLED
),
47 app_view(switches::kAppView
,
48 FeatureSwitch::DEFAULT_DISABLED
) {}
50 // Enables extensions to be easily installed from sites other than the web
52 FeatureSwitch easy_off_store_install
;
54 FeatureSwitch force_dev_mode_highlighting
;
56 // Should we prompt the user before allowing external extensions to install?
58 FeatureSwitch prompt_for_external_extensions
;
60 FeatureSwitch error_console
;
61 FeatureSwitch enable_override_bookmarks_ui
;
62 FeatureSwitch extension_action_redesign
;
63 FeatureSwitch scripts_require_action
;
64 FeatureSwitch embedded_extension_options
;
65 FeatureSwitch app_view
;
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 return &g_common_switches
.Get().extension_action_redesign
;
91 FeatureSwitch
* FeatureSwitch::scripts_require_action() {
92 return &g_common_switches
.Get().scripts_require_action
;
94 FeatureSwitch
* FeatureSwitch::embedded_extension_options() {
95 return &g_common_switches
.Get().embedded_extension_options
;
97 FeatureSwitch
* FeatureSwitch::app_view() {
98 return &g_common_switches
.Get().app_view
;
101 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch
* feature
,
104 previous_value_(feature
->GetOverrideValue()) {
105 feature_
->SetOverrideValue(
106 override_value
? OVERRIDE_ENABLED
: OVERRIDE_DISABLED
);
109 FeatureSwitch::ScopedOverride::~ScopedOverride() {
110 feature_
->SetOverrideValue(previous_value_
);
113 FeatureSwitch::FeatureSwitch(const char* switch_name
,
114 DefaultValue default_value
) {
115 Init(CommandLine::ForCurrentProcess(), switch_name
, default_value
);
118 FeatureSwitch::FeatureSwitch(const CommandLine
* command_line
,
119 const char* switch_name
,
120 DefaultValue default_value
) {
121 Init(command_line
, switch_name
, default_value
);
124 void FeatureSwitch::Init(const CommandLine
* command_line
,
125 const char* switch_name
,
126 DefaultValue default_value
) {
127 command_line_
= command_line
;
128 switch_name_
= switch_name
;
129 default_value_
= default_value
== DEFAULT_ENABLED
;
130 override_value_
= OVERRIDE_NONE
;
133 bool FeatureSwitch::IsEnabled() const {
134 if (override_value_
!= OVERRIDE_NONE
)
135 return override_value_
== OVERRIDE_ENABLED
;
138 return default_value_
;
140 std::string temp
= command_line_
->GetSwitchValueASCII(switch_name_
);
141 std::string switch_value
;
142 base::TrimWhitespaceASCII(temp
, base::TRIM_ALL
, &switch_value
);
144 if (switch_value
== "1")
147 if (switch_value
== "0")
150 if (!default_value_
&& command_line_
->HasSwitch(GetLegacyEnableFlag()))
153 if (default_value_
&& command_line_
->HasSwitch(GetLegacyDisableFlag()))
156 return default_value_
;
159 std::string
FeatureSwitch::GetLegacyEnableFlag() const {
160 DCHECK(switch_name_
);
161 return std::string("enable-") + switch_name_
;
164 std::string
FeatureSwitch::GetLegacyDisableFlag() const {
165 DCHECK(switch_name_
);
166 return std::string("disable-") + switch_name_
;
169 void FeatureSwitch::SetOverrideValue(OverrideValue override_value
) {
170 override_value_
= override_value
;
173 FeatureSwitch::OverrideValue
FeatureSwitch::GetOverrideValue() const {
174 return override_value_
;
177 } // namespace extensions