| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 5 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 25 #include "ui/views/window/dialog_delegate.h" | 25 #include "ui/views/window/dialog_delegate.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const int kRightColumnWidth = 210; | 29 const int kRightColumnWidth = 210; |
| 30 const int kIconSize = 64; | 30 const int kIconSize = 64; |
| 31 | 31 |
| 32 class ExtensionUninstallDialogDelegateView; | 32 class ExtensionUninstallDialogDelegateView; |
| 33 | 33 |
| 34 // Returns parent window for extension uninstall dialog. | |
| 35 gfx::NativeWindow GetParent(Browser* browser) { | |
| 36 if (browser && browser->window()) | |
| 37 return browser->window()->GetNativeWindow(); | |
| 38 return NULL; | |
| 39 } | |
| 40 | |
| 41 // Views implementation of the uninstall dialog. | 34 // Views implementation of the uninstall dialog. |
| 42 class ExtensionUninstallDialogViews | 35 class ExtensionUninstallDialogViews |
| 43 : public extensions::ExtensionUninstallDialog { | 36 : public extensions::ExtensionUninstallDialog { |
| 44 public: | 37 public: |
| 45 ExtensionUninstallDialogViews( | 38 ExtensionUninstallDialogViews( |
| 46 Profile* profile, | 39 Profile* profile, |
| 47 Browser* browser, | 40 gfx::NativeWindow parent, |
| 48 extensions::ExtensionUninstallDialog::Delegate* delegate); | 41 extensions::ExtensionUninstallDialog::Delegate* delegate); |
| 49 virtual ~ExtensionUninstallDialogViews(); | 42 virtual ~ExtensionUninstallDialogViews(); |
| 50 | 43 |
| 51 // Forwards the accept and cancels to the delegate. | 44 // Forwards the accept and cancels to the delegate. |
| 52 void ExtensionUninstallAccepted(); | 45 void ExtensionUninstallAccepted(); |
| 53 void ExtensionUninstallCanceled(); | 46 void ExtensionUninstallCanceled(); |
| 54 | 47 |
| 55 ExtensionUninstallDialogDelegateView* view() { return view_; } | 48 ExtensionUninstallDialogDelegateView* view() { return view_; } |
| 56 | 49 |
| 57 private: | 50 private: |
| 58 virtual void Show() OVERRIDE; | 51 virtual void Show() OVERRIDE; |
| 52 virtual void RefreshIcon() OVERRIDE; |
| 59 | 53 |
| 60 ExtensionUninstallDialogDelegateView* view_; | 54 ExtensionUninstallDialogDelegateView* view_; |
| 61 bool show_in_app_list_; | 55 bool show_in_app_list_; |
| 62 | 56 |
| 63 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogViews); | 57 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogViews); |
| 64 }; | 58 }; |
| 65 | 59 |
| 66 // The dialog's view, owned by the views framework. | 60 // The dialog's view, owned by the views framework. |
| 67 class ExtensionUninstallDialogDelegateView : public views::DialogDelegateView { | 61 class ExtensionUninstallDialogDelegateView : public views::DialogDelegateView { |
| 68 public: | 62 public: |
| 69 ExtensionUninstallDialogDelegateView( | 63 ExtensionUninstallDialogDelegateView( |
| 70 ExtensionUninstallDialogViews* dialog_view, | 64 ExtensionUninstallDialogViews* dialog_view, |
| 71 const extensions::Extension* extension, | 65 const extensions::Extension* extension, |
| 72 const extensions::Extension* triggering_extension, | 66 const extensions::Extension* triggering_extension, |
| 73 gfx::ImageSkia* icon); | 67 gfx::ImageSkia* icon); |
| 74 virtual ~ExtensionUninstallDialogDelegateView(); | 68 virtual ~ExtensionUninstallDialogDelegateView(); |
| 75 | 69 |
| 70 void SetIcon(gfx::ImageSkia* icon); |
| 71 |
| 76 // Called when the ExtensionUninstallDialog has been destroyed to make sure | 72 // Called when the ExtensionUninstallDialog has been destroyed to make sure |
| 77 // we invalidate pointers. | 73 // we invalidate pointers. |
| 78 void DialogDestroyed() { dialog_ = NULL; } | 74 void DialogDestroyed() { dialog_ = NULL; } |
| 79 | 75 |
| 80 private: | 76 private: |
| 77 void SetIconInternal(gfx::ImageSkia* icon); |
| 78 |
| 81 // views::DialogDelegate: | 79 // views::DialogDelegate: |
| 82 virtual base::string16 GetDialogButtonLabel( | 80 virtual base::string16 GetDialogButtonLabel( |
| 83 ui::DialogButton button) const OVERRIDE; | 81 ui::DialogButton button) const OVERRIDE; |
| 84 virtual int GetDefaultDialogButton() const OVERRIDE { | 82 virtual int GetDefaultDialogButton() const OVERRIDE { |
| 85 // Default to accept when triggered via chrome://extensions page. | 83 // Default to accept when triggered via chrome://extensions page. |
| 86 return triggered_by_extension_ ? | 84 return triggered_by_extension_ ? |
| 87 ui::DIALOG_BUTTON_CANCEL : ui::DIALOG_BUTTON_OK; | 85 ui::DIALOG_BUTTON_CANCEL : ui::DIALOG_BUTTON_OK; |
| 88 } | 86 } |
| 89 virtual bool Accept() OVERRIDE; | 87 virtual bool Accept() OVERRIDE; |
| 90 virtual bool Cancel() OVERRIDE; | 88 virtual bool Cancel() OVERRIDE; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 104 | 102 |
| 105 views::ImageView* icon_; | 103 views::ImageView* icon_; |
| 106 views::Label* heading_; | 104 views::Label* heading_; |
| 107 bool triggered_by_extension_; | 105 bool triggered_by_extension_; |
| 108 | 106 |
| 109 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView); | 107 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView); |
| 110 }; | 108 }; |
| 111 | 109 |
| 112 ExtensionUninstallDialogViews::ExtensionUninstallDialogViews( | 110 ExtensionUninstallDialogViews::ExtensionUninstallDialogViews( |
| 113 Profile* profile, | 111 Profile* profile, |
| 114 Browser* browser, | 112 gfx::NativeWindow parent, |
| 115 extensions::ExtensionUninstallDialog::Delegate* delegate) | 113 extensions::ExtensionUninstallDialog::Delegate* delegate) |
| 116 : extensions::ExtensionUninstallDialog(profile, browser, delegate), | 114 : extensions::ExtensionUninstallDialog(profile, parent, delegate), |
| 117 view_(NULL), | 115 view_(NULL) { |
| 118 show_in_app_list_(!browser) { | |
| 119 } | 116 } |
| 120 | 117 |
| 121 ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() { | 118 ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() { |
| 122 // Close the widget (the views framework will delete view_). | 119 // Close the widget (the views framework will delete view_). |
| 123 if (view_) { | 120 if (view_) { |
| 124 view_->DialogDestroyed(); | 121 view_->DialogDestroyed(); |
| 125 view_->GetWidget()->CloseNow(); | 122 view_->GetWidget()->CloseNow(); |
| 126 } | 123 } |
| 127 } | 124 } |
| 128 | 125 |
| 129 void ExtensionUninstallDialogViews::Show() { | 126 void ExtensionUninstallDialogViews::Show() { |
| 130 // TODO(tapted): A true |desktop_type| needs to be passed in at creation time | |
| 131 // to remove reliance on GetActiveDesktop(). http://crbug.com/308360 | |
| 132 gfx::NativeWindow parent = show_in_app_list_ ? | |
| 133 AppListService::Get(chrome::GetActiveDesktop())->GetAppListWindow() : | |
| 134 GetParent(browser_); | |
| 135 if (browser_ && !parent) { | |
| 136 delegate_->ExtensionUninstallCanceled(); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 view_ = new ExtensionUninstallDialogDelegateView( | 127 view_ = new ExtensionUninstallDialogDelegateView( |
| 141 this, extension_, triggering_extension_, &icon_); | 128 this, extension_, triggering_extension_, &icon_); |
| 142 CreateBrowserModalDialogViews(view_, parent)->Show(); | 129 CreateBrowserModalDialogViews(view_, parent_)->Show(); |
| 130 } |
| 131 |
| 132 void ExtensionUninstallDialogViews::RefreshIcon() { |
| 133 view_->SetIcon(&icon_); |
| 143 } | 134 } |
| 144 | 135 |
| 145 void ExtensionUninstallDialogViews::ExtensionUninstallAccepted() { | 136 void ExtensionUninstallDialogViews::ExtensionUninstallAccepted() { |
| 146 // The widget gets destroyed when the dialog is accepted. | 137 // The widget gets destroyed when the dialog is accepted. |
| 147 view_ = NULL; | 138 view_ = NULL; |
| 148 delegate_->ExtensionUninstallAccepted(); | 139 delegate_->ExtensionUninstallAccepted(); |
| 149 } | 140 } |
| 150 | 141 |
| 151 void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() { | 142 void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() { |
| 152 // The widget gets destroyed when the dialog is canceled. | 143 // The widget gets destroyed when the dialog is canceled. |
| 153 view_ = NULL; | 144 view_ = NULL; |
| 154 delegate_->ExtensionUninstallCanceled(); | 145 delegate_->ExtensionUninstallCanceled(); |
| 155 } | 146 } |
| 156 | 147 |
| 157 ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView( | 148 ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView( |
| 158 ExtensionUninstallDialogViews* dialog_view, | 149 ExtensionUninstallDialogViews* dialog_view, |
| 159 const extensions::Extension* extension, | 150 const extensions::Extension* extension, |
| 160 const extensions::Extension* triggering_extension, | 151 const extensions::Extension* triggering_extension, |
| 161 gfx::ImageSkia* icon) | 152 gfx::ImageSkia* icon) |
| 162 : dialog_(dialog_view), | 153 : dialog_(dialog_view), |
| 163 triggered_by_extension_(triggering_extension != NULL) { | 154 triggered_by_extension_(triggering_extension != NULL) { |
| 164 // Scale down to icon size, but allow smaller icons (don't scale up). | |
| 165 gfx::Size size(icon->width(), icon->height()); | |
| 166 if (size.width() > kIconSize || size.height() > kIconSize) | |
| 167 size = gfx::Size(kIconSize, kIconSize); | |
| 168 icon_ = new views::ImageView(); | 155 icon_ = new views::ImageView(); |
| 169 icon_->SetImageSize(size); | 156 SetIconInternal(icon); |
| 170 icon_->SetImage(*icon); | |
| 171 AddChildView(icon_); | 157 AddChildView(icon_); |
| 172 | 158 |
| 173 heading_ = new views::Label(base::UTF8ToUTF16(dialog_->GetHeadingText())); | 159 heading_ = new views::Label(base::UTF8ToUTF16(dialog_->GetHeadingText())); |
| 174 heading_->SetMultiLine(true); | 160 heading_->SetMultiLine(true); |
| 175 heading_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 161 heading_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 176 AddChildView(heading_); | 162 AddChildView(heading_); |
| 177 } | 163 } |
| 178 | 164 |
| 179 ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() { | 165 ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() { |
| 180 } | 166 } |
| 181 | 167 |
| 168 void ExtensionUninstallDialogDelegateView::SetIcon(gfx::ImageSkia* icon) { |
| 169 SetIconInternal(icon); |
| 170 Layout(); |
| 171 } |
| 172 |
| 173 void ExtensionUninstallDialogDelegateView::SetIconInternal( |
| 174 gfx::ImageSkia* icon) { |
| 175 // Scale down to icon size, but allow smaller icons (don't scale up). |
| 176 gfx::Size size(icon->width(), icon->height()); |
| 177 if (size.width() > kIconSize || size.height() > kIconSize) |
| 178 size = gfx::Size(kIconSize, kIconSize); |
| 179 icon_->SetImageSize(size); |
| 180 icon_->SetImage(*icon); |
| 181 } |
| 182 |
| 182 base::string16 ExtensionUninstallDialogDelegateView::GetDialogButtonLabel( | 183 base::string16 ExtensionUninstallDialogDelegateView::GetDialogButtonLabel( |
| 183 ui::DialogButton button) const { | 184 ui::DialogButton button) const { |
| 184 return l10n_util::GetStringUTF16((button == ui::DIALOG_BUTTON_OK) ? | 185 return l10n_util::GetStringUTF16((button == ui::DIALOG_BUTTON_OK) ? |
| 185 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON : IDS_CANCEL); | 186 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON : IDS_CANCEL); |
| 186 } | 187 } |
| 187 | 188 |
| 188 bool ExtensionUninstallDialogDelegateView::Accept() { | 189 bool ExtensionUninstallDialogDelegateView::Accept() { |
| 189 if (dialog_) | 190 if (dialog_) |
| 190 dialog_->ExtensionUninstallAccepted(); | 191 dialog_->ExtensionUninstallAccepted(); |
| 191 return true; | 192 return true; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 heading_->SetX(x); | 239 heading_->SetX(x); |
| 239 heading_->SetY(y); | 240 heading_->SetY(y); |
| 240 } | 241 } |
| 241 } | 242 } |
| 242 | 243 |
| 243 } // namespace | 244 } // namespace |
| 244 | 245 |
| 245 // static | 246 // static |
| 246 extensions::ExtensionUninstallDialog* | 247 extensions::ExtensionUninstallDialog* |
| 247 extensions::ExtensionUninstallDialog::Create(Profile* profile, | 248 extensions::ExtensionUninstallDialog::Create(Profile* profile, |
| 248 Browser* browser, | 249 gfx::NativeWindow parent, |
| 249 Delegate* delegate) { | 250 Delegate* delegate) { |
| 250 return new ExtensionUninstallDialogViews(profile, browser, delegate); | 251 return new ExtensionUninstallDialogViews(profile, parent, delegate); |
| 251 } | 252 } |
| OLD | NEW |