1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ 6#define CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ 7#pragma once 8 9#include <string> 10 11#include "base/utf_string_conversions.h" 12#include "build/build_config.h" 13#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" 14#include "content/common/notification_observer.h" 15#include "content/common/notification_registrar.h" 16#include "ui/gfx/native_widget_types.h" 17 18class ExtensionHost; 19class NativeAppModalDialog; 20class TabContents; 21 22namespace IPC { 23class Message; 24} 25 26class JavaScriptAppModalDialogDelegate { 27 public: 28 // AppModalDialog calls this when the dialog is closed. 29 virtual void OnMessageBoxClosed(IPC::Message* reply_msg, 30 bool success, 31 const std::wstring& prompt) = 0; 32 33 // Indicates whether additional message boxes should be suppressed. 34 virtual void SetSuppressMessageBoxes(bool suppress_message_boxes) = 0; 35 36 // Returns the root native window with which the message box is associated. 37 virtual gfx::NativeWindow GetMessageBoxRootWindow() = 0; 38 39 // Returns the TabContents or ExtensionHost associated with this message 40 // box -- in practice, the object implementing this interface. Exactly one 41 // of these must be non-NULL; behavior is undefined (read: it'll probably 42 // crash) if that is not the case. 43 virtual TabContents* AsTabContents() = 0; 44 virtual ExtensionHost* AsExtensionHost() = 0; 45 46 protected: 47 virtual ~JavaScriptAppModalDialogDelegate() {} 48}; 49 50// A controller + model class for JavaScript alert, confirm, prompt, and 51// onbeforeunload dialog boxes. 52class JavaScriptAppModalDialog : public AppModalDialog, 53 public NotificationObserver { 54 public: 55 JavaScriptAppModalDialog(JavaScriptAppModalDialogDelegate* delegate, 56 const std::wstring& title, 57 int dialog_flags, 58 const std::wstring& message_text, 59 const std::wstring& default_prompt_text, 60 bool display_suppress_checkbox, 61 bool is_before_unload_dialog, 62 IPC::Message* reply_msg); 63 virtual ~JavaScriptAppModalDialog(); 64 65 // Overridden from AppModalDialog: 66 virtual NativeAppModalDialog* CreateNativeDialog(); 67 68 JavaScriptAppModalDialogDelegate* delegate() const { return delegate_; } 69 70 // Callbacks from NativeDialog when the user accepts or cancels the dialog. 71 void OnCancel(bool suppress_js_messages); 72 void OnAccept(const std::wstring& prompt_text, bool suppress_js_messages); 73 74 // NOTE: This is only called under Views, and should be removed. Any critical 75 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more. 76 void OnClose(); 77 78 // Accessors 79 int dialog_flags() const { return dialog_flags_; } 80 std::wstring message_text() const { return message_text_; } 81 std::wstring default_prompt_text() const { 82 return UTF16ToWideHack(default_prompt_text_); 83 } 84 bool display_suppress_checkbox() const { return display_suppress_checkbox_; } 85 bool is_before_unload_dialog() const { return is_before_unload_dialog_; } 86 87 private: 88 // Overridden from NotificationObserver: 89 virtual void Observe(NotificationType type, 90 const NotificationSource& source, 91 const NotificationDetails& details); 92 93 // Initializes for notifications to listen. 94 void InitNotifications(); 95 96 // Notifies the delegate with the result of the dialog. 97 void NotifyDelegate(bool success, const std::wstring& prompt_text, 98 bool suppress_js_messages); 99 100 NotificationRegistrar registrar_; 101 102 // An implementation of the client interface to provide supporting methods 103 // and receive results. 104 JavaScriptAppModalDialogDelegate* delegate_; 105 106 // The client_ as an ExtensionHost, cached for use during notifications that 107 // may arrive after the client has entered its destructor (and is thus 108 // treated as a base Delegate). This will be NULL if the |delegate_| is not an 109 // ExtensionHost. 110 ExtensionHost* extension_host_; 111 112 // Information about the message box is held in the following variables. 113 int dialog_flags_; 114 std::wstring message_text_; 115 string16 default_prompt_text_; 116 bool display_suppress_checkbox_; 117 bool is_before_unload_dialog_; 118 IPC::Message* reply_msg_; 119 120 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog); 121}; 122 123#endif // CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ 124