Chromium Code Reviews| Index: chrome/browser/ui/javascript_dialogs/javascript_dialog_views.cc |
| diff --git a/chrome/browser/ui/javascript_dialogs/javascript_dialog_views.cc b/chrome/browser/ui/javascript_dialogs/javascript_dialog_views.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be8b1c4824a758a801917e9bfbf49b45bbd758b3 |
| --- /dev/null |
| +++ b/chrome/browser/ui/javascript_dialogs/javascript_dialog_views.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/javascript_dialogs/javascript_dialog_views.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "components/constrained_window/constrained_window_views.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/views/controls/message_box_view.h" |
| +#include "ui/views/controls/textfield/textfield.h" |
| +#include "ui/views/layout/layout_constants.h" |
| + |
| +// static |
| +base::WeakPtr<JavaScriptDialogViews> JavaScriptDialogViews::Create( |
| + content::WebContents* parent_web_contents, |
| + content::WebContents* alerting_web_contents, |
| + const base::string16& title, |
| + content::JavaScriptMessageType message_type, |
| + const base::string16& message_text, |
| + const base::string16& default_prompt_text, |
| + const content::JavaScriptDialogManager::DialogClosedCallback& |
| + dialog_callback) { |
| + return (new JavaScriptDialogViews(parent_web_contents, alerting_web_contents, |
| + title, message_type, message_text, |
| + default_prompt_text, dialog_callback)) |
| + ->weak_factory_.GetWeakPtr(); |
| +} |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| +// 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.
|
| + |
| +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.
|
| + content::WebContents* parent_web_contents, |
| + content::WebContents* alerting_web_contents, |
| + const base::string16& title, |
| + content::JavaScriptMessageType message_type, |
| + const base::string16& message_text, |
| + const base::string16& default_prompt_text, |
| + const content::JavaScriptDialogManager::DialogClosedCallback& |
| + dialog_callback) |
| + : title_(title), |
| + message_type_(message_type), |
| + message_text_(message_text), |
| + default_prompt_text_(default_prompt_text), |
| + dialog_callback_(dialog_callback), |
| + weak_factory_(this) { |
| + int options = views::MessageBoxView::DETECT_DIRECTIONALITY; |
| + if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) |
| + options |= views::MessageBoxView::HAS_PROMPT_FIELD; |
| + |
| + views::MessageBoxView::InitParams params(message_text); |
| + params.options = options; |
| + params.default_prompt = default_prompt_text; |
| + message_box_view_ = new views::MessageBoxView(params); |
| + DCHECK(message_box_view_); |
| + |
| + constrained_window::ShowWebModalDialogViews(this, parent_web_contents); |
| +} |
| + |
| +JavaScriptDialogViews::~JavaScriptDialogViews() {} |
| + |
| +void JavaScriptDialogViews::CloseJavaScriptDialog() { |
| + 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.
|
| + GetWidget()->Close(); |
| +} |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| +// JavaScriptDialogViews, views::DialogDelegate implementation: |
| + |
| +int JavaScriptDialogViews::GetDefaultDialogButton() const { |
| + return ui::DIALOG_BUTTON_OK; |
| +} |
| + |
| +int JavaScriptDialogViews::GetDialogButtons() const { |
| + if (message_type_ == content::JAVASCRIPT_MESSAGE_TYPE_ALERT) |
| + return ui::DIALOG_BUTTON_OK; |
| + |
| + 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
|
| +} |
| + |
| +base::string16 JavaScriptDialogViews::GetWindowTitle() const { |
| + return title_; |
| +} |
| + |
| +bool JavaScriptDialogViews::Cancel() { |
| + if (dialog_callback_) |
| + dialog_callback_.Run(false, base::string16()); |
| + return true; |
| +} |
| + |
| +bool JavaScriptDialogViews::Accept() { |
| + if (dialog_callback_) |
| + dialog_callback_.Run(true, message_box_view_->GetInputText()); |
| + return true; |
| +} |
| + |
| +bool JavaScriptDialogViews::Close() { |
| + if (dialog_callback_) |
| + dialog_callback_.Run(false, base::string16()); |
| + return true; |
| +} |
| + |
| +void JavaScriptDialogViews::DeleteDelegate() { |
| + delete this; |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| +// JavaScriptDialogViews, views::WidgetDelegate implementation: |
| + |
| +views::View* JavaScriptDialogViews::GetContentsView() { |
| + return message_box_view_; |
| +} |
| + |
| +views::View* JavaScriptDialogViews::GetInitiallyFocusedView() { |
| + if (message_box_view_->text_box()) |
| + return message_box_view_->text_box(); |
| + 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
|
| +} |
| + |
| +views::Widget* JavaScriptDialogViews::GetWidget() { |
| + return message_box_view_->GetWidget(); |
| +} |
| + |
| +const views::Widget* JavaScriptDialogViews::GetWidget() const { |
| + return message_box_view_->GetWidget(); |
| +} |
| + |
| +ui::ModalType JavaScriptDialogViews::GetModalType() const { |
| + return ui::MODAL_TYPE_CHILD; |
| +} |