Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / feature_switch.cc
blob28c72c30c7ade824fd586113321d35e01ff44526
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(NULL,
24 #if defined(OS_WIN)
25 FeatureSwitch::DEFAULT_ENABLED),
26 #else
27 FeatureSwitch::DEFAULT_DISABLED),
28 #endif
29 error_console(switches::kErrorConsole, FeatureSwitch::DEFAULT_DISABLED),
30 enable_override_bookmarks_ui(switches::kEnableOverrideBookmarksUI,
31 FeatureSwitch::DEFAULT_DISABLED),
32 extension_action_redesign(switches::kExtensionActionRedesign,
33 FeatureSwitch::DEFAULT_DISABLED),
34 scripts_require_action(switches::kScriptsRequireAction,
35 FeatureSwitch::DEFAULT_DISABLED),
36 embedded_extension_options(switches::kEmbeddedExtensionOptions,
37 FeatureSwitch::DEFAULT_DISABLED),
38 surface_worker(switches::kSurfaceWorker,
39 FeatureSwitch::DEFAULT_DISABLED),
40 trace_app_source(switches::kTraceAppSource,
41 FeatureSwitch::DEFAULT_ENABLED) {
44 // Enables extensions to be easily installed from sites other than the web
45 // store.
46 FeatureSwitch easy_off_store_install;
48 FeatureSwitch force_dev_mode_highlighting;
50 // Should we prompt the user before allowing external extensions to install?
51 // Default is yes.
52 FeatureSwitch prompt_for_external_extensions;
54 FeatureSwitch error_console;
55 FeatureSwitch enable_override_bookmarks_ui;
56 FeatureSwitch extension_action_redesign;
57 FeatureSwitch scripts_require_action;
58 FeatureSwitch embedded_extension_options;
59 FeatureSwitch surface_worker;
60 FeatureSwitch trace_app_source;
63 base::LazyInstance<CommonSwitches> g_common_switches =
64 LAZY_INSTANCE_INITIALIZER;
66 } // namespace
68 FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() {
69 return &g_common_switches.Get().force_dev_mode_highlighting;
71 FeatureSwitch* FeatureSwitch::easy_off_store_install() {
72 return &g_common_switches.Get().easy_off_store_install;
74 FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
75 return &g_common_switches.Get().prompt_for_external_extensions;
77 FeatureSwitch* FeatureSwitch::error_console() {
78 return &g_common_switches.Get().error_console;
80 FeatureSwitch* FeatureSwitch::enable_override_bookmarks_ui() {
81 return &g_common_switches.Get().enable_override_bookmarks_ui;
83 FeatureSwitch* FeatureSwitch::extension_action_redesign() {
84 return &g_common_switches.Get().extension_action_redesign;
86 FeatureSwitch* FeatureSwitch::scripts_require_action() {
87 return &g_common_switches.Get().scripts_require_action;
89 FeatureSwitch* FeatureSwitch::embedded_extension_options() {
90 return &g_common_switches.Get().embedded_extension_options;
92 FeatureSwitch* FeatureSwitch::surface_worker() {
93 return &g_common_switches.Get().surface_worker;
95 FeatureSwitch* FeatureSwitch::trace_app_source() {
96 return &g_common_switches.Get().trace_app_source;
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(base::CommandLine::ForCurrentProcess(), switch_name, default_value);
116 FeatureSwitch::FeatureSwitch(const base::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 base::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