Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / proxy_overridden_bubble_controller.cc
blob552ecfb001ea072adec4714432c5e8351220f138
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 virtual ~ProxyOverriddenBubbleDelegate();
39 // ExtensionMessageBubbleController::Delegate methods.
40 virtual bool ShouldIncludeExtension(const std::string& extension_id) override;
41 virtual void AcknowledgeExtension(
42 const std::string& extension_id,
43 ExtensionMessageBubbleController::BubbleAction
44 user_action) override;
45 virtual void PerformAction(const ExtensionIdList& list) override;
46 virtual void OnClose() override;
47 virtual base::string16 GetTitle() const override;
48 virtual base::string16 GetMessageBody(
49 bool anchored_to_browser_action) const override;
50 virtual base::string16 GetOverflowText(
51 const base::string16& overflow_count) const override;
52 virtual GURL GetLearnMoreUrl() const override;
53 virtual base::string16 GetActionButtonLabel() const override;
54 virtual base::string16 GetDismissButtonLabel() const override;
55 virtual bool ShouldShowExtensionList() const override;
56 virtual void RestrictToSingleExtension(
57 const std::string& extension_id) override;
58 virtual void LogExtensionCount(size_t count) override;
59 virtual void LogAction(
60 ExtensionMessageBubbleController::BubbleAction
61 action) override;
63 private:
64 // Our extension service. Weak, not owned by us.
65 ExtensionService* service_;
67 // The ID of the extension we are showing the bubble for.
68 std::string extension_id_;
70 DISALLOW_COPY_AND_ASSIGN(ProxyOverriddenBubbleDelegate);
73 ProxyOverriddenBubbleDelegate::ProxyOverriddenBubbleDelegate(
74 ExtensionService* service,
75 Profile* profile)
76 : ExtensionMessageBubbleController::Delegate(profile),
77 service_(service) {
78 set_acknowledged_flag_pref_name(kProxyBubbleAcknowledged);
81 ProxyOverriddenBubbleDelegate::~ProxyOverriddenBubbleDelegate() {}
83 bool ProxyOverriddenBubbleDelegate::ShouldIncludeExtension(
84 const std::string& extension_id) {
85 if (!extension_id_.empty() && extension_id_ != extension_id)
86 return false;
88 const Extension* extension =
89 ExtensionRegistry::Get(profile())->enabled_extensions().GetByID(
90 extension_id);
91 if (!extension)
92 return false; // The extension provided is no longer enabled.
94 const Extension* overriding = GetExtensionOverridingProxy(profile());
95 if (!overriding || overriding->id() != extension_id)
96 return false;
98 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
99 base::TimeDelta since_install =
100 base::Time::Now() - prefs->GetInstallTime(extension->id());
101 if (since_install.InDays() < kDaysSinceInstallMin)
102 return false;
104 if (HasBubbleInfoBeenAcknowledged(extension_id))
105 return false;
107 return true;
110 void ProxyOverriddenBubbleDelegate::AcknowledgeExtension(
111 const std::string& extension_id,
112 ExtensionMessageBubbleController::BubbleAction user_action) {
113 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE)
114 SetBubbleInfoBeenAcknowledged(extension_id, true);
117 void ProxyOverriddenBubbleDelegate::PerformAction(const ExtensionIdList& list) {
118 for (size_t i = 0; i < list.size(); ++i)
119 service_->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
122 void ProxyOverriddenBubbleDelegate::OnClose() {
123 ExtensionToolbarModel* toolbar_model =
124 ExtensionToolbarModel::Get(profile());
125 if (toolbar_model)
126 toolbar_model->StopHighlighting();
129 base::string16 ProxyOverriddenBubbleDelegate::GetTitle() const {
130 return l10n_util::GetStringUTF16(
131 IDS_EXTENSIONS_PROXY_CONTROLLED_TITLE_HOME_PAGE_BUBBLE);
134 base::string16 ProxyOverriddenBubbleDelegate::GetMessageBody(
135 bool anchored_to_browser_action) const {
136 if (anchored_to_browser_action) {
137 return l10n_util::GetStringUTF16(
138 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE_EXTENSION_SPECIFIC);
139 } else {
140 return l10n_util::GetStringUTF16(
141 IDS_EXTENSIONS_PROXY_CONTROLLED_FIRST_LINE);
145 base::string16 ProxyOverriddenBubbleDelegate::GetOverflowText(
146 const base::string16& overflow_count) const {
147 // Does not have more than one extension in the list at a time.
148 NOTREACHED();
149 return base::string16();
152 GURL ProxyOverriddenBubbleDelegate::GetLearnMoreUrl() const {
153 return GURL(chrome::kExtensionControlledSettingLearnMoreURL);
156 base::string16 ProxyOverriddenBubbleDelegate::GetActionButtonLabel() const {
157 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_RESTORE_SETTINGS);
160 base::string16 ProxyOverriddenBubbleDelegate::GetDismissButtonLabel() const {
161 return l10n_util::GetStringUTF16(IDS_EXTENSION_CONTROLLED_KEEP_CHANGES);
164 bool ProxyOverriddenBubbleDelegate::ShouldShowExtensionList() const {
165 return false;
168 void ProxyOverriddenBubbleDelegate::RestrictToSingleExtension(
169 const std::string& extension_id) {
170 extension_id_ = extension_id;
173 void ProxyOverriddenBubbleDelegate::LogExtensionCount(size_t count) {
174 UMA_HISTOGRAM_COUNTS_100("ProxyOverriddenBubble.ExtensionCount", count);
177 void ProxyOverriddenBubbleDelegate::LogAction(
178 ExtensionMessageBubbleController::BubbleAction action) {
179 UMA_HISTOGRAM_ENUMERATION("ProxyOverriddenBubble.UserSelection",
180 action,
181 ExtensionMessageBubbleController::ACTION_BOUNDARY);
184 } // namespace
186 ////////////////////////////////////////////////////////////////////////////////
187 // ProxyOverriddenBubbleController
189 ProxyOverriddenBubbleController::ProxyOverriddenBubbleController(
190 Profile* profile)
191 : ExtensionMessageBubbleController(
192 new ProxyOverriddenBubbleDelegate(
193 ExtensionSystem::Get(profile)->extension_service(),
194 profile),
195 profile),
196 profile_(profile) {}
198 ProxyOverriddenBubbleController::~ProxyOverriddenBubbleController() {}
200 bool ProxyOverriddenBubbleController::ShouldShow(
201 const std::string& extension_id) {
202 if (!delegate()->ShouldIncludeExtension(extension_id))
203 return false;
205 delegate()->RestrictToSingleExtension(extension_id);
206 return true;
209 bool ProxyOverriddenBubbleController::CloseOnDeactivate() {
210 return false;
213 } // namespace extensions