- add sources.
[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/browser/ui/pdf/pdf_tab_helper.h"
6
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"
20
21 namespace {
22
23 // PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a
24 // password.
25 class PDFPasswordDialogViews : public views::DialogDelegate {
26  public:
27   PDFPasswordDialogViews(content::WebContents* web_contents,
28                          const base::string16& prompt,
29                          const PasswordDialogClosedCallback& callback);
30   virtual ~PDFPasswordDialogViews();
31
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;
38
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;
48
49  private:
50   // The message box view whose commands we handle.
51   views::MessageBoxView* message_box_view_;
52
53   views::Widget* dialog_;
54   content::BrowserContext* browser_context_;
55
56   PasswordDialogClosedCallback callback_;
57
58   DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews);
59 };
60
61 PDFPasswordDialogViews::PDFPasswordDialogViews(
62     content::WebContents* web_contents,
63     const base::string16& prompt,
64     const PasswordDialogClosedCallback& callback)
65     : message_box_view_(NULL),
66       dialog_(NULL),
67       browser_context_(web_contents->GetBrowserContext()),
68       callback_(callback) {
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);
74
75   message_box_view_->text_box()->SetObscured(true);
76
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(
83       this,
84       web_contents->GetView()->GetNativeView(),
85       modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
86   web_contents_modal_dialog_manager->ShowDialog(dialog_->GetNativeView());
87 }
88
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());
94   }
95 }
96
97 //////////////////////////////////////////////////////////////////////////////
98 // PDFPasswordDialogViews, views::DialogDelegate implementation:
99
100 base::string16 PDFPasswordDialogViews::GetWindowTitle() const {
101   return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE);
102 }
103
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();
111 }
112
113 bool PDFPasswordDialogViews::Cancel() {
114   callback_.Run(false, base::string16());
115   callback_.Reset();
116   return true;
117 }
118
119 bool PDFPasswordDialogViews::Accept() {
120   callback_.Run(true, message_box_view_->text_box()->text());
121   callback_.Reset();
122   return true;
123 }
124
125 ///////////////////////////////////////////////////////////////////////////////
126 // PDFPasswordDialogViews, views::WidgetDelegate implementation:
127
128 views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() {
129   return message_box_view_->text_box();
130 }
131
132 views::View* PDFPasswordDialogViews::GetContentsView() {
133   return message_box_view_;
134 }
135
136 // TODO(wittman): Remove this override once we move to the new style frame view
137 // on all dialogs.
138 views::NonClientFrameView* PDFPasswordDialogViews::CreateNonClientFrameView(
139     views::Widget* widget) {
140   return CreateConstrainedStyleNonClientFrameView(widget, browser_context_);
141 }
142
143 views::Widget* PDFPasswordDialogViews::GetWidget() {
144   return message_box_view_->GetWidget();
145 }
146
147 const views::Widget* PDFPasswordDialogViews::GetWidget() const {
148   return message_box_view_->GetWidget();
149 }
150
151 void PDFPasswordDialogViews::DeleteDelegate() {
152   delete this;
153 }
154
155 ui::ModalType PDFPasswordDialogViews::GetModalType() const {
156 #if defined(USE_ASH)
157   return ui::MODAL_TYPE_CHILD;
158 #else
159   return views::WidgetDelegate::GetModalType();
160 #endif
161 }
162
163 }  // namespace
164
165 void ShowPDFPasswordDialog(content::WebContents* web_contents,
166                            const base::string16& prompt,
167                            const PasswordDialogClosedCallback& callback) {
168   new PDFPasswordDialogViews(web_contents, prompt, callback);
169 }