summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/extensions/extension_message_bubble_factory.cc
blob: d89837742f863500475a6206cd71daa9e70d5fa0 (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
// Copyright 2015 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 "chrome/browser/ui/extensions/extension_message_bubble_factory.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/metrics/field_trial.h"
#include "chrome/browser/extensions/dev_mode_bubble_controller.h"
#include "chrome/browser/extensions/extension_message_bubble_controller.h"
#include "chrome/browser/extensions/install_verifier.h"
#include "chrome/browser/extensions/proxy_overridden_bubble_controller.h"
#include "chrome/browser/extensions/settings_api_bubble_controller.h"
#include "chrome/browser/extensions/settings_api_helpers.h"
#include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/channel_info.h"
#include "components/version_info/version_info.h"
#include "extensions/common/feature_switch.h"

namespace {

// A map of all profiles evaluated, so we can tell if it's the initial check.
// TODO(devlin): It would be nice to coalesce all the "profiles evaluated" maps
// that are in the different bubble controllers.
base::LazyInstance<std::set<Profile*> > g_profiles_evaluated =
    LAZY_INSTANCE_INITIALIZER;

// This is used to turn on override whether bubbles are enabled or disabled for
// testing.
ExtensionMessageBubbleFactory::OverrideForTesting g_override_for_testing =
    ExtensionMessageBubbleFactory::NO_OVERRIDE;

const char kEnableDevModeWarningExperimentName[] =
    "ExtensionDeveloperModeWarning";

#if !defined(OS_WIN)
const char kEnableProxyWarningExperimentName[] = "ExtensionProxyWarning";
#endif

bool IsExperimentEnabled(const char* experiment_name) {
  // Don't allow turning it off via command line.
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kForceFieldTrials)) {
    std::string forced_trials =
        command_line->GetSwitchValueASCII(switches::kForceFieldTrials);
    if (forced_trials.find(experiment_name))
      return true;
  }
  return base::FieldTrialList::FindFullName(experiment_name) == "Enabled";
}

bool EnableSuspiciousExtensionsBubble() {
  return g_override_for_testing ==
             ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
         extensions::InstallVerifier::ShouldEnforce();
}

bool EnableSettingsApiBubble() {
#if defined(OS_WIN)
  return true;
#else
  return g_override_for_testing ==
         ExtensionMessageBubbleFactory::OVERRIDE_ENABLED;
#endif
}

bool EnableProxyOverrideBubble() {
#if defined(OS_WIN)
  return true;
#else
  return g_override_for_testing ==
             ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
         IsExperimentEnabled(kEnableProxyWarningExperimentName);
#endif
}

bool EnableDevModeBubble() {
  if (extensions::FeatureSwitch::force_dev_mode_highlighting()->IsEnabled())
    return true;

#if defined(OS_WIN)
  if (chrome::GetChannel() >= version_info::Channel::BETA)
    return true;
#endif

  return g_override_for_testing ==
             ExtensionMessageBubbleFactory::OVERRIDE_ENABLED ||
         IsExperimentEnabled(kEnableDevModeWarningExperimentName);
}

}  // namespace

ExtensionMessageBubbleFactory::ExtensionMessageBubbleFactory(Browser* browser)
    : browser_(browser) {
}

ExtensionMessageBubbleFactory::~ExtensionMessageBubbleFactory() {
}

scoped_ptr<extensions::ExtensionMessageBubbleController>
ExtensionMessageBubbleFactory::GetController() {
  Profile* original_profile = browser_->profile()->GetOriginalProfile();
  std::set<Profile*>& profiles_evaluated = g_profiles_evaluated.Get();
  bool is_initial_check = profiles_evaluated.count(original_profile) == 0;
  profiles_evaluated.insert(original_profile);

  if (g_override_for_testing == OVERRIDE_DISABLED)
    return scoped_ptr<extensions::ExtensionMessageBubbleController>();

  // The list of suspicious extensions takes priority over the dev mode bubble
  // and the settings API bubble, since that needs to be shown as soon as we
  // disable something. The settings API bubble is shown on first startup after
  // an extension has changed the startup pages and it is acceptable if that
  // waits until the next startup because of the suspicious extension bubble.
  // The dev mode bubble is not time sensitive like the other two so we'll catch
  // the dev mode extensions on the next startup/next window that opens. That
  // way, we're not too spammy with the bubbles.
  if (EnableSuspiciousExtensionsBubble()) {
    scoped_ptr<extensions::SuspiciousExtensionBubbleController> controller(
        new extensions::SuspiciousExtensionBubbleController(browser_));
    if (controller->ShouldShow())
      return controller.Pass();
  }

  if (EnableSettingsApiBubble()) {
    // No use showing this if it's not the startup of the profile.
    if (is_initial_check) {
      scoped_ptr<extensions::SettingsApiBubbleController> controller(
          new extensions::SettingsApiBubbleController(
              browser_, extensions::BUBBLE_TYPE_STARTUP_PAGES));
      if (controller->ShouldShow())
        return controller.Pass();
    }
  }

  if (EnableProxyOverrideBubble()) {
    scoped_ptr<extensions::ProxyOverriddenBubbleController> controller(
        new extensions::ProxyOverriddenBubbleController(browser_));
    if (controller->ShouldShow())
      return controller.Pass();
  }

  if (EnableDevModeBubble()) {
    scoped_ptr<extensions::DevModeBubbleController> controller(
        new extensions::DevModeBubbleController(browser_));
    if (controller->ShouldShow())
      return controller.Pass();
  }

  return scoped_ptr<extensions::ExtensionMessageBubbleController>();
}

// static
void ExtensionMessageBubbleFactory::set_override_for_tests(
    OverrideForTesting override) {
  g_override_for_testing = override;
}