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/views/constrained_window_views.h"
6 #include "chrome/grit/generated_resources.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 virtual ~PDFPasswordDialogViews();
26 // views::DialogDelegate:
27 virtual base::string16
GetWindowTitle() const override
;
28 virtual base::string16
GetDialogButtonLabel(
29 ui::DialogButton button
) const override
;
30 virtual bool Cancel() override
;
31 virtual bool Accept() override
;
33 // views::WidgetDelegate:
34 virtual views::View
* GetInitiallyFocusedView() override
;
35 virtual views::View
* GetContentsView() override
;
36 virtual views::Widget
* GetWidget() override
;
37 virtual const views::Widget
* GetWidget() const override
;
38 virtual void DeleteDelegate() override
;
39 virtual ui::ModalType
GetModalType() const override
;
42 // The message box view whose commands we handle.
43 views::MessageBoxView
* message_box_view_
;
45 pdf::PasswordDialogClosedCallback callback_
;
47 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews
);
50 PDFPasswordDialogViews::PDFPasswordDialogViews(
51 content::WebContents
* web_contents
,
52 const base::string16
& prompt
,
53 const pdf::PasswordDialogClosedCallback
& callback
)
54 : message_box_view_(NULL
), callback_(callback
) {
55 views::MessageBoxView::InitParams
init_params(prompt
);
56 init_params
.options
= views::MessageBoxView::HAS_PROMPT_FIELD
;
57 init_params
.inter_row_vertical_spacing
=
58 views::kUnrelatedControlVerticalSpacing
;
59 message_box_view_
= new views::MessageBoxView(init_params
);
60 message_box_view_
->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
61 ShowWebModalDialogViews(this, web_contents
);
64 PDFPasswordDialogViews::~PDFPasswordDialogViews() {
65 if (!callback_
.is_null()) {
66 // This dialog was torn down without either OK or cancel being clicked; be
67 // considerate and at least do the callback.
68 callback_
.Run(false, base::string16());
72 //////////////////////////////////////////////////////////////////////////////
73 // PDFPasswordDialogViews, views::DialogDelegate implementation:
75 base::string16
PDFPasswordDialogViews::GetWindowTitle() const {
76 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE
);
79 base::string16
PDFPasswordDialogViews::GetDialogButtonLabel(
80 ui::DialogButton button
) const {
81 if (button
== ui::DIALOG_BUTTON_OK
)
82 return l10n_util::GetStringUTF16(IDS_OK
);
83 if (button
== ui::DIALOG_BUTTON_CANCEL
)
84 return l10n_util::GetStringUTF16(IDS_CANCEL
);
85 return base::string16();
88 bool PDFPasswordDialogViews::Cancel() {
89 callback_
.Run(false, base::string16());
94 bool PDFPasswordDialogViews::Accept() {
95 callback_
.Run(true, message_box_view_
->text_box()->text());
100 ///////////////////////////////////////////////////////////////////////////////
101 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
103 views::View
* PDFPasswordDialogViews::GetInitiallyFocusedView() {
104 return message_box_view_
->text_box();
107 views::View
* PDFPasswordDialogViews::GetContentsView() {
108 return message_box_view_
;
111 views::Widget
* PDFPasswordDialogViews::GetWidget() {
112 return message_box_view_
->GetWidget();
115 const views::Widget
* PDFPasswordDialogViews::GetWidget() const {
116 return message_box_view_
->GetWidget();
119 void PDFPasswordDialogViews::DeleteDelegate() {
123 ui::ModalType
PDFPasswordDialogViews::GetModalType() const {
124 return ui::MODAL_TYPE_CHILD
;
129 void ShowPDFPasswordDialog(content::WebContents
* web_contents
,
130 const base::string16
& prompt
,
131 const pdf::PasswordDialogClosedCallback
& callback
) {
132 new PDFPasswordDialogViews(web_contents
, prompt
, callback
);