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.
5 #include "chrome/browser/extensions/settings_api_bubble_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/startup/startup_browser_creator.h"
11 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
12 #include "chrome/common/url_constants.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extension_system.h"
15 #include "grit/chromium_strings.h"
16 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using extensions::ExtensionMessageBubbleController;
20 using extensions::SettingsApiBubbleController;
21 using extensions::SettingsOverrides;
25 ////////////////////////////////////////////////////////////////////////////////
26 // SettingsApiBubbleDelegate
28 class SettingsApiBubbleDelegate
29 : public extensions::ExtensionMessageBubbleController::Delegate {
31 explicit SettingsApiBubbleDelegate(ExtensionService* service,
33 extensions::SettingsApiOverrideType type);
34 virtual ~SettingsApiBubbleDelegate();
36 // ExtensionMessageBubbleController::Delegate methods.
37 virtual bool ShouldIncludeExtension(const std::string& extension_id) OVERRIDE;
38 virtual void AcknowledgeExtension(
39 const std::string& extension_id,
40 extensions::ExtensionMessageBubbleController::BubbleAction user_action)
42 virtual void PerformAction(const extensions::ExtensionIdList& list) OVERRIDE;
43 virtual base::string16 GetTitle() const OVERRIDE;
44 virtual base::string16 GetMessageBody() const OVERRIDE;
45 virtual base::string16 GetOverflowText(
46 const base::string16& overflow_count) const OVERRIDE;
47 virtual base::string16 GetLearnMoreLabel() const OVERRIDE;
48 virtual GURL GetLearnMoreUrl() const OVERRIDE;
49 virtual base::string16 GetActionButtonLabel() const OVERRIDE;
50 virtual base::string16 GetDismissButtonLabel() const OVERRIDE;
51 virtual bool ShouldShowExtensionList() const OVERRIDE;
52 virtual void LogExtensionCount(size_t count) OVERRIDE;
53 virtual void LogAction(
54 extensions::ExtensionMessageBubbleController::BubbleAction action)
58 // Our extension service. Weak, not owned by us.
59 ExtensionService* service_;
61 // A weak pointer to the profile we are associated with. Not owned by us.
64 // The type of settings override this bubble will report on. This can be, for
65 // example, a bubble to notify the user that the search engine has been
66 // changed by an extension (or homepage/startup pages/etc).
67 extensions::SettingsApiOverrideType type_;
69 // The ID of the extension we are showing the bubble for.
70 std::string extension_id_;
72 DISALLOW_COPY_AND_ASSIGN(SettingsApiBubbleDelegate);
75 SettingsApiBubbleDelegate::SettingsApiBubbleDelegate(
76 ExtensionService* service,
78 extensions::SettingsApiOverrideType type)
79 : service_(service), profile_(profile), type_(type) {}
81 SettingsApiBubbleDelegate::~SettingsApiBubbleDelegate() {}
83 bool SettingsApiBubbleDelegate::ShouldIncludeExtension(
84 const std::string& extension_id) {
85 using extensions::ExtensionRegistry;
86 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
87 const extensions::Extension* extension =
88 registry->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
90 return false; // The extension provided is no longer enabled.
92 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
93 if (prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id))
96 const SettingsOverrides* settings = SettingsOverrides::Get(extension);
100 bool should_include = false;
102 case extensions::BUBBLE_TYPE_HOME_PAGE:
103 should_include = settings->homepage != NULL;
105 case extensions::BUBBLE_TYPE_STARTUP_PAGES:
106 should_include = !settings->startup_pages.empty();
108 case extensions::BUBBLE_TYPE_SEARCH_ENGINE:
109 should_include = settings->search_engine != NULL;
113 if (should_include && extension_id_ != extension_id) {
114 DCHECK(extension_id_.empty());
115 extension_id_ = extension_id;
117 return should_include;
120 void SettingsApiBubbleDelegate::AcknowledgeExtension(
121 const std::string& extension_id,
122 ExtensionMessageBubbleController::BubbleAction user_action) {
123 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
124 prefs->SetSettingsApiBubbleBeenAcknowledged(extension_id, true);
127 void SettingsApiBubbleDelegate::PerformAction(
128 const extensions::ExtensionIdList& list) {
129 for (size_t i = 0; i < list.size(); ++i) {
130 service_->DisableExtension(list[i],
131 extensions::Extension::DISABLE_USER_ACTION);
135 base::string16 SettingsApiBubbleDelegate::GetTitle() const {
137 case extensions::BUBBLE_TYPE_HOME_PAGE:
138 return l10n_util::GetStringUTF16(
139 IDS_EXTENSIONS_SETTINGS_API_TITLE_HOME_PAGE_BUBBLE);
140 case extensions::BUBBLE_TYPE_STARTUP_PAGES:
141 return l10n_util::GetStringUTF16(
142 IDS_EXTENSIONS_SETTINGS_API_TITLE_STARTUP_PAGES_BUBBLE);
143 case extensions::BUBBLE_TYPE_SEARCH_ENGINE:
144 return l10n_util::GetStringUTF16(
145 IDS_EXTENSIONS_SETTINGS_API_TITLE_SEARCH_ENGINE_BUBBLE);
148 return base::string16();
151 base::string16 SettingsApiBubbleDelegate::GetMessageBody() const {
152 using extensions::ExtensionRegistry;
153 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
154 const extensions::Extension* extension =
155 registry->GetExtensionById(extension_id_, ExtensionRegistry::ENABLED);
156 const SettingsOverrides* settings =
157 extension ? SettingsOverrides::Get(extension) : NULL;
158 if (!extension || !settings) {
160 return base::string16();
163 bool home_change = settings->homepage != NULL;
164 bool startup_change = !settings->startup_pages.empty();
165 bool search_change = settings->search_engine != NULL;
169 case extensions::BUBBLE_TYPE_HOME_PAGE:
170 body = l10n_util::GetStringUTF16(
171 IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_HOME_PAGE);
172 if (startup_change && search_change) {
173 body += l10n_util::GetStringUTF16(
174 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_AND_SEARCH);
175 } else if (startup_change) {
176 body += l10n_util::GetStringUTF16(
177 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_PAGES);
178 } else if (search_change) {
179 body += l10n_util::GetStringUTF16(
180 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_SEARCH_ENGINE);
183 case extensions::BUBBLE_TYPE_STARTUP_PAGES:
184 body = l10n_util::GetStringUTF16(
185 IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_START_PAGES);
186 if (home_change && search_change) {
187 body += l10n_util::GetStringUTF16(
188 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_AND_SEARCH);
189 } else if (home_change) {
190 body += l10n_util::GetStringUTF16(
191 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_PAGE);
192 } else if (search_change) {
193 body += l10n_util::GetStringUTF16(
194 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_SEARCH_ENGINE);
197 case extensions::BUBBLE_TYPE_SEARCH_ENGINE:
198 body = l10n_util::GetStringUTF16(
199 IDS_EXTENSIONS_SETTINGS_API_FIRST_LINE_SEARCH_ENGINE);
200 if (startup_change && home_change) {
201 body += l10n_util::GetStringUTF16(
202 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_AND_HOME);
203 } else if (startup_change) {
204 body += l10n_util::GetStringUTF16(
205 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_START_PAGES);
206 } else if (home_change) {
207 body += l10n_util::GetStringUTF16(
208 IDS_EXTENSIONS_SETTINGS_API_SECOND_LINE_HOME_PAGE);
213 body += l10n_util::GetStringUTF16(
214 IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION);
218 base::string16 SettingsApiBubbleDelegate::GetOverflowText(
219 const base::string16& overflow_count) const {
220 // Does not have more than one extension in the list at a time.
222 return base::string16();
225 base::string16 SettingsApiBubbleDelegate::GetLearnMoreLabel() const {
226 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
229 GURL SettingsApiBubbleDelegate::GetLearnMoreUrl() const {
230 return GURL(chrome::kSettingsApiLearnMoreURL);
233 base::string16 SettingsApiBubbleDelegate::GetActionButtonLabel() const {
234 return l10n_util::GetStringUTF16(
235 IDS_EXTENSIONS_SETTINGS_API_RESTORE_SETTINGS);
238 base::string16 SettingsApiBubbleDelegate::GetDismissButtonLabel() const {
239 return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SETTINGS_API_KEEP_CHANGES);
242 bool SettingsApiBubbleDelegate::ShouldShowExtensionList() const {
246 void SettingsApiBubbleDelegate::LogExtensionCount(size_t count) {
247 UMA_HISTOGRAM_COUNTS_100("SettingsApiBubble.ExtensionCount", count);
250 void SettingsApiBubbleDelegate::LogAction(
251 ExtensionMessageBubbleController::BubbleAction action) {
252 UMA_HISTOGRAM_ENUMERATION("SettingsApiBubble.UserSelection",
254 ExtensionMessageBubbleController::ACTION_BOUNDARY);
259 namespace extensions {
261 ////////////////////////////////////////////////////////////////////////////////
262 // SettingsApiBubbleController
264 SettingsApiBubbleController::SettingsApiBubbleController(
266 SettingsApiOverrideType type)
267 : ExtensionMessageBubbleController(
268 new SettingsApiBubbleDelegate(
269 ExtensionSystem::Get(profile)->extension_service(),
276 SettingsApiBubbleController::~SettingsApiBubbleController() {}
278 bool SettingsApiBubbleController::ShouldShow(const std::string& extension_id) {
279 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
280 if (prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id))
283 if (!delegate()->ShouldIncludeExtension(extension_id))
286 // If the browser is showing the 'Chrome crashed' infobar, it won't be showing
287 // the startup pages, so there's no point in showing the bubble now.
288 if (type_ == BUBBLE_TYPE_STARTUP_PAGES)
289 return profile_->GetLastSessionExitType() != Profile::EXIT_CRASHED;
294 bool SettingsApiBubbleController::CloseOnDeactivate() {
295 // Startup bubbles tend to get lost in the focus storm that happens on
296 // startup. Other types should dismiss on focus loss.
297 return type_ != BUBBLE_TYPE_STARTUP_PAGES;
300 } // namespace extensions