[Extensions UI] Have the ExtensionMessageBubble linked to a browser
[chromium-blink-merge.git] / chrome / browser / extensions / suspicious_extension_bubble_controller.cc
blobbe2d2e7c36dd8ec0a8e2c9d8b99ca16c884904fa
1 // Copyright (c) 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 "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
7 #include "base/lazy_instance.h"
8 #include "base/metrics/histogram.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/extension_message_bubble.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/extension.h"
20 #include "grit/components_strings.h"
21 #include "ui/base/l10n/l10n_util.h"
23 using extensions::ExtensionMessageBubbleController;
25 namespace {
27 // Whether the user has been notified about extension being wiped out.
28 const char kWipeoutAcknowledged[] = "ack_wiped";
30 base::LazyInstance<std::set<Profile*> > g_shown_for_profiles =
31 LAZY_INSTANCE_INITIALIZER;
33 ////////////////////////////////////////////////////////////////////////////////
34 // SuspiciousExtensionBubbleDelegate
36 class SuspiciousExtensionBubbleDelegate
37 : public ExtensionMessageBubbleController::Delegate {
38 public:
39 explicit SuspiciousExtensionBubbleDelegate(Profile* profile);
40 ~SuspiciousExtensionBubbleDelegate() override;
42 // ExtensionMessageBubbleController::Delegate methods.
43 bool ShouldIncludeExtension(const std::string& extension_id) override;
44 void AcknowledgeExtension(
45 const std::string& extension_id,
46 ExtensionMessageBubbleController::BubbleAction user_action) override;
47 void PerformAction(const extensions::ExtensionIdList& list) override;
48 base::string16 GetTitle() const override;
49 base::string16 GetMessageBody(bool anchored_to_browser_action,
50 int extension_count) const override;
51 base::string16 GetOverflowText(
52 const base::string16& overflow_count) const override;
53 GURL GetLearnMoreUrl() const override;
54 base::string16 GetActionButtonLabel() const override;
55 base::string16 GetDismissButtonLabel() const override;
56 bool ShouldShowExtensionList() const override;
57 bool ShouldHighlightExtensions() const override;
58 void LogExtensionCount(size_t count) override;
59 void LogAction(
60 ExtensionMessageBubbleController::BubbleAction action) override;
62 private:
63 DISALLOW_COPY_AND_ASSIGN(SuspiciousExtensionBubbleDelegate);
66 SuspiciousExtensionBubbleDelegate::SuspiciousExtensionBubbleDelegate(
67 Profile* profile)
68 : extensions::ExtensionMessageBubbleController::Delegate(profile) {
69 set_acknowledged_flag_pref_name(kWipeoutAcknowledged);
72 SuspiciousExtensionBubbleDelegate::~SuspiciousExtensionBubbleDelegate() {
75 bool SuspiciousExtensionBubbleDelegate::ShouldIncludeExtension(
76 const std::string& extension_id) {
77 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(
78 profile());
79 if (!prefs->IsExtensionDisabled(extension_id))
80 return false;
82 int disable_reasons = prefs->GetDisableReasons(extension_id);
83 if (disable_reasons & extensions::Extension::DISABLE_NOT_VERIFIED)
84 return !HasBubbleInfoBeenAcknowledged(extension_id);
86 return false;
89 void SuspiciousExtensionBubbleDelegate::AcknowledgeExtension(
90 const std::string& extension_id,
91 ExtensionMessageBubbleController::BubbleAction user_action) {
92 SetBubbleInfoBeenAcknowledged(extension_id, true);
95 void SuspiciousExtensionBubbleDelegate::PerformAction(
96 const extensions::ExtensionIdList& list) {
97 // This bubble solicits no action from the user. Or as Nimoy would have it:
98 // "Well, my work here is done".
101 base::string16 SuspiciousExtensionBubbleDelegate::GetTitle() const {
102 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNSUPPORTED_DISABLED_TITLE);
105 base::string16 SuspiciousExtensionBubbleDelegate::GetMessageBody(
106 bool anchored_to_browser_action,
107 int extension_count) const {
108 int message_id = extension_count == 1 ?
109 IDS_EXTENSIONS_SINGLE_UNSUPPORTED_DISABLED_BODY :
110 IDS_EXTENSIONS_MULTIPLE_UNSUPPORTED_DISABLED_BODY;
111 return l10n_util::GetStringFUTF16(
112 message_id, l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
115 base::string16 SuspiciousExtensionBubbleDelegate::GetOverflowText(
116 const base::string16& overflow_count) const {
117 return l10n_util::GetStringFUTF16(
118 IDS_EXTENSIONS_DISABLED_AND_N_MORE,
119 overflow_count);
122 GURL SuspiciousExtensionBubbleDelegate::GetLearnMoreUrl() const {
123 return GURL(chrome::kRemoveNonCWSExtensionURL);
126 base::string16
127 SuspiciousExtensionBubbleDelegate::GetActionButtonLabel() const {
128 return base::string16();
131 base::string16
132 SuspiciousExtensionBubbleDelegate::GetDismissButtonLabel() const {
133 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNSUPPORTED_DISABLED_BUTTON);
136 bool SuspiciousExtensionBubbleDelegate::ShouldShowExtensionList() const {
137 return true;
140 bool SuspiciousExtensionBubbleDelegate::ShouldHighlightExtensions() const {
141 return false;
144 void SuspiciousExtensionBubbleDelegate::LogExtensionCount(
145 size_t count) {
146 UMA_HISTOGRAM_COUNTS_100(
147 "ExtensionBubble.ExtensionWipeoutCount", count);
150 void SuspiciousExtensionBubbleDelegate::LogAction(
151 ExtensionMessageBubbleController::BubbleAction action) {
152 UMA_HISTOGRAM_ENUMERATION(
153 "ExtensionBubble.WipeoutUserSelection",
154 action, ExtensionMessageBubbleController::ACTION_BOUNDARY);
157 } // namespace
159 namespace extensions {
161 ////////////////////////////////////////////////////////////////////////////////
162 // SuspiciousExtensionBubbleController
164 // static
165 void SuspiciousExtensionBubbleController::ClearProfileListForTesting() {
166 g_shown_for_profiles.Get().clear();
169 SuspiciousExtensionBubbleController::SuspiciousExtensionBubbleController(
170 Browser* browser)
171 : ExtensionMessageBubbleController(
172 new SuspiciousExtensionBubbleDelegate(browser->profile()),
173 browser) {}
175 SuspiciousExtensionBubbleController::~SuspiciousExtensionBubbleController() {
178 bool SuspiciousExtensionBubbleController::ShouldShow() {
179 return !g_shown_for_profiles.Get().count(profile()->GetOriginalProfile()) &&
180 !GetExtensionList().empty();
183 void SuspiciousExtensionBubbleController::Show(ExtensionMessageBubble* bubble) {
184 g_shown_for_profiles.Get().insert(profile()->GetOriginalProfile());
185 ExtensionMessageBubbleController::Show(bubble);
188 } // namespace extensions