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_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.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 // True if the user has already seen a JavaScript dialog from the origin.
24 bool has_already_shown_a_dialog_
;
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
<std::string
, 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
);
66 // The serialized form of the origin of the last committed URL in
67 // |web_contents_|. See |extra_data_map_|.
68 static std::string
GetSerializedOriginForWebContents(
69 content::WebContents
* contents
);
72 content::JavaScriptMessageType
javascript_message_type() const {
73 return javascript_message_type_
;
75 base::string16
message_text() const { return message_text_
; }
76 base::string16
default_prompt_text() const { return default_prompt_text_
; }
77 bool display_suppress_checkbox() const { return display_suppress_checkbox_
; }
78 bool is_before_unload_dialog() const { return is_before_unload_dialog_
; }
79 bool is_reload() const { return is_reload_
; }
82 // Notifies the delegate with the result of the dialog.
83 void NotifyDelegate(bool success
, const base::string16
& prompt_text
,
84 bool suppress_js_messages
);
86 // A map of extra Chrome-only data associated with the delegate_. The keys
87 // come from |GetSerializedOriginForWebContents|.
88 ExtraDataMap
* extra_data_map_
;
90 // Information about the message box is held in the following variables.
91 const content::JavaScriptMessageType javascript_message_type_
;
92 base::string16 message_text_
;
93 base::string16 default_prompt_text_
;
94 bool display_suppress_checkbox_
;
95 bool is_before_unload_dialog_
;
98 content::JavaScriptDialogManager::DialogClosedCallback callback_
;
100 // Used only for testing. Specifies alternative prompt text that should be
101 // used when notifying the delegate, if |use_override_prompt_text_| is true.
102 base::string16 override_prompt_text_
;
103 bool use_override_prompt_text_
;
105 DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog
);
108 } // namespace app_modal
110 #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_APP_MODAL_DIALOG_H_