javascript_app_modal_dialog.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#ifndef CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JAVASCRIPT_APP_MODAL_DIALOG_H_
6#define CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JAVASCRIPT_APP_MODAL_DIALOG_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/time.h"
11#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h"
12#include "content/public/browser/javascript_dialog_manager.h"
13
14// Extra data for JavaScript dialogs to add Chrome-only features.
15class ChromeJavaScriptDialogExtraData {
16 public:
17  ChromeJavaScriptDialogExtraData();
18
19  // The time that the last JavaScript dialog was dismissed.
20  base::TimeTicks last_javascript_message_dismissal_;
21
22  // True if the user has decided to block future JavaScript dialogs.
23  bool suppress_javascript_messages_;
24};
25
26// A controller + model class for JavaScript alert, confirm, prompt, and
27// onbeforeunload dialog boxes.
28class JavaScriptAppModalDialog : public AppModalDialog {
29 public:
30  JavaScriptAppModalDialog(
31      content::WebContents* web_contents,
32      ChromeJavaScriptDialogExtraData* extra_data,
33      const string16& title,
34      content::JavaScriptMessageType javascript_message_type,
35      const string16& message_text,
36      const string16& default_prompt_text,
37      bool display_suppress_checkbox,
38      bool is_before_unload_dialog,
39      bool is_reload,
40      const content::JavaScriptDialogManager::DialogClosedCallback& callback);
41  virtual ~JavaScriptAppModalDialog();
42
43  // Overridden from AppModalDialog:
44  virtual NativeAppModalDialog* CreateNativeDialog() OVERRIDE;
45  virtual bool IsJavaScriptModalDialog() OVERRIDE;
46  virtual void Invalidate() OVERRIDE;
47
48  // Callbacks from NativeDialog when the user accepts or cancels the dialog.
49  void OnCancel(bool suppress_js_messages);
50  void OnAccept(const string16& prompt_text, bool suppress_js_messages);
51
52  // NOTE: This is only called under Views, and should be removed. Any critical
53  // work should be done in OnCancel or OnAccept. See crbug.com/63732 for more.
54  void OnClose();
55
56  // Used only for testing. The dialog will use the given text when notifying
57  // its delegate instead of whatever the UI reports.
58  void SetOverridePromptText(const string16& prompt_text);
59
60  // Accessors
61  content::JavaScriptMessageType javascript_message_type() const {
62    return javascript_message_type_;
63  }
64  string16 message_text() const { return message_text_; }
65  string16 default_prompt_text() const { return default_prompt_text_; }
66  bool display_suppress_checkbox() const { return display_suppress_checkbox_; }
67  bool is_before_unload_dialog() const { return is_before_unload_dialog_; }
68  bool is_reload() const { return is_reload_; }
69
70 private:
71  // Notifies the delegate with the result of the dialog.
72  void NotifyDelegate(bool success, const string16& prompt_text,
73                      bool suppress_js_messages);
74
75  // The extra Chrome-only data associated with the delegate_.
76  ChromeJavaScriptDialogExtraData* extra_data_;
77
78  // Information about the message box is held in the following variables.
79  const content::JavaScriptMessageType javascript_message_type_;
80  string16 message_text_;
81  string16 default_prompt_text_;
82  bool display_suppress_checkbox_;
83  bool is_before_unload_dialog_;
84  bool is_reload_;
85
86  content::JavaScriptDialogManager::DialogClosedCallback callback_;
87
88  // Used only for testing. Specifies alternative prompt text that should be
89  // used when notifying the delegate, if |use_override_prompt_text_| is true.
90  string16 override_prompt_text_;
91  bool use_override_prompt_text_;
92
93  DISALLOW_COPY_AND_ASSIGN(JavaScriptAppModalDialog);
94};
95
96#endif  // CHROME_BROWSER_UI_APP_MODAL_DIALOGS_JAVASCRIPT_APP_MODAL_DIALOG_H_
97