OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ | 5 #ifndef COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ |
6 #define COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ | 6 #define COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/app_modal/app_modal_dialog.h" | |
14 #include "content/public/browser/javascript_dialog_manager.h" | 13 #include "content/public/browser/javascript_dialog_manager.h" |
15 | 14 |
16 namespace app_modal { | 15 namespace app_modal { |
17 | 16 |
| 17 class NativeAppModalDialog; |
| 18 |
18 // Extra data for JavaScript dialogs to add Chrome-only features. | 19 // Extra data for JavaScript dialogs to add Chrome-only features. |
19 class ChromeJavaScriptDialogExtraData { | 20 class ChromeJavaScriptDialogExtraData { |
20 public: | 21 public: |
21 ChromeJavaScriptDialogExtraData(); | 22 ChromeJavaScriptDialogExtraData(); |
22 | 23 |
23 // True if the user has already seen a JavaScript dialog from the WebContents. | 24 // True if the user has already seen a JavaScript dialog from the WebContents. |
24 bool has_already_shown_a_dialog_; | 25 bool has_already_shown_a_dialog_; |
25 | 26 |
26 // True if the user has decided to block future JavaScript dialogs. | 27 // True if the user has decided to block future JavaScript dialogs. |
27 bool suppress_javascript_messages_; | 28 bool suppress_javascript_messages_; |
28 | 29 |
29 // Number of dialogs from the origin that were suppressed. | 30 // Number of dialogs from the origin that were suppressed. |
30 int suppressed_dialog_count_; | 31 int suppressed_dialog_count_; |
31 }; | 32 }; |
32 | 33 |
33 // A controller + model class for JavaScript alert, confirm, prompt, and | 34 // A controller + model class for JavaScript alert, confirm, prompt, and |
34 // onbeforeunload dialog boxes. | 35 // onbeforeunload dialog boxes. |
35 class JavaScriptAppModalDialog : public AppModalDialog { | 36 class JavaScriptAppModalDialog { |
36 public: | 37 public: |
37 typedef std::map<void*, ChromeJavaScriptDialogExtraData> ExtraDataMap; | 38 typedef std::map<void*, ChromeJavaScriptDialogExtraData> ExtraDataMap; |
38 | 39 |
39 JavaScriptAppModalDialog( | 40 JavaScriptAppModalDialog( |
40 content::WebContents* web_contents, | 41 content::WebContents* web_contents, |
41 ExtraDataMap* extra_data_map, | 42 ExtraDataMap* extra_data_map, |
42 const base::string16& title, | 43 const base::string16& title, |
43 content::JavaScriptDialogType javascript_dialog_type, | 44 content::JavaScriptDialogType javascript_dialog_type, |
44 const base::string16& message_text, | 45 const base::string16& message_text, |
45 const base::string16& default_prompt_text, | 46 const base::string16& default_prompt_text, |
46 bool display_suppress_checkbox, | 47 bool display_suppress_checkbox, |
47 bool is_before_unload_dialog, | 48 bool is_before_unload_dialog, |
48 bool is_reload, | 49 bool is_reload, |
49 const content::JavaScriptDialogManager::DialogClosedCallback& callback); | 50 const content::JavaScriptDialogManager::DialogClosedCallback& callback); |
50 ~JavaScriptAppModalDialog() override; | 51 ~JavaScriptAppModalDialog(); |
51 | 52 |
52 // Overridden from AppModalDialog: | 53 // Called by the AppModalDialogQueue to show this dialog. |
53 NativeAppModalDialog* CreateNativeDialog() override; | 54 void ShowModalDialog(); |
54 bool IsJavaScriptModalDialog() override; | 55 |
55 void Invalidate() override; | 56 // Called by the AppModalDialogQueue to activate the dialog. |
| 57 void ActivateModalDialog(); |
| 58 |
| 59 // Closes the dialog if it is showing. |
| 60 void CloseModalDialog(); |
| 61 |
| 62 // Returns true if the dialog is still valid. As dialogs are created they are |
| 63 // added to the AppModalDialogQueue. When the current modal dialog finishes |
| 64 // and it's time to show the next dialog in the queue IsValid is invoked. |
| 65 // If IsValid returns false the dialog is deleted and not shown. |
| 66 bool IsValid(); |
| 67 |
| 68 // Invalidates the dialog, therefore causing it to not be shown when its turn |
| 69 // to be shown comes around. |
| 70 void Invalidate(); |
56 | 71 |
57 // Callbacks from NativeDialog when the user accepts or cancels the dialog. | 72 // Callbacks from NativeDialog when the user accepts or cancels the dialog. |
58 void OnCancel(bool suppress_js_messages); | 73 void OnCancel(bool suppress_js_messages); |
59 void OnAccept(const base::string16& prompt_text, bool suppress_js_messages); | 74 void OnAccept(const base::string16& prompt_text, bool suppress_js_messages); |
60 | 75 |
61 // NOTE: This is only called under Views, and should be removed. Any critical | 76 // NOTE: This is only called under Views, and should be removed. Any critical |
62 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more. | 77 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more. |
63 void OnClose(); | 78 void OnClose(); |
64 | 79 |
65 // Used only for testing. The dialog will use the given text when notifying | 80 // Used only for testing. The dialog will use the given text when notifying |
66 // its delegate instead of whatever the UI reports. | 81 // its delegate instead of whatever the UI reports. |
67 void SetOverridePromptText(const base::string16& prompt_text); | 82 void SetOverridePromptText(const base::string16& prompt_text); |
68 | 83 |
69 // Accessors | 84 // Accessors. |
| 85 base::string16 title() const { return title_; } |
| 86 NativeAppModalDialog* native_dialog() const { return native_dialog_; } |
| 87 content::WebContents* web_contents() const { return web_contents_; } |
70 content::JavaScriptDialogType javascript_dialog_type() const { | 88 content::JavaScriptDialogType javascript_dialog_type() const { |
71 return javascript_dialog_type_; | 89 return javascript_dialog_type_; |
72 } | 90 } |
73 base::string16 message_text() const { return message_text_; } | 91 base::string16 message_text() const { return message_text_; } |
74 base::string16 default_prompt_text() const { return default_prompt_text_; } | 92 base::string16 default_prompt_text() const { return default_prompt_text_; } |
75 bool display_suppress_checkbox() const { return display_suppress_checkbox_; } | 93 bool display_suppress_checkbox() const { return display_suppress_checkbox_; } |
76 bool is_before_unload_dialog() const { return is_before_unload_dialog_; } | 94 bool is_before_unload_dialog() const { return is_before_unload_dialog_; } |
77 bool is_reload() const { return is_reload_; } | 95 bool is_reload() const { return is_reload_; } |
78 | 96 |
79 private: | 97 private: |
80 // Notifies the delegate with the result of the dialog. | 98 // Notifies the delegate with the result of the dialog. |
81 void NotifyDelegate(bool success, const base::string16& prompt_text, | 99 void NotifyDelegate(bool success, const base::string16& prompt_text, |
82 bool suppress_js_messages); | 100 bool suppress_js_messages); |
83 | 101 |
84 void CallDialogClosedCallback(bool success, | 102 void CallDialogClosedCallback(bool success, |
85 const base::string16& prompt_text); | 103 const base::string16& prompt_text); |
86 | 104 |
| 105 // Completes dialog handling, shows next modal dialog from the queue. |
| 106 // TODO(beng): Get rid of this method. |
| 107 void CompleteDialog(); |
| 108 |
| 109 // The title of the dialog. |
| 110 base::string16 title_; |
| 111 |
| 112 // // True if CompleteDialog was called. |
| 113 bool completed_; |
| 114 |
| 115 // False if the dialog should no longer be shown, e.g. because the underlying |
| 116 // tab navigated away while the dialog was queued. |
| 117 bool valid_; |
| 118 |
| 119 // // The toolkit-specific implementation of the app modal dialog box. |
| 120 NativeAppModalDialog* native_dialog_; |
| 121 |
| 122 // The WebContents that opened this dialog. |
| 123 content::WebContents* web_contents_; |
| 124 |
87 // A map of extra Chrome-only data associated with the delegate_. Can be | 125 // A map of extra Chrome-only data associated with the delegate_. Can be |
88 // inspected via |extra_data_map_[web_contents_]|. | 126 // inspected via |extra_data_map_[web_contents_]|. |
89 ExtraDataMap* extra_data_map_; | 127 ExtraDataMap* extra_data_map_; |
90 | 128 |
91 // Information about the message box is held in the following variables. | 129 // Information about the message box is held in the following variables. |
92 const content::JavaScriptDialogType javascript_dialog_type_; | 130 const content::JavaScriptDialogType javascript_dialog_type_; |
93 base::string16 message_text_; | 131 base::string16 message_text_; |
94 base::string16 default_prompt_text_; | 132 base::string16 default_prompt_text_; |
95 bool display_suppress_checkbox_; | 133 bool display_suppress_checkbox_; |
96 bool is_before_unload_dialog_; | 134 bool is_before_unload_dialog_; |
97 bool is_reload_; | 135 bool is_reload_; |
98 | 136 |
99 content::JavaScriptDialogManager::DialogClosedCallback callback_; | 137 content::JavaScriptDialogManager::DialogClosedCallback callback_; |
100 | 138 |
101 // Used only for testing. Specifies alternative prompt text that should be | 139 // Used only for testing. Specifies alternative prompt text that should be |
102 // used when notifying the delegate, if |use_override_prompt_text_| is true. | 140 // used when notifying the delegate, if |use_override_prompt_text_| is true. |
103 base::string16 override_prompt_text_; | 141 base::string16 override_prompt_text_; |
104 bool use_override_prompt_text_; | 142 bool use_override_prompt_text_; |
105 | 143 |
106 base::TimeTicks creation_time_; | 144 base::TimeTicks creation_time_; |
107 | 145 |
108 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog); | 146 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog); |
109 }; | 147 }; |
110 | 148 |
| 149 // An interface to observe that a modal dialog is shown. |
| 150 class AppModalDialogObserver { |
| 151 public: |
| 152 AppModalDialogObserver(); |
| 153 virtual ~AppModalDialogObserver(); |
| 154 |
| 155 // Called when the modal dialog is shown. |
| 156 virtual void Notify(JavaScriptAppModalDialog* dialog) = 0; |
| 157 |
| 158 private: |
| 159 DISALLOW_COPY_AND_ASSIGN(AppModalDialogObserver); |
| 160 }; |
| 161 |
111 } // namespace app_modal | 162 } // namespace app_modal |
112 | 163 |
113 #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ | 164 #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_ |
OLD | NEW |