da364b2781e949169ad996e51bf43cb25f363639
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / suspicious_extension_bubble_controller.cc
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.
4
5 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h"
6
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"
22
23 using extensions::ExtensionMessageBubbleController;
24
25 namespace {
26
27 // Whether the user has been notified about extension being wiped out.
28 const char kWipeoutAcknowledged[] = "ack_wiped";
29
30 base::LazyInstance<std::set<Profile*> > g_shown_for_profiles =
31   LAZY_INSTANCE_INITIALIZER;
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // SuspiciousExtensionBubbleDelegate
35
36 class SuspiciousExtensionBubbleDelegate
37     : public ExtensionMessageBubbleController::Delegate {
38  public:
39   explicit SuspiciousExtensionBubbleDelegate(Profile* profile);
40   ~SuspiciousExtensionBubbleDelegate() override;
41
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) const override;
50   base::string16 GetOverflowText(
51       const base::string16& overflow_count) const override;
52   GURL GetLearnMoreUrl() const override;
53   base::string16 GetActionButtonLabel() const override;
54   base::string16 GetDismissButtonLabel() const override;
55   bool ShouldShowExtensionList() const override;
56   void LogExtensionCount(size_t count) override;
57   void LogAction(
58       ExtensionMessageBubbleController::BubbleAction action) override;
59
60   DISALLOW_COPY_AND_ASSIGN(SuspiciousExtensionBubbleDelegate);
61 };
62
63 SuspiciousExtensionBubbleDelegate::SuspiciousExtensionBubbleDelegate(
64     Profile* profile)
65     : extensions::ExtensionMessageBubbleController::Delegate(profile) {
66   set_acknowledged_flag_pref_name(kWipeoutAcknowledged);
67 }
68
69 SuspiciousExtensionBubbleDelegate::~SuspiciousExtensionBubbleDelegate() {
70 }
71
72 bool SuspiciousExtensionBubbleDelegate::ShouldIncludeExtension(
73       const std::string& extension_id) {
74   extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(
75       profile());
76   if (!prefs->IsExtensionDisabled(extension_id))
77     return false;
78
79   int disable_reasons = prefs->GetDisableReasons(extension_id);
80   if (disable_reasons & extensions::Extension::DISABLE_NOT_VERIFIED)
81     return !HasBubbleInfoBeenAcknowledged(extension_id);
82
83   return false;
84 }
85
86 void SuspiciousExtensionBubbleDelegate::AcknowledgeExtension(
87     const std::string& extension_id,
88     ExtensionMessageBubbleController::BubbleAction user_action) {
89   SetBubbleInfoBeenAcknowledged(extension_id, true);
90 }
91
92 void SuspiciousExtensionBubbleDelegate::PerformAction(
93     const extensions::ExtensionIdList& list) {
94   // This bubble solicits no action from the user. Or as Nimoy would have it:
95   // "Well, my work here is done".
96 }
97
98 base::string16 SuspiciousExtensionBubbleDelegate::GetTitle() const {
99   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNSUPPORTED_DISABLED_TITLE);
100 }
101
102 base::string16 SuspiciousExtensionBubbleDelegate::GetMessageBody(
103     bool anchored_to_browser_action) const {
104   return l10n_util::GetStringFUTF16(IDS_EXTENSIONS_UNSUPPORTED_DISABLED_BODY,
105       l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
106 }
107
108 base::string16 SuspiciousExtensionBubbleDelegate::GetOverflowText(
109     const base::string16& overflow_count) const {
110   return l10n_util::GetStringFUTF16(
111             IDS_EXTENSIONS_DISABLED_AND_N_MORE,
112             overflow_count);
113 }
114
115 GURL SuspiciousExtensionBubbleDelegate::GetLearnMoreUrl() const {
116   return GURL(chrome::kRemoveNonCWSExtensionURL);
117 }
118
119 base::string16
120 SuspiciousExtensionBubbleDelegate::GetActionButtonLabel() const {
121   return base::string16();
122 }
123
124 base::string16
125 SuspiciousExtensionBubbleDelegate::GetDismissButtonLabel() const {
126   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNSUPPORTED_DISABLED_BUTTON);
127 }
128
129 bool
130 SuspiciousExtensionBubbleDelegate::ShouldShowExtensionList() const {
131   return true;
132 }
133
134 void SuspiciousExtensionBubbleDelegate::LogExtensionCount(
135     size_t count) {
136   UMA_HISTOGRAM_COUNTS_100(
137       "ExtensionBubble.ExtensionWipeoutCount", count);
138 }
139
140 void SuspiciousExtensionBubbleDelegate::LogAction(
141     ExtensionMessageBubbleController::BubbleAction action) {
142   UMA_HISTOGRAM_ENUMERATION(
143       "ExtensionBubble.WipeoutUserSelection",
144       action, ExtensionMessageBubbleController::ACTION_BOUNDARY);
145 }
146
147 }  // namespace
148
149 namespace extensions {
150
151 ////////////////////////////////////////////////////////////////////////////////
152 // SuspiciousExtensionBubbleController
153
154 // static
155 void SuspiciousExtensionBubbleController::ClearProfileListForTesting() {
156   g_shown_for_profiles.Get().clear();
157 }
158
159 SuspiciousExtensionBubbleController::SuspiciousExtensionBubbleController(
160     Profile* profile)
161     : ExtensionMessageBubbleController(
162           new SuspiciousExtensionBubbleDelegate(profile),
163           profile),
164       profile_(profile) {}
165
166 SuspiciousExtensionBubbleController::~SuspiciousExtensionBubbleController() {
167 }
168
169 bool SuspiciousExtensionBubbleController::ShouldShow() {
170   return !g_shown_for_profiles.Get().count(profile_->GetOriginalProfile()) &&
171       !GetExtensionList().empty();
172 }
173
174 void SuspiciousExtensionBubbleController::Show(ExtensionMessageBubble* bubble) {
175   g_shown_for_profiles.Get().insert(profile_->GetOriginalProfile());
176   ExtensionMessageBubbleController::Show(bubble);
177 }
178
179 }  // namespace extensions