[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / extensions / proxy_overridden_bubble_controller.cc
blobd25204d58bdf097f6c06fcd19cd6a3ac57e658dd
1 // Copyright 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/proxy_overridden_bubble_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_toolbar_model.h"
10 #include "chrome/browser/extensions/settings_api_helpers.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/extension_system.h"
16 #include "grit/components_strings.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace extensions {
21 namespace {
23 // The minimum time to wait (since the extension was installed) before notifying
24 // the user about it.
25 const int kDaysSinceInstallMin = 7;
27 // Whether the user has been notified about extension overriding the proxy.
28 const char kProxyBubbleAcknowledged[] = "ack_proxy_bubble";
30 ////////////////////////////////////////////////////////////////////////////////
31 // ProxyOverriddenBubbleDelegate
33 class ProxyOverriddenBubbleDelegate
34 : public ExtensionMessageBubbleController::Delegate {
35 public:
36 ProxyOverriddenBubbleDelegate(ExtensionService* service, Profile* profile);
37 ~ProxyOverriddenBubbleDelegate() override;
39 // ExtensionMessageBubbleController::Delegate methods.
40 bool ShouldIncludeExtension(const std::string& extension_id) override;
41 void AcknowledgeExtension(
42 const std::string& extension_id,
43 ExtensionMessageBubbleController::BubbleAction user_action) override;
44 void PerformAction(const ExtensionIdList& list) override;
45 void OnClose() override;
46 base::string16 GetTitle() const override;
47 base::string16 GetMessageBody(bool anchored_to_browser_action) const override;
48 base::string16 GetOverflowText(
49 const base::string16& overflow_count) const override;
50 GURL GetLearnMoreUrl() const override;
51 base::string16 GetActionButtonLabel() const override;
52 base::string16 GetDismissButtonLabel() const override;
53 bool ShouldShowExtensionList() const override;
54 bool ShouldHighlightExtensions() const override;
55 void RestrictToSingleExtension(const std::string& extension_id) override;
56 void LogExtensionCount(size_t count) override;
57 void LogAction(
58 ExtensionMessageBubbleController::BubbleAction action) override;
60 private:
61 // Our extension service. Weak, not owned by us.
62 ExtensionService* service_;
64 // The ID of the extension we are showing the bubble for.
65 std::string extension_id_;
67 DISALLOW_COPY_AND_ASSIGN(ProxyOverriddenBubbleDelegate);
70 ProxyOverriddenBubbleDelegate::ProxyOverriddenBubbleDelegate(
71 ExtensionService* service,
72 Profile* profile)
73 : ExtensionMessageBubbleController::Delegate(profile),
74 service_(service) {
75 set_acknowledged_flag_pref_name(kProxyBubbleAcknowledged);
78 ProxyOverriddenBubbleDelegate::~ProxyOverriddenBubbleDelegate() {}
80 bool ProxyOverriddenBubbleDelegate::ShouldIncludeExtension(
81 const std::string& extension_id) {
82 if (!extension_id_.empty() && extension_id_ != extension_id)
83 return false;
85 const Extension* extension =
86 ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(
87 extension_id);
88 if (!extension)
89 return false; // The extension provided is no longer enabled.
91 const Extension* overriding = GetExtensionOverridingProxy(profile());
92 if (!overriding || overriding->id() != extension_id)
93 return false;
95 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
96 base::TimeDelta since_install =
97 base::Time::Now() - prefs->GetInstallTime(extension->id());
98 if (since_install.InDays() < kDaysSinceInstallMin)
99 return false;
101 if (HasBubbleInfoBeenAcknowledged(extension_id))
102 return false;
104 return true;
107 void ProxyOverriddenBubbleDelegate::AcknowledgeExtension(
108 const std::string& extension_id,
109 ExtensionMessageBubbleController::BubbleAction user_action) {
110 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
111 SetBubbleInfoBeenAcknowledged(extension_id, true);
114 void ProxyOverriddenBubbleDelegate::PerformAction(const ExtensionIdList& list) {
115 for (size_t i = 0; i < list.size(); ++i)
116 service_->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
119 void ProxyOverriddenBubbleDelegate::OnClose() {
120 ExtensionToolbarModel* toolbar_model =
121 ExtensionToolbarModel::Get(profile());
122 if (toolbar_model)
123 toolbar_model->StopHighlighting();
126 base::string16 ProxyOverriddenBubbleDelegate::GetTitle() const {
127 return l10n_util::GetStringUTF16(
128 IDS_EXTENSIONS_PROXY_CONTROLLED_TITLE_HOME_PAGE_BUBBLE);
131 base::string16 ProxyOverriddenBubbleDelegate::GetMessageBody(
132 bool anchored_to_browser_action) const {
133 if (anchored_to_browser_action) {
134 return l10n_util::GetStringUTF16(
135 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE_EXTENSION_SPECIFIC);
136 } else {
137 return l10n_util::GetStringUTF16(
138 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE);
142 base::string16 ProxyOverriddenBubbleDelegate::GetOverflowText(
143 const base::string16& overflow_count) const {
144 // Does not have more than one extension in the list at a time.
145 NOTREACHED();
146 return base::string16();
149 GURL ProxyOverriddenBubbleDelegate::GetLearnMoreUrl() const {
150 return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
153 base::string16 ProxyOverriddenBubbleDelegate::GetActionButtonLabel() const {
154 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
157 base::string16 ProxyOverriddenBubbleDelegate::GetDismissButtonLabel() const {
158 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
161 bool ProxyOverriddenBubbleDelegate::ShouldShowExtensionList() const {
162 return false;
165 bool ProxyOverriddenBubbleDelegate::ShouldHighlightExtensions() const {
166 return true;
169 void ProxyOverriddenBubbleDelegate::RestrictToSingleExtension(
170 const std::string& extension_id) {
171 extension_id_ = extension_id;
174 void ProxyOverriddenBubbleDelegate::LogExtensionCount(size_t count) {
175 UMA_HISTOGRAM_COUNTS_100("ProxyOverriddenBubble.ExtensionCount", count);
178 void ProxyOverriddenBubbleDelegate::LogAction(
179 ExtensionMessageBubbleController::BubbleAction action) {
180 UMA_HISTOGRAM_ENUMERATION("ProxyOverriddenBubble.UserSelection",
181 action,
182 ExtensionMessageBubbleController::ACTION_BOUNDARY);
185 } // namespace
187 ////////////////////////////////////////////////////////////////////////////////
188 // ProxyOverriddenBubbleController
190 ProxyOverriddenBubbleController::ProxyOverriddenBubbleController(
191 Profile* profile)
192 : ExtensionMessageBubbleController(
193 new ProxyOverriddenBubbleDelegate(
194 ExtensionSystem::Get(profile)->extension_service(),
195 profile),
196 profile),
197 profile_(profile) {}
199 ProxyOverriddenBubbleController::~ProxyOverriddenBubbleController() {}
201 bool ProxyOverriddenBubbleController::ShouldShow(
202 const std::string& extension_id) {
203 if (!delegate()->ShouldIncludeExtension(extension_id))
204 return false;
206 delegate()->RestrictToSingleExtension(extension_id);
207 return true;
210 bool ProxyOverriddenBubbleController::CloseOnDeactivate() {
211 return false;
214 } // namespace extensions