Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_message_bubble_factory.cc
bloba483caf1bb71283c054109a012643483a5e62656
1 // Copyright 2015 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 "chrome/browser/ui/extensions/extension_message_bubble_factory.h"
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/field_trial.h"
10 #include "chrome/browser/extensions/dev_mode_bubble_controller.h"
11 #include "chrome/browser/extensions/extension_message_bubble_controller.h"
12 #include "chrome/browser/extensions/install_verifier.h"
13 #include "chrome/browser/extensions/proxy_overridden_bubble_controller.h"
14 #include "chrome/browser/extensions/settings_api_bubble_controller.h"
15 #include "chrome/browser/extensions/settings_api_helpers.h"
16 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/channel_info.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "components/version_info/version_info.h"
22 #include "extensions/common/feature_switch.h"
24 namespace {
26 // A map of all profiles evaluated, so we can tell if it's the initial check.
27 // TODO(devlin): It would be nice to coalesce all the "profiles evaluated" maps
28 // that are in the different bubble controllers.
29 base::LazyInstance<std::set<Profile*> > g_profiles_evaluated =
30 LAZY_INSTANCE_INITIALIZER;
32 // This is used to turn on override whether bubbles are enabled or disabled for
33 // testing.
34 ExtensionMessageBubbleFactory::OverrideForTesting g_override_for_testing =
35 ExtensionMessageBubbleFactory::NO_OVERRIDE;
37 const char kEnableDevModeWarningExperimentName[] =
38 "ExtensionDeveloperModeWarning";
40 #if !defined(OS_WIN)
41 const char kEnableProxyWarningExperimentName[] = "ExtensionProxyWarning";
42 #endif
44 bool IsExperimentEnabled(const char* experiment_name) {
45 // Don't allow turning it off via command line.
46 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
47 if (command_line->HasSwitch(switches::kForceFieldTrials)) {
48 std::string forced_trials =
49 command_line->GetSwitchValueASCII(switches::kForceFieldTrials);
50 if (forced_trials.find(experiment_name))
51 return true;
53 return base::FieldTrialList::FindFullName(experiment_name) == "Enabled";
56 bool EnableSuspiciousExtensionsBubble() {
57 return g_override_for_testing ==
58 ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
59 extensions::InstallVerifier::ShouldEnforce();
62 bool EnableSettingsApiBubble() {
63 #if defined(OS_WIN)
64 return true;
65 #else
66 return g_override_for_testing ==
67 ExtensionMessageBubbleFactory::OVERRIDE_ENABLED;
68 #endif
71 bool EnableProxyOverrideBubble() {
72 #if defined(OS_WIN)
73 return true;
74 #else
75 return g_override_for_testing ==
76 ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
77 IsExperimentEnabled(kEnableProxyWarningExperimentName);
78 #endif
81 bool EnableDevModeBubble() {
82 if (extensions::FeatureSwitch::force_dev_mode_highlighting()->IsEnabled())
83 return true;
85 #if defined(OS_WIN)
86 if (chrome::GetChannel() >= version_info::Channel::BETA)
87 return true;
88 #endif
90 return g_override_for_testing ==
91 ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
92 IsExperimentEnabled(kEnableDevModeWarningExperimentName);
95 } // namespace
97 ExtensionMessageBubbleFactory::ExtensionMessageBubbleFactory(Browser* browser)
98 : browser_(browser) {
101 ExtensionMessageBubbleFactory::~ExtensionMessageBubbleFactory() {
104 scoped_ptr<extensions::ExtensionMessageBubbleController>
105 ExtensionMessageBubbleFactory::GetController() {
106 Profile* original_profile = browser_->profile()->GetOriginalProfile();
107 std::set<Profile*>& profiles_evaluated = g_profiles_evaluated.Get();
108 bool is_initial_check = profiles_evaluated.count(original_profile) == 0;
109 profiles_evaluated.insert(original_profile);
111 if (g_override_for_testing == OVERRIDE_DISABLED)
112 return scoped_ptr<extensions::ExtensionMessageBubbleController>();
114 // The list of suspicious extensions takes priority over the dev mode bubble
115 // and the settings API bubble, since that needs to be shown as soon as we
116 // disable something. The settings API bubble is shown on first startup after
117 // an extension has changed the startup pages and it is acceptable if that
118 // waits until the next startup because of the suspicious extension bubble.
119 // The dev mode bubble is not time sensitive like the other two so we'll catch
120 // the dev mode extensions on the next startup/next window that opens. That
121 // way, we're not too spammy with the bubbles.
122 if (EnableSuspiciousExtensionsBubble()) {
123 scoped_ptr<extensions::SuspiciousExtensionBubbleController> controller(
124 new extensions::SuspiciousExtensionBubbleController(browser_));
125 if (controller->ShouldShow())
126 return controller.Pass();
129 if (EnableSettingsApiBubble()) {
130 // No use showing this if it's not the startup of the profile.
131 if (is_initial_check) {
132 scoped_ptr<extensions::SettingsApiBubbleController> controller(
133 new extensions::SettingsApiBubbleController(
134 browser_, extensions::BUBBLE_TYPE_STARTUP_PAGES));
135 if (controller->ShouldShow())
136 return controller.Pass();
140 if (EnableProxyOverrideBubble()) {
141 // TODO(devlin): Move the "GetExtensionOverridingProxy" part into the
142 // proxy bubble controller.
143 const extensions::Extension* extension =
144 extensions::GetExtensionOverridingProxy(browser_->profile());
145 if (extension) {
146 scoped_ptr<extensions::ProxyOverriddenBubbleController> controller(
147 new extensions::ProxyOverriddenBubbleController(browser_));
148 if (controller->ShouldShow(extension->id()))
149 return controller.Pass();
153 if (EnableDevModeBubble()) {
154 scoped_ptr<extensions::DevModeBubbleController> controller(
155 new extensions::DevModeBubbleController(browser_));
156 if (controller->ShouldShow())
157 return controller.Pass();
160 return scoped_ptr<extensions::ExtensionMessageBubbleController>();
163 // static
164 void ExtensionMessageBubbleFactory::set_override_for_tests(
165 OverrideForTesting override) {
166 g_override_for_testing = override;