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/grit/generated_resources.h"
6 #include "components/constrained_window/constrained_window_views.h"
7 #include "components/pdf/browser/pdf_web_contents_helper_client.h"
8 #include "content/public/browser/web_contents.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/views/controls/message_box_view.h"
11 #include "ui/views/controls/textfield/textfield.h"
12 #include "ui/views/layout/layout_constants.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_delegate.h"
18 // Runs a tab-modal dialog that asks the user for a password.
19 class PDFPasswordDialogViews
: public views::DialogDelegate
{
21 PDFPasswordDialogViews(content::WebContents
* web_contents
,
22 const base::string16
& prompt
,
23 const pdf::PasswordDialogClosedCallback
& callback
);
24 ~PDFPasswordDialogViews() override
;
26 // views::DialogDelegate:
27 base::string16
GetWindowTitle() const override
;
28 base::string16
GetDialogButtonLabel(ui::DialogButton button
) const override
;
29 bool Cancel() override
;
30 bool Accept() override
;
32 // views::WidgetDelegate:
33 views::View
* GetInitiallyFocusedView() override
;
34 views::View
* GetContentsView() override
;
35 views::Widget
* GetWidget() override
;
36 const views::Widget
* GetWidget() const override
;
37 void DeleteDelegate() override
;
38 ui::ModalType
GetModalType() const override
;
41 // The message box view whose commands we handle.
42 views::MessageBoxView
* message_box_view_
;
44 pdf::PasswordDialogClosedCallback callback_
;
46 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews
);
49 PDFPasswordDialogViews::PDFPasswordDialogViews(
50 content::WebContents
* web_contents
,
51 const base::string16
& prompt
,
52 const pdf::PasswordDialogClosedCallback
& callback
)
53 : message_box_view_(NULL
), callback_(callback
) {
54 views::MessageBoxView::InitParams
init_params(prompt
);
55 init_params
.options
= views::MessageBoxView::HAS_PROMPT_FIELD
;
56 init_params
.inter_row_vertical_spacing
=
57 views::kUnrelatedControlVerticalSpacing
;
58 message_box_view_
= new views::MessageBoxView(init_params
);
59 message_box_view_
->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
60 constrained_window::ShowWebModalDialogViews(this, web_contents
);
63 PDFPasswordDialogViews::~PDFPasswordDialogViews() {
64 if (!callback_
.is_null()) {
65 // This dialog was torn down without either OK or cancel being clicked; be
66 // considerate and at least do the callback.
67 callback_
.Run(false, base::string16());
71 //////////////////////////////////////////////////////////////////////////////
72 // PDFPasswordDialogViews, views::DialogDelegate implementation:
74 base::string16
PDFPasswordDialogViews::GetWindowTitle() const {
75 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE
);
78 base::string16
PDFPasswordDialogViews::GetDialogButtonLabel(
79 ui::DialogButton button
) const {
80 if (button
== ui::DIALOG_BUTTON_OK
)
81 return l10n_util::GetStringUTF16(IDS_OK
);
82 if (button
== ui::DIALOG_BUTTON_CANCEL
)
83 return l10n_util::GetStringUTF16(IDS_CANCEL
);
84 return base::string16();
87 bool PDFPasswordDialogViews::Cancel() {
88 callback_
.Run(false, base::string16());
93 bool PDFPasswordDialogViews::Accept() {
94 callback_
.Run(true, message_box_view_
->text_box()->text());
99 ///////////////////////////////////////////////////////////////////////////////
100 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
102 views::View
* PDFPasswordDialogViews::GetInitiallyFocusedView() {
103 return message_box_view_
->text_box();
106 views::View
* PDFPasswordDialogViews::GetContentsView() {
107 return message_box_view_
;
110 views::Widget
* PDFPasswordDialogViews::GetWidget() {
111 return message_box_view_
->GetWidget();
114 const views::Widget
* PDFPasswordDialogViews::GetWidget() const {
115 return message_box_view_
->GetWidget();
118 void PDFPasswordDialogViews::DeleteDelegate() {
122 ui::ModalType
PDFPasswordDialogViews::GetModalType() const {
123 return ui::MODAL_TYPE_CHILD
;
128 void ShowPDFPasswordDialog(content::WebContents
* web_contents
,
129 const base::string16
& prompt
,
130 const pdf::PasswordDialogClosedCallback
& callback
) {
131 new PDFPasswordDialogViews(web_contents
, prompt
, callback
);