OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ | 5 #ifndef CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ |
6 #define 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 | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/utf_string_conversions.h" | 11 #include "base/time.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" | 13 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" |
| 14 #include "content/browser/javascript_dialogs.h" |
14 #include "content/common/notification_observer.h" | 15 #include "content/common/notification_observer.h" |
15 #include "content/common/notification_registrar.h" | 16 #include "content/common/notification_registrar.h" |
16 #include "ui/gfx/native_widget_types.h" | |
17 | 17 |
18 class ExtensionHost; | 18 class ExtensionHost; |
19 class NativeAppModalDialog; | 19 class NativeAppModalDialog; |
20 class TabContents; | 20 class TabContents; |
21 | 21 |
22 namespace IPC { | 22 namespace IPC { |
23 class Message; | 23 class Message; |
24 } | 24 } |
25 | 25 |
26 class JavaScriptAppModalDialogDelegate { | 26 // Extra data for JavaScript dialogs to add Chrome-only features. |
| 27 class ChromeJavaScriptDialogExtraData { |
27 public: | 28 public: |
28 // AppModalDialog calls this when the dialog is closed. | 29 ChromeJavaScriptDialogExtraData(); |
29 virtual void OnMessageBoxClosed(IPC::Message* reply_msg, | |
30 bool success, | |
31 const std::wstring& prompt) = 0; | |
32 | 30 |
33 // Indicates whether additional message boxes should be suppressed. | 31 // The time that the last JavaScript dialog was dismissed. |
34 virtual void SetSuppressMessageBoxes(bool suppress_message_boxes) = 0; | 32 base::TimeTicks last_javascript_message_dismissal_; |
35 | 33 |
36 // Returns the root native window with which the message box is associated. | 34 // True if the user has decided to block future JavaScript dialogs. |
37 virtual gfx::NativeWindow GetMessageBoxRootWindow() = 0; | 35 bool suppress_javascript_messages_; |
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 }; | 36 }; |
49 | 37 |
50 // A controller + model class for JavaScript alert, confirm, prompt, and | 38 // A controller + model class for JavaScript alert, confirm, prompt, and |
51 // onbeforeunload dialog boxes. | 39 // onbeforeunload dialog boxes. |
52 class JavaScriptAppModalDialog : public AppModalDialog, | 40 class JavaScriptAppModalDialog : public AppModalDialog, |
53 public NotificationObserver { | 41 public NotificationObserver { |
54 public: | 42 public: |
55 JavaScriptAppModalDialog(JavaScriptAppModalDialogDelegate* delegate, | 43 JavaScriptAppModalDialog(content::JavaScriptDialogDelegate* delegate, |
56 const std::wstring& title, | 44 ChromeJavaScriptDialogExtraData* extra_data, |
| 45 const string16& title, |
57 int dialog_flags, | 46 int dialog_flags, |
58 const std::wstring& message_text, | 47 const string16& message_text, |
59 const std::wstring& default_prompt_text, | 48 const string16& default_prompt_text, |
60 bool display_suppress_checkbox, | 49 bool display_suppress_checkbox, |
61 bool is_before_unload_dialog, | 50 bool is_before_unload_dialog, |
62 IPC::Message* reply_msg); | 51 IPC::Message* reply_msg); |
63 virtual ~JavaScriptAppModalDialog(); | 52 virtual ~JavaScriptAppModalDialog(); |
64 | 53 |
65 // Overridden from AppModalDialog: | 54 // Overridden from AppModalDialog: |
66 virtual NativeAppModalDialog* CreateNativeDialog(); | 55 virtual NativeAppModalDialog* CreateNativeDialog(); |
67 virtual bool IsJavaScriptModalDialog(); | 56 virtual bool IsJavaScriptModalDialog(); |
68 | 57 |
69 JavaScriptAppModalDialogDelegate* delegate() const { return delegate_; } | 58 content::JavaScriptDialogDelegate* delegate() const { return delegate_; } |
70 | 59 |
71 // Callbacks from NativeDialog when the user accepts or cancels the dialog. | 60 // Callbacks from NativeDialog when the user accepts or cancels the dialog. |
72 void OnCancel(bool suppress_js_messages); | 61 void OnCancel(bool suppress_js_messages); |
73 void OnAccept(const std::wstring& prompt_text, bool suppress_js_messages); | 62 void OnAccept(const string16& prompt_text, bool suppress_js_messages); |
74 | 63 |
75 // NOTE: This is only called under Views, and should be removed. Any critical | 64 // NOTE: This is only called under Views, and should be removed. Any critical |
76 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more. | 65 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more. |
77 void OnClose(); | 66 void OnClose(); |
78 | 67 |
79 // Used only for testing. The dialog will use the given text when notifying | 68 // Used only for testing. The dialog will use the given text when notifying |
80 // its delegate instead of whatever the UI reports. | 69 // its delegate instead of whatever the UI reports. |
81 void SetOverridePromptText(const string16& prompt_text); | 70 void SetOverridePromptText(const string16& prompt_text); |
82 | 71 |
83 // Accessors | 72 // Accessors |
84 int dialog_flags() const { return dialog_flags_; } | 73 int dialog_flags() const { return dialog_flags_; } |
85 std::wstring message_text() const { return message_text_; } | 74 string16 message_text() const { return message_text_; } |
86 std::wstring default_prompt_text() const { | 75 string16 default_prompt_text() const { return default_prompt_text_; } |
87 return UTF16ToWideHack(default_prompt_text_); | |
88 } | |
89 bool display_suppress_checkbox() const { return display_suppress_checkbox_; } | 76 bool display_suppress_checkbox() const { return display_suppress_checkbox_; } |
90 bool is_before_unload_dialog() const { return is_before_unload_dialog_; } | 77 bool is_before_unload_dialog() const { return is_before_unload_dialog_; } |
91 | 78 |
92 private: | 79 private: |
93 // Overridden from NotificationObserver: | 80 // Overridden from NotificationObserver: |
94 virtual void Observe(NotificationType type, | 81 virtual void Observe(NotificationType type, |
95 const NotificationSource& source, | 82 const NotificationSource& source, |
96 const NotificationDetails& details); | 83 const NotificationDetails& details); |
97 | 84 |
98 // Initializes for notifications to listen. | 85 // Initializes for notifications to listen. |
99 void InitNotifications(); | 86 void InitNotifications(); |
100 | 87 |
101 // Notifies the delegate with the result of the dialog. | 88 // Notifies the delegate with the result of the dialog. |
102 void NotifyDelegate(bool success, const std::wstring& prompt_text, | 89 void NotifyDelegate(bool success, const string16& prompt_text, |
103 bool suppress_js_messages); | 90 bool suppress_js_messages); |
104 | 91 |
105 NotificationRegistrar registrar_; | 92 NotificationRegistrar registrar_; |
106 | 93 |
107 // An implementation of the client interface to provide supporting methods | 94 // An implementation of the client interface to provide supporting methods |
108 // and receive results. | 95 // and receive results. |
109 JavaScriptAppModalDialogDelegate* delegate_; | 96 content::JavaScriptDialogDelegate* delegate_; |
| 97 |
| 98 // The extra Chrome-only data associated with the delegate_. |
| 99 ChromeJavaScriptDialogExtraData* extra_data_; |
110 | 100 |
111 // The client_ as an ExtensionHost, cached for use during notifications that | 101 // The client_ as an ExtensionHost, cached for use during notifications that |
112 // may arrive after the client has entered its destructor (and is thus | 102 // may arrive after the client has entered its destructor (and is thus |
113 // treated as a base Delegate). This will be NULL if the |delegate_| is not an | 103 // treated as a base Delegate). This will be NULL if the |delegate_| is not an |
114 // ExtensionHost. | 104 // ExtensionHost. |
115 ExtensionHost* extension_host_; | 105 ExtensionHost* extension_host_; |
116 | 106 |
117 // Information about the message box is held in the following variables. | 107 // Information about the message box is held in the following variables. |
118 int dialog_flags_; | 108 int dialog_flags_; |
119 std::wstring message_text_; | 109 string16 message_text_; |
120 string16 default_prompt_text_; | 110 string16 default_prompt_text_; |
121 bool display_suppress_checkbox_; | 111 bool display_suppress_checkbox_; |
122 bool is_before_unload_dialog_; | 112 bool is_before_unload_dialog_; |
123 IPC::Message* reply_msg_; | 113 IPC::Message* reply_msg_; |
124 | 114 |
125 // Used only for testing. Specifies alternative prompt text that should be | 115 // Used only for testing. Specifies alternative prompt text that should be |
126 // used when notifying the delegate, if |use_override_prompt_text_| is true. | 116 // used when notifying the delegate, if |use_override_prompt_text_| is true. |
127 string16 override_prompt_text_; | 117 string16 override_prompt_text_; |
128 bool use_override_prompt_text_; | 118 bool use_override_prompt_text_; |
129 | 119 |
130 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog); | 120 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog); |
131 }; | 121 }; |
132 | 122 |
133 #endif // CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ | 123 #endif // CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JS_MODAL_DIALOG_H_ |
OLD | NEW |