[Extensions UI] Have the ExtensionMessageBubble linked to a browser
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_message_bubble_factory.cc
blob92bf0287e88bc0af06829a2afee3143fd233f9ab
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 all bubbles for testing.
33 bool g_enabled_for_tests = false;
35 const char kEnableDevModeWarningExperimentName[] =
36 "ExtensionDeveloperModeWarning";
38 #if !defined(OS_WIN)
39 const char kEnableProxyWarningExperimentName[] = "ExtensionProxyWarning";
40 #endif
42 bool IsExperimentEnabled(const char* experiment_name) {
43 // Don't allow turning it off via command line.
44 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
45 if (command_line->HasSwitch(switches::kForceFieldTrials)) {
46 std::string forced_trials =
47 command_line->GetSwitchValueASCII(switches::kForceFieldTrials);
48 if (forced_trials.find(experiment_name))
49 return true;
51 return base::FieldTrialList::FindFullName(experiment_name) == "Enabled";
54 bool EnableSuspiciousExtensionsBubble() {
55 return g_enabled_for_tests || extensions::InstallVerifier::ShouldEnforce();
58 bool EnableSettingsApiBubble() {
59 #if defined(OS_WIN)
60 return true;
61 #else
62 return g_enabled_for_tests;
63 #endif
66 bool EnableProxyOverrideBubble() {
67 #if defined(OS_WIN)
68 return true;
69 #else
70 return g_enabled_for_tests ||
71 IsExperimentEnabled(kEnableProxyWarningExperimentName);
72 #endif
75 bool EnableDevModeBubble() {
76 if (extensions::FeatureSwitch::force_dev_mode_highlighting()->IsEnabled())
77 return true;
79 #if defined(OS_WIN)
80 if (chrome::GetChannel() >= version_info::Channel::BETA)
81 return true;
82 #endif
84 return g_enabled_for_tests ||
85 IsExperimentEnabled(kEnableDevModeWarningExperimentName);
88 } // namespace
90 ExtensionMessageBubbleFactory::ExtensionMessageBubbleFactory(Browser* browser)
91 : browser_(browser) {
94 ExtensionMessageBubbleFactory::~ExtensionMessageBubbleFactory() {
97 scoped_ptr<extensions::ExtensionMessageBubbleController>
98 ExtensionMessageBubbleFactory::GetController() {
99 Profile* original_profile = browser_->profile()->GetOriginalProfile();
100 std::set<Profile*>& profiles_evaluated = g_profiles_evaluated.Get();
101 bool is_initial_check = profiles_evaluated.count(original_profile) == 0;
102 profiles_evaluated.insert(original_profile);
104 // The list of suspicious extensions takes priority over the dev mode bubble
105 // and the settings API bubble, since that needs to be shown as soon as we
106 // disable something. The settings API bubble is shown on first startup after
107 // an extension has changed the startup pages and it is acceptable if that
108 // waits until the next startup because of the suspicious extension bubble.
109 // The dev mode bubble is not time sensitive like the other two so we'll catch
110 // the dev mode extensions on the next startup/next window that opens. That
111 // way, we're not too spammy with the bubbles.
112 if (EnableSuspiciousExtensionsBubble()) {
113 scoped_ptr<extensions::SuspiciousExtensionBubbleController> controller(
114 new extensions::SuspiciousExtensionBubbleController(browser_));
115 if (controller->ShouldShow())
116 return controller.Pass();
119 if (EnableSettingsApiBubble()) {
120 // No use showing this if it's not the startup of the profile.
121 if (is_initial_check) {
122 scoped_ptr<extensions::SettingsApiBubbleController> controller(
123 new extensions::SettingsApiBubbleController(
124 browser_, extensions::BUBBLE_TYPE_STARTUP_PAGES));
125 if (controller->ShouldShow())
126 return controller.Pass();
130 if (EnableProxyOverrideBubble()) {
131 // TODO(devlin): Move the "GetExtensionOverridingProxy" part into the
132 // proxy bubble controller.
133 const extensions::Extension* extension =
134 extensions::GetExtensionOverridingProxy(browser_->profile());
135 if (extension) {
136 scoped_ptr<extensions::ProxyOverriddenBubbleController> controller(
137 new extensions::ProxyOverriddenBubbleController(browser_));
138 if (controller->ShouldShow(extension->id()))
139 return controller.Pass();
143 if (EnableDevModeBubble()) {
144 scoped_ptr<extensions::DevModeBubbleController> controller(
145 new extensions::DevModeBubbleController(browser_));
146 if (controller->ShouldShow())
147 return controller.Pass();
150 return scoped_ptr<extensions::ExtensionMessageBubbleController>();
153 // static
154 void ExtensionMessageBubbleFactory::set_enabled_for_tests(bool enabled) {
155 g_enabled_for_tests = enabled;