Extension Toolbar redesign, part 1 (overflow)
[chromium-blink-merge.git] / extensions / common / feature_switch.cc
blobeaee4a7fb1d152860ed78313cb3543acdfa8b811
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(
21 NULL,
22 FeatureSwitch::DEFAULT_DISABLED),
23 force_dev_mode_highlighting(
24 switches::kForceDevModeHighlighting,
25 FeatureSwitch::DEFAULT_DISABLED),
26 global_commands(
27 switches::kGlobalCommands,
28 #if defined(OS_CHROMEOS)
29 FeatureSwitch::DEFAULT_DISABLED),
30 #else
31 FeatureSwitch::DEFAULT_ENABLED),
32 #endif
33 prompt_for_external_extensions(
34 NULL,
35 #if defined(OS_WIN)
36 FeatureSwitch::DEFAULT_ENABLED),
37 #else
38 FeatureSwitch::DEFAULT_DISABLED),
39 #endif
40 error_console(
41 switches::kErrorConsole,
42 FeatureSwitch::DEFAULT_DISABLED),
43 enable_override_bookmarks_ui(
44 switches::kEnableOverrideBookmarksUI,
45 FeatureSwitch::DEFAULT_DISABLED),
46 extension_action_redesign(
47 switches::kExtensionActionRedesign,
48 FeatureSwitch::DEFAULT_DISABLED),
49 scripts_require_action(switches::kScriptsRequireAction,
50 FeatureSwitch::DEFAULT_DISABLED) {}
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;
57 FeatureSwitch global_commands;
59 // Should we prompt the user before allowing external extensions to install?
60 // Default is yes.
61 FeatureSwitch prompt_for_external_extensions;
63 FeatureSwitch error_console;
64 FeatureSwitch enable_override_bookmarks_ui;
65 FeatureSwitch extension_action_redesign;
66 FeatureSwitch scripts_require_action;
69 base::LazyInstance<CommonSwitches> g_common_switches =
70 LAZY_INSTANCE_INITIALIZER;
72 } // namespace
74 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() {
75 return &g_common_switches.Get().force_dev_mode_highlighting;
77 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
78 return &g_common_switches.Get().easy_off_store_install;
80 FeatureSwitch* FeatureSwitch::global_commands() {
81 return &g_common_switches.Get().global_commands;
83 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
84 return &g_common_switches.Get().prompt_for_external_extensions;
86 FeatureSwitch* FeatureSwitch::error_console() {
87 return &g_common_switches.Get().error_console;
89 FeatureSwitch* FeatureSwitch::enable_override_bookmarks_ui() {
90 return &g_common_switches.Get().enable_override_bookmarks_ui;
92 FeatureSwitch* FeatureSwitch::extension_action_redesign() {
93 return &g_common_switches.Get().extension_action_redesign;
95 FeatureSwitch* FeatureSwitch::scripts_require_action() {
96 return &g_common_switches.Get().scripts_require_action;
99 FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
100 bool override_value)
101 : feature_(feature),
102 previous_value_(feature->GetOverrideValue()) {
103 feature_->SetOverrideValue(
104 override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
107 FeatureSwitch::ScopedOverride::~ScopedOverride() {
108 feature_->SetOverrideValue(previous_value_);
111 FeatureSwitch::FeatureSwitch(const char* switch_name,
112 DefaultValue default_value) {
113 Init(CommandLine::ForCurrentProcess(), switch_name, default_value);
116 FeatureSwitch::FeatureSwitch(const CommandLine* command_line,
117 const char* switch_name,
118 DefaultValue default_value) {
119 Init(command_line, switch_name, default_value);
122 void FeatureSwitch::Init(const CommandLine* command_line,
123 const char* switch_name,
124 DefaultValue default_value) {
125 command_line_ = command_line;
126 switch_name_ = switch_name;
127 default_value_ = default_value == DEFAULT_ENABLED;
128 override_value_ = OVERRIDE_NONE;
131 bool FeatureSwitch::IsEnabled() const {
132 if (override_value_ != OVERRIDE_NONE)
133 return override_value_ == OVERRIDE_ENABLED;
135 if (!switch_name_)
136 return default_value_;
138 std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
139 std::string switch_value;
140 base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value);
142 if (switch_value == "1")
143 return true;
145 if (switch_value == "0")
146 return false;
148 if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
149 return true;
151 if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
152 return false;
154 return default_value_;
157 std::string FeatureSwitch::GetLegacyEnableFlag() const {
158 DCHECK(switch_name_);
159 return std::string("enable-") + switch_name_;
162 std::string FeatureSwitch::GetLegacyDisableFlag() const {
163 DCHECK(switch_name_);
164 return std::string("disable-") + switch_name_;
167 void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
168 override_value_ = override_value;
171 FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
172 return override_value_;
175 } // namespace extensions