pdf_password_dialog.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "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" 18 19namespace { 20 21// PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a 22// password. 23class PDFPasswordDialogViews : public views::DialogDelegate { 24 public: 25 PDFPasswordDialogViews(content::WebContents* web_contents, 26 const base::string16& prompt, 27 const PasswordDialogClosedCallback& callback); 28 virtual ~PDFPasswordDialogViews(); 29 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; 36 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; 44 45 private: 46 // The message box view whose commands we handle. 47 views::MessageBoxView* message_box_view_; 48 49 views::Widget* dialog_; 50 51 PasswordDialogClosedCallback callback_; 52 53 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews); 54}; 55 56PDFPasswordDialogViews::PDFPasswordDialogViews( 57 content::WebContents* web_contents, 58 const base::string16& prompt, 59 const PasswordDialogClosedCallback& callback) 60 : message_box_view_(NULL), 61 dialog_(NULL), 62 callback_(callback) { 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); 69 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->ShowDialog(dialog_->GetNativeView()); 78} 79 80PDFPasswordDialogViews::~PDFPasswordDialogViews() { 81 if (!callback_.is_null()) { 82 // This dialog was torn down without either OK or cancel being clicked; be 83 // considerate and at least do the callback. 84 callback_.Run(false, base::string16()); 85 } 86} 87 88////////////////////////////////////////////////////////////////////////////// 89// PDFPasswordDialogViews, views::DialogDelegate implementation: 90 91base::string16 PDFPasswordDialogViews::GetWindowTitle() const { 92 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE); 93} 94 95base::string16 PDFPasswordDialogViews::GetDialogButtonLabel( 96 ui::DialogButton button) const { 97 if (button == ui::DIALOG_BUTTON_OK) 98 return l10n_util::GetStringUTF16(IDS_OK); 99 if (button == ui::DIALOG_BUTTON_CANCEL) 100 return l10n_util::GetStringUTF16(IDS_CANCEL); 101 return base::string16(); 102} 103 104bool PDFPasswordDialogViews::Cancel() { 105 callback_.Run(false, base::string16()); 106 callback_.Reset(); 107 return true; 108} 109 110bool PDFPasswordDialogViews::Accept() { 111 callback_.Run(true, message_box_view_->text_box()->text()); 112 callback_.Reset(); 113 return true; 114} 115 116/////////////////////////////////////////////////////////////////////////////// 117// PDFPasswordDialogViews, views::WidgetDelegate implementation: 118 119views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() { 120 return message_box_view_->text_box(); 121} 122 123views::View* PDFPasswordDialogViews::GetContentsView() { 124 return message_box_view_; 125} 126 127views::Widget* PDFPasswordDialogViews::GetWidget() { 128 return message_box_view_->GetWidget(); 129} 130 131const views::Widget* PDFPasswordDialogViews::GetWidget() const { 132 return message_box_view_->GetWidget(); 133} 134 135void PDFPasswordDialogViews::DeleteDelegate() { 136 delete this; 137} 138 139ui::ModalType PDFPasswordDialogViews::GetModalType() const { 140#if defined(USE_ASH) 141 return ui::MODAL_TYPE_CHILD; 142#else 143 return views::WidgetDelegate::GetModalType(); 144#endif 145} 146 147} // namespace 148 149void ShowPDFPasswordDialog(content::WebContents* web_contents, 150 const base::string16& prompt, 151 const PasswordDialogClosedCallback& callback) { 152 new PDFPasswordDialogViews(web_contents, prompt, callback); 153} 154