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