[email protected] | e3c0bc2 | 2012-02-24 01:34:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 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/extension_uninstall_dialog.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/message_loop.h" |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 9 | #include "chrome/browser/extensions/image_loader.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7f2d7d3 | 2012-06-25 21:47:31 | [diff] [blame] | 11 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 12 | #include "chrome/common/extensions/extension.h" |
[email protected] | faf8719 | 2012-08-17 00:07:59 | [diff] [blame] | 13 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 14 | #include "chrome/common/extensions/extension_icon_set.h" |
| 15 | #include "chrome/common/extensions/extension_resource.h" |
[email protected] | 7f2d7d3 | 2012-06-25 21:47:31 | [diff] [blame] | 16 | #include "content/public/browser/notification_service.h" |
| 17 | #include "content/public/browser/notification_source.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 18 | #include "grit/generated_resources.h" |
[email protected] | 2a281335 | 2012-07-11 22:20:23 | [diff] [blame] | 19 | #include "grit/theme_resources.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 20 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 21 | #include "ui/gfx/image/image.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 22 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Returns pixel size under maximal scale factor for the icon whose device |
| 26 | // independent size is |size_in_dip| |
| 27 | int GetSizeForMaxScaleFactor(int size_in_dip) { |
[email protected] | 059d76b | 2012-11-16 17:25:42 | [diff] [blame] | 28 | ui::ScaleFactor max_scale_factor = ui::GetMaxScaleFactor(); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 29 | float max_scale_factor_scale = ui::GetScaleFactorScale(max_scale_factor); |
| 30 | |
| 31 | return static_cast<int>(size_in_dip * max_scale_factor_scale); |
| 32 | } |
| 33 | |
| 34 | // Returns bitmap for the default icon with size equal to the default icon's |
| 35 | // pixel size under maximal supported scale factor. |
| 36 | SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 37 | return extensions::Extension::GetDefaultIcon(is_app). |
[email protected] | 059d76b | 2012-11-16 17:25:42 | [diff] [blame] | 38 | GetRepresentation(ui::GetMaxScaleFactor()).sk_bitmap(); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 43 | // Size of extension icon in top left of dialog. |
| 44 | static const int kIconSize = 69; |
| 45 | |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 46 | ExtensionUninstallDialog::ExtensionUninstallDialog( |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 47 | Browser* browser, |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 48 | ExtensionUninstallDialog::Delegate* delegate) |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 49 | : browser_(browser), |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 50 | delegate_(delegate), |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 51 | extension_(NULL), |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 52 | state_(kImageIsLoading), |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 53 | ui_loop_(MessageLoop::current()) { |
[email protected] | 7f2d7d3 | 2012-06-25 21:47:31 | [diff] [blame] | 54 | if (browser) { |
| 55 | registrar_.Add(this, |
| 56 | chrome::NOTIFICATION_BROWSER_CLOSING, |
| 57 | content::Source<Browser>(browser)); |
| 58 | } |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 59 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 60 | |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 61 | ExtensionUninstallDialog::~ExtensionUninstallDialog() { |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 62 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 63 | |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 64 | void ExtensionUninstallDialog::ConfirmUninstall( |
| 65 | const extensions::Extension* extension) { |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 66 | DCHECK(ui_loop_ == MessageLoop::current()); |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 67 | extension_ = extension; |
| 68 | |
| 69 | ExtensionResource image = |
[email protected] | faf8719 | 2012-08-17 00:07:59 | [diff] [blame] | 70 | extension_->GetIconResource(extension_misc::EXTENSION_ICON_LARGE, |
[email protected] | e3c0bc2 | 2012-02-24 01:34:15 | [diff] [blame] | 71 | ExtensionIconSet::MATCH_BIGGER); |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 72 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 73 | // Load the icon whose pixel size is large enough to be displayed under |
| 74 | // maximal supported scale factor. UI code will scale the icon down if needed. |
| 75 | int pixel_size = GetSizeForMaxScaleFactor(kIconSize); |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 76 | |
| 77 | // Load the image asynchronously. The response will be sent to OnImageLoaded. |
| 78 | state_ = kImageIsLoading; |
| 79 | extensions::ImageLoader* loader = |
| 80 | extensions::ImageLoader::Get(browser_->profile()); |
| 81 | loader->LoadImageAsync(extension_, image, |
| 82 | gfx::Size(pixel_size, pixel_size), |
| 83 | base::Bind(&ExtensionUninstallDialog::OnImageLoaded, |
| 84 | AsWeakPtr())); |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 87 | void ExtensionUninstallDialog::SetIcon(const gfx::Image& image) { |
| 88 | if (image.IsEmpty()) { |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 89 | // Let's set default icon bitmap whose size is equal to the default icon's |
| 90 | // pixel size under maximal supported scale factor. If the bitmap is larger |
| 91 | // than the one we need, it will be scaled down by the ui code. |
| 92 | // TODO(tbarzic): We should use IconImage here and load the required bitmap |
| 93 | // lazily. |
[email protected] | 1bc3ef0 | 2012-10-24 16:49:59 | [diff] [blame] | 94 | icon_ = gfx::ImageSkia( |
| 95 | GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app())); |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 96 | } else { |
[email protected] | 6280b588 | 2012-06-05 21:56:41 | [diff] [blame] | 97 | icon_ = *image.ToImageSkia(); |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 101 | void ExtensionUninstallDialog::OnImageLoaded(const gfx::Image& image) { |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 102 | SetIcon(image); |
| 103 | |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 104 | // Show the dialog unless the browser has been closed while we were waiting |
| 105 | // for the image. |
| 106 | DCHECK(state_ == kImageIsLoading || state_ == kBrowserIsClosing); |
| 107 | if (state_ == kImageIsLoading) { |
| 108 | state_ = kDialogIsShowing; |
| 109 | DCHECK(browser_ != NULL); |
| 110 | Show(); |
| 111 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 112 | } |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 113 | |
[email protected] | 7f2d7d3 | 2012-06-25 21:47:31 | [diff] [blame] | 114 | void ExtensionUninstallDialog::Observe( |
| 115 | int type, |
| 116 | const content::NotificationSource& source, |
| 117 | const content::NotificationDetails& details) { |
| 118 | DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSING); |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 119 | |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame^] | 120 | // If the browser is closed while waiting for the image, we need to send a |
| 121 | // "cancel" event here, because there will not be another opportunity to |
| 122 | // notify the delegate of the cancellation as we won't open the dialog. |
| 123 | if (state_ == kImageIsLoading) { |
| 124 | state_ = kBrowserIsClosing; |
| 125 | DCHECK(browser_ != NULL); |
| 126 | browser_ = NULL; |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 127 | delegate_->ExtensionUninstallCanceled(); |
| 128 | } |
| 129 | } |