Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / feature_switch.cc
blobbf7b538564f0b152e9c2ea9ffab67dfd30b900ca
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 class CommonSwitches {
18 public:
19 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,
26 #else
27 NULL,
28 #endif
29 #if defined(OS_WIN)
30 FeatureSwitch::DEFAULT_ENABLED),
31 #else
32 FeatureSwitch::DEFAULT_DISABLED),
33 #endif
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 surface_worker(switches::kSurfaceWorker,
46 FeatureSwitch::DEFAULT_DISABLED),
47 trace_app_source(switches::kTraceAppSource,
48 FeatureSwitch::DEFAULT_ENABLED) {
51 // Enables extensions to be easily installed from sites other than the web
52 // store.
53 FeatureSwitch easy_off_store_install;
55 FeatureSwitch force_dev_mode_highlighting;
57 // Should we prompt the user before allowing external extensions to install?
58 // Default is yes.
59 FeatureSwitch prompt_for_external_extensions;
61 FeatureSwitch error_console;
62 FeatureSwitch enable_override_bookmarks_ui;
63 FeatureSwitch extension_action_redesign;
64 FeatureSwitch extension_action_redesign_override;
65 FeatureSwitch scripts_require_action;
66 FeatureSwitch embedded_extension_options;
67 FeatureSwitch surface_worker;
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::surface_worker() {
111 return &g_common_switches.Get().surface_worker;
113 FeatureSwitch* FeatureSwitch::trace_app_source() {
114 return &g_common_switches.Get().trace_app_source;
117 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
118 bool override_value)
119 : feature_(feature),
120 previous_value_(feature->GetOverrideValue()) {
121 feature_->SetOverrideValue(
122 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
125 FeatureSwitch::ScopedOverride::~ScopedOverride() {
126 feature_->SetOverrideValue(previous_value_);
129 FeatureSwitch::FeatureSwitch(const char* switch_name,
130 DefaultValue default_value) {
131 Init(base::CommandLine::ForCurrentProcess(), switch_name, default_value);
134 FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line,
135 const char* switch_name,
136 DefaultValue default_value) {
137 Init(command_line, switch_name, default_value);
140 void FeatureSwitch::Init(const base::CommandLine* command_line,
141 const char* switch_name,
142 DefaultValue default_value) {
143 command_line_ = command_line;
144 switch_name_ = switch_name;
145 default_value_ = default_value == DEFAULT_ENABLED;
146 override_value_ = OVERRIDE_NONE;
149 bool FeatureSwitch::IsEnabled() const {
150 if (override_value_ != OVERRIDE_NONE)
151 return override_value_ == OVERRIDE_ENABLED;
153 if (!switch_name_)
154 return default_value_;
156 std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
157 std::string switch_value;
158 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value);
160 if (switch_value == "1")
161 return true;
163 if (switch_value == "0")
164 return false;
166 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
167 return true;
169 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
170 return false;
172 return default_value_;
175 std::string FeatureSwitch::GetLegacyEnableFlag() const {
176 DCHECK(switch_name_);
177 return std::string("enable-") + switch_name_;
180 std::string FeatureSwitch::GetLegacyDisableFlag() const {
181 DCHECK(switch_name_);
182 return std::string("disable-") + switch_name_;
185 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
186 override_value_ = override_value;
189 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
190 return override_value_;
193 } // namespace extensions