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 "content/public/browser/web_contents_view.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/views/controls/message_box_view.h"
16 #include "ui/views/controls/textfield/textfield.h"
17 #include "ui/views/layout/layout_constants.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/window/dialog_delegate.h"
23 // PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a
25 class PDFPasswordDialogViews : public views::DialogDelegate {
27 PDFPasswordDialogViews(content::WebContents* web_contents,
28 const base::string16& prompt,
29 const PasswordDialogClosedCallback& callback);
30 virtual ~PDFPasswordDialogViews();
32 // views::DialogDelegate:
33 virtual base::string16 GetWindowTitle() const OVERRIDE;
34 virtual base::string16 GetDialogButtonLabel(
35 ui::DialogButton button) const OVERRIDE;
36 virtual bool Cancel() OVERRIDE;
37 virtual bool Accept() OVERRIDE;
39 // views::WidgetDelegate:
40 virtual views::View* GetInitiallyFocusedView() OVERRIDE;
41 virtual views::View* GetContentsView() OVERRIDE;
42 virtual views::NonClientFrameView* CreateNonClientFrameView(
43 views::Widget* widget) OVERRIDE;
44 virtual views::Widget* GetWidget() OVERRIDE;
45 virtual const views::Widget* GetWidget() const OVERRIDE;
46 virtual void DeleteDelegate() OVERRIDE;
47 virtual ui::ModalType GetModalType() const OVERRIDE;
50 // The message box view whose commands we handle.
51 views::MessageBoxView* message_box_view_;
53 views::Widget* dialog_;
54 content::BrowserContext* browser_context_;
56 PasswordDialogClosedCallback callback_;
58 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews);
61 PDFPasswordDialogViews::PDFPasswordDialogViews(
62 content::WebContents* web_contents,
63 const base::string16& prompt,
64 const PasswordDialogClosedCallback& callback)
65 : message_box_view_(NULL),
67 browser_context_(web_contents->GetBrowserContext()),
69 views::MessageBoxView::InitParams init_params(prompt);
70 init_params.options = views::MessageBoxView::HAS_PROMPT_FIELD;
71 init_params.inter_row_vertical_spacing =
72 views::kUnrelatedControlVerticalSpacing;
73 message_box_view_ = new views::MessageBoxView(init_params);
75 message_box_view_->text_box()->SetObscured(true);
77 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
78 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
79 web_modal::WebContentsModalDialogManagerDelegate* modal_delegate =
80 web_contents_modal_dialog_manager->delegate();
81 DCHECK(modal_delegate);
82 dialog_ = views::Widget::CreateWindowAsFramelessChild(
84 web_contents->GetView()->GetNativeView(),
85 modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
86 web_contents_modal_dialog_manager->ShowDialog(dialog_->GetNativeView());
89 PDFPasswordDialogViews::~PDFPasswordDialogViews() {
90 if (!callback_.is_null()) {
91 // This dialog was torn down without either OK or cancel being clicked; be
92 // considerate and at least do the callback.
93 callback_.Run(false, base::string16());
97 //////////////////////////////////////////////////////////////////////////////
98 // PDFPasswordDialogViews, views::DialogDelegate implementation:
100 base::string16 PDFPasswordDialogViews::GetWindowTitle() const {
101 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE);
104 base::string16 PDFPasswordDialogViews::GetDialogButtonLabel(
105 ui::DialogButton button) const {
106 if (button == ui::DIALOG_BUTTON_OK)
107 return l10n_util::GetStringUTF16(IDS_OK);
108 if (button == ui::DIALOG_BUTTON_CANCEL)
109 return l10n_util::GetStringUTF16(IDS_CANCEL);
110 return base::string16();
113 bool PDFPasswordDialogViews::Cancel() {
114 callback_.Run(false, base::string16());
119 bool PDFPasswordDialogViews::Accept() {
120 callback_.Run(true, message_box_view_->text_box()->text());
125 ///////////////////////////////////////////////////////////////////////////////
126 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
128 views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() {
129 return message_box_view_->text_box();
132 views::View* PDFPasswordDialogViews::GetContentsView() {
133 return message_box_view_;
136 // TODO(wittman): Remove this override once we move to the new style frame view
138 views::NonClientFrameView* PDFPasswordDialogViews::CreateNonClientFrameView(
139 views::Widget* widget) {
140 return CreateConstrainedStyleNonClientFrameView(widget, browser_context_);
143 views::Widget* PDFPasswordDialogViews::GetWidget() {
144 return message_box_view_->GetWidget();
147 const views::Widget* PDFPasswordDialogViews::GetWidget() const {
148 return message_box_view_->GetWidget();
151 void PDFPasswordDialogViews::DeleteDelegate() {
155 ui::ModalType PDFPasswordDialogViews::GetModalType() const {
157 return ui::MODAL_TYPE_CHILD;
159 return views::WidgetDelegate::GetModalType();
165 void ShowPDFPasswordDialog(content::WebContents* web_contents,
166 const base::string16& prompt,
167 const PasswordDialogClosedCallback& callback) {
168 new PDFPasswordDialogViews(web_contents, prompt, callback);