shell_javascript_dialog_manager.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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. 4 5#include "content/shell/browser/shell_javascript_dialog_manager.h" 6 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" 16 17namespace content { 18 19ShellJavaScriptDialogManager::ShellJavaScriptDialogManager() { 20} 21 22ShellJavaScriptDialogManager::~ShellJavaScriptDialogManager() { 23} 24 25void 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 bool user_gesture, 33 const DialogClosedCallback& callback, 34 bool* did_suppress_message) { 35 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { 36 callback.Run(true, string16()); 37 return; 38 } 39 40 if (!dialog_request_callback_.is_null()) { 41 dialog_request_callback_.Run(); 42 callback.Run(true, string16()); 43 dialog_request_callback_.Reset(); 44 return; 45 } 46 47#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK) 48 *did_suppress_message = false; 49 50 if (dialog_) { 51 // One dialog at a time, please. 52 *did_suppress_message = true; 53 return; 54 } 55 56 string16 new_message_text = net::FormatUrl(origin_url, accept_lang) + 57 ASCIIToUTF16("\n\n") + 58 message_text; 59 gfx::NativeWindow parent_window = 60 web_contents->GetView()->GetTopLevelNativeWindow(); 61 62 dialog_.reset(new ShellJavaScriptDialog(this, 63 parent_window, 64 javascript_message_type, 65 new_message_text, 66 default_prompt_text, 67 callback)); 68#else 69 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if 70 *did_suppress_message = true; 71 return; 72#endif 73} 74 75void ShellJavaScriptDialogManager::RunBeforeUnloadDialog( 76 WebContents* web_contents, 77 const string16& message_text, 78 bool is_reload, 79 const DialogClosedCallback& callback) { 80 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) { 81 callback.Run(true, string16()); 82 return; 83 } 84 85 if (!dialog_request_callback_.is_null()) { 86 dialog_request_callback_.Run(); 87 callback.Run(true, string16()); 88 dialog_request_callback_.Reset(); 89 return; 90 } 91 92#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK) 93 if (dialog_) { 94 // Seriously!? 95 callback.Run(true, string16()); 96 return; 97 } 98 99 string16 new_message_text = 100 message_text + 101 ASCIIToUTF16("\n\nIs it OK to leave/reload this page?"); 102 103 gfx::NativeWindow parent_window = 104 web_contents->GetView()->GetTopLevelNativeWindow(); 105 106 dialog_.reset(new ShellJavaScriptDialog(this, 107 parent_window, 108 JAVASCRIPT_MESSAGE_TYPE_CONFIRM, 109 new_message_text, 110 string16(), // default_prompt_text 111 callback)); 112#else 113 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if 114 callback.Run(true, string16()); 115 return; 116#endif 117} 118 119void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs( 120 WebContents* web_contents) { 121#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK) 122 if (dialog_) { 123 dialog_->Cancel(); 124 dialog_.reset(); 125 } 126#else 127 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if 128#endif 129} 130 131void ShellJavaScriptDialogManager::WebContentsDestroyed( 132 WebContents* web_contents) { 133} 134 135void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog* dialog) { 136#if defined(OS_MACOSX) || defined(OS_WIN) || defined(TOOLKIT_GTK) 137 DCHECK_EQ(dialog, dialog_.get()); 138 dialog_.reset(); 139#else 140 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if 141#endif 142} 143 144} // namespace content 145