summaryrefslogtreecommitdiffstats
path: root/extensions/common/feature_switch.cc
blob: d8c6b98e5ddecbc56ca176c8cb6e657c82964a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/feature_switch.h"

#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "extensions/common/switches.h"

namespace extensions {

namespace {

const char kEnableMediaRouterExperiment[] = "EnableMediaRouter";
const char kEnableMediaRouterWithCastExtensionExperiment[] =
    "EnableMediaRouterWithCastExtension";
const char kExtensionActionRedesignExperiment[] = "ExtensionActionRedesign";

class CommonSwitches {
 public:
  CommonSwitches()
      : easy_off_store_install(nullptr, FeatureSwitch::DEFAULT_DISABLED),
        force_dev_mode_highlighting(switches::kForceDevModeHighlighting,
                                    FeatureSwitch::DEFAULT_DISABLED),
        prompt_for_external_extensions(
#if defined(CHROMIUM_BUILD)
            switches::kPromptForExternalExtensions,
#else
            nullptr,
#endif
#if defined(OS_WIN)
            FeatureSwitch::DEFAULT_ENABLED),
#else
            FeatureSwitch::DEFAULT_DISABLED),
#endif
        error_console(switches::kErrorConsole, FeatureSwitch::DEFAULT_DISABLED),
        enable_override_bookmarks_ui(switches::kEnableOverrideBookmarksUI,
                                     FeatureSwitch::DEFAULT_DISABLED),
        extension_action_redesign(switches::kExtensionActionRedesign,
                                  kExtensionActionRedesignExperiment,
                                  FeatureSwitch::DEFAULT_DISABLED),
        extension_action_redesign_override(switches::kExtensionActionRedesign,
                                           FeatureSwitch::DEFAULT_ENABLED),
        scripts_require_action(switches::kScriptsRequireAction,
                               FeatureSwitch::DEFAULT_DISABLED),
        embedded_extension_options(switches::kEmbeddedExtensionOptions,
                                   FeatureSwitch::DEFAULT_DISABLED),
        trace_app_source(switches::kTraceAppSource,
                         FeatureSwitch::DEFAULT_ENABLED),
        // The switch media-router is defined in
        // chrome/common/chrome_switches.cc, but we can't depend on chrome here.
        media_router("media-router",
                     kEnableMediaRouterExperiment,
                     FeatureSwitch::DEFAULT_DISABLED),
        media_router_with_cast_extension(
            "media-router",
            kEnableMediaRouterWithCastExtensionExperiment,
            FeatureSwitch::DEFAULT_DISABLED) {
  }

  // Enables extensions to be easily installed from sites other than the web
  // store.
  FeatureSwitch easy_off_store_install;

  FeatureSwitch force_dev_mode_highlighting;

  // Should we prompt the user before allowing external extensions to install?
  // Default is yes.
  FeatureSwitch prompt_for_external_extensions;

  FeatureSwitch error_console;
  FeatureSwitch enable_override_bookmarks_ui;
  FeatureSwitch extension_action_redesign;
  FeatureSwitch extension_action_redesign_override;
  FeatureSwitch scripts_require_action;
  FeatureSwitch embedded_extension_options;
  FeatureSwitch trace_app_source;
  FeatureSwitch media_router;
  FeatureSwitch media_router_with_cast_extension;
};

base::LazyInstance<CommonSwitches> g_common_switches =
    LAZY_INSTANCE_INITIALIZER;

}  // namespace

