blob: 083f17b21143ba1f787a3bb786817c86a9d3c68b [file] [log] [blame]
[email protected]fedec012012-01-28 03:09:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]12f74a92010-02-05 22:32:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
oshimaf65398422014-11-18 23:30:425#include "components/app_modal/javascript_app_modal_dialog.h"
[email protected]12f74a92010-02-05 22:32:146
oshima0929be2a2014-11-19 22:21:037#include "components/app_modal/javascript_dialog_manager.h"
oshimaf65398422014-11-18 23:30:428#include "components/app_modal/javascript_native_dialog_factory.h"
[email protected]dbb97ba2013-09-09 22:15:259#include "ui/gfx/text_elider.h"
[email protected]12f74a92010-02-05 22:32:1410
[email protected]51da7e32012-01-30 19:24:5211
oshima0929be2a2014-11-19 22:21:0312namespace app_modal {
[email protected]999adfd62010-06-02 19:42:4213namespace {
14
[email protected]416126c52011-03-29 16:28:5715// Control maximum sizes of various texts passed to us from javascript.
[email protected]7be64502011-05-03 17:51:4716#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]416126c52011-03-29 16:28:5717// Two-dimensional eliding. Reformat the text of the message dialog
18// inserting line breaks because otherwise a single long line can overflow
19// the message dialog (and crash/hang the GTK, depending on the version).
[email protected]af4b9df2010-12-22 21:19:0220const int kMessageTextMaxRows = 32;
21const int kMessageTextMaxCols = 132;
[email protected]416126c52011-03-29 16:28:5722const int kDefaultPromptMaxRows = 24;
23const int kDefaultPromptMaxCols = 132;
[email protected]dcd0249872013-12-06 23:58:4524void EnforceMaxTextSize(const base::string16& in_string,
25 base::string16* out_string) {
[email protected]dbb97ba2013-09-09 22:15:2526 gfx::ElideRectangleString(in_string, kMessageTextMaxRows,
[email protected]416126c52011-03-29 16:28:5727 kMessageTextMaxCols, false, out_string);
28}
[email protected]dcd0249872013-12-06 23:58:4529void EnforceMaxPromptSize(const base::string16& in_string,
30 base::string16* out_string) {
[email protected]dbb97ba2013-09-09 22:15:2531 gfx::ElideRectangleString(in_string, kDefaultPromptMaxRows,
[email protected]416126c52011-03-29 16:28:5732 kDefaultPromptMaxCols, false, out_string);
33}
34#else
35// One-dimensional eliding. Trust the window system to break the string
36// appropriately, but limit its overall length to something reasonable.
37const int kMessageTextMaxSize = 3000;
38const int kDefaultPromptMaxSize = 2000;
[email protected]dcd0249872013-12-06 23:58:4539void EnforceMaxTextSize(const base::string16& in_string,
40 base::string16* out_string) {
[email protected]dbb97ba2013-09-09 22:15:2541 gfx::ElideString(in_string, kMessageTextMaxSize, out_string);
[email protected]416126c52011-03-29 16:28:5742}
[email protected]dcd0249872013-12-06 23:58:4543void EnforceMaxPromptSize(const base::string16& in_string,
44 base::string16* out_string) {
[email protected]dbb97ba2013-09-09 22:15:2545 gfx::ElideString(in_string, kDefaultPromptMaxSize, out_string);
[email protected]416126c52011-03-29 16:28:5746}
47#endif
[email protected]999adfd62010-06-02 19:42:4248
49} // namespace
50
[email protected]3ab9cb82011-06-03 18:02:0751ChromeJavaScriptDialogExtraData::ChromeJavaScriptDialogExtraData()
52 : suppress_javascript_messages_(false) {
53}
54
[email protected]12f74a92010-02-05 22:32:1455JavaScriptAppModalDialog::JavaScriptAppModalDialog(
jochena2afdec2015-01-23 13:09:2256 content::WebContents* web_contents,
[email protected]1f422a7c2013-05-15 17:06:4157 ExtraDataMap* extra_data_map,
[email protected]dcd0249872013-12-06 23:58:4558 const base::string16& title,
[email protected]be2510c02012-05-28 14:52:1459 content::JavaScriptMessageType javascript_message_type,
[email protected]dcd0249872013-12-06 23:58:4560 const base::string16& message_text,
61 const base::string16& default_prompt_text,
[email protected]12f74a92010-02-05 22:32:1462 bool display_suppress_checkbox,
63 bool is_before_unload_dialog,
[email protected]3b3301f62012-02-29 04:32:3264 bool is_reload,
oshima0929be2a2014-11-19 22:21:0365 const content::JavaScriptDialogManager::DialogClosedCallback& callback)
[email protected]51da7e32012-01-30 19:24:5266 : AppModalDialog(web_contents, title),
[email protected]1f422a7c2013-05-15 17:06:4167 extra_data_map_(extra_data_map),
[email protected]269f86d2011-12-07 02:43:4768 javascript_message_type_(javascript_message_type),
[email protected]12f74a92010-02-05 22:32:1469 display_suppress_checkbox_(display_suppress_checkbox),
70 is_before_unload_dialog_(is_before_unload_dialog),
[email protected]3b3301f62012-02-29 04:32:3271 is_reload_(is_reload),
[email protected]51da7e32012-01-30 19:24:5272 callback_(callback),
[email protected]16623742011-05-16 20:04:2673 use_override_prompt_text_(false) {
[email protected]3ab9cb82011-06-03 18:02:0774 EnforceMaxTextSize(message_text, &message_text_);
75 EnforceMaxPromptSize(default_prompt_text, &default_prompt_text_);
[email protected]12f74a92010-02-05 22:32:1476}
77
78JavaScriptAppModalDialog::~JavaScriptAppModalDialog() {
79}
80
[email protected]160ad3d2010-09-28 15:40:2081NativeAppModalDialog* JavaScriptAppModalDialog::CreateNativeDialog() {
jochena2afdec2015-01-23 13:09:2282 return JavaScriptDialogManager::GetInstance()
83 ->native_dialog_factory()
84 ->CreateNativeJavaScriptDialog(this);
[email protected]160ad3d2010-09-28 15:40:2085}
86
[email protected]16623742011-05-16 20:04:2687bool JavaScriptAppModalDialog::IsJavaScriptModalDialog() {
88 return true;
89}
90
[email protected]a1e97f02011-06-30 14:04:3491void JavaScriptAppModalDialog::Invalidate() {
[email protected]1f422a7c2013-05-15 17:06:4192 if (!IsValid())
[email protected]12f74a92010-02-05 22:32:1493 return;
94
[email protected]1f422a7c2013-05-15 17:06:4195 AppModalDialog::Invalidate();
[email protected]af905902013-10-01 21:38:5196 if (!callback_.is_null()) {
[email protected]dcd0249872013-12-06 23:58:4597 callback_.Run(false, base::string16());
[email protected]af905902013-10-01 21:38:5198 callback_.Reset();
99 }
[email protected]1f422a7c2013-05-15 17:06:41100 if (native_dialog())
[email protected]66711f6f2010-06-23 01:17:35101 CloseModalDialog();
[email protected]12f74a92010-02-05 22:32:14102}
103
[email protected]ab96d312010-10-14 13:38:51104void JavaScriptAppModalDialog::OnCancel(bool suppress_js_messages) {
[email protected]12f74a92010-02-05 22:32:14105 // We need to do this before WM_DESTROY (WindowClosing()) as any parent frame
106 // will receive its activation messages before this dialog receives
107 // WM_DESTROY. The parent frame would then try to activate any modal dialogs
108 // that were still open in the ModalDialogQueue, which would send activation
109 // back to this one. The framework should be improved to handle this, so this
110 // is a temporary workaround.
111 CompleteDialog();
112
[email protected]dcd0249872013-12-06 23:58:45113 NotifyDelegate(false, base::string16(), suppress_js_messages);
[email protected]12f74a92010-02-05 22:32:14114}
115
[email protected]dcd0249872013-12-06 23:58:45116void JavaScriptAppModalDialog::OnAccept(const base::string16& prompt_text,
[email protected]12f74a92010-02-05 22:32:14117 bool suppress_js_messages) {
[email protected]dcd0249872013-12-06 23:58:45118 base::string16 prompt_text_to_use = prompt_text;
[email protected]16623742011-05-16 20:04:26119 // This is only for testing.
120 if (use_override_prompt_text_)
[email protected]3ab9cb82011-06-03 18:02:07121 prompt_text_to_use = override_prompt_text_;
[email protected]16623742011-05-16 20:04:26122
[email protected]12f74a92010-02-05 22:32:14123 CompleteDialog();
[email protected]16623742011-05-16 20:04:26124 NotifyDelegate(true, prompt_text_to_use, suppress_js_messages);
[email protected]12f74a92010-02-05 22:32:14125}
[email protected]7d784302010-04-09 21:41:05126
127void JavaScriptAppModalDialog::OnClose() {
[email protected]dcd0249872013-12-06 23:58:45128 NotifyDelegate(false, base::string16(), false);
[email protected]7d784302010-04-09 21:41:05129}
130
[email protected]16623742011-05-16 20:04:26131void JavaScriptAppModalDialog::SetOverridePromptText(
[email protected]dcd0249872013-12-06 23:58:45132 const base::string16& override_prompt_text) {
[email protected]16623742011-05-16 20:04:26133 override_prompt_text_ = override_prompt_text;
134 use_override_prompt_text_ = true;
135}
136
[email protected]594d0622010-12-07 05:33:07137void JavaScriptAppModalDialog::NotifyDelegate(bool success,
[email protected]dcd0249872013-12-06 23:58:45138 const base::string16& user_input,
[email protected]594d0622010-12-07 05:33:07139 bool suppress_js_messages) {
[email protected]1f422a7c2013-05-15 17:06:41140 if (!IsValid())
[email protected]594d0622010-12-07 05:33:07141 return;
142
[email protected]af905902013-10-01 21:38:51143 if (!callback_.is_null()) {
144 callback_.Run(success, user_input);
145 callback_.Reset();
146 }
[email protected]3ab9cb82011-06-03 18:02:07147
[email protected]1f422a7c2013-05-15 17:06:41148 // The callback_ above may delete web_contents_, thus removing the extra
oshima0929be2a2014-11-19 22:21:03149 // data from the map owned by ::JavaScriptDialogManager. Make sure
[email protected]1f422a7c2013-05-15 17:06:41150 // to only use the data if still present. http://crbug.com/236476
151 ExtraDataMap::iterator extra_data = extra_data_map_->find(web_contents());
152 if (extra_data != extra_data_map_->end()) {
153 extra_data->second.last_javascript_message_dismissal_ =
154 base::TimeTicks::Now();
155 extra_data->second.suppress_javascript_messages_ = suppress_js_messages;
156 }
[email protected]594d0622010-12-07 05:33:07157
158 // On Views, we can end up coming through this code path twice :(.
159 // See crbug.com/63732.
[email protected]1f422a7c2013-05-15 17:06:41160 AppModalDialog::Invalidate();
[email protected]7d784302010-04-09 21:41:05161}
oshima0929be2a2014-11-19 22:21:03162
163} // namespace app_modal