Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "content/public/browser/web_contents_view.h" | |
| 13 #include "grit/generated_resources.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/views/controls/message_box_view.h" | |
| 16 #include "ui/views/controls/textfield/textfield.h" | |
| 17 #include "ui/views/layout/layout_constants.h" | |
| 18 #include "ui/views/widget/widget.h" | |
| 19 #include "ui/views/window/dialog_delegate.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class PDFPasswordDialogViews : public views::DialogDelegate { | |
|
sky
2013/09/16 17:18:38
Add a description.
Avi (use Gerrit)
2013/09/16 18:57:14
Done.
| |
| 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* GetContentsView() OVERRIDE; | |
| 39 virtual views::NonClientFrameView* CreateNonClientFrameView( | |
| 40 views::Widget* widget) OVERRIDE; | |
| 41 virtual views::Widget* GetWidget() OVERRIDE; | |
| 42 virtual const views::Widget* GetWidget() const OVERRIDE; | |
| 43 virtual void DeleteDelegate() OVERRIDE; | |
| 44 virtual ui::ModalType GetModalType() const OVERRIDE; | |
| 45 | |
| 46 private: | |
| 47 // The message box view whose commands we handle. | |
| 48 views::MessageBoxView* message_box_view_; | |
| 49 | |
| 50 views::Widget* dialog_; | |
| 51 content::BrowserContext* browser_context_; | |
| 52 | |
| 53 PasswordDialogClosedCallback callback_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews); | |
| 56 }; | |
| 57 | |
| 58 PDFPasswordDialogViews::PDFPasswordDialogViews( | |
| 59 content::WebContents* web_contents, | |
| 60 const base::string16& prompt, | |
| 61 const PasswordDialogClosedCallback& callback) | |
| 62 : browser_context_(web_contents->GetBrowserContext()), | |
| 63 callback_(callback) { | |
|
sky
2013/09/16 17:18:38
nit: initialize dialog_ to NULL in the member list
Avi (use Gerrit)
2013/09/16 18:57:14
Done.
| |
| 64 views::MessageBoxView::InitParams init_params(prompt); | |
| 65 init_params.options = views::MessageBoxView::HAS_PROMPT_FIELD; | |
| 66 init_params.inter_row_vertical_spacing = | |
| 67 views::kUnrelatedControlVerticalSpacing; | |
| 68 message_box_view_ = new views::MessageBoxView(init_params); | |
| 69 | |
| 70 message_box_view_->text_box()->SetObscured(true); | |
| 71 | |
| 72 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
| 73 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents); | |
| 74 web_modal::WebContentsModalDialogManagerDelegate* modal_delegate = | |
| 75 web_contents_modal_dialog_manager->delegate(); | |
| 76 DCHECK(modal_delegate); | |
| 77 dialog_ = views::Widget::CreateWindowAsFramelessChild( | |
| 78 this, | |
| 79 web_contents->GetView()->GetNativeView(), | |
| 80 modal_delegate->GetWebContentsModalDialogHost()->GetHostView()); | |
| 81 web_contents_modal_dialog_manager->ShowDialog(dialog_->GetNativeView()); | |
| 82 | |
| 83 message_box_view_->text_box()->RequestFocus(); | |
|
sky
2013/09/16 17:18:38
Override GetInitiallyFocusedView instead.
Avi (use Gerrit)
2013/09/16 18:57:14
Done.
| |
| 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::GetContentsView() { | |
| 126 return message_box_view_; | |
| 127 } | |
| 128 | |
| 129 // TODO(wittman): Remove this override once we move to the new style frame view | |
| 130 // on all dialogs. | |
| 131 views::NonClientFrameView* PDFPasswordDialogViews::CreateNonClientFrameView( | |
| 132 views::Widget* widget) { | |
| 133 return CreateConstrainedStyleNonClientFrameView(widget, browser_context_); | |
| 134 } | |
| 135 | |
| 136 views::Widget* PDFPasswordDialogViews::GetWidget() { | |
| 137 return message_box_view_->GetWidget(); | |
| 138 } | |
| 139 | |
| 140 const views::Widget* PDFPasswordDialogViews::GetWidget() const { | |
| 141 return message_box_view_->GetWidget(); | |
| 142 } | |
| 143 | |
| 144 void PDFPasswordDialogViews::DeleteDelegate() { | |
| 145 delete this; | |
| 146 } | |
| 147 | |
| 148 ui::ModalType PDFPasswordDialogViews::GetModalType() const { | |
| 149 #if defined(USE_ASH) | |
| 150 return ui::MODAL_TYPE_CHILD; | |
| 151 #else | |
| 152 return views::WidgetDelegate::GetModalType(); | |
| 153 #endif | |
| 154 } | |
| 155 | |
| 156 } // namespace | |
| 157 | |
| 158 void ShowPDFPasswordDialog(content::WebContents* web_contents, | |
| 159 const base::string16& prompt, | |
| 160 const PasswordDialogClosedCallback& callback) { | |
| 161 new PDFPasswordDialogViews(web_contents, prompt, callback); | |
| 162 } | |
| OLD | NEW |