[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 | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 7 | #include "build/build_config.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 8 | #include "components/app_modal/javascript_dialog_manager.h" |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 9 | #include "components/app_modal/javascript_native_dialog_factory.h" |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 10 | #include "content/public/browser/web_contents.h" |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 11 | #include "ui/gfx/text_elider.h" |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 12 | #include "url/origin.h" |
[email protected] | 51da7e3 | 2012-01-30 19:24:52 | [diff] [blame] | 13 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 14 | namespace app_modal { |
[email protected] | 999adfd6 | 2010-06-02 19:42:42 | [diff] [blame] | 15 | namespace { |
| 16 | |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 17 | // Control maximum sizes of various texts passed to us from javascript. |
[email protected] | 7be6450 | 2011-05-03 17:51:47 | [diff] [blame] | 18 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 19 | // Two-dimensional eliding. Reformat the text of the message dialog |
| 20 | // inserting line breaks because otherwise a single long line can overflow |
| 21 | // the message dialog (and crash/hang the GTK, depending on the version). |
[email protected] | af4b9df | 2010-12-22 21:19:02 | [diff] [blame] | 22 | const int kMessageTextMaxRows = 32; |
| 23 | const int kMessageTextMaxCols = 132; |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 24 | const int kDefaultPromptMaxRows = 24; |
| 25 | const int kDefaultPromptMaxCols = 132; |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 26 | void EnforceMaxTextSize(const base::string16& in_string, |
| 27 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 28 | gfx::ElideRectangleString(in_string, kMessageTextMaxRows, |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 29 | kMessageTextMaxCols, false, out_string); |
| 30 | } |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 31 | void EnforceMaxPromptSize(const base::string16& in_string, |
| 32 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 33 | gfx::ElideRectangleString(in_string, kDefaultPromptMaxRows, |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 34 | kDefaultPromptMaxCols, false, out_string); |
| 35 | } |
| 36 | #else |
| 37 | // One-dimensional eliding. Trust the window system to break the string |
| 38 | // appropriately, but limit its overall length to something reasonable. |
thestig | bbf93ac | 2016-02-02 00:24:49 | [diff] [blame^] | 39 | const size_t kMessageTextMaxSize = 2000; |
| 40 | const size_t kDefaultPromptMaxSize = 2000; |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 41 | void EnforceMaxTextSize(const base::string16& in_string, |
| 42 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 43 | gfx::ElideString(in_string, kMessageTextMaxSize, out_string); |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 44 | } |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 45 | void EnforceMaxPromptSize(const base::string16& in_string, |
| 46 | base::string16* out_string) { |
[email protected] | dbb97ba | 2013-09-09 22:15:25 | [diff] [blame] | 47 | gfx::ElideString(in_string, kDefaultPromptMaxSize, out_string); |
[email protected] | 416126c5 | 2011-03-29 16:28:57 | [diff] [blame] | 48 | } |
| 49 | #endif |
[email protected] | 999adfd6 | 2010-06-02 19:42:42 | [diff] [blame] | 50 | |
| 51 | } // namespace |
| 52 | |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 53 | ChromeJavaScriptDialogExtraData::ChromeJavaScriptDialogExtraData() |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 54 | : has_already_shown_a_dialog_(false), |
| 55 | suppress_javascript_messages_(false) {} |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 56 | |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 57 | JavaScriptAppModalDialog::JavaScriptAppModalDialog( |
jochen | a2afdec | 2015-01-23 13:09:22 | [diff] [blame] | 58 | content::WebContents* web_contents, |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 59 | ExtraDataMap* extra_data_map, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 60 | const base::string16& title, |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 61 | content::JavaScriptMessageType javascript_message_type, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 62 | const base::string16& message_text, |
| 63 | const base::string16& default_prompt_text, |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 64 | bool display_suppress_checkbox, |
| 65 | bool is_before_unload_dialog, |
[email protected] | 3b3301f6 | 2012-02-29 04:32:32 | [diff] [blame] | 66 | bool is_reload, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 67 | const content::JavaScriptDialogManager::DialogClosedCallback& callback) |
[email protected] | 51da7e3 | 2012-01-30 19:24:52 | [diff] [blame] | 68 | : AppModalDialog(web_contents, title), |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 69 | extra_data_map_(extra_data_map), |
[email protected] | 269f86d | 2011-12-07 02:43:47 | [diff] [blame] | 70 | javascript_message_type_(javascript_message_type), |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 71 | display_suppress_checkbox_(display_suppress_checkbox), |
| 72 | is_before_unload_dialog_(is_before_unload_dialog), |
[email protected] | 3b3301f6 | 2012-02-29 04:32:32 | [diff] [blame] | 73 | is_reload_(is_reload), |
[email protected] | 51da7e3 | 2012-01-30 19:24:52 | [diff] [blame] | 74 | callback_(callback), |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 75 | use_override_prompt_text_(false) { |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 76 | EnforceMaxTextSize(message_text, &message_text_); |
| 77 | EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | JavaScriptAppModalDialog::~JavaScriptAppModalDialog() { |
| 81 | } |
| 82 | |
[email protected] | 160ad3d | 2010-09-28 15:40:20 | [diff] [blame] | 83 | NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() { |
jochen | a2afdec | 2015-01-23 13:09:22 | [diff] [blame] | 84 | return JavaScriptDialogManager::GetInstance() |
| 85 | ->native_dialog_factory() |
| 86 | ->CreateNativeJavaScriptDialog(this); |
[email protected] | 160ad3d | 2010-09-28 15:40:20 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 89 | bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() { |
| 90 | return true; |
| 91 | } |
| 92 | |
[email protected] | a1e97f0 | 2011-06-30 14:04:34 | [diff] [blame] | 93 | void JavaScriptAppModalDialog::Invalidate() { |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 94 | if (!IsValid()) |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 95 | return; |
| 96 | |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 97 | AppModalDialog::Invalidate(); |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 98 | if (!callback_.is_null()) { |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 99 | callback_.Run(false, base::string16()); |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 100 | callback_.Reset(); |
| 101 | } |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 102 | if (native_dialog()) |
[email protected] | 66711f6f | 2010-06-23 01:17:35 | [diff] [blame] | 103 | CloseModalDialog(); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | ab96d31 | 2010-10-14 13:38:51 | [diff] [blame] | 106 | void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) { |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 107 | // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame |
| 108 | // will receive its activation messages before this dialog receives |
| 109 | // WM_DESTROY. The parent frame would then try to activate any modal dialogs |
| 110 | // that were still open in the ModalDialogQueue, which would send activation |
| 111 | // back to this one. The framework should be improved to handle this, so this |
| 112 | // is a temporary workaround. |
| 113 | CompleteDialog(); |
| 114 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 115 | NotifyDelegate(false, base::string16(), suppress_js_messages); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 118 | void JavaScriptAppModalDialog::OnAccept(const base::string16& prompt_text, |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 119 | bool suppress_js_messages) { |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 120 | base::string16 prompt_text_to_use = prompt_text; |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 121 | // This is only for testing. |
| 122 | if (use_override_prompt_text_) |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 123 | prompt_text_to_use = override_prompt_text_; |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 124 | |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 125 | CompleteDialog(); |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 126 | NotifyDelegate(true, prompt_text_to_use, suppress_js_messages); |
[email protected] | 12f74a9 | 2010-02-05 22:32:14 | [diff] [blame] | 127 | } |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 128 | |
| 129 | void JavaScriptAppModalDialog::OnClose() { |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 130 | NotifyDelegate(false, base::string16(), false); |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 133 | void JavaScriptAppModalDialog::SetOverridePromptText( |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 134 | const base::string16& override_prompt_text) { |
[email protected] | 1662374 | 2011-05-16 20:04:26 | [diff] [blame] | 135 | override_prompt_text_ = override_prompt_text; |
| 136 | use_override_prompt_text_ = true; |
| 137 | } |
| 138 | |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 139 | void JavaScriptAppModalDialog::NotifyDelegate(bool success, |
[email protected] | dcd024987 | 2013-12-06 23:58:45 | [diff] [blame] | 140 | const base::string16& user_input, |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 141 | bool suppress_js_messages) { |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 142 | if (!IsValid()) |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 143 | return; |
| 144 | |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 145 | if (!callback_.is_null()) { |
| 146 | callback_.Run(success, user_input); |
| 147 | callback_.Reset(); |
| 148 | } |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 149 | |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 150 | // The callback_ above may delete web_contents_, thus removing the extra |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 151 | // data from the map owned by ::JavaScriptDialogManager. Make sure |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 152 | // to only use the data if still present. http://crbug.com/236476 |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 153 | ExtraDataMap::iterator extra_data = |
| 154 | extra_data_map_->find(GetSerializedOriginForWebContents(web_contents())); |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 155 | if (extra_data != extra_data_map_->end()) { |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 156 | extra_data->second.has_already_shown_a_dialog_ = true; |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 157 | extra_data->second.suppress_javascript_messages_ = suppress_js_messages; |
| 158 | } |
[email protected] | 594d062 | 2010-12-07 05:33:07 | [diff] [blame] | 159 | |
| 160 | // On Views, we can end up coming through this code path twice :(. |
| 161 | // See crbug.com/63732. |
[email protected] | 1f422a7c | 2013-05-15 17:06:41 | [diff] [blame] | 162 | AppModalDialog::Invalidate(); |
[email protected] | 7d78430 | 2010-04-09 21:41:05 | [diff] [blame] | 163 | } |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 164 | |
palmer | d8b2ff0 | 2015-08-18 00:24:59 | [diff] [blame] | 165 | // static |
| 166 | std::string JavaScriptAppModalDialog::GetSerializedOriginForWebContents( |
| 167 | content::WebContents* contents) { |
| 168 | if (!contents) |
| 169 | return url::Origin().Serialize(); |
| 170 | return url::Origin(contents->GetLastCommittedURL()).Serialize(); |
| 171 | } |
| 172 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 173 | } // namespace app_modal |