Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / shell / browser / shell_javascript_dialog_manager.cc
blobae086330c2c3df85a5be98396bd21d5d155a5cd9
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"
16 namespace content {
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());
35 return;
38 if (!dialog_request_callback_.is_null()) {
39 dialog_request_callback_.Run();
40 callback.Run(true, base::string16());
41 dialog_request_callback_.Reset();
42 return;
45 #if defined(OS_MACOSX) || defined(OS_WIN)
46 *did_suppress_message = false;
48 if (dialog_) {
49 // One dialog at a time, please.
50 *did_suppress_message = true;
51 return;
54 base::string16 new_message_text = net::FormatUrl(origin_url, accept_lang) +
55 base::ASCIIToUTF16("\n\n") +
56 message_text;
57 gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
59 dialog_.reset(new ShellJavaScriptDialog(this,
60 parent_window,
61 javascript_message_type,
62 new_message_text,
63 default_prompt_text,
64 callback));
65 #else
66 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
67 *did_suppress_message = true;
68 return;
69 #endif
72 void ShellJavaScriptDialogManager::RunBeforeUnloadDialog(
73 WebContents* web_contents,
74 const base::string16& message_text,
75 bool is_reload,
76 const DialogClosedCallback& callback) {
77 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
78 callback.Run(true, base::string16());
79 return;
82 if (!dialog_request_callback_.is_null()) {
83 dialog_request_callback_.Run();
84 callback.Run(true, base::string16());
85 dialog_request_callback_.Reset();
86 return;
89 #if defined(OS_MACOSX) || defined(OS_WIN)
90 if (dialog_) {
91 // Seriously!?
92 callback.Run(true, base::string16());
93 return;
96 base::string16 new_message_text =
97 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,
103 parent_window,
104 JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
105 new_message_text,
106 base::string16(), // default
107 callback));
108 #else
109 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
110 callback.Run(true, base::string16());
111 return;
112 #endif
115 void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs(
116 WebContents* web_contents) {
117 #if defined(OS_MACOSX) || defined(OS_WIN)
118 if (dialog_) {
119 dialog_->Cancel();
120 dialog_.reset();
122 #else
123 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
124 #endif
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());
134 dialog_.reset();
135 #else
136 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
137 #endif
140 } // namespace content