Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / extensions / ntp_overridden_bubble_controller.cc
blob86e437d2e1db68bbf35eb73f331300e831f89fee
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;
20 namespace {
22 // Whether the user has been notified about extension overriding the new tab
23 // page.
24 const char kNtpBubbleAcknowledged[] = "ack_ntp_bubble";
26 ////////////////////////////////////////////////////////////////////////////////
27 // NtpOverriddenBubbleDelegate
29 class NtpOverriddenBubbleDelegate
30 : public extensions::ExtensionMessageBubbleController::Delegate {
31 public:
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)
40 override;
41 void PerformAction(const extensions::ExtensionIdList& list) override;
42 base::string16 GetTitle() const override;
43 base::string16 GetMessageBody(bool anchored_to_browser_action,
44 int extension_count) const override;
45 base::string16 GetOverflowText(
46 const base::string16& overflow_count) const override;
47 GURL GetLearnMoreUrl() const override;
48 base::string16 GetActionButtonLabel() const override;
49 base::string16 GetDismissButtonLabel() const override;
50 bool ShouldShowExtensionList() const override;
51 bool ShouldHighlightExtensions() const override;
52 void RestrictToSingleExtension(const std::string& extension_id) override;
53 void LogExtensionCount(size_t count) override;
54 void LogAction(extensions::ExtensionMessageBubbleController::BubbleAction
55 action) override;
57 private:
58 // Our extension service. Weak, not owned by us.
59 ExtensionService* service_;
61 // The ID of the extension we are showing the bubble for.
62 std::string extension_id_;
64 DISALLOW_COPY_AND_ASSIGN(NtpOverriddenBubbleDelegate);
67 NtpOverriddenBubbleDelegate::NtpOverriddenBubbleDelegate(
68 ExtensionService* service,
69 Profile* profile)
70 : extensions::ExtensionMessageBubbleController::Delegate(profile),
71 service_(service) {
72 set_acknowledged_flag_pref_name(kNtpBubbleAcknowledged);
75 NtpOverriddenBubbleDelegate::~NtpOverriddenBubbleDelegate() {}
77 bool NtpOverriddenBubbleDelegate::ShouldIncludeExtension(
78 const std::string& extension_id) {
79 if (!extension_id_.empty() && extension_id_ != extension_id)
80 return false;
82 using extensions::ExtensionRegistry;
83 ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
84 const extensions::Extension* extension =
85 registry->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
86 if (!extension)
87 return false; // The extension provided is no longer enabled.
89 if (HasBubbleInfoBeenAcknowledged(extension_id))
90 return false;
92 return true;
95 void NtpOverriddenBubbleDelegate::AcknowledgeExtension(
96 const std::string& extension_id,
97 ExtensionMessageBubbleController::BubbleAction user_action) {
98 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
99 SetBubbleInfoBeenAcknowledged(extension_id, true);
102 void NtpOverriddenBubbleDelegate::PerformAction(
103 const extensions::ExtensionIdList& list) {
104 for (size_t i = 0; i < list.size(); ++i) {
105 service_->DisableExtension(list[i],
106 extensions::Extension::DISABLE_USER_ACTION);
110 base::string16 NtpOverriddenBubbleDelegate::GetTitle() const {
111 return l10n_util::GetStringUTF16(
112 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE);
115 base::string16 NtpOverriddenBubbleDelegate::GetMessageBody(
116 bool anchored_to_browser_action,
117 int extension_count) const {
118 base::string16 body =
119 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE);
120 body += l10n_util::GetStringUTF16(
121 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION);
122 return body;
125 base::string16 NtpOverriddenBubbleDelegate::GetOverflowText(
126 const base::string16& overflow_count) const {
127 // Does not have more than one extension in the list at a time.
128 NOTREACHED();
129 return base::string16();
132 GURL NtpOverriddenBubbleDelegate::GetLearnMoreUrl() const {
133 return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
136 base::string16 NtpOverriddenBubbleDelegate::GetActionButtonLabel() const {
137 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
140 base::string16 NtpOverriddenBubbleDelegate::GetDismissButtonLabel() const {
141 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
144 bool NtpOverriddenBubbleDelegate::ShouldShowExtensionList() const {
145 return false;
148 bool NtpOverriddenBubbleDelegate::ShouldHighlightExtensions() const {
149 return false;
152 void NtpOverriddenBubbleDelegate::RestrictToSingleExtension(
153 const std::string& extension_id) {
154 extension_id_ = extension_id;
157 void NtpOverriddenBubbleDelegate::LogExtensionCount(size_t count) {
160 void NtpOverriddenBubbleDelegate::LogAction(
161 ExtensionMessageBubbleController::BubbleAction action) {
162 UMA_HISTOGRAM_ENUMERATION(
163 "ExtensionOverrideBubble.NtpOverriddenUserSelection",
164 action,
165 ExtensionMessageBubbleController::ACTION_BOUNDARY);
168 } // namespace
170 namespace extensions {
172 ////////////////////////////////////////////////////////////////////////////////
173 // NtpOverriddenBubbleController
175 NtpOverriddenBubbleController::NtpOverriddenBubbleController(Profile* profile)
176 : ExtensionMessageBubbleController(
177 new NtpOverriddenBubbleDelegate(
178 ExtensionSystem::Get(profile)->extension_service(),
179 profile),
180 profile),
181 profile_(profile) {}
183 NtpOverriddenBubbleController::~NtpOverriddenBubbleController() {}
185 bool NtpOverriddenBubbleController::ShouldShow(
186 const std::string& extension_id) {
187 if (!delegate()->ShouldIncludeExtension(extension_id))
188 return false;
190 delegate()->RestrictToSingleExtension(extension_id);
191 return true;
194 bool NtpOverriddenBubbleController::CloseOnDeactivate() {
195 return true;
198 } // namespace extensions