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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_
6 #define COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.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"
18 // Extra data for JavaScript dialogs to add Chrome-only features.
19 class ChromeJavaScriptDialogExtraData
{
21 ChromeJavaScriptDialogExtraData();
23 // The time that the last JavaScript dialog was dismissed.
24 base::TimeTicks last_javascript_message_dismissal_
;
26 // True if the user has decided to block future JavaScript dialogs.
27 bool suppress_javascript_messages_
;
30 // A controller + model class for JavaScript alert, confirm, prompt, and
31 // onbeforeunload dialog boxes.
32 class JavaScriptAppModalDialog
: public AppModalDialog
{
34 typedef std::map
<void*, ChromeJavaScriptDialogExtraData
> ExtraDataMap
;
36 JavaScriptAppModalDialog(
37 content::WebContents
* web_contents
,
38 ExtraDataMap
* extra_data_map
,
39 const base::string16
& title
,
40 content::JavaScriptMessageType javascript_message_type
,
41 const base::string16
& message_text
,
42 const base::string16
& default_prompt_text
,
43 bool display_suppress_checkbox
,
44 bool is_before_unload_dialog
,
46 const content::JavaScriptDialogManager::DialogClosedCallback
& callback
);
47 ~JavaScriptAppModalDialog() override
;
49 // Overridden from AppModalDialog:
50 NativeAppModalDialog
* CreateNativeDialog() override
;
51 bool IsJavaScriptModalDialog() override
;
52 void Invalidate() override
;
54 // Callbacks from NativeDialog when the user accepts or cancels the dialog.
55 void OnCancel(bool suppress_js_messages
);
56 void OnAccept(const base::string16
& prompt_text
, bool suppress_js_messages
);
58 // NOTE: This is only called under Views, and should be removed. Any critical
59 // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more.
62 // Used only for testing. The dialog will use the given text when notifying
63 // its delegate instead of whatever the UI reports.
64 void SetOverridePromptText(const base::string16
& prompt_text
);
67 content::JavaScriptMessageType
javascript_message_type() const {
68 return javascript_message_type_
;
70 base::string16
message_text() const { return message_text_
; }
71 base::string16
default_prompt_text() const { return default_prompt_text_
; }
72 bool display_suppress_checkbox() const { return display_suppress_checkbox_
; }
73 bool is_before_unload_dialog() const { return is_before_unload_dialog_
; }
74 bool is_reload() const { return is_reload_
; }
77 // Notifies the delegate with the result of the dialog.
78 void NotifyDelegate(bool success
, const base::string16
& prompt_text
,
79 bool suppress_js_messages
);
81 // A map of extra Chrome-only data associated with the delegate_.
82 // Can be inspected via extra_data_map_[web_contents_].
83 ExtraDataMap
* extra_data_map_
;
85 // Information about the message box is held in the following variables.
86 const content::JavaScriptMessageType javascript_message_type_
;
87 base::string16 message_text_
;
88 base::string16 default_prompt_text_
;
89 bool display_suppress_checkbox_
;
90 bool is_before_unload_dialog_
;
93 content::JavaScriptDialogManager::DialogClosedCallback callback_
;
95 // Used only for testing. Specifies alternative prompt text that should be
96 // used when notifying the delegate, if |use_override_prompt_text_| is true.
97 base::string16 override_prompt_text_
;
98 bool use_override_prompt_text_
;
100 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog
);
103 } // namespace app_modal
105 #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_