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