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 "grit/generated_resources.h" 13 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/views/controls/message_box_view.h" 15 #include "ui/views/controls/textfield/textfield.h" 16 #include "ui/views/layout/layout_constants.h" 17 #include "ui/views/widget/widget.h" 18 #include "ui/views/window/dialog_delegate.h" 19 20 namespace { 21 22 // PDFPasswordDialogViews runs a tab-modal dialog that asks the user for a 23 // password. 24 class PDFPasswordDialogViews : public views::DialogDelegate { 25 public: 26 PDFPasswordDialogViews(content::WebContents* web_contents, 27 const base::string16& prompt, 28 const PasswordDialogClosedCallback& callback); 29 virtual ~PDFPasswordDialogViews(); 30 31 // views::DialogDelegate: 32 virtual base::string16 GetWindowTitle() const OVERRIDE; 33 virtual base::string16 GetDialogButtonLabel( 34 ui::DialogButton button) const OVERRIDE; 35 virtual bool Cancel() OVERRIDE; 36 virtual bool Accept() OVERRIDE; 37 38 // views::WidgetDelegate: 39 virtual views::View* GetInitiallyFocusedView() OVERRIDE; 40 virtual views::View* GetContentsView() OVERRIDE; 41 virtual views::NonClientFrameView* CreateNonClientFrameView( 42 views::Widget* widget) OVERRIDE; 43 virtual views::Widget* GetWidget() OVERRIDE; 44 virtual const views::Widget* GetWidget() const OVERRIDE; 45 virtual void DeleteDelegate() OVERRIDE; 46 virtual ui::ModalType GetModalType() const OVERRIDE; 47 48 private: 49 // The message box view whose commands we handle. 50 views::MessageBoxView* message_box_view_; 51 52 views::Widget* dialog_; 53 content::BrowserContext* browser_context_; 54 55 PasswordDialogClosedCallback callback_; 56 57 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews); 58 }; 59 60 PDFPasswordDialogViews::PDFPasswordDialogViews( 61 content::WebContents* web_contents, 62 const base::string16& prompt, 63 const PasswordDialogClosedCallback& callback) 64 : message_box_view_(NULL), 65 dialog_(NULL), 66 browser_context_(web_contents->GetBrowserContext()), 67 callback_(callback) { 68 views::MessageBoxView::InitParams init_params(prompt); 69 init_params.options = views::MessageBoxView::HAS_PROMPT_FIELD; 70 init_params.inter_row_vertical_spacing = 71 views::kUnrelatedControlVerticalSpacing; 72 message_box_view_ = new views::MessageBoxView(init_params); 73 74 message_box_view_->text_box()->SetObscured(true); 75 76 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = 77 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); 78 web_modal::WebContentsModalDialogManagerDelegate* modal_delegate = 79 web_contents_modal_dialog_manager->delegate(); 80 DCHECK(modal_delegate); 81 dialog_ = views::Widget::CreateWindowAsFramelessChild( 82 this, modal_delegate->GetWebContentsModalDialogHost()->GetHostView()); 83 web_contents_modal_dialog_manager->ShowDialog(dialog_->GetNativeView()); 84 } 85 86 PDFPasswordDialogViews::~PDFPasswordDialogViews() { 87 if (!callback_.is_null()) { 88 // This dialog was torn down without either OK or cancel being clicked; be 89 // considerate and at least do the callback. 90 callback_.Run(false, base::string16()); 91 } 92 } 93 94 ////////////////////////////////////////////////////////////////////////////// 95 // PDFPasswordDialogViews, views::DialogDelegate implementation: 96 97 base::string16 PDFPasswordDialogViews::GetWindowTitle() const { 98 return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE); 99 } 100 101 base::string16 PDFPasswordDialogViews::GetDialogButtonLabel( 102 ui::DialogButton button) const { 103 if (button == ui::DIALOG_BUTTON_OK) 104 return l10n_util::GetStringUTF16(IDS_OK); 105 if (button == ui::DIALOG_BUTTON_CANCEL) 106 return l10n_util::GetStringUTF16(IDS_CANCEL); 107 return base::string16(); 108 } 109 110 bool PDFPasswordDialogViews::Cancel() { 111 callback_.Run(false, base::string16()); 112 callback_.Reset(); 113 return true; 114 } 115 116 bool PDFPasswordDialogViews::Accept() { 117 callback_.Run(true, message_box_view_->text_box()->text()); 118 callback_.Reset(); 119 return true; 120 } 121 122 /////////////////////////////////////////////////////////////////////////////// 123 // PDFPasswordDialogViews, views::WidgetDelegate implementation: 124 125 views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() { 126 return message_box_view_->text_box(); 127 } 128 129 views::View* PDFPasswordDialogViews::GetContentsView() { 130 return message_box_view_; 131 } 132 133 // TODO(wittman): Remove this override once we move to the new style frame view 134 // on all dialogs. 135 views::NonClientFrameView* PDFPasswordDialogViews::CreateNonClientFrameView( 136 views::Widget* widget) { 137 return CreateConstrainedStyleNonClientFrameView(widget, browser_context_); 138 } 139 140 views::Widget* PDFPasswordDialogViews::GetWidget() { 141 return message_box_view_->GetWidget(); 142 } 143 144 const views::Widget* PDFPasswordDialogViews::GetWidget() const { 145 return message_box_view_->GetWidget(); 146 } 147 148 void PDFPasswordDialogViews::DeleteDelegate() { 149 delete this; 150 } 151 152 ui::ModalType PDFPasswordDialogViews::GetModalType() const { 153 #if defined(USE_ASH) 154 return ui::MODAL_TYPE_CHILD; 155 #else 156 return views::WidgetDelegate::GetModalType(); 157 #endif 158 } 159 160 } // namespace 161 162 void ShowPDFPasswordDialog(content::WebContents* web_contents, 163 const base::string16& prompt, 164 const PasswordDialogClosedCallback& callback) { 165 new PDFPasswordDialogViews(web_contents, prompt, callback); 166 } 167