pdf_password_dialog.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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/views/constrained_window_views.h" 6#include "chrome/grit/generated_resources.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 16namespace { 17 18// Runs a tab-modal dialog that asks the user for a password. 19class PDFPasswordDialogViews : public views::DialogDelegate { 20 public: 21 PDFPasswordDialogViews(content::WebContents* web_contents, 22 const base::string16& prompt, 23 const pdf::PasswordDialogClosedCallback& callback); 24 virtual ~PDFPasswordDialogViews(); 25 26 // views::DialogDelegate: 27 virtual base::string16 GetWindowTitle() const OVERRIDE; 28 virtual base::string16 GetDialogButtonLabel( 29 ui::DialogButton button) const OVERRIDE; 30 virtual bool Cancel() OVERRIDE; 31 virtual bool Accept() OVERRIDE; 32 33 // views::WidgetDelegate: 34 virtual views::View* GetInitiallyFocusedView() OVERRIDE; 35 virtual views::View* GetContentsView() OVERRIDE; 36 virtual views::Widget* GetWidget() OVERRIDE; 37 virtual const views::Widget* GetWidget() const OVERRIDE; 38 virtual void DeleteDelegate() OVERRIDE; 39 virtual ui::ModalType GetModalType() const OVERRIDE; 40 41 private: 42 // The message box view whose commands we handle. 43 views::MessageBoxView* message_box_view_; 44 45 pdf::PasswordDialogClosedCallback callback_; 46 47 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews); 48}; 49 50PDFPasswordDialogViews::PDFPasswordDialogViews( 51 content::WebContents* web_contents, 52 const base::string16& prompt, 53 const pdf::PasswordDialogClosedCallback& callback) 54 : message_box_view_(NULL), callback_(callback) { 55 views::MessageBoxView::InitParams init_params(prompt); 56 init_params.options = views::MessageBoxView::HAS_PROMPT_FIELD; 57 init_params.inter_row_vertical_spacing = 58 views::kUnrelatedControlVerticalSpacing; 59 message_box_view_ = new views::MessageBoxView(init_params); 60 message_box_view_->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); 61 ShowWebModalDialogViews(this, web_contents); 62} 63 64PDFPasswordDialogViews::~PDFPasswordDialogViews() { 65 if (!callback_.is_null()) { 66 // This dialog was torn down without either OK or cancel being clicked; be 67 // considerate and at least do the callback. 68 callback_.Run(false, base::string16()); 69 } 70} 71 72////////////////////////////////////////////////////////////////////////////// 73// PDFPasswordDialogViews, views::DialogDelegate implementation: 74 75base::string16 PDFPasswordDialogViews::GetWindowTitle() const { 76 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE); 77} 78 79base::string16 PDFPasswordDialogViews::GetDialogButtonLabel( 80 ui::DialogButton button) const { 81 if (button == ui::DIALOG_BUTTON_OK) 82 return l10n_util::GetStringUTF16(IDS_OK); 83 if (button == ui::DIALOG_BUTTON_CANCEL) 84 return l10n_util::GetStringUTF16(IDS_CANCEL); 85 return base::string16(); 86} 87 88bool PDFPasswordDialogViews::Cancel() { 89 callback_.Run(false, base::string16()); 90 callback_.Reset(); 91 return true; 92} 93 94bool PDFPasswordDialogViews::Accept() { 95 callback_.Run(true, message_box_view_->text_box()->text()); 96 callback_.Reset(); 97 return true; 98} 99 100/////////////////////////////////////////////////////////////////////////////// 101// PDFPasswordDialogViews, views::WidgetDelegate implementation: 102 103views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() { 104 return message_box_view_->text_box(); 105} 106 107views::View* PDFPasswordDialogViews::GetContentsView() { 108 return message_box_view_; 109} 110 111views::Widget* PDFPasswordDialogViews::GetWidget() { 112 return message_box_view_->GetWidget(); 113} 114 115const views::Widget* PDFPasswordDialogViews::GetWidget() const { 116 return message_box_view_->GetWidget(); 117} 118 119void PDFPasswordDialogViews::DeleteDelegate() { 120 delete this; 121} 122 123ui::ModalType PDFPasswordDialogViews::GetModalType() const { 124 return ui::MODAL_TYPE_CHILD; 125} 126 127} // namespace 128 129void ShowPDFPasswordDialog(content::WebContents* web_contents, 130 const base::string16& prompt, 131 const pdf::PasswordDialogClosedCallback& callback) { 132 new PDFPasswordDialogViews(web_contents, prompt, callback); 133} 134