app_modal_dialog_controller.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_CONTROLLER_H_
  5. #define COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_CONTROLLER_H_
  6. #include <map>
  7. #include "base/callback.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "content/public/browser/javascript_dialog_manager.h"
  11. namespace javascript_dialogs {
  12. class AppModalDialogView;
  13. // Extra data for JavaScript dialogs to add Chrome-only features.
  14. class ChromeJavaScriptDialogExtraData {
  15. public:
  16. ChromeJavaScriptDialogExtraData();
  17. // True if the user has already seen a JavaScript dialog from the WebContents.
  18. bool has_already_shown_a_dialog_;
  19. // True if the user has decided to block future JavaScript dialogs.
  20. bool suppress_javascript_messages_;
  21. };
  22. // A controller + model class for JavaScript alert, confirm, prompt, and
  23. // onbeforeunload dialog boxes.
  24. class AppModalDialogController {
  25. public:
  26. typedef std::map<void*, ChromeJavaScriptDialogExtraData> ExtraDataMap;
  27. AppModalDialogController(
  28. content::WebContents* web_contents,
  29. ExtraDataMap* extra_data_map,
  30. const std::u16string& title,
  31. content::JavaScriptDialogType javascript_dialog_type,
  32. const std::u16string& message_text,
  33. const std::u16string& default_prompt_text,
  34. bool display_suppress_checkbox,
  35. bool is_before_unload_dialog,
  36. bool is_reload,
  37. content::JavaScriptDialogManager::DialogClosedCallback callback);
  38. AppModalDialogController(const AppModalDialogController&) = delete;
  39. AppModalDialogController& operator=(const AppModalDialogController&) = delete;
  40. ~AppModalDialogController();
  41. // Called by the AppModalDialogQueue to show this dialog.
  42. void ShowModalDialog();
  43. // Called by the AppModalDialogQueue to activate the dialog.
  44. void ActivateModalDialog();
  45. // Closes the dialog if it is showing.
  46. void CloseModalDialog();
  47. // Returns true if the dialog is still valid. As dialogs are created they are
  48. // added to the AppModalDialogQueue. When the current modal dialog finishes
  49. // and it's time to show the next dialog in the queue IsValid is invoked.
  50. // If IsValid returns false the dialog is deleted and not shown.
  51. bool IsValid();
  52. // Invalidates the dialog, therefore causing it to not be shown when its turn
  53. // to be shown comes around.
  54. void Invalidate();
  55. // Callbacks from NativeDialog when the user accepts or cancels the dialog.
  56. void OnCancel(bool suppress_js_messages);
  57. void OnAccept(const std::u16string& 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.
  60. void OnClose();
  61. // Used only for testing. The dialog will use the given text when notifying
  62. // its delegate instead of whatever the UI reports.
  63. void SetOverridePromptText(const std::u16string& prompt_text);
  64. // Accessors.
  65. std::u16string title() const { return title_; }
  66. AppModalDialogView* view() const { return view_; }
  67. content::WebContents* web_contents() const { return web_contents_; }
  68. content::JavaScriptDialogType javascript_dialog_type() const {
  69. return javascript_dialog_type_;
  70. }
  71. std::u16string message_text() const { return message_text_; }
  72. std::u16string default_prompt_text() const { return default_prompt_text_; }
  73. bool display_suppress_checkbox() const { return display_suppress_checkbox_; }
  74. bool is_before_unload_dialog() const { return is_before_unload_dialog_; }
  75. bool is_reload() const { return is_reload_; }
  76. private:
  77. // Notifies the delegate with the result of the dialog.
  78. void NotifyDelegate(bool success,
  79. const std::u16string& prompt_text,
  80. bool suppress_js_messages);
  81. void CallDialogClosedCallback(bool success,
  82. const std::u16string& prompt_text);
  83. // Completes dialog handling, shows next modal dialog from the queue.
  84. // TODO(beng): Get rid of this method.
  85. void CompleteDialog();
  86. // The title of the dialog.
  87. const std::u16string title_;
  88. // False if the dialog should no longer be shown, e.g. because the underlying
  89. // tab navigated away while the dialog was queued.
  90. bool valid_;
  91. // The toolkit-specific implementation of the app modal dialog box. When
  92. // non-null, |view_| owns |this|.
  93. raw_ptr<AppModalDialogView> view_;
  94. // The WebContents that opened this dialog.
  95. raw_ptr<content::WebContents> web_contents_;
  96. // A map of extra Chrome-only data associated with the delegate_. Can be
  97. // inspected via |extra_data_map_[web_contents_]|.
  98. raw_ptr<ExtraDataMap> extra_data_map_;
  99. // Information about the message box is held in the following variables.
  100. const content::JavaScriptDialogType javascript_dialog_type_;
  101. const std::u16string message_text_;
  102. const std::u16string default_prompt_text_;
  103. const bool display_suppress_checkbox_;
  104. const bool is_before_unload_dialog_;
  105. const bool is_reload_;
  106. content::JavaScriptDialogManager::DialogClosedCallback callback_;
  107. // Used only for testing. Specifies alternative prompt text that should be
  108. // used when notifying the delegate, if |use_override_prompt_text_| is true.
  109. std::u16string override_prompt_text_;
  110. bool use_override_prompt_text_;
  111. };
  112. // An interface to observe that a modal dialog is shown.
  113. class AppModalDialogObserver {
  114. public:
  115. AppModalDialogObserver();
  116. AppModalDialogObserver(const AppModalDialogObserver&) = delete;
  117. AppModalDialogObserver& operator=(const AppModalDialogObserver&) = delete;
  118. virtual ~AppModalDialogObserver();
  119. // Called when the modal dialog is shown.
  120. virtual void Notify(AppModalDialogController* dialog) = 0;
  121. };
  122. } // namespace javascript_dialogs
  123. #endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_CONTROLLER_H_