1 // Copyright 2013 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/ui/pdf/pdf_tab_helper.h"
7 #include "chrome/browser/ui/views/constrained_window_views.h"
8 #include "components/web_modal/web_contents_modal_dialog_host.h"
9 #include "components/web_modal/web_contents_modal_dialog_manager.h"
10 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
11 #include "content/public/browser/web_contents.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/views/controls/message_box_view.h"
15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/layout/layout_constants.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_delegate.h"
22 // PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a
24 class PDFPasswordDialogViews
: public views::DialogDelegate
{
26 PDFPasswordDialogViews(content::WebContents
* web_contents
,
27 const base::string16
& prompt
,
28 const PasswordDialogClosedCallback
& callback
);
29 virtual ~PDFPasswordDialogViews();
31 // views::DialogDelegate:
32 virtual base::string16
GetWindowTitle() const OVERRIDE
;
33 virtual base::string16
GetDialogButtonLabel(
34 ui::DialogButton button
) const OVERRIDE
;
35 virtual bool Cancel() OVERRIDE
;
36 virtual bool Accept() OVERRIDE
;
38 // views::WidgetDelegate:
39 virtual views::View
* GetInitiallyFocusedView() OVERRIDE
;
40 virtual views::View
* GetContentsView() OVERRIDE
;
41 virtual views::NonClientFrameView
* CreateNonClientFrameView(
42 views::Widget
* widget
) OVERRIDE
;
43 virtual views::Widget
* GetWidget() OVERRIDE
;
44 virtual const views::Widget
* GetWidget() const OVERRIDE
;
45 virtual void DeleteDelegate() OVERRIDE
;
46 virtual ui::ModalType
GetModalType() const OVERRIDE
;
49 // The message box view whose commands we handle.
50 views::MessageBoxView
* message_box_view_
;
52 views::Widget
* dialog_
;
53 content::BrowserContext
* browser_context_
;
55 PasswordDialogClosedCallback callback_
;
57 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews
);
60 PDFPasswordDialogViews::PDFPasswordDialogViews(
61 content::WebContents
* web_contents
,
62 const base::string16
& prompt
,
63 const PasswordDialogClosedCallback
& callback
)
64 : message_box_view_(NULL
),
66 browser_context_(web_contents
->GetBrowserContext()),
68 views::MessageBoxView::InitParams
init_params(prompt
);
69 init_params
.options
= views::MessageBoxView::HAS_PROMPT_FIELD
;
70 init_params
.inter_row_vertical_spacing
=
71 views::kUnrelatedControlVerticalSpacing
;
72 message_box_view_
= new views::MessageBoxView(init_params
);
73 message_box_view_
->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
75 web_modal::WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
76 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents
);
77 web_modal::WebContentsModalDialogManagerDelegate
* modal_delegate
=
78 web_contents_modal_dialog_manager
->delegate();
79 DCHECK(modal_delegate
);
80 dialog_
= views::Widget::CreateWindowAsFramelessChild(
81 this, modal_delegate
->GetWebContentsModalDialogHost()->GetHostView());
82 web_contents_modal_dialog_manager
->ShowDialog(dialog_
->GetNativeView());
85 PDFPasswordDialogViews::~PDFPasswordDialogViews() {
86 if (!callback_
.is_null()) {
87 // This dialog was torn down without either OK or cancel being clicked; be
88 // considerate and at least do the callback.
89 callback_
.Run(false, base::string16());
93 //////////////////////////////////////////////////////////////////////////////
94 // PDFPasswordDialogViews, views::DialogDelegate implementation:
96 base::string16
PDFPasswordDialogViews::GetWindowTitle() const {
97 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE
);
100 base::string16
PDFPasswordDialogViews::GetDialogButtonLabel(
101 ui::DialogButton button
) const {
102 if (button
== ui::DIALOG_BUTTON_OK
)
103 return l10n_util::GetStringUTF16(IDS_OK
);
104 if (button
== ui::DIALOG_BUTTON_CANCEL
)
105 return l10n_util::GetStringUTF16(IDS_CANCEL
);
106 return base::string16();
109 bool PDFPasswordDialogViews::Cancel() {
110 callback_
.Run(false, base::string16());
115 bool PDFPasswordDialogViews::Accept() {
116 callback_
.Run(true, message_box_view_
->text_box()->text());
121 ///////////////////////////////////////////////////////////////////////////////
122 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
124 views::View
* PDFPasswordDialogViews::GetInitiallyFocusedView() {
125 return message_box_view_
->text_box();
128 views::View
* PDFPasswordDialogViews::GetContentsView() {
129 return message_box_view_
;
132 // TODO(wittman): Remove this override once we move to the new style frame view
134 views::NonClientFrameView
* PDFPasswordDialogViews::CreateNonClientFrameView(
135 views::Widget
* widget
) {
136 return CreateConstrainedStyleNonClientFrameView(widget
, browser_context_
);
139 views::Widget
* PDFPasswordDialogViews::GetWidget() {
140 return message_box_view_
->GetWidget();
143 const views::Widget
* PDFPasswordDialogViews::GetWidget() const {
144 return message_box_view_
->GetWidget();
147 void PDFPasswordDialogViews::DeleteDelegate() {
151 ui::ModalType
PDFPasswordDialogViews::GetModalType() const {
153 return ui::MODAL_TYPE_CHILD
;
155 return views::WidgetDelegate::GetModalType();
161 void ShowPDFPasswordDialog(content::WebContents
* web_contents
,
162 const base::string16
& prompt
,
163 const PasswordDialogClosedCallback
& callback
) {
164 new PDFPasswordDialogViews(web_contents
, prompt
, callback
);