Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / download / download_in_progress_dialog_view.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/download/download_in_progress_dialog_view.h"
6
7 #include <algorithm>
8
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/ui/views/constrained_window_views.h"
11 #include "chrome/grit/chromium_strings.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "chrome/grit/locale_settings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/gfx/size.h"
16 #include "ui/views/border.h"
17 #include "ui/views/controls/message_box_view.h"
18 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/widget/widget.h"
20
21 // static
22 void DownloadInProgressDialogView::Show(
23     gfx::NativeWindow parent,
24     int download_count,
25     Browser::DownloadClosePreventionType dialog_type,
26     bool app_modal,
27     const base::Callback<void(bool)>& callback) {
28   DownloadInProgressDialogView* window = new DownloadInProgressDialogView(
29       download_count, dialog_type, app_modal, callback);
30   CreateBrowserModalDialogViews(window, parent)->Show();
31 }
32
33 DownloadInProgressDialogView::DownloadInProgressDialogView(
34     int download_count,
35     Browser::DownloadClosePreventionType dialog_type,
36     bool app_modal,
37     const base::Callback<void(bool)>& callback)
38     : app_modal_(app_modal),
39       callback_(callback),
40       message_box_view_(NULL) {
41   base::string16 explanation_text;
42   switch (dialog_type) {
43     case Browser::DOWNLOAD_CLOSE_BROWSER_SHUTDOWN:
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       break;
58     case Browser::DOWNLOAD_CLOSE_LAST_WINDOW_IN_INCOGNITO_PROFILE:
59       if (download_count == 1) {
60         title_text_ = l10n_util::GetStringUTF16(
61             IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_TITLE);
62         explanation_text = l10n_util::GetStringUTF16(
63             IDS_SINGLE_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_EXPLANATION);
64       } else {
65         title_text_ = l10n_util::GetStringUTF16(
66             IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_TITLE);
67         explanation_text = l10n_util::GetStringUTF16(
68             IDS_MULTIPLE_INCOGNITO_DOWNLOADS_REMOVE_CONFIRM_EXPLANATION);
69       }
70       ok_button_text_ = l10n_util::GetStringUTF16(
71           IDS_INCOGNITO_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
72       break;
73     default:
74       // This dialog should have been created within the same thread invocation
75       // as the original test that lead to us, so it should always not be ok
76       // to close.
77       NOTREACHED();
78   }
79   cancel_button_text_ = l10n_util::GetStringUTF16(
80       IDS_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
81
82   message_box_view_ = new views::MessageBoxView(
83       views::MessageBoxView::InitParams(explanation_text));
84 }
85
86 DownloadInProgressDialogView::~DownloadInProgressDialogView() {}
87
88 int DownloadInProgressDialogView::GetDefaultDialogButton() const {
89   return ui::DIALOG_BUTTON_CANCEL;
90 }
91
92 base::string16 DownloadInProgressDialogView::GetDialogButtonLabel(
93     ui::DialogButton button) const {
94   return (button == ui::DIALOG_BUTTON_OK) ?
95       ok_button_text_ : cancel_button_text_;
96 }
97
98 bool DownloadInProgressDialogView::Cancel() {
99   callback_.Run(false);
100   return true;
101 }
102
103 bool DownloadInProgressDialogView::Accept() {
104   callback_.Run(true);
105   return true;
106 }
107
108 ui::ModalType DownloadInProgressDialogView::GetModalType() const {
109   return app_modal_ ? ui::MODAL_TYPE_SYSTEM : ui::MODAL_TYPE_WINDOW;
110 }
111
112 base::string16 DownloadInProgressDialogView::GetWindowTitle() const {
113   return title_text_;
114 }
115
116 void DownloadInProgressDialogView::DeleteDelegate() {
117   delete this;
118 }
119
120 views::Widget* DownloadInProgressDialogView::GetWidget() {
121   return message_box_view_->GetWidget();
122 }
123
124 const views::Widget* DownloadInProgressDialogView::GetWidget() const {
125   return message_box_view_->GetWidget();
126 }
127
128 views::View* DownloadInProgressDialogView::GetContentsView() {
129   return message_box_view_;
130 }