Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / extensions / proxy_overridden_bubble_controller.cc
bloba3cde4cacb2f36c9ebb0fe95f232d9cd66182080
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 void RestrictToSingleExtension(const std::string& extension_id) override;
55 void LogExtensionCount(size_t count) override;
56 void LogAction(
57 ExtensionMessageBubbleController::BubbleAction action) override;
59 private:
60 // Our extension service. Weak, not owned by us.
61 ExtensionService* service_;
63 // The ID of the extension we are showing the bubble for.
64 std::string extension_id_;
66 DISALLOW_COPY_AND_ASSIGN(ProxyOverriddenBubbleDelegate);
69 ProxyOverriddenBubbleDelegate::ProxyOverriddenBubbleDelegate(
70 ExtensionService* service,
71 Profile* profile)
72 : ExtensionMessageBubbleController::Delegate(profile),
73 service_(service) {
74 set_acknowledged_flag_pref_name(kProxyBubbleAcknowledged);
77 ProxyOverriddenBubbleDelegate::~ProxyOverriddenBubbleDelegate() {}
79 bool ProxyOverriddenBubbleDelegate::ShouldIncludeExtension(
80 const std::string& extension_id) {
81 if (!extension_id_.empty() && extension_id_ != extension_id)
82 return false;
84 const Extension* extension =
85 ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(
86 extension_id);
87 if (!extension)
88 return false; // The extension provided is no longer enabled.
90 const Extension* overriding = GetExtensionOverridingProxy(profile());
91 if (!overriding || overriding->id() != extension_id)
92 return false;
94 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
95 base::TimeDelta since_install =
96 base::Time::Now() - prefs->GetInstallTime(extension->id());
97 if (since_install.InDays() < kDaysSinceInstallMin)
98 return false;
100 if (HasBubbleInfoBeenAcknowledged(extension_id))
101 return false;
103 return true;
106 void ProxyOverriddenBubbleDelegate::AcknowledgeExtension(
107 const std::string& extension_id,
108 ExtensionMessageBubbleController::BubbleAction user_action) {
109 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
110 SetBubbleInfoBeenAcknowledged(extension_id, true);
113 void ProxyOverriddenBubbleDelegate::PerformAction(const ExtensionIdList& list) {
114 for (size_t i = 0; i < list.size(); ++i)
115 service_->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
118 void ProxyOverriddenBubbleDelegate::OnClose() {
119 ExtensionToolbarModel* toolbar_model =
120 ExtensionToolbarModel::Get(profile());
121 if (toolbar_model)
122 toolbar_model->StopHighlighting();
125 base::string16 ProxyOverriddenBubbleDelegate::GetTitle() const {
126 return l10n_util::GetStringUTF16(
127 IDS_EXTENSIONS_PROXY_CONTROLLED_TITLE_HOME_PAGE_BUBBLE);
130 base::string16 ProxyOverriddenBubbleDelegate::GetMessageBody(
131 bool anchored_to_browser_action) const {
132 if (anchored_to_browser_action) {
133 return l10n_util::GetStringUTF16(
134 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE_EXTENSION_SPECIFIC);
135 } else {
136 return l10n_util::GetStringUTF16(
137 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE);
141 base::string16 ProxyOverriddenBubbleDelegate::GetOverflowText(
142 const base::string16& overflow_count) const {
143 // Does not have more than one extension in the list at a time.
144 NOTREACHED();
145 return base::string16();
148 GURL ProxyOverriddenBubbleDelegate::GetLearnMoreUrl() const {
149 return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
152 base::string16 ProxyOverriddenBubbleDelegate::GetActionButtonLabel() const {
153 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
156 base::string16 ProxyOverriddenBubbleDelegate::GetDismissButtonLabel() const {
157 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
160 bool ProxyOverriddenBubbleDelegate::ShouldShowExtensionList() const {
161 return false;
164 void ProxyOverriddenBubbleDelegate::RestrictToSingleExtension(
165 const std::string& extension_id) {
166 extension_id_ = extension_id;
169 void ProxyOverriddenBubbleDelegate::LogExtensionCount(size_t count) {
170 UMA_HISTOGRAM_COUNTS_100("ProxyOverriddenBubble.ExtensionCount", count);
173 void ProxyOverriddenBubbleDelegate::LogAction(
174 ExtensionMessageBubbleController::BubbleAction action) {
175 UMA_HISTOGRAM_ENUMERATION("ProxyOverriddenBubble.UserSelection",
176 action,
177 ExtensionMessageBubbleController::ACTION_BOUNDARY);
180 } // namespace
182 ////////////////////////////////////////////////////////////////////////////////
183 // ProxyOverriddenBubbleController
185 ProxyOverriddenBubbleController::ProxyOverriddenBubbleController(
186 Profile* profile)
187 : ExtensionMessageBubbleController(
188 new ProxyOverriddenBubbleDelegate(
189 ExtensionSystem::Get(profile)->extension_service(),
190 profile),
191 profile),
192 profile_(profile) {}
194 ProxyOverriddenBubbleController::~ProxyOverriddenBubbleController() {}
196 bool ProxyOverriddenBubbleController::ShouldShow(
197 const std::string& extension_id) {
198 if (!delegate()->ShouldIncludeExtension(extension_id))
199 return false;
201 delegate()->RestrictToSingleExtension(extension_id);
202 return true;
205 bool ProxyOverriddenBubbleController::CloseOnDeactivate() {
206 return false;
209 } // namespace extensions