Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
benrg
2012/04/17 01:32:44
I'm still pretty clueless about Chrome memory mana
James Cook
2012/04/17 19:03:21
I think this is OK. The semantics of memory clean
| |
| 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/ui/views/download/download_in_progress_dialog_view.h" | 5 #include "chrome/browser/ui/views/download/download_in_progress_dialog_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "grit/chromium_strings.h" | 11 #include "grit/chromium_strings.h" |
| 12 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 13 #include "grit/locale_settings.h" | 13 #include "grit/locale_settings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 17 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
| 18 #include "ui/views/controls/label.h" | 18 #include "ui/views/controls/message_box_view.h" |
| 19 #include "ui/views/layout/grid_layout.h" | 19 #include "ui/views/layout/grid_layout.h" |
| 20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 void DownloadInProgressDialogView::Show(Browser* browser, | 23 void DownloadInProgressDialogView::Show(Browser* browser, |
| 24 gfx::NativeWindow parent_window) { | 24 gfx::NativeWindow parent_window) { |
| 25 DownloadInProgressDialogView* window = | 25 DownloadInProgressDialogView* window = |
| 26 new DownloadInProgressDialogView(browser); | 26 new DownloadInProgressDialogView(browser); |
| 27 views::Widget::CreateWindowWithParent(window, parent_window)->Show(); | 27 views::Widget::CreateWindowWithParent(window, parent_window)->Show(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 DownloadInProgressDialogView::DownloadInProgressDialogView(Browser* browser) | 30 DownloadInProgressDialogView::DownloadInProgressDialogView(Browser* browser) |
| 31 : browser_(browser), | 31 : browser_(browser), |
| 32 warning_(NULL), | 32 message_box_view_(NULL) { |
| 33 explanation_(NULL) { | |
| 34 int download_count; | 33 int download_count; |
| 35 Browser::DownloadClosePreventionType type = | 34 Browser::DownloadClosePreventionType type = |
| 36 browser_->OkToCloseWithInProgressDownloads(&download_count); | 35 browser_->OkToCloseWithInProgressDownloads(&download_count); |
| 37 | 36 |
| 38 // This dialog should have been created within the same thread invocation | 37 // This dialog should have been created within the same thread invocation |
| 39 // as the original test that lead to us, so it should always not be ok | 38 // as the original test that lead to us, so it should always not be ok |
| 40 // to close. | 39 // to close. |
| 41 DCHECK_NE(Browser::DOWNLOAD_CLOSE_OK, type); | 40 DCHECK_NE(Browser::DOWNLOAD_CLOSE_OK, type); |
| 42 | 41 |
| 43 // TODO(rdsmith): This dialog should be different depending on whether we're | 42 string16 explanation_text; |
| 44 // closing the last incognito window of a profile or doing browser shutdown. | 43 if (type == Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN) { |
|
James Cook
2012/04/17 19:03:21
Maybe a switch statement on the type with a NOTREA
benrg
2012/04/17 20:39:14
Done.
| |
| 45 // See http://crbug.com/88421. | 44 if (download_count == 1) { |
| 45 title_text_ = l10n_util::GetStringUTF16( | |
| 46 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_TITLE); | |
| 47 explanation_text = l10n_util::GetStringUTF16( | |
| 48 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION); | |
| 49 } else { | |
| 50 title_text_ = l10n_util::GetStringUTF16( | |
| 51 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_TITLE); | |
| 52 explanation_text = l10n_util::GetStringUTF16( | |
| 53 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION); | |
| 54 } | |
| 55 ok_button_text_ = l10n_util::GetStringUTF16( | |
| 56 IDS_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL); | |
| 57 } else { // DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE | |
| 58 if (download_count == 1) { | |
| 59 title_text_ = l10n_util::GetStringUTF16( | |
| 60 IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_TITLE); | |
| 61 explanation_text = l10n_util::GetStringUTF16( | |
| 62 IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION); | |
| 63 } else { | |
| 64 title_text_ = l10n_util::GetStringUTF16( | |
| 65 IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_TITLE); | |
| 66 explanation_text = l10n_util::GetStringUTF16( | |
| 67 IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION); | |
| 68 } | |
| 69 ok_button_text_ = l10n_util::GetStringUTF16( | |
| 70 IDS_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL); | |
| 71 } | |
| 72 cancel_button_text_ = l10n_util::GetStringUTF16( | |
| 73 IDS_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); | |
| 46 | 74 |
| 47 string16 warning_text; | 75 message_box_view_ = new views::MessageBoxView( |
| 48 string16 explanation_text; | 76 views::MessageBoxView::NO_OPTIONS, explanation_text, string16()); |
| 49 if (download_count == 1) { | |
| 50 warning_text = l10n_util::GetStringUTF16( | |
| 51 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_WARNING); | |
| 52 explanation_text = l10n_util::GetStringUTF16( | |
| 53 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION); | |
| 54 ok_button_text_ = l10n_util::GetStringUTF16( | |
| 55 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL); | |
| 56 cancel_button_text_ = l10n_util::GetStringUTF16( | |
| 57 IDS_SINGLE_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); | |
| 58 } else { | |
| 59 warning_text = l10n_util::GetStringFUTF16( | |
| 60 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_WARNING, | |
| 61 base::IntToString16(download_count)); | |
| 62 explanation_text = l10n_util::GetStringUTF16( | |
| 63 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION); | |
| 64 ok_button_text_ = l10n_util::GetStringUTF16( | |
| 65 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_OK_BUTTON_LABEL); | |
| 66 cancel_button_text_ = l10n_util::GetStringUTF16( | |
| 67 IDS_MULTIPLE_DOWNLOADS_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL); | |
| 68 } | |
| 69 | |
| 70 // There are two lines of text: the bold warning label and the text | |
| 71 // explanation label. | |
| 72 views::GridLayout* layout = new views::GridLayout(this); | |
| 73 SetLayoutManager(layout); | |
| 74 const int columnset_id = 0; | |
| 75 views::ColumnSet* column_set = layout->AddColumnSet(columnset_id); | |
| 76 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, 1, | |
| 77 views::GridLayout::USE_PREF, 0, 0); | |
| 78 | |
| 79 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 80 gfx::Font bold_font = rb.GetFont( | |
| 81 ui::ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD); | |
| 82 warning_ = new views::Label(warning_text, bold_font); | |
| 83 warning_->SetMultiLine(true); | |
| 84 warning_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 85 warning_->set_border(views::Border::CreateEmptyBorder(10, 10, 10, 10)); | |
| 86 layout->StartRow(0, columnset_id); | |
| 87 layout->AddView(warning_); | |
| 88 | |
| 89 explanation_ = new views::Label(explanation_text); | |
| 90 explanation_->SetMultiLine(true); | |
| 91 explanation_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 92 explanation_->set_border(views::Border::CreateEmptyBorder(10, 10, 10, 10)); | |
| 93 layout->StartRow(0, columnset_id); | |
| 94 layout->AddView(explanation_); | |
| 95 | |
| 96 dialog_dimensions_ = views::Widget::GetLocalizedContentsSize( | |
| 97 IDS_DOWNLOAD_IN_PROGRESS_WIDTH_CHARS, | |
| 98 IDS_DOWNLOAD_IN_PROGRESS_MINIMUM_HEIGHT_LINES); | |
| 99 const int height = | |
| 100 warning_->GetHeightForWidth(dialog_dimensions_.width()) + | |
| 101 explanation_->GetHeightForWidth(dialog_dimensions_.width()); | |
| 102 dialog_dimensions_.set_height(std::max(height, | |
| 103 dialog_dimensions_.height())); | |
| 104 } | 77 } |
| 105 | 78 |
| 106 DownloadInProgressDialogView::~DownloadInProgressDialogView() {} | 79 DownloadInProgressDialogView::~DownloadInProgressDialogView() {} |
| 107 | 80 |
| 108 gfx::Size DownloadInProgressDialogView::GetPreferredSize() { | 81 int DownloadInProgressDialogView::GetDefaultDialogButton() const { |
| 109 return dialog_dimensions_; | 82 return ui::DIALOG_BUTTON_CANCEL; |
| 110 } | 83 } |
| 111 | 84 |
| 112 string16 DownloadInProgressDialogView::GetDialogButtonLabel( | 85 string16 DownloadInProgressDialogView::GetDialogButtonLabel( |
| 113 ui::DialogButton button) const { | 86 ui::DialogButton button) const { |
| 114 return (button == ui::DIALOG_BUTTON_OK) ? | 87 return (button == ui::DIALOG_BUTTON_OK) ? |
| 115 ok_button_text_ : cancel_button_text_; | 88 ok_button_text_ : cancel_button_text_; |
| 116 } | 89 } |
| 117 | 90 |
| 118 int DownloadInProgressDialogView::GetDefaultDialogButton() const { | |
| 119 return ui::DIALOG_BUTTON_CANCEL; | |
| 120 } | |
| 121 | |
| 122 bool DownloadInProgressDialogView::Cancel() { | 91 bool DownloadInProgressDialogView::Cancel() { |
| 123 browser_->InProgressDownloadResponse(false); | 92 browser_->InProgressDownloadResponse(false); |
| 124 return true; | 93 return true; |
| 125 } | 94 } |
| 126 | 95 |
| 127 bool DownloadInProgressDialogView::Accept() { | 96 bool DownloadInProgressDialogView::Accept() { |
| 128 browser_->InProgressDownloadResponse(true); | 97 browser_->InProgressDownloadResponse(true); |
| 129 return true; | 98 return true; |
| 130 } | 99 } |
| 131 | 100 |
| 132 ui::ModalType DownloadInProgressDialogView::GetModalType() const { | 101 ui::ModalType DownloadInProgressDialogView::GetModalType() const { |
| 133 return ui::MODAL_TYPE_WINDOW; | 102 return ui::MODAL_TYPE_WINDOW; |
| 134 } | 103 } |
| 135 | 104 |
| 136 string16 DownloadInProgressDialogView::GetWindowTitle() const { | 105 string16 DownloadInProgressDialogView::GetWindowTitle() const { |
| 137 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | 106 return title_text_; |
| 107 } | |
| 108 | |
| 109 void DownloadInProgressDialogView::DeleteDelegate() { | |
| 110 delete this; | |
| 111 } | |
| 112 | |
| 113 views::Widget* DownloadInProgressDialogView::GetWidget() { | |
| 114 return message_box_view_->GetWidget(); | |
| 115 } | |
| 116 | |
| 117 const views::Widget* DownloadInProgressDialogView::GetWidget() const { | |
| 118 return message_box_view_->GetWidget(); | |
| 138 } | 119 } |
| 139 | 120 |
| 140 views::View* DownloadInProgressDialogView::GetContentsView() { | 121 views::View* DownloadInProgressDialogView::GetContentsView() { |
| 141 return this; | 122 return message_box_view_; |
| 142 } | 123 } |
| OLD | NEW |