1 // Copyright (c) 2014 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/ntp_overridden_bubble_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extension_system.h"
15 #include "grit/components_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using extensions::ExtensionMessageBubbleController
;
19 using extensions::NtpOverriddenBubbleController
;
23 // Whether the user has been notified about extension overriding the new tab
25 const char kNtpBubbleAcknowledged
[] = "ack_ntp_bubble";
27 ////////////////////////////////////////////////////////////////////////////////
28 // NtpOverriddenBubbleDelegate
30 class NtpOverriddenBubbleDelegate
31 : public extensions::ExtensionMessageBubbleController::Delegate
{
33 explicit NtpOverriddenBubbleDelegate(Profile
* profile
);
34 ~NtpOverriddenBubbleDelegate() override
;
36 // ExtensionMessageBubbleController::Delegate methods.
37 bool ShouldIncludeExtension(const std::string
& extension_id
) override
;
38 void AcknowledgeExtension(
39 const std::string
& extension_id
,
40 extensions::ExtensionMessageBubbleController::BubbleAction user_action
)
42 void PerformAction(const extensions::ExtensionIdList
& list
) override
;
43 base::string16
GetTitle() const override
;
44 base::string16
GetMessageBody(bool anchored_to_browser_action
,
45 int extension_count
) const override
;
46 base::string16
GetOverflowText(
47 const base::string16
& overflow_count
) const override
;
48 GURL
GetLearnMoreUrl() const override
;
49 base::string16
GetActionButtonLabel() const override
;
50 base::string16
GetDismissButtonLabel() const override
;
51 bool ShouldShowExtensionList() const override
;
52 bool ShouldHighlightExtensions() const override
;
53 void RestrictToSingleExtension(const std::string
& extension_id
) override
;
54 void LogExtensionCount(size_t count
) override
;
55 void LogAction(extensions::ExtensionMessageBubbleController::BubbleAction
59 // The ID of the extension we are showing the bubble for.
60 std::string extension_id_
;
62 DISALLOW_COPY_AND_ASSIGN(NtpOverriddenBubbleDelegate
);
65 NtpOverriddenBubbleDelegate::NtpOverriddenBubbleDelegate(
67 : extensions::ExtensionMessageBubbleController::Delegate(profile
) {
68 set_acknowledged_flag_pref_name(kNtpBubbleAcknowledged
);
71 NtpOverriddenBubbleDelegate::~NtpOverriddenBubbleDelegate() {}
73 bool NtpOverriddenBubbleDelegate::ShouldIncludeExtension(
74 const std::string
& extension_id
) {
75 if (!extension_id_
.empty() && extension_id_
!= extension_id
)
78 const extensions::Extension
* extension
=
79 registry()->GetExtensionById(extension_id
,
80 extensions::ExtensionRegistry::ENABLED
);
82 return false; // The extension provided is no longer enabled.
84 if (HasBubbleInfoBeenAcknowledged(extension_id
))
90 void NtpOverriddenBubbleDelegate::AcknowledgeExtension(
91 const std::string
& extension_id
,
92 ExtensionMessageBubbleController::BubbleAction user_action
) {
93 if (user_action
!= ExtensionMessageBubbleController::ACTION_EXECUTE
)
94 SetBubbleInfoBeenAcknowledged(extension_id
, true);
97 void NtpOverriddenBubbleDelegate::PerformAction(
98 const extensions::ExtensionIdList
& list
) {
99 for (size_t i
= 0; i
< list
.size(); ++i
) {
100 service()->DisableExtension(list
[i
],
101 extensions::Extension::DISABLE_USER_ACTION
);
105 base::string16
NtpOverriddenBubbleDelegate::GetTitle() const {
106 return l10n_util::GetStringUTF16(
107 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE
);
110 base::string16
NtpOverriddenBubbleDelegate::GetMessageBody(
111 bool anchored_to_browser_action
,
112 int extension_count
) const {
113 base::string16 body
=
114 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE
);
115 body
+= l10n_util::GetStringUTF16(
116 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION
);
120 base::string16
NtpOverriddenBubbleDelegate::GetOverflowText(
121 const base::string16
& overflow_count
) const {
122 // Does not have more than one extension in the list at a time.
124 return base::string16();
127 GURL
NtpOverriddenBubbleDelegate::GetLearnMoreUrl() const {
128 return GURL(chrome::kExtensionControlledSettingLearnMoreURL
);
131 base::string16
NtpOverriddenBubbleDelegate::GetActionButtonLabel() const {
132 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS
);
135 base::string16
NtpOverriddenBubbleDelegate::GetDismissButtonLabel() const {
136 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES
);
139 bool NtpOverriddenBubbleDelegate::ShouldShowExtensionList() const {
143 bool NtpOverriddenBubbleDelegate::ShouldHighlightExtensions() const {
147 void NtpOverriddenBubbleDelegate::RestrictToSingleExtension(
148 const std::string
& extension_id
) {
149 extension_id_
= extension_id
;
152 void NtpOverriddenBubbleDelegate::LogExtensionCount(size_t count
) {
155 void NtpOverriddenBubbleDelegate::LogAction(
156 ExtensionMessageBubbleController::BubbleAction action
) {
157 UMA_HISTOGRAM_ENUMERATION(
158 "ExtensionOverrideBubble.NtpOverriddenUserSelection",
160 ExtensionMessageBubbleController::ACTION_BOUNDARY
);
165 namespace extensions
{
167 ////////////////////////////////////////////////////////////////////////////////
168 // NtpOverriddenBubbleController
170 NtpOverriddenBubbleController::NtpOverriddenBubbleController(Browser
* browser
)
171 : ExtensionMessageBubbleController(
172 new NtpOverriddenBubbleDelegate(browser
->profile()),
175 NtpOverriddenBubbleController::~NtpOverriddenBubbleController() {}
177 bool NtpOverriddenBubbleController::ShouldShow(
178 const std::string
& extension_id
) {
179 if (!delegate()->ShouldIncludeExtension(extension_id
))
182 delegate()->RestrictToSingleExtension(extension_id
);
186 bool NtpOverriddenBubbleController::CloseOnDeactivate() {
190 } // namespace extensions