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 "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/extension_system.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
17 using extensions::ExtensionMessageBubbleController
;
18 using extensions::NtpOverriddenBubbleController
;
22 ////////////////////////////////////////////////////////////////////////////////
23 // NtpOverriddenBubbleDelegate
25 class NtpOverriddenBubbleDelegate
26 : public extensions::ExtensionMessageBubbleController::Delegate
{
28 NtpOverriddenBubbleDelegate(ExtensionService
* service
, Profile
* profile
);
29 virtual ~NtpOverriddenBubbleDelegate();
31 // ExtensionMessageBubbleController::Delegate methods.
32 virtual bool ShouldIncludeExtension(const std::string
& extension_id
) OVERRIDE
;
33 virtual void AcknowledgeExtension(
34 const std::string
& extension_id
,
35 extensions::ExtensionMessageBubbleController::BubbleAction
36 user_action
) OVERRIDE
;
37 virtual void PerformAction(const extensions::ExtensionIdList
& list
) OVERRIDE
;
38 virtual base::string16
GetTitle() const OVERRIDE
;
39 virtual base::string16
GetMessageBody(
40 bool anchored_to_browser_action
) const OVERRIDE
;
41 virtual base::string16
GetOverflowText(
42 const base::string16
& overflow_count
) const OVERRIDE
;
43 virtual base::string16
GetLearnMoreLabel() const OVERRIDE
;
44 virtual GURL
GetLearnMoreUrl() const OVERRIDE
;
45 virtual base::string16
GetActionButtonLabel() const OVERRIDE
;
46 virtual base::string16
GetDismissButtonLabel() const OVERRIDE
;
47 virtual bool ShouldShowExtensionList() const OVERRIDE
;
48 virtual void RestrictToSingleExtension(
49 const std::string
& extension_id
) OVERRIDE
;
50 virtual void LogExtensionCount(size_t count
) OVERRIDE
;
51 virtual void LogAction(
52 extensions::ExtensionMessageBubbleController::BubbleAction
56 // Our extension service. Weak, not owned by us.
57 ExtensionService
* service_
;
59 // A weak pointer to the profile we are associated with. Not owned by us.
62 // The ID of the extension we are showing the bubble for.
63 std::string extension_id_
;
65 DISALLOW_COPY_AND_ASSIGN(NtpOverriddenBubbleDelegate
);
68 NtpOverriddenBubbleDelegate::NtpOverriddenBubbleDelegate(
69 ExtensionService
* service
,
71 : service_(service
), profile_(profile
) {}
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 extensions::ExtensionPrefs
* prefs
= extensions::ExtensionPrefs::Get(profile_
);
88 if (prefs
->HasNtpOverriddenBubbleBeenAcknowledged(extension_id
))
94 void NtpOverriddenBubbleDelegate::AcknowledgeExtension(
95 const std::string
& extension_id
,
96 ExtensionMessageBubbleController::BubbleAction user_action
) {
97 if (user_action
!= ExtensionMessageBubbleController::ACTION_EXECUTE
) {
98 extensions::ExtensionPrefs
* prefs
=
99 extensions::ExtensionPrefs::Get(profile_
);
100 prefs
->SetNtpOverriddenBubbleBeenAcknowledged(extension_id
, true);
104 void NtpOverriddenBubbleDelegate::PerformAction(
105 const extensions::ExtensionIdList
& list
) {
106 for (size_t i
= 0; i
< list
.size(); ++i
) {
107 service_
->DisableExtension(list
[i
],
108 extensions::Extension::DISABLE_USER_ACTION
);
112 base::string16
NtpOverriddenBubbleDelegate::GetTitle() const {
113 return l10n_util::GetStringUTF16(
114 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE
);
117 base::string16
NtpOverriddenBubbleDelegate::GetMessageBody(
118 bool anchored_to_browser_action
) const {
119 base::string16 body
=
120 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE
);
121 body
+= l10n_util::GetStringUTF16(
122 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION
);
126 base::string16
NtpOverriddenBubbleDelegate::GetOverflowText(
127 const base::string16
& overflow_count
) const {
128 // Does not have more than one extension in the list at a time.
130 return base::string16();
133 base::string16
NtpOverriddenBubbleDelegate::GetLearnMoreLabel() const {
134 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
137 GURL
NtpOverriddenBubbleDelegate::GetLearnMoreUrl() const {
138 return GURL(chrome::kExtensionControlledSettingLearnMoreURL
);
141 base::string16
NtpOverriddenBubbleDelegate::GetActionButtonLabel() const {
142 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS
);
145 base::string16
NtpOverriddenBubbleDelegate::GetDismissButtonLabel() const {
146 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES
);
149 bool NtpOverriddenBubbleDelegate::ShouldShowExtensionList() const {
153 void NtpOverriddenBubbleDelegate::RestrictToSingleExtension(
154 const std::string
& extension_id
) {
155 extension_id_
= extension_id
;
158 void NtpOverriddenBubbleDelegate::LogExtensionCount(size_t count
) {
161 void NtpOverriddenBubbleDelegate::LogAction(
162 ExtensionMessageBubbleController::BubbleAction action
) {
163 UMA_HISTOGRAM_ENUMERATION(
164 "ExtensionOverrideBubble.NtpOverriddenUserSelection",
166 ExtensionMessageBubbleController::ACTION_BOUNDARY
);
171 namespace extensions
{
173 ////////////////////////////////////////////////////////////////////////////////
174 // NtpOverriddenBubbleController
176 NtpOverriddenBubbleController::NtpOverriddenBubbleController(Profile
* profile
)
177 : ExtensionMessageBubbleController(
178 new NtpOverriddenBubbleDelegate(
179 ExtensionSystem::Get(profile
)->extension_service(),
184 NtpOverriddenBubbleController::~NtpOverriddenBubbleController() {}
186 bool NtpOverriddenBubbleController::ShouldShow(
187 const std::string
& extension_id
) {
188 if (!delegate()->ShouldIncludeExtension(extension_id
))
191 delegate()->RestrictToSingleExtension(extension_id
);
195 bool NtpOverriddenBubbleController::CloseOnDeactivate() {
199 } // namespace extensions