[email protected] | fedec01 | 2012-01-28 03:09:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 5 | #include "components/app_modal/javascript_app_modal_dialog.h" |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 6 | |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 9 | #include "base/metrics/histogram_macros.h" |
| 10 | #include "base/time/time.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 11 | #include "build/build_config.h" |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 12 | #include "components/app_modal/app_modal_dialog_queue.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 13 | #include "components/app_modal/javascript_dialog_manager.h" |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 14 | #include "components/app_modal/javascript_native_dialog_factory.h" |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 15 | #include "components/app_modal/native_app_modal_dialog.h" |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 16 | #include "ui/gfx/text_elider.h" |
[email protected] | 51da7e3 | 2012-01-30 19:24:52 | [diff] [blame] | 17 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 18 | namespace app_modal { |
[email protected] | 999adfd6 | 2010-06-02 19:42:42 | [diff] [blame] | 19 | namespace { |
| 20 | |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 21 | AppModalDialogObserver* app_modal_dialog_observer = nullptr; |
| 22 | |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 23 | // Control maximum sizes of various texts passed to us from javascript. |
[email protected] | 7be6450 | 2011-05-03 17:51:47 | [diff] [blame] | 24 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 25 | // Two-dimensional eliding. Reformat the text of the message dialog |
| 26 | // inserting line breaks because otherwise a single long line can overflow |
| 27 | // the message dialog (and crash/hang the GTK, depending on the version). |
[email protected] | af4b9df | 2010-12-22 21:19:02 | [diff] [blame] | 28 | const int kMessageTextMaxRows = 32; |
| 29 | const int kMessageTextMaxCols = 132; |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 30 | const int kDefaultPromptMaxRows = 24; |
| 31 | const int kDefaultPromptMaxCols = 132; |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 32 | void EnforceMaxTextSize(const base::string16& in_string, |
| 33 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 34 | gfx::ElideRectangleString(in_string, kMessageTextMaxRows, |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 35 | kMessageTextMaxCols, false, out_string); |
| 36 | } |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 37 | void EnforceMaxPromptSize(const base::string16& in_string, |
| 38 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 39 | gfx::ElideRectangleString(in_string, kDefaultPromptMaxRows, |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 40 | kDefaultPromptMaxCols, false, out_string); |
| 41 | } |
| 42 | #else |
| 43 | // One-dimensional eliding. Trust the window system to break the string |
| 44 | // appropriately, but limit its overall length to something reasonable. |
thestig | bbf93ac | 2016-02-02 00:24:49 | [diff] [blame] | 45 | const size_t kMessageTextMaxSize = 2000; |
| 46 | const size_t kDefaultPromptMaxSize = 2000; |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 47 | void EnforceMaxTextSize(const base::string16& in_string, |
| 48 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 49 | gfx::ElideString(in_string, kMessageTextMaxSize, out_string); |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 50 | } |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 51 | void EnforceMaxPromptSize(const base::string16& in_string, |
| 52 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 53 | gfx::ElideString(in_string, kDefaultPromptMaxSize, out_string); |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 54 | } |
| 55 | #endif |
[email protected] | 999adfd6 | 2010-06-02 19:42:42 | [diff] [blame] | 56 | |
| 57 | } // namespace |
| 58 | |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 59 | ChromeJavaScriptDialogExtraData::ChromeJavaScriptDialogExtraData() |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 60 | : has_already_shown_a_dialog_(false), |
joenotcharles | 505f421 | 2016-02-11 19:28:53 | [diff] [blame] | 61 | suppress_javascript_messages_(false), |
| 62 | suppressed_dialog_count_(0) {} |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 63 | |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 64 | JavaScriptAppModalDialog::JavaScriptAppModalDialog( |
jochen | a2afdec | 2015-01-23 13:09:22 | [diff] [blame] | 65 | content::WebContents* web_contents, |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 66 | ExtraDataMap* extra_data_map, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 67 | const base::string16& title, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 68 | content::JavaScriptDialogType javascript_dialog_type, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 69 | const base::string16& message_text, |
| 70 | const base::string16& default_prompt_text, |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 71 | bool display_suppress_checkbox, |
| 72 | bool is_before_unload_dialog, |
[email protected] | 3b3301f6 | 2012-02-29 04:32:32 | [diff] [blame] | 73 | bool is_reload, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 74 | content::JavaScriptDialogManager::DialogClosedCallback callback) |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 75 | : title_(title), |
| 76 | completed_(false), |
| 77 | valid_(true), |
| 78 | native_dialog_(nullptr), |
| 79 | web_contents_(web_contents), |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 80 | extra_data_map_(extra_data_map), |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 81 | javascript_dialog_type_(javascript_dialog_type), |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 82 | display_suppress_checkbox_(display_suppress_checkbox), |
| 83 | is_before_unload_dialog_(is_before_unload_dialog), |
[email protected] | 3b3301f6 | 2012-02-29 04:32:32 | [diff] [blame] | 84 | is_reload_(is_reload), |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 85 | callback_(std::move(callback)), |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 86 | use_override_prompt_text_(false), |
| 87 | creation_time_(base::TimeTicks::Now()) { |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 88 | EnforceMaxTextSize(message_text, &message_text_); |
| 89 | EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 93 | CompleteDialog(); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 94 | } |
| 95 | |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 96 | void JavaScriptAppModalDialog::ShowModalDialog() { |
| 97 | native_dialog_ = JavaScriptDialogManager::GetInstance() |
| 98 | ->native_dialog_factory() |
| 99 | ->CreateNativeJavaScriptDialog(this); |
| 100 | native_dialog_->ShowAppModalDialog(); |
| 101 | if (app_modal_dialog_observer) |
| 102 | app_modal_dialog_observer->Notify(this); |
[email protected] | 160ad3d | 2010-09-28 15:40:20 | [diff] [blame] | 103 | } |
| 104 | |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 105 | void JavaScriptAppModalDialog::ActivateModalDialog() { |
| 106 | DCHECK(native_dialog_); |
| 107 | native_dialog_->ActivateAppModalDialog(); |
| 108 | } |
| 109 | |
| 110 | void JavaScriptAppModalDialog::CloseModalDialog() { |
| 111 | DCHECK(native_dialog_); |
| 112 | native_dialog_->CloseAppModalDialog(); |
| 113 | } |
| 114 | |
| 115 | void JavaScriptAppModalDialog::CompleteDialog() { |
| 116 | if (!completed_) { |
| 117 | completed_ = true; |
| 118 | AppModalDialogQueue::GetInstance()->ShowNextDialog(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | bool JavaScriptAppModalDialog::IsValid() { |
| 123 | return valid_; |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 124 | } |
| 125 | |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 126 | void JavaScriptAppModalDialog::Invalidate() { |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 127 | if (!valid_) |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 128 | return; |
| 129 | |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 130 | valid_ = false; |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 131 | CallDialogClosedCallback(false, base::string16()); |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 132 | if (native_dialog_) |
[email protected] | 66711f6f | 2010-06-23 01:17:35 | [diff] [blame] | 133 | CloseModalDialog(); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 134 | } |
| 135 | |
[email protected] | ab96d31 | 2010-10-14 13:38:51 | [diff] [blame] | 136 | void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 137 | // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
| 138 | // will receive its activation messages before this dialog receives |
| 139 | // WM_DESTROY. The parent frame would then try to activate any modal dialogs |
| 140 | // that were still open in the ModalDialogQueue, which would send activation |
| 141 | // back to this one. The framework should be improved to handle this, so this |
| 142 | // is a temporary workaround. |
| 143 | CompleteDialog(); |
| 144 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 145 | NotifyDelegate(false, base::string16(), suppress_js_messages); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 148 | void JavaScriptAppModalDialog::OnAccept(const base::string16& prompt_text, |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 149 | bool suppress_js_messages) { |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 150 | base::string16 prompt_text_to_use = prompt_text; |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 151 | // This is only for testing. |
| 152 | if (use_override_prompt_text_) |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 153 | prompt_text_to_use = override_prompt_text_; |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 154 | |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 155 | CompleteDialog(); |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 156 | NotifyDelegate(true, prompt_text_to_use, suppress_js_messages); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 157 | } |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 158 | |
| 159 | void JavaScriptAppModalDialog::OnClose() { |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 160 | NotifyDelegate(false, base::string16(), false); |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 163 | void JavaScriptAppModalDialog::SetOverridePromptText( |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 164 | const base::string16& override_prompt_text) { |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 165 | override_prompt_text_ = override_prompt_text; |
| 166 | use_override_prompt_text_ = true; |
| 167 | } |
| 168 | |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 169 | void JavaScriptAppModalDialog::NotifyDelegate(bool success, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 170 | const base::string16& user_input, |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 171 | bool suppress_js_messages) { |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 172 | if (!valid_) |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 173 | return; |
| 174 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 175 | CallDialogClosedCallback(success, user_input); |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 176 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 177 | // The close callback above may delete web_contents_, thus removing the extra |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 178 | // data from the map owned by ::JavaScriptDialogManager. Make sure |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 179 | // to only use the data if still present. http://crbug.com/236476 |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 180 | ExtraDataMap::iterator extra_data = extra_data_map_->find(web_contents_); |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 181 | if (extra_data != extra_data_map_->end()) { |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 182 | extra_data->second.has_already_shown_a_dialog_ = true; |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 183 | extra_data->second.suppress_javascript_messages_ = suppress_js_messages; |
| 184 | } |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 185 | |
| 186 | // On Views, we can end up coming through this code path twice :(. |
| 187 | // See crbug.com/63732. |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 188 | valid_ = false; |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 189 | } |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 190 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 191 | void JavaScriptAppModalDialog::CallDialogClosedCallback(bool success, |
| 192 | const base::string16& user_input) { |
| 193 | // TODO(joenotcharles): Both the callers of this function also check IsValid |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 194 | // and call Invalidate, but in different orders. If the difference is not |
| 195 | // significant, more common code could be moved here. |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 196 | UMA_HISTOGRAM_MEDIUM_TIMES( |
| 197 | "JSDialogs.FineTiming.TimeBetweenDialogCreatedAndSameDialogClosed", |
| 198 | base::TimeTicks::Now() - creation_time_); |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 199 | if (!callback_.is_null()) |
| 200 | std::move(callback_).Run(success, user_input); |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 201 | } |
| 202 | |
avi | 373e72a | 2017-05-26 20:33:52 | [diff] [blame] | 203 | AppModalDialogObserver::AppModalDialogObserver() { |
| 204 | DCHECK(!app_modal_dialog_observer); |
| 205 | app_modal_dialog_observer = this; |
| 206 | } |
| 207 | |
| 208 | AppModalDialogObserver::~AppModalDialogObserver() { |
| 209 | DCHECK(app_modal_dialog_observer); |
| 210 | app_modal_dialog_observer = nullptr; |
| 211 | } |
| 212 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 213 | } // namespace app_modal |