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