Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / chrome / browser / extensions / external_install_error.cc
blob306fa30a7a160886a70f14a3f01287925f52f743
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/external_install_error.h"
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/external_install_manager.h"
13 #include "chrome/browser/extensions/webstore_data_fetcher.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/global_error/global_error.h"
18 #include "chrome/browser/ui/global_error/global_error_service.h"
19 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/browser/uninstall_reason.h"
25 #include "extensions/common/constants.h"
26 #include "extensions/common/extension.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/gfx/image/image.h"
29 #include "ui/gfx/image/image_skia_operations.h"
31 namespace extensions {
33 namespace {
35 // Return the menu label for a global error.
36 base::string16 GetMenuItemLabel(const Extension* extension) {
37 if (!extension)
38 return base::string16();
40 int id = -1;
41 if (extension->is_app())
42 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP;
43 else if (extension->is_theme())
44 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME;
45 else
46 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION;
48 return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension->name()));
51 // A global error that spawns a dialog when the menu item is clicked.
52 class ExternalInstallMenuAlert : public GlobalError {
53 public:
54 explicit ExternalInstallMenuAlert(ExternalInstallError* error);
55 ~ExternalInstallMenuAlert() override;
57 private:
58 // GlobalError implementation.
59 Severity GetSeverity() override;
60 bool HasMenuItem() override;
61 int MenuItemCommandID() override;
62 base::string16 MenuItemLabel() override;
63 void ExecuteMenuItem(Browser* browser) override;
64 bool HasBubbleView() override;
65 bool HasShownBubbleView() override;
66 void ShowBubbleView(Browser* browser) override;
67 GlobalErrorBubbleViewBase* GetBubbleView() override;
69 // The owning ExternalInstallError.
70 ExternalInstallError* error_;
72 DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert);
75 // A global error that spawns a bubble when the menu item is clicked.
76 class ExternalInstallBubbleAlert : public GlobalErrorWithStandardBubble {
77 public:
78 explicit ExternalInstallBubbleAlert(ExternalInstallError* error,
79 ExtensionInstallPrompt::Prompt* prompt);
80 ~ExternalInstallBubbleAlert() override;
82 private:
83 // GlobalError implementation.
84 Severity GetSeverity() override;
85 bool HasMenuItem() override;
86 int MenuItemCommandID() override;
87 base::string16 MenuItemLabel() override;
88 void ExecuteMenuItem(Browser* browser) override;
90 // GlobalErrorWithStandardBubble implementation.
91 gfx::Image GetBubbleViewIcon() override;
92 base::string16 GetBubbleViewTitle() override;
93 std::vector<base::string16> GetBubbleViewMessages() override;
94 base::string16 GetBubbleViewAcceptButtonLabel() override;
95 base::string16 GetBubbleViewCancelButtonLabel() override;
96 void OnBubbleViewDidClose(Browser* browser) override;
97 void BubbleViewAcceptButtonPressed(Browser* browser) override;
98 void BubbleViewCancelButtonPressed(Browser* browser) override;
100 // The owning ExternalInstallError.
101 ExternalInstallError* error_;
103 // The Prompt with all information, which we then use to populate the bubble.
104 ExtensionInstallPrompt::Prompt* prompt_;
106 DISALLOW_COPY_AND_ASSIGN(ExternalInstallBubbleAlert);
109 ////////////////////////////////////////////////////////////////////////////////
110 // ExternalInstallMenuAlert
112 ExternalInstallMenuAlert::ExternalInstallMenuAlert(ExternalInstallError* error)
113 : error_(error) {
116 ExternalInstallMenuAlert::~ExternalInstallMenuAlert() {
119 GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() {
120 return SEVERITY_LOW;
123 bool ExternalInstallMenuAlert::HasMenuItem() {
124 return true;
127 int ExternalInstallMenuAlert::MenuItemCommandID() {
128 return IDC_EXTERNAL_EXTENSION_ALERT;
131 base::string16 ExternalInstallMenuAlert::MenuItemLabel() {
132 return GetMenuItemLabel(error_->GetExtension());
135 void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) {
136 error_->ShowDialog(browser);
139 bool ExternalInstallMenuAlert::HasBubbleView() {
140 return false;
143 bool ExternalInstallMenuAlert::HasShownBubbleView() {
144 NOTREACHED();
145 return true;
148 void ExternalInstallMenuAlert::ShowBubbleView(Browser* browser) {
149 NOTREACHED();
152 GlobalErrorBubbleViewBase* ExternalInstallMenuAlert::GetBubbleView() {
153 return NULL;
156 ////////////////////////////////////////////////////////////////////////////////
157 // ExternalInstallBubbleAlert
159 ExternalInstallBubbleAlert::ExternalInstallBubbleAlert(
160 ExternalInstallError* error,
161 ExtensionInstallPrompt::Prompt* prompt)
162 : error_(error), prompt_(prompt) {
163 DCHECK(error_);
164 DCHECK(prompt_);
167 ExternalInstallBubbleAlert::~ExternalInstallBubbleAlert() {
170 GlobalError::Severity ExternalInstallBubbleAlert::GetSeverity() {
171 return SEVERITY_LOW;
174 bool ExternalInstallBubbleAlert::HasMenuItem() {
175 return true;
178 int ExternalInstallBubbleAlert::MenuItemCommandID() {
179 return IDC_EXTERNAL_EXTENSION_ALERT;
182 base::string16 ExternalInstallBubbleAlert::MenuItemLabel() {
183 return GetMenuItemLabel(error_->GetExtension());
186 void ExternalInstallBubbleAlert::ExecuteMenuItem(Browser* browser) {
187 ShowBubbleView(browser);
190 gfx::Image ExternalInstallBubbleAlert::GetBubbleViewIcon() {
191 if (prompt_->icon().IsEmpty())
192 return GlobalErrorWithStandardBubble::GetBubbleViewIcon();
193 // Scale icon to a reasonable size.
194 return gfx::Image(gfx::ImageSkiaOperations::CreateResizedImage(
195 *prompt_->icon().ToImageSkia(),
196 skia::ImageOperations::RESIZE_BEST,
197 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
198 extension_misc::EXTENSION_ICON_SMALL)));
201 base::string16 ExternalInstallBubbleAlert::GetBubbleViewTitle() {
202 return prompt_->GetDialogTitle();
205 std::vector<base::string16>
206 ExternalInstallBubbleAlert::GetBubbleViewMessages() {
207 ExtensionInstallPrompt::PermissionsType regular_permissions =
208 ExtensionInstallPrompt::PermissionsType::REGULAR_PERMISSIONS;
209 ExtensionInstallPrompt::PermissionsType withheld_permissions =
210 ExtensionInstallPrompt::PermissionsType::WITHHELD_PERMISSIONS;
212 std::vector<base::string16> messages;
213 if (prompt_->GetPermissionCount(regular_permissions)) {
214 messages.push_back(prompt_->GetPermissionsHeading(regular_permissions));
215 for (size_t i = 0; i < prompt_->GetPermissionCount(regular_permissions);
216 ++i) {
217 messages.push_back(l10n_util::GetStringFUTF16(
218 IDS_EXTENSION_PERMISSION_LINE,
219 prompt_->GetPermission(i, regular_permissions)));
222 if (prompt_->GetPermissionCount(withheld_permissions)) {
223 messages.push_back(prompt_->GetPermissionsHeading(withheld_permissions));
224 for (size_t i = 0; i < prompt_->GetPermissionCount(withheld_permissions);
225 ++i) {
226 messages.push_back(l10n_util::GetStringFUTF16(
227 IDS_EXTENSION_PERMISSION_LINE,
228 prompt_->GetPermission(i, withheld_permissions)));
231 // TODO(yoz): OAuth issue advice?
232 return messages;
235 base::string16 ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() {
236 return prompt_->GetAcceptButtonLabel();
239 base::string16 ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() {
240 return prompt_->GetAbortButtonLabel();
243 void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser* browser) {
246 void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed(
247 Browser* browser) {
248 error_->InstallUIProceed();
251 void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed(
252 Browser* browser) {
253 error_->InstallUIAbort(true);
256 } // namespace
258 ////////////////////////////////////////////////////////////////////////////////
259 // ExternalInstallError
261 ExternalInstallError::ExternalInstallError(
262 content::BrowserContext* browser_context,
263 const std::string& extension_id,
264 AlertType alert_type,
265 ExternalInstallManager* manager)
266 : browser_context_(browser_context),
267 extension_id_(extension_id),
268 alert_type_(alert_type),
269 manager_(manager),
270 error_service_(GlobalErrorServiceFactory::GetForProfile(
271 Profile::FromBrowserContext(browser_context_))),
272 weak_factory_(this) {
273 prompt_ = new ExtensionInstallPrompt::Prompt(
274 ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT);
276 webstore_data_fetcher_.reset(new WebstoreDataFetcher(
277 this, browser_context_->GetRequestContext(), GURL(), extension_id_));
278 webstore_data_fetcher_->Start();
281 ExternalInstallError::~ExternalInstallError() {
282 if (global_error_.get())
283 error_service_->RemoveGlobalError(global_error_.get());
286 void ExternalInstallError::InstallUIProceed() {
287 const Extension* extension = GetExtension();
288 if (extension) {
289 ExtensionSystem::Get(browser_context_)
290 ->extension_service()
291 ->GrantPermissionsAndEnableExtension(extension);
292 // Since the manager listens for the extension to be loaded, this will
293 // remove the error...
294 } else {
295 // ... Otherwise we have to do it explicitly.
296 manager_->RemoveExternalInstallError();
300 void ExternalInstallError::InstallUIAbort(bool user_initiated) {
301 if (user_initiated && GetExtension()) {
302 ExtensionSystem::Get(browser_context_)
303 ->extension_service()
304 ->UninstallExtension(extension_id_,
305 extensions::UNINSTALL_REASON_INSTALL_CANCELED,
306 base::Bind(&base::DoNothing),
307 NULL); // Ignore error.
308 // Since the manager listens for the extension to be removed, this will
309 // remove the error...
310 } else {
311 // ... Otherwise we have to do it explicitly.
312 manager_->RemoveExternalInstallError();
316 void ExternalInstallError::ShowDialog(Browser* browser) {
317 DCHECK(install_ui_.get());
318 DCHECK(prompt_.get());
319 DCHECK(browser);
320 content::WebContents* web_contents = NULL;
321 web_contents = browser->tab_strip_model()->GetActiveWebContents();
322 install_ui_show_params_.reset(
323 new ExtensionInstallPromptShowParams(web_contents));
324 ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run(
325 install_ui_show_params_.get(), this, prompt_);
328 const Extension* ExternalInstallError::GetExtension() const {
329 return ExtensionRegistry::Get(browser_context_)
330 ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING);
333 void ExternalInstallError::OnWebstoreRequestFailure() {
334 OnFetchComplete();
337 void ExternalInstallError::OnWebstoreResponseParseSuccess(
338 scoped_ptr<base::DictionaryValue> webstore_data) {
339 std::string localized_user_count;
340 double average_rating = 0;
341 int rating_count = 0;
342 if (!webstore_data->GetString(kUsersKey, &localized_user_count) ||
343 !webstore_data->GetDouble(kAverageRatingKey, &average_rating) ||
344 !webstore_data->GetInteger(kRatingCountKey, &rating_count)) {
345 // If we don't get a valid webstore response, short circuit, and continue
346 // to show a prompt without webstore data.
347 OnFetchComplete();
348 return;
351 bool show_user_count = true;
352 webstore_data->GetBoolean(kShowUserCountKey, &show_user_count);
354 prompt_->SetWebstoreData(
355 localized_user_count, show_user_count, average_rating, rating_count);
356 OnFetchComplete();
359 void ExternalInstallError::OnWebstoreResponseParseFailure(
360 const std::string& error) {
361 OnFetchComplete();
364 void ExternalInstallError::OnFetchComplete() {
365 // Create a new ExtensionInstallPrompt. We pass in NULL for the UI
366 // components because we display at a later point, and don't want
367 // to pass ones which may be invalidated.
368 install_ui_.reset(
369 new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_),
370 NULL)); // NULL native window.
372 install_ui_->ConfirmExternalInstall(
373 this,
374 GetExtension(),
375 base::Bind(&ExternalInstallError::OnDialogReady,
376 weak_factory_.GetWeakPtr()),
377 prompt_);
380 void ExternalInstallError::OnDialogReady(
381 ExtensionInstallPromptShowParams* show_params,
382 ExtensionInstallPrompt::Delegate* prompt_delegate,
383 scoped_refptr<ExtensionInstallPrompt::Prompt> prompt) {
384 DCHECK_EQ(this, prompt_delegate);
385 prompt_ = prompt;
387 if (alert_type_ == BUBBLE_ALERT) {
388 global_error_.reset(new ExternalInstallBubbleAlert(this, prompt_.get()));
389 error_service_->AddGlobalError(global_error_.get());
391 Browser* browser =
392 chrome::FindTabbedBrowser(Profile::FromBrowserContext(browser_context_),
393 true,
394 chrome::GetActiveDesktop());
395 if (browser)
396 global_error_->ShowBubbleView(browser);
397 } else {
398 DCHECK(alert_type_ == MENU_ALERT);
399 global_error_.reset(new ExternalInstallMenuAlert(this));
400 error_service_->AddGlobalError(global_error_.get());
404 } // namespace extensions