FeatureSwitch* FeatureSwitch::force_dev_mode_highlighting() {
  return &g_common_switches.Get().force_dev_mode_highlighting;
}
FeatureSwitch* FeatureSwitch::easy_off_store_install() {
  return &g_common_switches.Get().easy_off_store_install;
}
FeatureSwitch* FeatureSwitch::prompt_for_external_extensions() {
  return &g_common_switches.Get().prompt_for_external_extensions;
}
FeatureSwitch* FeatureSwitch::error_console() {
  return &g_common_switches.Get().error_console;
}
FeatureSwitch* FeatureSwitch::enable_override_bookmarks_ui() {
  return &g_common_switches.Get().enable_override_bookmarks_ui;
}
FeatureSwitch* FeatureSwitch::extension_action_redesign() {
  // Force-enable the redesigned extension action toolbar when the Media Router
  // is enabled. Should be removed when the toolbar redesign is used by default.
  // See crbug.com/514694
  // TODO(kmarshall): Remove this override.
  if (media_router()->IsEnabled())
    return &g_common_switches.Get().extension_action_redesign_override;

  return &g_common_switches.Get().extension_action_redesign;
}
FeatureSwitch* FeatureSwitch::scripts_require_action() {
  return &g_common_switches.Get().scripts_require_action;
}
FeatureSwitch* FeatureSwitch::embedded_extension_options() {
  return &g_common_switches.Get().embedded_extension_options;
}
FeatureSwitch* FeatureSwitch::trace_app_source() {
  return &g_common_switches.Get().trace_app_source;
}
FeatureSwitch* FeatureSwitch::media_router() {
  return &g_common_switches.Get().media_router;
}
FeatureSwitch* FeatureSwitch::media_router_with_cast_extension() {
  return &g_common_switches.Get().media_router_with_cast_extension;
}

FeatureSwitch::ScopedOverride::ScopedOverride(FeatureSwitch* feature,
                                              bool override_value)
    : feature_(feature),
      previous_value_(feature->GetOverrideValue()) {
  feature_->SetOverrideValue(
      override_value ? OVERRIDE_ENABLED : OVERRIDE_DISABLED);
}

FeatureSwitch::ScopedOverride::~ScopedOverride() {
  feature_->SetOverrideValue(previous_value_);
}

FeatureSwitch::FeatureSwitch(const char* switch_name,
                             DefaultValue default_value)
    : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
                    switch_name,
                    default_value) {}

FeatureSwitch::FeatureSwitch(const char* switch_name,
                             const char* field_trial_name,
                             DefaultValue default_value)
    : FeatureSwitch(base::CommandLine::ForCurrentProcess(),
                    switch_name,
                    field_trial_name,
                    default_value) {}

FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line,
                             const char* switch_name,
                             DefaultValue default_value)
    : FeatureSwitch(command_line, switch_name, nullptr, default_value) {}

FeatureSwitch::FeatureSwitch(const base::CommandLine* command_line,
                             const char* switch_name,
                             const char* field_trial_name,
                             DefaultValue default_value)
    : command_line_(command_line),
      switch_name_(switch_name),
      field_trial_name_(field_trial_name),
      default_value_(default_value == DEFAULT_ENABLED),
      override_value_(OVERRIDE_NONE) {}

bool FeatureSwitch::IsEnabled() const {
  if (override_value_ != OVERRIDE_NONE)
    return override_value_ == OVERRIDE_ENABLED;

  if (!switch_name_)
    return default_value_;

  std::string temp = command_line_->GetSwitchValueASCII(switch_name_);
  std::string switch_value;
  base::TrimWhitespaceASCII(temp, base::TRIM_ALL, &switch_value);

  if (switch_value == "1")
    return true;

  if (switch_value == "0")
    return false;

  if (!default_value_ && command_line_->HasSwitch(GetLegacyEnableFlag()))
    return true;

  if (default_value_ && command_line_->HasSwitch(GetLegacyDisableFlag()))
    return false;

  if (field_trial_name_) {
    std::string group_name =
        base::FieldTrialList::FindFullName(field_trial_name_);
    if (group_name == "Enabled")
      return true;
    if (group_name == "Disabled")
      return false;
  }

  return default_value_;
}

std::string FeatureSwitch::GetLegacyEnableFlag() const {
  DCHECK(switch_name_);
  return std::string("enable-") + switch_name_;
}

std::string FeatureSwitch::GetLegacyDisableFlag() const {
  DCHECK(switch_name_);
  return std::string("disable-") + switch_name_;
}

void FeatureSwitch::SetOverrideValue(OverrideValue override_value) {
  override_value_ = override_value;
}

FeatureSwitch::OverrideValue FeatureSwitch::GetOverrideValue() const {
  return override_value_;
}

}  // namespace extensions