1 // Copyright 2014 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.
5 #include "components/app_modal/javascript_dialog_manager.h"
8 #include "base/i18n/rtl.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/app_modal/app_modal_dialog.h"
11 #include "components/app_modal/app_modal_dialog_queue.h"
12 #include "components/app_modal/javascript_dialog_extensions_client.h"
13 #include "components/app_modal/javascript_native_dialog_factory.h"
14 #include "components/app_modal/native_app_modal_dialog.h"
15 #include "content/public/common/javascript_message_type.h"
16 #include "grit/components_strings.h"
17 #include "net/base/net_util.h"
18 #include "ui/base/l10n/l10n_util.h"
23 class DefaultExtensionsClient
: public JavaScriptDialogExtensionsClient
{
25 DefaultExtensionsClient() {}
26 ~DefaultExtensionsClient() override
{}
29 // JavaScriptDialogExtensionsClient:
30 void OnDialogOpened(content::WebContents
* web_contents
) override
{}
31 void OnDialogClosed(content::WebContents
* web_contents
) override
{}
32 bool GetExtensionName(content::WebContents
* web_contents
,
33 const GURL
& origin_url
,
34 std::string
* name_out
) override
{
38 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient
);
43 ////////////////////////////////////////////////////////////////////////////////
44 // JavaScriptDialogManager, public:
47 JavaScriptDialogManager
* JavaScriptDialogManager::GetInstance() {
48 return Singleton
<JavaScriptDialogManager
>::get();
51 void JavaScriptDialogManager::SetNativeDialogFactory(
52 scoped_ptr
<JavaScriptNativeDialogFactory
> factory
) {
53 native_dialog_factory_
= factory
.Pass();
56 void JavaScriptDialogManager::SetExtensionsClient(
57 scoped_ptr
<JavaScriptDialogExtensionsClient
> extensions_client
) {
58 extensions_client_
= extensions_client
.Pass();
61 ////////////////////////////////////////////////////////////////////////////////
62 // JavaScriptDialogManager, private:
64 JavaScriptDialogManager::JavaScriptDialogManager()
65 : extensions_client_(new DefaultExtensionsClient
) {
68 JavaScriptDialogManager::~JavaScriptDialogManager() {
71 void JavaScriptDialogManager::RunJavaScriptDialog(
72 content::WebContents
* web_contents
,
73 const GURL
& origin_url
,
74 const std::string
& accept_lang
,
75 content::JavaScriptMessageType message_type
,
76 const base::string16
& message_text
,
77 const base::string16
& default_prompt_text
,
78 const DialogClosedCallback
& callback
,
79 bool* did_suppress_message
) {
80 *did_suppress_message
= false;
82 ChromeJavaScriptDialogExtraData
* extra_data
=
83 &javascript_dialog_extra_data_
[web_contents
];
85 if (extra_data
->suppress_javascript_messages_
) {
86 *did_suppress_message
= true;
90 base::TimeDelta time_since_last_message
= base::TimeTicks::Now() -
91 extra_data
->last_javascript_message_dismissal_
;
92 bool display_suppress_checkbox
= false;
93 // If a WebContents is impolite and displays a second JavaScript
94 // alert within kJavaScriptMessageExpectedDelay of a previous
95 // JavaScript alert being dismissed, show a checkbox offering to
96 // suppress future alerts from this WebContents.
97 const int kJavaScriptMessageExpectedDelay
= 1000;
99 if (time_since_last_message
<
100 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay
)) {
101 display_suppress_checkbox
= true;
103 display_suppress_checkbox
= false;
106 bool is_alert
= message_type
== content::JAVASCRIPT_MESSAGE_TYPE_ALERT
;
107 base::string16 dialog_title
=
108 GetTitle(web_contents
, origin_url
, accept_lang
, is_alert
);
110 extensions_client_
->OnDialogOpened(web_contents
);
112 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
114 &javascript_dialog_extra_data_
,
119 display_suppress_checkbox
,
120 false, // is_before_unload_dialog
122 base::Bind(&JavaScriptDialogManager::OnDialogClosed
,
123 base::Unretained(this), web_contents
, callback
)));
126 void JavaScriptDialogManager::RunBeforeUnloadDialog(
127 content::WebContents
* web_contents
,
128 const base::string16
& message_text
,
130 const DialogClosedCallback
& callback
) {
131 const base::string16 title
= l10n_util::GetStringUTF16(is_reload
?
132 IDS_BEFORERELOAD_MESSAGEBOX_TITLE
: IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE
);
133 const base::string16 footer
= l10n_util::GetStringUTF16(is_reload
?
134 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER
: IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER
);
136 base::string16 full_message
=
137 message_text
+ base::ASCIIToUTF16("\n\n") + footer
;
139 extensions_client_
->OnDialogOpened(web_contents
);
141 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
143 &javascript_dialog_extra_data_
,
145 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM
,
147 base::string16(), // default_prompt_text
148 false, // display_suppress_checkbox
149 true, // is_before_unload_dialog
151 base::Bind(&JavaScriptDialogManager::OnDialogClosed
,
152 base::Unretained(this), web_contents
, callback
)));
155 bool JavaScriptDialogManager::HandleJavaScriptDialog(
156 content::WebContents
* web_contents
,
158 const base::string16
* prompt_override
) {
159 AppModalDialogQueue
* dialog_queue
= AppModalDialogQueue::GetInstance();
160 if (!dialog_queue
->HasActiveDialog() ||
161 !dialog_queue
->active_dialog()->IsJavaScriptModalDialog() ||
162 dialog_queue
->active_dialog()->web_contents() != web_contents
) {
165 JavaScriptAppModalDialog
* dialog
= static_cast<JavaScriptAppModalDialog
*>(
166 dialog_queue
->active_dialog());
169 dialog
->SetOverridePromptText(*prompt_override
);
170 dialog
->native_dialog()->AcceptAppModalDialog();
172 dialog
->native_dialog()->CancelAppModalDialog();
177 void JavaScriptDialogManager::WebContentsDestroyed(
178 content::WebContents
* web_contents
) {
179 CancelActiveAndPendingDialogs(web_contents
);
180 javascript_dialog_extra_data_
.erase(web_contents
);
183 base::string16
JavaScriptDialogManager::GetTitle(
184 content::WebContents
* web_contents
,
185 const GURL
& origin_url
,
186 const std::string
& accept_lang
,
188 // If the URL hasn't any host, return the default string.
189 if (!origin_url
.has_host()) {
190 return l10n_util::GetStringUTF16(
191 is_alert
? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
192 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE
);
195 // For extensions, show the extension name, but only if the origin of
196 // the alert matches the top-level WebContents.
198 if (extensions_client_
->GetExtensionName(web_contents
, origin_url
, &name
))
199 return base::UTF8ToUTF16(name
);
201 // Otherwise, return the formatted URL.
202 // In this case, force URL to have LTR directionality.
203 base::string16 url_string
= net::FormatUrl(origin_url
, accept_lang
);
204 return l10n_util::GetStringFUTF16(
205 is_alert
? IDS_JAVASCRIPT_ALERT_TITLE
206 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE
,
207 base::i18n::GetDisplayStringInLTRDirectionality(url_string
));
210 void JavaScriptDialogManager::CancelActiveAndPendingDialogs(
211 content::WebContents
* web_contents
) {
212 AppModalDialogQueue
* queue
= AppModalDialogQueue::GetInstance();
213 AppModalDialog
* active_dialog
= queue
->active_dialog();
214 if (active_dialog
&& active_dialog
->web_contents() == web_contents
)
215 active_dialog
->Invalidate();
216 for (AppModalDialogQueue::iterator i
= queue
->begin();
217 i
!= queue
->end(); ++i
) {
218 if ((*i
)->web_contents() == web_contents
)
223 void JavaScriptDialogManager::OnDialogClosed(
224 content::WebContents
* web_contents
,
225 DialogClosedCallback callback
,
227 const base::string16
& user_input
) {
228 // If an extension opened this dialog then the extension may shut down its
229 // lazy background page after the dialog closes. (Dialogs are closed before
230 // their WebContents is destroyed so |web_contents| is still valid here.)
231 extensions_client_
->OnDialogClosed(web_contents
);
233 callback
.Run(success
, user_input
);
236 } // namespace app_modal