Add ICU message format support
[chromium-blink-merge.git] / content / shell / browser / shell_javascript_dialog_manager.cc
blob3eceb34934636120f874c931431172aaafb3ae07
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/blink_test_controller.h"
12 #include "content/shell/browser/shell_javascript_dialog.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 (!dialog_request_callback_.is_null()) {
34 dialog_request_callback_.Run();
35 callback.Run(true, base::string16());
36 dialog_request_callback_.Reset();
37 return;
40 #if defined(OS_MACOSX) || defined(OS_WIN)
41 *did_suppress_message = false;
43 if (dialog_) {
44 // One dialog at a time, please.
45 *did_suppress_message = true;
46 return;
49 base::string16 new_message_text = net::FormatUrl(origin_url, accept_lang) +
50 base::ASCIIToUTF16("\n\n") +
51 message_text;
52 gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
54 dialog_.reset(new ShellJavaScriptDialog(this,
55 parent_window,
56 javascript_message_type,
57 new_message_text,
58 default_prompt_text,
59 callback));
60 #else
61 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
62 *did_suppress_message = true;
63 return;
64 #endif
67 void ShellJavaScriptDialogManager::RunBeforeUnloadDialog(
68 WebContents* web_contents,
69 const base::string16& message_text,
70 bool is_reload,
71 const DialogClosedCallback& callback) {
72 if (!dialog_request_callback_.is_null()) {
73 dialog_request_callback_.Run();
74 callback.Run(true, base::string16());
75 dialog_request_callback_.Reset();
76 return;
79 #if defined(OS_MACOSX) || defined(OS_WIN)
80 if (dialog_) {
81 // Seriously!?
82 callback.Run(true, base::string16());
83 return;
86 base::string16 new_message_text =
87 message_text +
88 base::ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
90 gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
92 dialog_.reset(new ShellJavaScriptDialog(this,
93 parent_window,
94 JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
95 new_message_text,
96 base::string16(), // default
97 callback));
98 #else
99 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
100 callback.Run(true, base::string16());
101 return;
102 #endif
105 void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs(
106 WebContents* web_contents) {
107 #if defined(OS_MACOSX) || defined(OS_WIN)
108 if (dialog_) {
109 dialog_->Cancel();
110 dialog_.reset();
112 #else
113 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
114 #endif
117 void ShellJavaScriptDialogManager::ResetDialogState(WebContents* web_contents) {
120 void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog* dialog) {
121 #if defined(OS_MACOSX) || defined(OS_WIN)
122 DCHECK_EQ(dialog, dialog_.get());
123 dialog_.reset();
124 #else
125 // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
126 #endif
129 } // namespace content