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/shell/browser/shell_javascript_dialog.h"
12 #include "content/shell/browser/webkit_test_controller.h"
13 #include "content/shell/common/shell_switches.h"
14 #include "net/base/net_util.h"
18 ShellJavaScriptDialogManager::ShellJavaScriptDialogManager() {
21 ShellJavaScriptDialogManager::~ShellJavaScriptDialogManager() {
24 void ShellJavaScriptDialogManager::RunJavaScriptDialog(
25 WebContents
* web_contents
,
26 const GURL
& origin_url
,
27 const std::string
& accept_lang
,
28 JavaScriptMessageType javascript_message_type
,
29 const base::string16
& message_text
,
30 const base::string16
& default_prompt_text
,
31 const DialogClosedCallback
& callback
,
32 bool* did_suppress_message
) {
33 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
34 callback
.Run(true, base::string16());
38 if (!dialog_request_callback_
.is_null()) {
39 dialog_request_callback_
.Run();
40 callback
.Run(true, base::string16());
41 dialog_request_callback_
.Reset();
45 #if defined(OS_MACOSX) || defined(OS_WIN)
46 *did_suppress_message
= false;
49 // One dialog at a time, please.
50 *did_suppress_message
= true;
54 base::string16 new_message_text
= net::FormatUrl(origin_url
, accept_lang
) +
55 base::ASCIIToUTF16("\n\n") +
57 gfx::NativeWindow parent_window
= web_contents
->GetTopLevelNativeWindow();
59 dialog_
.reset(new ShellJavaScriptDialog(this,
61 javascript_message_type
,
66 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
67 *did_suppress_message
= true;
72 void ShellJavaScriptDialogManager::RunBeforeUnloadDialog(
73 WebContents
* web_contents
,
74 const base::string16
& message_text
,
76 const DialogClosedCallback
& callback
) {
77 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
78 callback
.Run(true, base::string16());
82 if (!dialog_request_callback_
.is_null()) {
83 dialog_request_callback_
.Run();
84 callback
.Run(true, base::string16());
85 dialog_request_callback_
.Reset();
89 #if defined(OS_MACOSX) || defined(OS_WIN)
92 callback
.Run(true, base::string16());
96 base::string16 new_message_text
=
98 base::ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
100 gfx::NativeWindow parent_window
= web_contents
->GetTopLevelNativeWindow();
102 dialog_
.reset(new ShellJavaScriptDialog(this,
104 JAVASCRIPT_MESSAGE_TYPE_CONFIRM
,
106 base::string16(), // default
109 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
110 callback
.Run(true, base::string16());
115 void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs(
116 WebContents
* web_contents
) {
117 #if defined(OS_MACOSX) || defined(OS_WIN)
123 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
127 void ShellJavaScriptDialogManager::WebContentsDestroyed(
128 WebContents
* web_contents
) {
131 void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog
* dialog
) {
132 #if defined(OS_MACOSX) || defined(OS_WIN)
133 DCHECK_EQ(dialog
, dialog_
.get());
136 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
140 } // namespace content