Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / feature_switch.cc
blob5d6bb6d91738840fbacec681feaf6f70426148e9
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 {
15 namespace {
17 const char kExtensionActionRedesignExperiment[] = "ExtensionActionRedesign";
19 class CommonSwitches {
20 public:
21 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,
28 #else
29 nullptr,
30 #endif
31 #if defined(OS_WIN)
32 FeatureSwitch::DEFAULT_ENABLED),
33 #else
34 FeatureSwitch::DEFAULT_DISABLED),
35 #endif
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
53 // store.
54 FeatureSwitch easy_off_store_install;
56 FeatureSwitch force_dev_mode_highlighting;
58 // Should we prompt the user before allowing external extensions to install?
59 // Default is yes.
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;
74 } // namespace
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,
115 bool override_value)
116 : feature_(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(),
129 switch_name,
130 default_value) {}
132 FeatureSwitch::FeatureSwitch(const char* switch_name,
133 const char* field_trial_name,
134 DefaultValue default_value)
135 : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
136 switch_name,
137 field_trial_name,
138 default_value) {}
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;
159 if (!switch_name_)
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")
167 return true;
169 if (switch_value == "0")
170 return false;
172 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
173 return true;
175 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
176 return false;
178 if (field_trial_name_) {
179 std::string group_name =
180 base::FieldTrialList::FindFullName(field_trial_name_);
181 if (group_name == "Enabled")
182 return true;
183 if (group_name == "Disabled")
184 return false;
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