26f142f4f38f5c13024d3fabb646079bca1408df
[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/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/extensions/extension_message_bubble.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21
22 namespace {
23
24 base::LazyInstance<std::set<Profile*> > g_shown_for_profiles =
25   LAZY_INSTANCE_INITIALIZER;
26
27 ////////////////////////////////////////////////////////////////////////////////
28 // SuspiciousExtensionBubbleDelegate
29
30 SuspiciousExtensionBubbleDelegate::SuspiciousExtensionBubbleDelegate(
31     ExtensionService* service)
32     : service_(service) {
33 }
34
35 SuspiciousExtensionBubbleDelegate::~SuspiciousExtensionBubbleDelegate() {
36 }
37
38 bool SuspiciousExtensionBubbleDelegate::ShouldIncludeExtension(
39       const std::string& extension_id) {
40   extensions::ExtensionPrefs* prefs = service_->extension_prefs();
41   if (!prefs->IsExtensionDisabled(extension_id))
42     return false;
43
44   int disble_reasons = prefs->GetDisableReasons(extension_id);
45   if (disble_reasons & extensions::Extension::DISABLE_NOT_VERIFIED)
46     return !prefs->HasWipeoutBeenAcknowledged(extension_id);
47
48   return false;
49 }
50
51 void SuspiciousExtensionBubbleDelegate::AcknowledgeExtension(
52     const std::string& extension_id,
53     ExtensionMessageBubbleController::BubbleAction user_action) {
54   extensions::ExtensionPrefs* prefs = service_->extension_prefs();
55   prefs->SetWipeoutAcknowledged(extension_id, true);
56 }
57
58 void SuspiciousExtensionBubbleDelegate::PerformAction(
59     const extensions::ExtensionIdList& list) {
60   // This bubble solicits no action from the user. Or as Nimoy would have it:
61   // "Well, my work here is done".
62 }
63
64 base::string16 SuspiciousExtensionBubbleDelegate::GetTitle() const {
65   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SUSPICIOUS_DISABLED_TITLE);
66 }
67
68 base::string16 SuspiciousExtensionBubbleDelegate::GetMessageBody() const {
69   return l10n_util::GetStringFUTF16(IDS_EXTENSIONS_SUSPICIOUS_DISABLED_BODY,
70       l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE));
71 }
72
73 base::string16 SuspiciousExtensionBubbleDelegate::GetOverflowText(
74     const base::string16& overflow_count) const {
75   base::string16 overflow_string = l10n_util::GetStringUTF16(
76       IDS_EXTENSIONS_SUSPICIOUS_DISABLED_AND_N_MORE);
77   base::string16 new_string;
78
79   // Just before string freeze, we checked in # as a substitution value for
80   // this string, whereas we should have used $1. It was discovered too late,
81   // so we do the substitution by hand in that case.
82   if (overflow_string.find(base::ASCIIToUTF16("#")) != base::string16::npos) {
83     base::ReplaceChars(overflow_string, base::ASCIIToUTF16("#").c_str(),
84                        overflow_count, &new_string);
85   } else {
86     new_string = l10n_util::GetStringFUTF16(
87             IDS_EXTENSIONS_SUSPICIOUS_DISABLED_AND_N_MORE,
88             overflow_count);
89   }
90
91   return new_string;
92 }
93
94 base::string16
95 SuspiciousExtensionBubbleDelegate::GetLearnMoreLabel() const {
96   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
97 }
98
99 GURL SuspiciousExtensionBubbleDelegate::GetLearnMoreUrl() const {
100   return GURL(chrome::kRemoveNonCWSExtensionURL);
101 }
102
103 base::string16
104 SuspiciousExtensionBubbleDelegate::GetActionButtonLabel() const {
105   return base::string16();
106 }
107
108 base::string16
109 SuspiciousExtensionBubbleDelegate::GetDismissButtonLabel() const {
110   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SUSPICIOUS_DISABLED_BUTTON);
111 }
112
113 bool
114 SuspiciousExtensionBubbleDelegate::ShouldShowExtensionList() const {
115   return true;
116 }
117
118 void SuspiciousExtensionBubbleDelegate::LogExtensionCount(
119     size_t count) {
120   UMA_HISTOGRAM_COUNTS_100(
121       "ExtensionWipeoutBubble.ExtensionWipeoutCount", count);
122 }
123
124 void SuspiciousExtensionBubbleDelegate::LogAction(
125     ExtensionMessageBubbleController::BubbleAction action) {
126   UMA_HISTOGRAM_ENUMERATION(
127       "ExtensionWipeoutBubble.UserSelection",
128       action, ExtensionMessageBubbleController::ACTION_BOUNDARY);
129 }
130
131 }  // namespace
132
133 namespace extensions {
134
135 ////////////////////////////////////////////////////////////////////////////////
136 // SuspiciousExtensionBubbleController
137
138 // static
139 void SuspiciousExtensionBubbleController::ClearProfileListForTesting() {
140   g_shown_for_profiles.Get().clear();
141 }
142
143 SuspiciousExtensionBubbleController::SuspiciousExtensionBubbleController(
144     Profile* profile)
145     : ExtensionMessageBubbleController(
146           new SuspiciousExtensionBubbleDelegate(
147               extensions::ExtensionSystem::Get(profile)->extension_service()),
148           profile),
149       profile_(profile) {
150 }
151
152 SuspiciousExtensionBubbleController::~SuspiciousExtensionBubbleController() {
153 }
154
155 bool SuspiciousExtensionBubbleController::ShouldShow() {
156   return !g_shown_for_profiles.Get().count(profile_) &&
157       !GetExtensionList().empty();
158 }
159
160 void SuspiciousExtensionBubbleController::Show(ExtensionMessageBubble* bubble) {
161   g_shown_for_profiles.Get().insert(profile_);
162   ExtensionMessageBubbleController::Show(bubble);
163 }
164
165 }  // namespace extensions