Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / pdf_password_dialog.cc
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.
4
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"
15
16 namespace {
17
18 // Runs a tab-modal dialog that asks the user for a password.
19 class PDFPasswordDialogViews : public views::DialogDelegate {
20  public:
21   PDFPasswordDialogViews(content::WebContents* web_contents,
22                          const base::string16& prompt,
23                          const pdf::PasswordDialogClosedCallback& callback);
24   ~PDFPasswordDialogViews() override;
25
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;
31
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;
39
40  private:
41   // The message box view whose commands we handle.
42   views::MessageBoxView* message_box_view_;
43
44   pdf::PasswordDialogClosedCallback callback_;
45
46   DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews);
47 };
48
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   ShowWebModalDialogViews(this, web_contents);
61 }
62
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());
68   }
69 }
70
71 //////////////////////////////////////////////////////////////////////////////
72 // PDFPasswordDialogViews, views::DialogDelegate implementation:
73
74 base::string16 PDFPasswordDialogViews::GetWindowTitle() const {
75   return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE);
76 }
77
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();
85 }
86
87 bool PDFPasswordDialogViews::Cancel() {
88   callback_.Run(false, base::string16());
89   callback_.Reset();
90   return true;
91 }
92
93 bool PDFPasswordDialogViews::Accept() {
94   callback_.Run(true, message_box_view_->text_box()->text());
95   callback_.Reset();
96   return true;
97 }
98
99 ///////////////////////////////////////////////////////////////////////////////
100 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
101
102 views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() {
103   return message_box_view_->text_box();
104 }
105
106 views::View* PDFPasswordDialogViews::GetContentsView() {
107   return message_box_view_;
108 }
109
110 views::Widget* PDFPasswordDialogViews::GetWidget() {
111   return message_box_view_->GetWidget();
112 }
113
114 const views::Widget* PDFPasswordDialogViews::GetWidget() const {
115   return message_box_view_->GetWidget();
116 }
117
118 void PDFPasswordDialogViews::DeleteDelegate() {
119   delete this;
120 }
121
122 ui::ModalType PDFPasswordDialogViews::GetModalType() const {
123   return ui::MODAL_TYPE_CHILD;
124 }
125
126 }  // namespace
127
128 void ShowPDFPasswordDialog(content::WebContents* web_contents,
129                            const base::string16& prompt,
130                            const pdf::PasswordDialogClosedCallback& callback) {
131   new PDFPasswordDialogViews(web_contents, prompt, callback);
132 }