Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / proxy_overridden_bubble_controller.cc
blob0c47a281ffd9e3441642286fb64a35c30e1485f0
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 "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/settings_api_helpers.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h"
18 #include "grit/components_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
21 namespace extensions {
23 namespace {
25 // The minimum time to wait (since the extension was installed) before notifying
26 // the user about it.
27 const int kDaysSinceInstallMin = 7;
29 // Whether the user has been notified about extension overriding the proxy.
30 const char kProxyBubbleAcknowledged[] = "ack_proxy_bubble";
32 ////////////////////////////////////////////////////////////////////////////////
33 // ProxyOverriddenBubbleDelegate
35 class ProxyOverriddenBubbleDelegate
36 : public ExtensionMessageBubbleController::Delegate {
37 public:
38 explicit ProxyOverriddenBubbleDelegate(Profile* profile);
39 ~ProxyOverriddenBubbleDelegate() override;
41 // ExtensionMessageBubbleController::Delegate methods.
42 bool ShouldIncludeExtension(const std::string& extension_id) override;
43 void AcknowledgeExtension(
44 const std::string& extension_id,
45 ExtensionMessageBubbleController::BubbleAction user_action) override;
46 void PerformAction(const ExtensionIdList& list) override;
47 base::string16 GetTitle() const override;
48 base::string16 GetMessageBody(bool anchored_to_browser_action,
49 int extension_count) const override;
50 base::string16 GetOverflowText(
51 const base::string16& overflow_count) const override;
52 GURL GetLearnMoreUrl() const override;
53 base::string16 GetActionButtonLabel() const override;
54 base::string16 GetDismissButtonLabel() const override;
55 bool ShouldShowExtensionList() const override;
56 bool ShouldHighlightExtensions() const override;
57 void RestrictToSingleExtension(const std::string& extension_id) override;
58 void LogExtensionCount(size_t count) override;
59 void LogAction(
60 ExtensionMessageBubbleController::BubbleAction action) override;
62 private:
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 Profile* profile)
71 : ExtensionMessageBubbleController::Delegate(profile) {
72 set_acknowledged_flag_pref_name(kProxyBubbleAcknowledged);
75 ProxyOverriddenBubbleDelegate::~ProxyOverriddenBubbleDelegate() {}
77 bool ProxyOverriddenBubbleDelegate::ShouldIncludeExtension(
78 const std::string& extension_id) {
79 if (!extension_id_.empty() && extension_id_ != extension_id)
80 return false;
82 const Extension* extension =
83 registry()->enabled_extensions().GetByID(extension_id);
84 if (!extension)
85 return false; // The extension provided is no longer enabled.
87 const Extension* overriding = GetExtensionOverridingProxy(profile());
88 if (!overriding || overriding->id() != extension_id)
89 return false;
91 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
92 base::TimeDelta since_install =
93 base::Time::Now() - prefs->GetInstallTime(extension->id());
94 if (since_install.InDays() < kDaysSinceInstallMin)
95 return false;
97 if (HasBubbleInfoBeenAcknowledged(extension_id))
98 return false;
100 return true;
103 void ProxyOverriddenBubbleDelegate::AcknowledgeExtension(
104 const std::string& extension_id,
105 ExtensionMessageBubbleController::BubbleAction user_action) {
106 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
107 SetBubbleInfoBeenAcknowledged(extension_id, true);
110 void ProxyOverriddenBubbleDelegate::PerformAction(const ExtensionIdList& list) {
111 for (size_t i = 0; i < list.size(); ++i)
112 service()->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
115 base::string16 ProxyOverriddenBubbleDelegate::GetTitle() const {
116 return l10n_util::GetStringUTF16(
117 IDS_EXTENSIONS_PROXY_CONTROLLED_TITLE_HOME_PAGE_BUBBLE);
120 base::string16 ProxyOverriddenBubbleDelegate::GetMessageBody(
121 bool anchored_to_browser_action,
122 int extension_count) const {
123 if (anchored_to_browser_action) {
124 return l10n_util::GetStringUTF16(
125 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE_EXTENSION_SPECIFIC);
126 } else {
127 const Extension* extension = registry()->GetExtensionById(
128 extension_id_, ExtensionRegistry::EVERYTHING);
129 // If the bubble is about to show, the extension should certainly exist.
130 CHECK(extension);
131 return l10n_util::GetStringFUTF16(
132 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE,
133 base::UTF8ToUTF16(extension->name()));
137 base::string16 ProxyOverriddenBubbleDelegate::GetOverflowText(
138 const base::string16& overflow_count) const {
139 // Does not have more than one extension in the list at a time.
140 NOTREACHED();
141 return base::string16();
144 GURL ProxyOverriddenBubbleDelegate::GetLearnMoreUrl() const {
145 return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
148 base::string16 ProxyOverriddenBubbleDelegate::GetActionButtonLabel() const {
149 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
152 base::string16 ProxyOverriddenBubbleDelegate::GetDismissButtonLabel() const {
153 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
156 bool ProxyOverriddenBubbleDelegate::ShouldShowExtensionList() const {
157 return false;
160 bool ProxyOverriddenBubbleDelegate::ShouldHighlightExtensions() const {
161 return true;
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 Browser* browser)
187 : ExtensionMessageBubbleController(
188 new ProxyOverriddenBubbleDelegate(browser->profile()),
189 browser) {}
191 ProxyOverriddenBubbleController::~ProxyOverriddenBubbleController() {}
193 bool ProxyOverriddenBubbleController::ShouldShow(
194 const std::string& extension_id) {
195 if (!delegate()->ShouldIncludeExtension(extension_id))
196 return false;
198 delegate()->RestrictToSingleExtension(extension_id);
199 return true;
202 bool ProxyOverriddenBubbleController::CloseOnDeactivate() {
203 return false;
206 } // namespace extensions