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 JavaScriptDialogViews::~JavaScriptDialogViews() = default; |
| 15 |
| 16 // static |
| 17 base::WeakPtr<JavaScriptDialogViews> JavaScriptDialogViews::Create( |
| 18 content::WebContents* parent_web_contents, |
| 19 content::WebContents* alerting_web_contents, |
| 20 const base::string16& title, |
| 21 content::JavaScriptMessageType message_type, |
| 22 const base::string16& message_text, |
| 23 const base::string16& default_prompt_text, |
| 24 const content::JavaScriptDialogManager::DialogClosedCallback& |
| 25 dialog_callback) { |
| 26 return (new JavaScriptDialogViews(parent_web_contents, alerting_web_contents, |
| 27 title, message_type, message_text, |
| 28 default_prompt_text, dialog_callback)) |
| 29 ->weak_factory_.GetWeakPtr(); |
| 30 } |
| 31 |
| 32 void JavaScriptDialogViews::CloseJavaScriptDialog() { |
| 33 dialog_callback_.Reset(); |
| 34 GetWidget()->Close(); |
| 35 } |
| 36 |
| 37 int JavaScriptDialogViews::GetDefaultDialogButton() const { |
| 38 return ui::DIALOG_BUTTON_OK; |
| 39 } |
| 40 |
| 41 int JavaScriptDialogViews::GetDialogButtons() const { |
| 42 const bool is_alert = message_type_ == content::JAVASCRIPT_MESSAGE_TYPE_ALERT; |
| 43 return ui::DIALOG_BUTTON_OK | (is_alert ? 0 : ui::DIALOG_BUTTON_CANCEL); |
| 44 } |
| 45 |
| 46 base::string16 JavaScriptDialogViews::GetWindowTitle() const { |
| 47 return title_; |
| 48 } |
| 49 |
| 50 bool JavaScriptDialogViews::Cancel() { |
| 51 if (dialog_callback_) |
| 52 dialog_callback_.Run(false, base::string16()); |
| 53 return true; |
| 54 } |
| 55 |
| 56 bool JavaScriptDialogViews::Accept() { |
| 57 if (dialog_callback_) |
| 58 dialog_callback_.Run(true, message_box_view_->GetInputText()); |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool JavaScriptDialogViews::Close() { |
| 63 if (dialog_callback_) |
| 64 dialog_callback_.Run(false, base::string16()); |
| 65 return true; |
| 66 } |
| 67 |
| 68 void JavaScriptDialogViews::DeleteDelegate() { |
| 69 delete this; |
| 70 } |
| 71 |
| 72 views::View* JavaScriptDialogViews::GetContentsView() { |
| 73 return message_box_view_; |
| 74 } |
| 75 |
| 76 views::View* JavaScriptDialogViews::GetInitiallyFocusedView() { |
| 77 auto* text_box = message_box_view_->text_box(); |
| 78 return text_box ? text_box : views::DialogDelegate::GetInitiallyFocusedView(); |
| 79 } |
| 80 |
| 81 views::Widget* JavaScriptDialogViews::GetWidget() { |
| 82 return message_box_view_->GetWidget(); |
| 83 } |
| 84 |
| 85 const views::Widget* JavaScriptDialogViews::GetWidget() const { |
| 86 return message_box_view_->GetWidget(); |
| 87 } |
| 88 |
| 89 ui::ModalType JavaScriptDialogViews::GetModalType() const { |
| 90 return ui::MODAL_TYPE_CHILD; |
| 91 } |
| 92 |
| 93 JavaScriptDialogViews::JavaScriptDialogViews( |
| 94 content::WebContents* parent_web_contents, |
| 95 content::WebContents* alerting_web_contents, |
| 96 const base::string16& title, |
| 97 content::JavaScriptMessageType message_type, |
| 98 const base::string16& message_text, |
| 99 const base::string16& default_prompt_text, |
| 100 const content::JavaScriptDialogManager::DialogClosedCallback& |
| 101 dialog_callback) |
| 102 : title_(title), |
| 103 message_type_(message_type), |
| 104 message_text_(message_text), |
| 105 default_prompt_text_(default_prompt_text), |
| 106 dialog_callback_(dialog_callback), |
| 107 weak_factory_(this) { |
| 108 int options = views::MessageBoxView::DETECT_DIRECTIONALITY; |
| 109 if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) |
| 110 options |= views::MessageBoxView::HAS_PROMPT_FIELD; |
| 111 |
| 112 views::MessageBoxView::InitParams params(message_text); |
| 113 params.options = options; |
| 114 params.default_prompt = default_prompt_text; |
| 115 message_box_view_ = new views::MessageBoxView(params); |
| 116 DCHECK(message_box_view_); |
| 117 |
| 118 constrained_window::ShowWebModalDialogViews(this, parent_web_contents); |
| 119 } |
OLD | NEW |