1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// Copyright 2014 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 "components/app_modal/javascript_dialog_manager.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/i18n/rtl.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "components/app_modal/app_modal_dialog.h"
#include "components/app_modal/app_modal_dialog_queue.h"
#include "components/app_modal/javascript_dialog_extensions_client.h"
#include "components/app_modal/javascript_native_dialog_factory.h"
#include "components/app_modal/native_app_modal_dialog.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/javascript_message_type.h"
#include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/font_list.h"
namespace app_modal {
namespace {
#if !defined(OS_ANDROID)
// Keep in sync with kDefaultMessageWidth, but allow some space for the rest of
// the text.
const int kUrlElideWidth = 350;
#endif
class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient {
public:
DefaultExtensionsClient() {}
~DefaultExtensionsClient() override {}
private:
// JavaScriptDialogExtensionsClient:
void OnDialogOpened(content::WebContents* web_contents) override {}
void OnDialogClosed(content::WebContents* web_contents) override {}
bool GetExtensionName(content::WebContents* web_contents,
const GURL& origin_url,
std::string* name_out) override {
return false;
}
DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient);
};
bool ShouldDisplaySuppressCheckbox(
ChromeJavaScriptDialogExtraData* extra_data) {
return extra_data->has_already_shown_a_dialog_;
}
void LogUMAMessageLengthStats(const base::string16& message) {
UMA_HISTOGRAM_COUNTS("JSDialogs.CountOfJSDialogMessageCharacters",
static_cast<int32_t>(message.length()));
int32_t newline_count =
std::count_if(message.begin(), message.end(),
[](const base::char16& c) { return c == '\n'; });
UMA_HISTOGRAM_COUNTS("JSDialogs.CountOfJSDialogMessageNewlines",
newline_count);
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// JavaScriptDialogManager, public:
// static
JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() {
return base::Singleton<JavaScriptDialogManager>::get();
}
void JavaScriptDialogManager::SetNativeDialogFactory(
scoped_ptr<JavaScriptNativeDialogFactory> factory) {
native_dialog_factory_ = std::move(factory);
}
void JavaScriptDialogManager::SetExtensionsClient(
scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
extensions_client_ = std::move(extensions_client);
}
////////////////////////////////////////////////////////////////////////////////
// JavaScriptDialogManager, private:
JavaScriptDialogManager::JavaScriptDialogManager()
: extensions_client_(new DefaultExtensionsClient) {
}
JavaScriptDialogManager::~JavaScriptDialogManager() {
}
void JavaScriptDialogManager::RunJavaScriptDialog(
content::WebContents* web_contents,
const GURL& origin_url,
const std::string& accept_lang,
content::JavaScriptMessageType message_type,
const base::string16& message_text,
const base::string16& default_prompt_text,
const DialogClosedCallback& callback,
bool* did_suppress_message) {
*did_suppress_message = false;
ChromeJavaScriptDialogExtraData* extra_data =
&javascript_dialog_extra_data_[web_contents];
if (extra_data->suppress_javascript_messages_) {
// If a page tries to open dialogs in a tight loop, the number of
// suppressions logged can grow out of control. Arbitrarily cap the number
// logged at 100. That many suppressed dialogs is enough to indicate the
// page is doing something very hinky.
if (extra_data->suppressed_dialog_count_ < 100) {
// Log a suppressed dialog as one that opens and then closes immediately.
UMA_HISTOGRAM_MEDIUM_TIMES(
"JSDialogs.FineTiming.TimeBetweenDialogCreatedAndSameDialogClosed",
base::TimeDelta());
// Only increment the count if it's not already at the limit, to prevent
// overflow.
extra_data->suppressed_dialog_count_++;
}
*did_suppress_message = true;
return;
}
base::TimeTicks now = base::TimeTicks::Now();
if (!last_creation_time_.is_null()) {
// A new dialog has been created: log the time since the last one was
// created.
UMA_HISTOGRAM_MEDIUM_TIMES(
"JSDialogs.FineTiming.TimeBetweenDialogCreatedAndNextDialogCreated",
now - last_creation_time_);
}
last_creation_time_ = now;
// Also log the time since a dialog was closed, but only if this is the first
// dialog that was opened since the closing.
if (!last_close_time_.is_null()) {
UMA_HISTOGRAM_MEDIUM_TIMES(
"JSDialogs.FineTiming.TimeBetweenDialogClosedAndNextDialogCreated",
now - last_close_time_);
last_close_time_ = base::TimeTicks();
}
bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT;
base::string16 dialog_title =
GetTitle(web_contents, origin_url, accept_lang, is_alert);
extensions_client_->OnDialogOpened(web_contents);
LogUMAMessageLengthStats(message_text);
AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
web_contents,
&javascript_dialog_extra_data_,
dialog_title,
message_type,
message_text,
default_prompt_text,
ShouldDisplaySuppressCheckbox(extra_data),
false, // is_before_unload_dialog
false, // is_reload
base::Bind(&JavaScriptDialogManager::OnDialogClosed,
base::Unretained(this), web_contents, callback)));
}
void JavaScriptDialogManager::RunBeforeUnloadDialog(
content::WebContents* web_contents,
bool is_reload,
const DialogClosedCallback& callback) {
ChromeJavaScriptDialogExtraData* extra_data =
&javascript_dialog_extra_data_[web_contents];
if (extra_data->suppress_javascript_messages_) {
// If a site harassed the user enough for them to put it on mute, then it
// lost its privilege to deny unloading.
callback.Run(true, base::string16());
return;
}
// Build the dialog message. We explicitly do _not_ allow the webpage to
// specify the contents of this dialog, because most of the time nowadays it's
// used for scams.
//
// This does not violate the spec. Per
// https://html.spec.whatwg.org/#prompt-to-unload-a-document, step 7:
//
// "The prompt shown by the user agent may include the string of the
// returnValue attribute, or some leading subset thereof."
//
// The prompt MAY include the string. It doesn't any more. Scam web page
// authors have abused this, so we're taking away the toys from everyone. This
// is why we can't have nice things.
const base::string16 title = l10n_util::GetStringUTF16(is_reload ?
IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE);
const base::string16 message =
l10n_util::GetStringUTF16(IDS_BEFOREUNLOAD_MESSAGEBOX_MESSAGE);
extensions_client_->OnDialogOpened(web_contents);
AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
web_contents,
&javascript_dialog_extra_data_,
title,
content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
message,
base::string16(), // default_prompt_text
ShouldDisplaySuppressCheckbox(extra_data),
true, // is_before_unload_dialog
is_reload,
base::Bind(&JavaScriptDialogManager::OnBeforeUnloadDialogClosed,
base::Unretained(this), web_contents, callback)));
}
bool JavaScriptDialogManager::HandleJavaScriptDialog(
content::WebContents* web_contents,
bool accept,
const base::string16* prompt_override) {
AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
if (!dialog_queue->HasActiveDialog() ||
!dialog_queue->active_dialog()->IsJavaScriptModalDialog() ||
dialog_queue->active_dialog()->web_contents() != web_contents) {
return false;
}
JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>(
dialog_queue->active_dialog());
if (accept) {
if (prompt_override)
dialog->SetOverridePromptText(*prompt_override);
dialog->native_dialog()->AcceptAppModalDialog();
} else {
dialog->native_dialog()->CancelAppModalDialog();
}
return true;
}
void JavaScriptDialogManager::ResetDialogState(
content::WebContents* web_contents) {
CancelActiveAndPendingDialogs(web_contents);
javascript_dialog_extra_data_.erase(web_contents);
}
base::string16 JavaScriptDialogManager::GetTitle(
content::WebContents* web_contents,
const GURL& origin_url,
const std::string& accept_lang,
bool is_alert) {
// For extensions, show the extension name, but only if the origin of
// the alert matches the top-level WebContents.
std::string name;
if (extensions_client_->GetExtensionName(web_contents, origin_url, &name))
return base::UTF8ToUTF16(name);
// Otherwise, return the formatted URL. For non-standard URLs such as |data:|,
// just say "This page".
bool is_same_origin_as_main_frame =
(web_contents->GetURL().GetOrigin() == origin_url.GetOrigin());
if (origin_url.IsStandard() && !origin_url.SchemeIsFile() &&
!origin_url.SchemeIsFileSystem()) {
#if !defined(OS_ANDROID)
base::string16 url_string =
url_formatter::ElideHost(origin_url, gfx::FontList(), kUrlElideWidth);
#else
base::string16 url_string =
url_formatter::FormatUrlForSecurityDisplayOmitScheme(origin_url,
accept_lang);
#endif
return l10n_util::GetStringFUTF16(
is_same_origin_as_main_frame ? IDS_JAVASCRIPT_MESSAGEBOX_TITLE
: IDS_JAVASCRIPT_MESSAGEBOX_TITLE_IFRAME,
base::i18n::GetDisplayStringInLTRDirectionality(url_string));
}
return l10n_util::GetStringUTF16(
is_same_origin_as_main_frame
? IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL
: IDS_JAVASCRIPT_MESSAGEBOX_TITLE_NONSTANDARD_URL_IFRAME);
}
void JavaScriptDialogManager::CancelActiveAndPendingDialogs(
content::WebContents* web_contents) {
AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance();
AppModalDialog* active_dialog = queue->active_dialog();
for (AppModalDialogQueue::iterator i = queue->begin();
i != queue->end(); ++i) {
// Invalidating the active dialog might trigger showing a not-yet
// invalidated dialog, so invalidate the active dialog last.
if ((*i) == active_dialog)
continue;
if ((*i)->web_contents() == web_contents)
(*i)->Invalidate();
}
if (active_dialog && active_dialog->web_contents() == web_contents)
active_dialog->Invalidate();
}
void JavaScriptDialogManager::OnBeforeUnloadDialogClosed(
content::WebContents* web_contents,
DialogClosedCallback callback,
bool success,
const base::string16& user_input) {
enum class StayVsLeave {
STAY = 0,
LEAVE = 1,
MAX,
};
UMA_HISTOGRAM_ENUMERATION(
"JSDialogs.OnBeforeUnloadStayVsLeave",
static_cast<int>(success ? StayVsLeave::LEAVE : StayVsLeave::STAY),
static_cast<int>(StayVsLeave::MAX));
OnDialogClosed(web_contents, callback, success, user_input);
}
void JavaScriptDialogManager::OnDialogClosed(
content::WebContents* web_contents,
DialogClosedCallback callback,
bool success,
const base::string16& user_input) {
// If an extension opened this dialog then the extension may shut down its
// lazy background page after the dialog closes. (Dialogs are closed before
// their WebContents is destroyed so |web_contents| is still valid here.)
extensions_client_->OnDialogClosed(web_contents);
last_close_time_ = base::TimeTicks::Now();
callback.Run(success, user_input);
}
} // namespace app_modal
|