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/common/url_constants.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/extension_system.h"
14 #include "grit/components_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
17 using extensions::ExtensionMessageBubbleController
;
18 using extensions::NtpOverriddenBubbleController
;
22 // Whether the user has been notified about extension overriding the new tab
24 const char kNtpBubbleAcknowledged
[] = "ack_ntp_bubble";
26 ////////////////////////////////////////////////////////////////////////////////
27 // NtpOverriddenBubbleDelegate
29 class NtpOverriddenBubbleDelegate
30 : public extensions::ExtensionMessageBubbleController::Delegate
{
32 NtpOverriddenBubbleDelegate(ExtensionService
* service
, Profile
* profile
);
33 ~NtpOverriddenBubbleDelegate() override
;
35 // ExtensionMessageBubbleController::Delegate methods.
36 bool ShouldIncludeExtension(const std::string
& extension_id
) override
;
37 void AcknowledgeExtension(
38 const std::string
& extension_id
,
39 extensions::ExtensionMessageBubbleController::BubbleAction user_action
)
41 void PerformAction(const extensions::ExtensionIdList
& list
) override
;
42 base::string16
GetTitle() const override
;
43 base::string16
GetMessageBody(bool anchored_to_browser_action
) const override
;
44 base::string16
GetOverflowText(
45 const base::string16
& overflow_count
) const override
;
46 GURL
GetLearnMoreUrl() const override
;
47 base::string16
GetActionButtonLabel() const override
;
48 base::string16
GetDismissButtonLabel() const override
;
49 bool ShouldShowExtensionList() const override
;
50 void RestrictToSingleExtension(const std::string
& extension_id
) override
;
51 void LogExtensionCount(size_t count
) override
;
52 void LogAction(extensions::ExtensionMessageBubbleController::BubbleAction
56 // Our extension service. Weak, not owned by us.
57 ExtensionService
* service_
;
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(
66 ExtensionService
* service
,
68 : extensions::ExtensionMessageBubbleController::Delegate(profile
),
70 set_acknowledged_flag_pref_name(kNtpBubbleAcknowledged
);
73 NtpOverriddenBubbleDelegate::~NtpOverriddenBubbleDelegate() {}
75 bool NtpOverriddenBubbleDelegate::ShouldIncludeExtension(
76 const std::string
& extension_id
) {
77 if (!extension_id_
.empty() && extension_id_
!= extension_id
)
80 using extensions::ExtensionRegistry
;
81 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
82 const extensions::Extension
* extension
=
83 registry
->GetExtensionById(extension_id
, ExtensionRegistry::ENABLED
);
85 return false; // The extension provided is no longer enabled.
87 if (HasBubbleInfoBeenAcknowledged(extension_id
))
93 void NtpOverriddenBubbleDelegate::AcknowledgeExtension(
94 const std::string
& extension_id
,
95 ExtensionMessageBubbleController::BubbleAction user_action
) {
96 if (user_action
!= ExtensionMessageBubbleController::ACTION_EXECUTE
)
97 SetBubbleInfoBeenAcknowledged(extension_id
, true);
100 void NtpOverriddenBubbleDelegate::PerformAction(
101 const extensions::ExtensionIdList
& list
) {
102 for (size_t i
= 0; i
< list
.size(); ++i
) {
103 service_
->DisableExtension(list
[i
],
104 extensions::Extension::DISABLE_USER_ACTION
);
108 base::string16
NtpOverriddenBubbleDelegate::GetTitle() const {
109 return l10n_util::GetStringUTF16(
110 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE
);
113 base::string16
NtpOverriddenBubbleDelegate::GetMessageBody(
114 bool anchored_to_browser_action
) const {
115 base::string16 body
=
116 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE
);
117 body
+= l10n_util::GetStringUTF16(
118 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION
);
122 base::string16
NtpOverriddenBubbleDelegate::GetOverflowText(
123 const base::string16
& overflow_count
) const {
124 // Does not have more than one extension in the list at a time.
126 return base::string16();
129 GURL
NtpOverriddenBubbleDelegate::GetLearnMoreUrl() const {
130 return GURL(chrome::kExtensionControlledSettingLearnMoreURL
);
133 base::string16
NtpOverriddenBubbleDelegate::GetActionButtonLabel() const {
134 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS
);
137 base::string16
NtpOverriddenBubbleDelegate::GetDismissButtonLabel() const {
138 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES
);
141 bool NtpOverriddenBubbleDelegate::ShouldShowExtensionList() const {
145 void NtpOverriddenBubbleDelegate::RestrictToSingleExtension(
146 const std::string
& extension_id
) {
147 extension_id_
= extension_id
;
150 void NtpOverriddenBubbleDelegate::LogExtensionCount(size_t count
) {
153 void NtpOverriddenBubbleDelegate::LogAction(
154 ExtensionMessageBubbleController::BubbleAction action
) {
155 UMA_HISTOGRAM_ENUMERATION(
156 "ExtensionOverrideBubble.NtpOverriddenUserSelection",
158 ExtensionMessageBubbleController::ACTION_BOUNDARY
);
163 namespace extensions
{
165 ////////////////////////////////////////////////////////////////////////////////
166 // NtpOverriddenBubbleController
168 NtpOverriddenBubbleController::NtpOverriddenBubbleController(Profile
* profile
)
169 : ExtensionMessageBubbleController(
170 new NtpOverriddenBubbleDelegate(
171 ExtensionSystem::Get(profile
)->extension_service(),
176 NtpOverriddenBubbleController::~NtpOverriddenBubbleController() {}
178 bool NtpOverriddenBubbleController::ShouldShow(
179 const std::string
& extension_id
) {
180 if (!delegate()->ShouldIncludeExtension(extension_id
))
183 delegate()->RestrictToSingleExtension(extension_id
);
187 bool NtpOverriddenBubbleController::CloseOnDeactivate() {
191 } // namespace extensions