13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_javascript_dialog_manager.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_javascript_dialog.h"
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/webkit_test_controller.h"
13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/shell/common/shell_switches.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ShellJavaScriptDialogManager::ShellJavaScriptDialogManager() {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ShellJavaScriptDialogManager::~ShellJavaScriptDialogManager() {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ShellJavaScriptDialogManager::RunJavaScriptDialog(
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* web_contents,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin_url,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& accept_lang,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    JavaScriptMessageType javascript_message_type,
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& message_text,
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& default_prompt_text,
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DialogClosedCallback& callback,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool* did_suppress_message) {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    callback.Run(true, base::string16());
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!dialog_request_callback_.is_null()) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_request_callback_.Run();
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    callback.Run(true, base::string16());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_request_callback_.Reset();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#if defined(OS_MACOSX) || defined(OS_WIN)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *did_suppress_message = false;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (dialog_) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // One dialog at a time, please.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *did_suppress_message = true;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 new_message_text = net::FormatUrl(origin_url, accept_lang) +
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              base::ASCIIToUTF16("\n\n") +
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              message_text;
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dialog_.reset(new ShellJavaScriptDialog(this,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          parent_window,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          javascript_message_type,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          new_message_text,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          default_prompt_text,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          callback));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *did_suppress_message = true;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ShellJavaScriptDialogManager::RunBeforeUnloadDialog(
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* web_contents,
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& message_text,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_reload,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DialogClosedCallback& callback) {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    callback.Run(true, base::string16());
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!dialog_request_callback_.is_null()) {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_request_callback_.Run();
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    callback.Run(true, base::string16());
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_request_callback_.Reset();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
89a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#if defined(OS_MACOSX) || defined(OS_WIN)
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (dialog_) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Seriously!?
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    callback.Run(true, base::string16());
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 new_message_text =
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      message_text +
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::ASCIIToUTF16("\n\nIs it OK to leave/reload this page?");
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dialog_.reset(new ShellJavaScriptDialog(this,
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          parent_window,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          new_message_text,
106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                          base::string16(),  // default
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          callback));
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
110a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  callback.Run(true, base::string16());
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void ShellJavaScriptDialogManager::CancelActiveAndPendingDialogs(
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* web_contents) {
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#if defined(OS_MACOSX) || defined(OS_WIN)
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (dialog_) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_->Cancel();
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dialog_.reset();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
127a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void ShellJavaScriptDialogManager::WebContentsDestroyed(
128a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    WebContents* web_contents) {
129a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
130a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ShellJavaScriptDialogManager::DialogClosed(ShellJavaScriptDialog* dialog) {
132a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#if defined(OS_MACOSX) || defined(OS_WIN)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_EQ(dialog, dialog_.get());
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dialog_.reset();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement ShellJavaScriptDialog for other platforms, drop this #if
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
141