a5819bc0f8126027be909031b3f384ded6ea3012
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / settings_api_bubble_controller.cc
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/settings_api_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/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"
18
19 using extensions::ExtensionMessageBubbleController;
20 using extensions::SettingsApiBubbleController;
21 using extensions::SettingsOverrides;
22
23 namespace {
24
25 ////////////////////////////////////////////////////////////////////////////////
26 // SettingsApiBubbleDelegate
27
28 class SettingsApiBubbleDelegate
29     : public extensions::ExtensionMessageBubbleController::Delegate {
30  public:
31   explicit SettingsApiBubbleDelegate(ExtensionService* service,
32                                      Profile* profile,
33                                      extensions::SettingsApiOverrideType type);
34   virtual ~SettingsApiBubbleDelegate();
35
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)
41       OVERRIDE;
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)
55       OVERRIDE;
56
57  private:
58   // Our extension service. Weak, not owned by us.
59   ExtensionService* service_;
60
61   // A weak pointer to the profile we are associated with. Not owned by us.
62   Profile* profile_;
63
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_;
68
69   // The ID of the extension we are showing the bubble for.
70   std::string extension_id_;
71
72   DISALLOW_COPY_AND_ASSIGN(SettingsApiBubbleDelegate);
73 };
74
75 SettingsApiBubbleDelegate::SettingsApiBubbleDelegate(
76     ExtensionService* service,
77     Profile* profile,
78     extensions::SettingsApiOverrideType type)
79     : service_(service), profile_(profile), type_(type) {}
80
81 SettingsApiBubbleDelegate::~SettingsApiBubbleDelegate() {}
82
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);
89   if (!extension)
90     return false;  // The extension provided is no longer enabled.
91
92   extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
93   if (prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id))
94     return false;
95
96   const SettingsOverrides* settings = SettingsOverrides::Get(extension);
97   if (!settings)
98     return false;
99
100   bool should_include = false;
101   switch (type_) {
102     case extensions::BUBBLE_TYPE_HOME_PAGE:
103       should_include = settings->homepage != NULL;
104       break;
105     case extensions::BUBBLE_TYPE_STARTUP_PAGES:
106       should_include = !settings->startup_pages.empty();
107       break;
108     case extensions::BUBBLE_TYPE_SEARCH_ENGINE:
109       should_include = settings->search_engine != NULL;
110       break;
111   }
112
113   if (should_include && extension_id_ != extension_id) {
114     DCHECK(extension_id_.empty());
115     extension_id_ = extension_id;
116   }
117   return should_include;
118 }
119
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);
125 }
126
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);
132   }
133 }
134
135 base::string16 SettingsApiBubbleDelegate::GetTitle() const {
136   switch (type_) {
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);
146   }
147   NOTREACHED();
148   return base::string16();
149 }
150
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) {
159     NOTREACHED();
160     return base::string16();
161   }
162
163   bool home_change = settings->homepage != NULL;
164   bool startup_change = !settings->startup_pages.empty();
165   bool search_change = settings->search_engine != NULL;
166
167   base::string16 body;
168   switch (type_) {
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);
181       }
182       break;
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);
195       }
196       break;
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);
209       }
210       break;
211   }
212   if (!body.empty())
213     body += l10n_util::GetStringUTF16(
214             IDS_EXTENSIONS_SETTINGS_API_THIRD_LINE_CONFIRMATION);
215   return body;
216 }
217
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.
221   NOTREACHED();
222   return base::string16();
223 }
224
225 base::string16 SettingsApiBubbleDelegate::GetLearnMoreLabel() const {
226   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
227 }
228
229 GURL SettingsApiBubbleDelegate::GetLearnMoreUrl() const {
230   return GURL(chrome::kSettingsApiLearnMoreURL);
231 }
232
233 base::string16 SettingsApiBubbleDelegate::GetActionButtonLabel() const {
234   return l10n_util::GetStringUTF16(
235       IDS_EXTENSIONS_SETTINGS_API_RESTORE_SETTINGS);
236 }
237
238 base::string16 SettingsApiBubbleDelegate::GetDismissButtonLabel() const {
239   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_SETTINGS_API_KEEP_CHANGES);
240 }
241
242 bool SettingsApiBubbleDelegate::ShouldShowExtensionList() const {
243   return false;
244 }
245
246 void SettingsApiBubbleDelegate::LogExtensionCount(size_t count) {
247   UMA_HISTOGRAM_COUNTS_100("SettingsApiBubble.ExtensionCount", count);
248 }
249
250 void SettingsApiBubbleDelegate::LogAction(
251     ExtensionMessageBubbleController::BubbleAction action) {
252   UMA_HISTOGRAM_ENUMERATION("SettingsApiBubble.UserSelection",
253                             action,
254                             ExtensionMessageBubbleController::ACTION_BOUNDARY);
255 }
256
257 }  // namespace
258
259 namespace extensions {
260
261 ////////////////////////////////////////////////////////////////////////////////
262 // SettingsApiBubbleController
263
264 SettingsApiBubbleController::SettingsApiBubbleController(
265     Profile* profile,
266     SettingsApiOverrideType type)
267     : ExtensionMessageBubbleController(
268           new SettingsApiBubbleDelegate(
269               ExtensionSystem::Get(profile)->extension_service(),
270               profile,
271               type),
272           profile),
273       profile_(profile),
274       type_(type) {}
275
276 SettingsApiBubbleController::~SettingsApiBubbleController() {}
277
278 bool SettingsApiBubbleController::ShouldShow(const std::string& extension_id) {
279   extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
280   if (prefs->HasSettingsApiBubbleBeenAcknowledged(extension_id))
281     return false;
282
283   if (!delegate()->ShouldIncludeExtension(extension_id))
284     return false;
285
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;
290
291   return true;
292 }
293
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;
298 }
299
300 }  // namespace extensions