Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_javascript_dialog_manager.cc
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/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"
15
16 namespace content {
17
18 ShellJavaScriptDialogManager::ShellJavaScriptDialogManager() {
19 }
20
21 ShellJavaScriptDialogManager::~ShellJavaScriptDialogManager() {
22 }
23
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;
38   }
39
40 #if defined(OS_MACOSX) || defined(OS_WIN)
41   *did_suppress_message = false;
42
43   if (dialog_) {
44     // One dialog at a time, please.
45     *did_suppress_message = true;
46     return;
47   }
48
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();
53
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
65 }
66
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;
77   }
78
79 #if defined(OS_MACOSX) || defined(OS_WIN)
80   if (dialog_) {
81     // Seriously!?
82     callback.Run(true, base::string16());
83     return;
84   }
85
86   base::string16 new_message_text =
87       message_text +
88       base::ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
89
90   gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
91
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
103 }
104
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();
111   }
112 #else
113   // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
114 #endif
115 }
116
117 void ShellJavaScriptDialogManager::WebContentsDestroyed(
118     WebContents* web_contents) {
119 }
120
121 void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog* dialog) {
122 #if defined(OS_MACOSX) || defined(OS_WIN)
123   DCHECK_EQ(dialog, dialog_.get());
124   dialog_.reset();
125 #else
126   // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
127 #endif
128 }
129
130 }  // namespace content