Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/javascript_dialogs/javascript_dialog_views.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "components/constrained_window/constrained_window_views.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 | |
| 14 // static | |
| 15 base::WeakPtr<JavaScriptDialogViews> JavaScriptDialogViews::Create( | |
| 16 content::WebContents* parent_web_contents, | |
| 17 content::WebContents* alerting_web_contents, | |
| 18 const base::string16& title, | |
| 19 content::JavaScriptMessageType message_type, | |
| 20 const base::string16& message_text, | |
| 21 const base::string16& default_prompt_text, | |
| 22 const content::JavaScriptDialogManager::DialogClosedCallback& | |
| 23 dialog_callback) { | |
| 24 return (new JavaScriptDialogViews(parent_web_contents, alerting_web_contents, | |
| 25 title, message_type, message_text, | |
| 26 default_prompt_text, dialog_callback)) | |
| 27 ->weak_factory_.GetWeakPtr(); | |
| 28 } | |
| 29 | |
| 30 ////////////////////////////////////////////////////////////////////////////// | |
| 31 // JavaScriptDialogViews, constructor & destructor: | |
|
Peter Kasting
2016/10/17 20:16:05
Nit: Personally I'm not a fan of these ///// comme
Avi (use Gerrit)
2016/10/18 02:46:42
Stolen from other Views code. Removed.
| |
| 32 | |
| 33 JavaScriptDialogViews::JavaScriptDialogViews( | |
|
Peter Kasting
2016/10/17 20:16:05
Nit: Function definition order should match .cc de
Avi (use Gerrit)
2016/10/18 02:46:41
Done.
| |
| 34 content::WebContents* parent_web_contents, | |
| 35 content::WebContents* alerting_web_contents, | |
| 36 const base::string16& title, | |
| 37 content::JavaScriptMessageType message_type, | |
| 38 const base::string16& message_text, | |
| 39 const base::string16& default_prompt_text, | |
| 40 const content::JavaScriptDialogManager::DialogClosedCallback& | |
| 41 dialog_callback) | |
| 42 : title_(title), | |
| 43 message_type_(message_type), | |
| 44 message_text_(message_text), | |
| 45 default_prompt_text_(default_prompt_text), | |
| 46 dialog_callback_(dialog_callback), | |
| 47 weak_factory_(this) { | |
| 48 int options = views::MessageBoxView::DETECT_DIRECTIONALITY; | |
| 49 if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) | |
| 50 options |= views::MessageBoxView::HAS_PROMPT_FIELD; | |
| 51 | |
| 52 views::MessageBoxView::InitParams params(message_text); | |
| 53 params.options = options; | |
| 54 params.default_prompt = default_prompt_text; | |
| 55 message_box_view_ = new views::MessageBoxView(params); | |
| 56 DCHECK(message_box_view_); | |
| 57 | |
| 58 constrained_window::ShowWebModalDialogViews(this, parent_web_contents); | |
| 59 } | |
| 60 | |
| 61 JavaScriptDialogViews::~JavaScriptDialogViews() {} | |
| 62 | |
| 63 void JavaScriptDialogViews::CloseJavaScriptDialog() { | |
| 64 dialog_callback_.Reset(); | |
|
Peter Kasting
2016/10/17 20:16:05
Why would we not call Close() instead? It's not o
Avi (use Gerrit)
2016/10/18 02:46:41
Close() is a Views method, and is an implementatio
Peter Kasting
2016/10/18 04:31:03
What I was more asking was, Close() runs the callb
Avi (use Gerrit)
2016/10/18 16:31:49
Yes; JavaScriptDialogManager's call CancelDialogs
Peter Kasting
2016/10/18 16:59:55
Seems fine.
| |
| 65 GetWidget()->Close(); | |
| 66 } | |
| 67 | |
| 68 ////////////////////////////////////////////////////////////////////////////// | |
| 69 // JavaScriptDialogViews, views::DialogDelegate implementation: | |
| 70 | |
| 71 int JavaScriptDialogViews::GetDefaultDialogButton() const { | |
| 72 return ui::DIALOG_BUTTON_OK; | |
| 73 } | |
| 74 | |
| 75 int JavaScriptDialogViews::GetDialogButtons() const { | |
| 76 if (message_type_ == content::JAVASCRIPT_MESSAGE_TYPE_ALERT) | |
| 77 return ui::DIALOG_BUTTON_OK; | |
| 78 | |
| 79 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL; | |
|
Peter Kasting
2016/10/17 20:16:05
Nit: Shorter:
const bool is_alert = message_typ
Avi (use Gerrit)
2016/10/18 02:46:42
OK.
(I don't know what Views idiom is, so I stole
Peter Kasting
2016/10/18 04:31:03
Dunno that we have a consistent idiom for this. P
| |
| 80 } | |
| 81 | |
| 82 base::string16 JavaScriptDialogViews::GetWindowTitle() const { | |
| 83 return title_; | |
| 84 } | |
| 85 | |
| 86 bool JavaScriptDialogViews::Cancel() { | |
| 87 if (dialog_callback_) | |
| 88 dialog_callback_.Run(false, base::string16()); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool JavaScriptDialogViews::Accept() { | |
| 93 if (dialog_callback_) | |
| 94 dialog_callback_.Run(true, message_box_view_->GetInputText()); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool JavaScriptDialogViews::Close() { | |
| 99 if (dialog_callback_) | |
| 100 dialog_callback_.Run(false, base::string16()); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 void JavaScriptDialogViews::DeleteDelegate() { | |
| 105 delete this; | |
| 106 } | |
| 107 | |
| 108 /////////////////////////////////////////////////////////////////////////////// | |
| 109 // JavaScriptDialogViews, views::WidgetDelegate implementation: | |
| 110 | |
| 111 views::View* JavaScriptDialogViews::GetContentsView() { | |
| 112 return message_box_view_; | |
| 113 } | |
| 114 | |
| 115 views::View* JavaScriptDialogViews::GetInitiallyFocusedView() { | |
| 116 if (message_box_view_->text_box()) | |
| 117 return message_box_view_->text_box(); | |
| 118 return views::DialogDelegate::GetInitiallyFocusedView(); | |
|
Peter Kasting
2016/10/17 20:16:05
Nit: Shorter:
auto* text_box = message_box_view
Avi (use Gerrit)
2016/10/18 02:46:41
ditto re stolen
| |
| 119 } | |
| 120 | |
| 121 views::Widget* JavaScriptDialogViews::GetWidget() { | |
| 122 return message_box_view_->GetWidget(); | |
| 123 } | |
| 124 | |
| 125 const views::Widget* JavaScriptDialogViews::GetWidget() const { | |
| 126 return message_box_view_->GetWidget(); | |
| 127 } | |
| 128 | |
| 129 ui::ModalType JavaScriptDialogViews::GetModalType() const { | |
| 130 return ui::MODAL_TYPE_CHILD; | |
| 131 } | |
| OLD | NEW |