1 // Copyright 2013 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 "content/shell/browser/shell_javascript_dialog_manager.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_view.h"
12 #include "content/shell/browser/shell_javascript_dialog.h"
13 #include "content/shell/browser/webkit_test_controller.h"
14 #include "content/shell/common/shell_switches.h"
15 #include "net/base/net_util.h"
19 ShellJavaScriptDialogManager::ShellJavaScriptDialogManager() {
22 ShellJavaScriptDialogManager::~ShellJavaScriptDialogManager() {
25 void ShellJavaScriptDialogManager::RunJavaScriptDialog(
26 WebContents
* web_contents
,
27 const GURL
& origin_url
,
28 const std::string
& accept_lang
,
29 JavaScriptMessageType javascript_message_type
,
30 const string16
& message_text
,
31 const string16
& default_prompt_text
,
32 const DialogClosedCallback
& callback
,
33 bool* did_suppress_message
) {
34 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
35 callback
.Run(true, string16());
39 if (!dialog_request_callback_
.is_null()) {
40 dialog_request_callback_
.Run();
41 callback
.Run(true, string16());
42 dialog_request_callback_
.Reset();
46 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
47 *did_suppress_message
= false;
50 // One dialog at a time, please.
51 *did_suppress_message
= true;
55 string16 new_message_text
= net::FormatUrl(origin_url
, accept_lang
) +
56 ASCIIToUTF16("\n\n") +
58 gfx::NativeWindow parent_window
=
59 web_contents
->GetView()->GetTopLevelNativeWindow();
61 dialog_
.reset(new ShellJavaScriptDialog(this,
63 javascript_message_type
,
68 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
69 *did_suppress_message
= true;
74 void ShellJavaScriptDialogManager::RunBeforeUnloadDialog(
75 WebContents
* web_contents
,
76 const string16
& message_text
,
78 const DialogClosedCallback
& callback
) {
79 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
80 callback
.Run(true, string16());
84 if (!dialog_request_callback_
.is_null()) {
85 dialog_request_callback_
.Run();
86 callback
.Run(true, string16());
87 dialog_request_callback_
.Reset();
91 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
94 callback
.Run(true, string16());
98 string16 new_message_text
=
100 ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
102 gfx::NativeWindow parent_window
=
103 web_contents
->GetView()->GetTopLevelNativeWindow();
105 dialog_
.reset(new ShellJavaScriptDialog(this,
107 JAVASCRIPT_MESSAGE_TYPE_CONFIRM
,
109 string16(), // default_prompt_text
112 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
113 callback
.Run(true, string16());
118 void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs(
119 WebContents
* web_contents
) {
120 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
126 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
130 void ShellJavaScriptDialogManager::WebContentsDestroyed(
131 WebContents
* web_contents
) {
134 void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog
* dialog
) {
135 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK)
136 DCHECK_EQ(dialog
, dialog_
.get());
139 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
143 } // namespace content