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
),
23 force_dev_mode_highlighting(
24 switches::kForceDevModeHighlighting
,
25 FeatureSwitch::DEFAULT_DISABLED
),
27 switches::kGlobalCommands
,
28 FeatureSwitch::DEFAULT_DISABLED
),
29 prompt_for_external_extensions(
30 switches::kPromptForExternalExtensions
,
32 FeatureSwitch::DEFAULT_ENABLED
),
34 FeatureSwitch::DEFAULT_DISABLED
),
37 switches::kErrorConsole
,
38 FeatureSwitch::DEFAULT_DISABLED
),
39 enable_override_bookmarks_ui(
40 switches::kEnableOverrideBookmarksUI
,
41 FeatureSwitch::DEFAULT_DISABLED
) {}
43 FeatureSwitch easy_off_store_install
;
44 FeatureSwitch force_dev_mode_highlighting
;
45 FeatureSwitch global_commands
;
46 FeatureSwitch prompt_for_external_extensions
;
47 FeatureSwitch error_console
;
48 FeatureSwitch enable_override_bookmarks_ui
;
51 base::LazyInstance
<CommonSwitches
> g_common_switches
=
52 LAZY_INSTANCE_INITIALIZER
;
56 FeatureSwitch
* FeatureSwitch::force_dev_mode_highlighting() {
57 return &g_common_switches
.Get().force_dev_mode_highlighting
;
59 FeatureSwitch
* FeatureSwitch::easy_off_store_install() {
60 return &g_common_switches
.Get().easy_off_store_install
;
62 FeatureSwitch
* FeatureSwitch::global_commands() {
63 return &g_common_switches
.Get().global_commands
;
65 FeatureSwitch
* FeatureSwitch::prompt_for_external_extensions() {
66 return &g_common_switches
.Get().prompt_for_external_extensions
;
68 FeatureSwitch
* FeatureSwitch::error_console() {
69 return &g_common_switches
.Get().error_console
;
71 FeatureSwitch
* FeatureSwitch::enable_override_bookmarks_ui() {
72 return &g_common_switches
.Get().enable_override_bookmarks_ui
;
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