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 "components/web_modal/web_contents_modal_dialog_host.h"
8 #include "components/web_modal/web_contents_modal_dialog_manager.h"
9 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
10 #include "content/public/browser/web_contents.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/views/controls/message_box_view.h"
14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/layout/layout_constants.h"
16 #include "ui/views/widget/widget.h"
17 #include "ui/views/window/dialog_delegate.h"
21 // PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a
23 class PDFPasswordDialogViews
: public views::DialogDelegate
{
25 PDFPasswordDialogViews(content::WebContents
* web_contents
,
26 const base::string16
& prompt
,
27 const PasswordDialogClosedCallback
& callback
);
28 virtual ~PDFPasswordDialogViews();
30 // views::DialogDelegate:
31 virtual base::string16
GetWindowTitle() const OVERRIDE
;
32 virtual base::string16
GetDialogButtonLabel(
33 ui::DialogButton button
) const OVERRIDE
;
34 virtual bool Cancel() OVERRIDE
;
35 virtual bool Accept() OVERRIDE
;
37 // views::WidgetDelegate:
38 virtual views::View
* GetInitiallyFocusedView() OVERRIDE
;
39 virtual views::View
* GetContentsView() OVERRIDE
;
40 virtual views::Widget
* GetWidget() OVERRIDE
;
41 virtual const views::Widget
* GetWidget() const OVERRIDE
;
42 virtual void DeleteDelegate() OVERRIDE
;
43 virtual ui::ModalType
GetModalType() const OVERRIDE
;
46 // The message box view whose commands we handle.
47 views::MessageBoxView
* message_box_view_
;
49 views::Widget
* dialog_
;
51 PasswordDialogClosedCallback callback_
;
53 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews
);
56 PDFPasswordDialogViews::PDFPasswordDialogViews(
57 content::WebContents
* web_contents
,
58 const base::string16
& prompt
,
59 const PasswordDialogClosedCallback
& callback
)
60 : message_box_view_(NULL
),
63 views::MessageBoxView::InitParams
init_params(prompt
);
64 init_params
.options
= views::MessageBoxView::HAS_PROMPT_FIELD
;
65 init_params
.inter_row_vertical_spacing
=
66 views::kUnrelatedControlVerticalSpacing
;
67 message_box_view_
= new views::MessageBoxView(init_params
);
68 message_box_view_
->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
70 web_modal::WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
71 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents
);
72 web_modal::WebContentsModalDialogManagerDelegate
* modal_delegate
=
73 web_contents_modal_dialog_manager
->delegate();
74 DCHECK(modal_delegate
);
75 dialog_
= views::Widget::CreateWindowAsFramelessChild(
76 this, modal_delegate
->GetWebContentsModalDialogHost()->GetHostView());
77 web_contents_modal_dialog_manager
->ShowModalDialog(
78 dialog_
->GetNativeView());
81 PDFPasswordDialogViews::~PDFPasswordDialogViews() {
82 if (!callback_
.is_null()) {
83 // This dialog was torn down without either OK or cancel being clicked; be
84 // considerate and at least do the callback.
85 callback_
.Run(false, base::string16());
89 //////////////////////////////////////////////////////////////////////////////
90 // PDFPasswordDialogViews, views::DialogDelegate implementation:
92 base::string16
PDFPasswordDialogViews::GetWindowTitle() const {
93 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE
);
96 base::string16
PDFPasswordDialogViews::GetDialogButtonLabel(
97 ui::DialogButton button
) const {
98 if (button
== ui::DIALOG_BUTTON_OK
)
99 return l10n_util::GetStringUTF16(IDS_OK
);
100 if (button
== ui::DIALOG_BUTTON_CANCEL
)
101 return l10n_util::GetStringUTF16(IDS_CANCEL
);
102 return base::string16();
105 bool PDFPasswordDialogViews::Cancel() {
106 callback_
.Run(false, base::string16());
111 bool PDFPasswordDialogViews::Accept() {
112 callback_
.Run(true, message_box_view_
->text_box()->text());
117 ///////////////////////////////////////////////////////////////////////////////
118 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
120 views::View
* PDFPasswordDialogViews::GetInitiallyFocusedView() {
121 return message_box_view_
->text_box();
124 views::View
* PDFPasswordDialogViews::GetContentsView() {
125 return message_box_view_
;
128 views::Widget
* PDFPasswordDialogViews::GetWidget() {
129 return message_box_view_
->GetWidget();
132 const views::Widget
* PDFPasswordDialogViews::GetWidget() const {
133 return message_box_view_
->GetWidget();
136 void PDFPasswordDialogViews::DeleteDelegate() {
140 ui::ModalType
PDFPasswordDialogViews::GetModalType() const {
142 return ui::MODAL_TYPE_CHILD
;
144 return views::WidgetDelegate::GetModalType();
150 void ShowPDFPasswordDialog(content::WebContents
* web_contents
,
151 const base::string16
& prompt
,
152 const PasswordDialogClosedCallback
& callback
) {
153 new PDFPasswordDialogViews(web_contents
, prompt
, callback
);