OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/extensions/ntp_controlled_bubble_controller.h" | |
6 | |
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 "extensions/browser/extension_registry.h" | |
12 #include "extensions/browser/extension_system.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 | |
16 using extensions::ExtensionMessageBubbleController; | |
17 using extensions::NtpControlledBubbleController; | |
18 | |
19 namespace { | |
20 | |
21 //////////////////////////////////////////////////////////////////////////////// | |
22 // NtpControlledBubbleDelegate | |
23 | |
24 class NtpControlledBubbleDelegate | |
25 : public extensions::ExtensionMessageBubbleController::Delegate { | |
26 public: | |
27 NtpControlledBubbleDelegate(ExtensionService* service, Profile* profile); | |
28 virtual ~NtpControlledBubbleDelegate(); | |
29 | |
30 // ExtensionMessageBubbleController::Delegate methods. | |
31 virtual bool ShouldIncludeExtension(const std::string& extension_id) OVERRIDE; | |
32 virtual void AcknowledgeExtension( | |
33 const std::string& extension_id, | |
34 extensions::ExtensionMessageBubbleController::BubbleAction | |
35 user_action) OVERRIDE; | |
36 virtual void PerformAction(const extensions::ExtensionIdList& list) OVERRIDE; | |
37 virtual base::string16 GetTitle() const OVERRIDE; | |
38 virtual base::string16 GetMessageBody() const OVERRIDE; | |
39 virtual base::string16 GetOverflowText( | |
40 const base::string16& overflow_count) const OVERRIDE; | |
41 virtual base::string16 GetLearnMoreLabel() const OVERRIDE; | |
42 virtual GURL GetLearnMoreUrl() const OVERRIDE; | |
43 virtual base::string16 GetActionButtonLabel() const OVERRIDE; | |
44 virtual base::string16 GetDismissButtonLabel() const OVERRIDE; | |
45 virtual bool ShouldShowExtensionList() const OVERRIDE; | |
46 virtual void RestrictToSingleExtension( | |
47 const std::string& extension_id) OVERRIDE; | |
48 virtual void LogExtensionCount(size_t count) OVERRIDE; | |
49 virtual void LogAction( | |
50 extensions::ExtensionMessageBubbleController::BubbleAction | |
51 action) OVERRIDE; | |
52 | |
53 private: | |
54 // Our extension service. Weak, not owned by us. | |
55 ExtensionService* service_; | |
56 | |
57 // A weak pointer to the profile we are associated with. Not owned by us. | |
58 Profile* profile_; | |
59 | |
60 // The ID of the extension we are showing the bubble for. | |
61 std::string extension_id_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(NtpControlledBubbleDelegate); | |
64 }; | |
65 | |
66 NtpControlledBubbleDelegate::NtpControlledBubbleDelegate( | |
67 ExtensionService* service, | |
68 Profile* profile) | |
69 : service_(service), profile_(profile) {} | |
70 | |
71 NtpControlledBubbleDelegate::~NtpControlledBubbleDelegate() {} | |
72 | |
73 bool NtpControlledBubbleDelegate::ShouldIncludeExtension( | |
74 const std::string& extension_id) { | |
75 if (!extension_id_.empty() && extension_id_ != extension_id) | |
76 return false; | |
77 | |
78 using extensions::ExtensionRegistry; | |
79 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); | |
80 const extensions::Extension* extension = | |
81 registry->GetExtensionById(extension_id, ExtensionRegistry::ENABLED); | |
82 if (!extension) | |
83 return false; // The extension provided is no longer enabled. | |
84 | |
85 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_); | |
86 if (prefs->HasNtpControlledBubbleBeenAcknowledged(extension_id)) | |
87 return false; | |
88 | |
89 return true; | |
90 } | |
91 | |
92 void NtpControlledBubbleDelegate::AcknowledgeExtension( | |
93 const std::string& extension_id, | |
94 ExtensionMessageBubbleController::BubbleAction user_action) { | |
95 if (user_action != ExtensionMessageBubbleController::ACTION_EXECUTE) { | |
96 extensions::ExtensionPrefs* prefs = | |
97 extensions::ExtensionPrefs::Get(profile_); | |
98 prefs->SetNtpControlledBubbleBeenAcknowledged(extension_id, true); | |
99 } | |
100 } | |
101 | |
102 void NtpControlledBubbleDelegate::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); | |
107 } | |
108 } | |
109 | |
110 base::string16 NtpControlledBubbleDelegate::GetTitle() const { | |
111 return l10n_util::GetStringUTF16( | |
112 IDS_EXTENSIONS_NTP_CONTROLLED_TITLE_HOME_PAGE_BUBBLE); | |
113 } | |
114 | |
115 base::string16 NtpControlledBubbleDelegate::GetMessageBody() const { | |
116 base::string16 body = | |
117 l10n_util::GetStringUTF16(IDS_EXTENSIONS_NTP_CONTROLLED_FIRST_LINE); | |
118 body += l10n_util::GetStringUTF16( | |
119 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION); | |
120 return body; | |
121 } | |
122 | |
123 base::string16 NtpControlledBubbleDelegate::GetOverflowText( | |
124 const base::string16& overflow_count) const { | |
125 // Does not have more than one extension in the list at a time. | |
126 NOTREACHED(); | |
127 return base::string16(); | |
128 } | |
129 | |
130 base::string16 NtpControlledBubbleDelegate::GetLearnMoreLabel() const { | |
131 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); | |
132 } | |
133 | |
134 GURL NtpControlledBubbleDelegate::GetLearnMoreUrl() const { | |
135 // TODO(finnur): Rename the const when things settle down (since it is used in | |
136 // more places than for the Settings API bubble now). | |
137 return GURL(chrome::kSettingsApiLearnMoreURL); | |
138 } | |
139 | |
140 base::string16 NtpControlledBubbleDelegate::GetActionButtonLabel() const { | |
141 // TODO(finnur): Rename the const when things settle down (since it is used in | |
142 // more places than for the Settings API bubble now). | |
143 return l10n_util::GetStringUTF16( | |
144 IDS_EXTENSIONS_SETTINGS_API_RESTORE_SETTINGS); | |
145 } | |
146 | |
147 base::string16 NtpControlledBubbleDelegate::GetDismissButtonLabel() const { | |
148 // TODO(finnur): Rename the const when things settle down (since it is used in | |
149 // more places than for the Settings API bubble now). | |
150 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SETTINGS_API_KEEP_CHANGES); | |
151 } | |
152 | |
153 bool NtpControlledBubbleDelegate::ShouldShowExtensionList() const { | |
154 return false; | |
155 } | |
156 | |
157 void NtpControlledBubbleDelegate::RestrictToSingleExtension( | |
Yoyo Zhou
2014/04/23 00:08:35
Considering the lifetime of NtpControlledBubbleCon
| |
158 const std::string& extension_id) { | |
159 extension_id_ = extension_id; | |
160 } | |
161 | |
162 void NtpControlledBubbleDelegate::LogExtensionCount(size_t count) { | |
163 UMA_HISTOGRAM_COUNTS_100("NtpControlledBubble.ExtensionCount", count); | |
164 } | |
165 | |
166 void NtpControlledBubbleDelegate::LogAction( | |
167 ExtensionMessageBubbleController::BubbleAction action) { | |
168 UMA_HISTOGRAM_ENUMERATION("NtpControlledBubble.UserSelection", | |
169 action, | |
170 ExtensionMessageBubbleController::ACTION_BOUNDARY); | |
171 } | |
172 | |
173 } // namespace | |
174 | |
175 namespace extensions { | |
176 | |
177 //////////////////////////////////////////////////////////////////////////////// | |
178 // NtpControlledBubbleController | |
179 | |
180 NtpControlledBubbleController::NtpControlledBubbleController(Profile* profile) | |
181 : ExtensionMessageBubbleController( | |
182 new NtpControlledBubbleDelegate( | |
183 ExtensionSystem::Get(profile)->extension_service(), | |
184 profile), | |
185 profile), | |
186 profile_(profile) {} | |
187 | |
188 NtpControlledBubbleController::~NtpControlledBubbleController() {} | |
189 | |
190 bool NtpControlledBubbleController::ShouldShow( | |
191 const std::string& extension_id) { | |
192 if (!delegate()->ShouldIncludeExtension(extension_id)) | |
193 return false; | |
194 | |
195 delegate()->RestrictToSingleExtension(extension_id); | |
196 return true; | |
197 } | |
198 | |
199 bool NtpControlledBubbleController::CloseOnDeactivate() { | |
200 return true; | |
201 } | |
202 | |
203 } // namespace extensions | |
OLD | NEW |