pdf_password_dialog.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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->ShowModalDialog( 78 dialog_->GetNativeView()); 79} 80 81PDFPasswordDialogViews::~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()); 86 } 87} 88 89////////////////////////////////////////////////////////////////////////////// 90// PDFPasswordDialogViews, views::DialogDelegate implementation: 91 92base::string16 PDFPasswordDialogViews::GetWindowTitle() const { 93 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE); 94} 95 96base::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(); 103} 104 105bool PDFPasswordDialogViews::Cancel() { 106 callback_.Run(false, base::string16()); 107 callback_.Reset(); 108 return true; 109} 110 111bool PDFPasswordDialogViews::Accept() { 112 callback_.Run(true, message_box_view_->text_box()->text()); 113 callback_.Reset(); 114 return true; 115} 116 117/////////////////////////////////////////////////////////////////////////////// 118// PDFPasswordDialogViews, views::WidgetDelegate implementation: 119 120views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() { 121 return message_box_view_->text_box(); 122} 123 124views::View* PDFPasswordDialogViews::GetContentsView() { 125 return message_box_view_; 126} 127 128views::Widget* PDFPasswordDialogViews::GetWidget() { 129 return message_box_view_->GetWidget(); 130} 131 132const views::Widget* PDFPasswordDialogViews::GetWidget() const { 133 return message_box_view_->GetWidget(); 134} 135 136void PDFPasswordDialogViews::DeleteDelegate() { 137 delete this; 138} 139 140ui::ModalType PDFPasswordDialogViews::GetModalType() const { 141#if defined(USE_ASH) 142 return ui::MODAL_TYPE_CHILD; 143#else 144 return views::WidgetDelegate::GetModalType(); 145#endif 146} 147 148} // namespace 149 150void ShowPDFPasswordDialog(content::WebContents* web_contents, 151 const base::string16& prompt, 152 const PasswordDialogClosedCallback& callback) { 153 new PDFPasswordDialogViews(web_contents, prompt, callback); 154} 155