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.
4
5 #include "content/web_test/browser/web_test_javascript_dialog_manager.h"
6
7 #include <utility>
8
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/strings/strcat.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/shell/browser/shell_javascript_dialog.h"
15 #include "content/shell/common/shell_switches.h"
16 #include "content/web_test/browser/web_test_control_host.h"
17
18 namespace content {
19
20 namespace {
DumpJavascriptDialog()21 bool DumpJavascriptDialog() {
22 base::Optional<bool> result =
23 WebTestControlHost::Get()
24 ->accumulated_web_test_runtime_flags_changes()
25 .FindBoolPath("dump_javascript_dialogs");
26 if (!result.has_value())
27 return true;
28 return result.value();
29 }
30
ShouldStayOnPageAfterHandlingBeforeUnload()31 bool ShouldStayOnPageAfterHandlingBeforeUnload() {
32 base::Optional<bool> result =
33 WebTestControlHost::Get()
34 ->accumulated_web_test_runtime_flags_changes()
35 .FindBoolPath("stay_on_page_after_handling_before_unload");
36 if (!result.has_value())
37 return false;
38 return result.value();
39 }
40
41 } // namespace
42
43 WebTestJavaScriptDialogManager::WebTestJavaScriptDialogManager() = default;
44
45 WebTestJavaScriptDialogManager::~WebTestJavaScriptDialogManager() = default;
46
RunJavaScriptDialog(WebContents * web_contents,RenderFrameHost * render_frame_host,JavaScriptDialogType dialog_type,const base::string16 & message_text,const base::string16 & default_prompt_text,DialogClosedCallback callback,bool * did_suppress_message)47 void WebTestJavaScriptDialogManager::RunJavaScriptDialog(
48 WebContents* web_contents,
49 RenderFrameHost* render_frame_host,
50 JavaScriptDialogType dialog_type,
51 const base::string16& message_text,
52 const base::string16& default_prompt_text,
53 DialogClosedCallback callback,
54 bool* did_suppress_message) {
55 if (DumpJavascriptDialog()) {
56 std::string message;
57 switch (dialog_type) {
58 case JAVASCRIPT_DIALOG_TYPE_ALERT:
59 message =
60 base::StrCat({"ALERT: ", base::UTF16ToUTF8(message_text), "\n"});
61 break;
62 case JAVASCRIPT_DIALOG_TYPE_CONFIRM:
63 message =
64 base::StrCat({"CONFIRM: ", base::UTF16ToUTF8(message_text), "\n"});
65 break;
66 case JAVASCRIPT_DIALOG_TYPE_PROMPT:
67 message = base::StrCat(
68 {"PROMPT: ", base::UTF16ToUTF8(message_text),
69 ", default text: ", base::UTF16ToUTF8(default_prompt_text), "\n"});
70 break;
71 }
72 WebTestControlHost::Get()->printer()->AddMessageRaw(message);
73 }
74 std::move(callback).Run(true, base::string16());
75 }
76
RunBeforeUnloadDialog(WebContents * web_contents,RenderFrameHost * render_frame_host,bool is_reload,DialogClosedCallback callback)77 void WebTestJavaScriptDialogManager::RunBeforeUnloadDialog(
78 WebContents* web_contents,
79 RenderFrameHost* render_frame_host,
80 bool is_reload,
81 DialogClosedCallback callback) {
82 if (DumpJavascriptDialog())
83 WebTestControlHost::Get()->printer()->AddMessageRaw("CONFIRM NAVIGATION\n");
84 std::move(callback).Run(!ShouldStayOnPageAfterHandlingBeforeUnload(),
85 base::string16());
86 }
87
88 } // namespace content
89