web_dialog_delegate.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
  5. #define UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
  6. #include <string>
  7. #include <vector>
  8. #include "content/public/browser/web_contents_delegate.h"
  9. #include "ui/base/ui_base_types.h"
  10. #include "ui/base/window_open_disposition.h"
  11. #include "ui/web_dialogs/web_dialogs_export.h"
  12. class GURL;
  13. namespace content {
  14. class RenderFrameHost;
  15. class WebContents;
  16. class WebUI;
  17. class WebUIMessageHandler;
  18. struct ContextMenuParams;
  19. struct OpenURLParams;
  20. }
  21. namespace gfx {
  22. class Size;
  23. }
  24. namespace ui {
  25. class Accelerator;
  26. // Implement this class to receive notifications.
  27. class WEB_DIALOGS_EXPORT WebDialogDelegate {
  28. public:
  29. enum class FrameKind {
  30. kDialog, // Does not include a title bar or frame caption buttons.
  31. kNonClient, // Includes a non client frame view with title & buttons.
  32. };
  33. // Returns the modal type for this dialog. Only called once, during
  34. // WebDialogView creation.
  35. virtual ModalType GetDialogModalType() const = 0;
  36. // Returns the title of the dialog.
  37. virtual std::u16string GetDialogTitle() const = 0;
  38. // Returns the title to be read with screen readers.
  39. virtual std::u16string GetAccessibleDialogTitle() const;
  40. // Returns the dialog's name identifier. Used to identify this dialog for
  41. // state restoration.
  42. virtual std::string GetDialogName() const;
  43. // Get the HTML file path for the content to load in the dialog.
  44. virtual GURL GetDialogContentURL() const = 0;
  45. // Get WebUIMessageHandler objects to handle messages from the HTML/JS page.
  46. // The handlers are used to send and receive messages from the page while it
  47. // is still open. Ownership of each handler is taken over by the WebUI
  48. // hosting the page.
  49. virtual void GetWebUIMessageHandlers(
  50. std::vector<content::WebUIMessageHandler*>* handlers) const = 0;
  51. // Get the size of the dialog. Implementations can safely assume |size| is a
  52. // valid pointer. Callers should be able to handle the case where
  53. // implementations do not write into |size|.
  54. virtual void GetDialogSize(gfx::Size* size) const = 0;
  55. // Get the minimum size of the dialog. The default implementation just calls
  56. // GetDialogSize().
  57. virtual void GetMinimumDialogSize(gfx::Size* size) const;
  58. // Gets the JSON string input to use when showing the dialog.
  59. virtual std::string GetDialogArgs() const = 0;
  60. // Returns true if the dialog can ever be resized. Default implementation
  61. // returns true.
  62. bool can_resize() const { return can_resize_; }
  63. void set_can_resize(bool can_resize) { can_resize_ = can_resize; }
  64. // Returns true if the dialog can ever be maximized. Default implementation
  65. // returns false.
  66. virtual bool CanMaximizeDialog() const;
  67. // Returns true if the dialog can ever be minimized. Default implementation
  68. // returns false.
  69. bool can_minimize() const { return can_minimize_; }
  70. void set_can_minimize(bool can_minimize) { can_minimize_ = can_minimize; }
  71. // A callback to notify the delegate that |source|'s loading state has
  72. // changed.
  73. virtual void OnLoadingStateChanged(content::WebContents* source) {}
  74. // A callback to notify the delegate that a web dialog has been shown.
  75. // |webui| is the WebUI with which the dialog is associated.
  76. virtual void OnDialogShown(content::WebUI* webui) {}
  77. // A callback to notify the delegate that the window is requesting to be
  78. // closed. If this returns true, the dialog is closed, otherwise the
  79. // dialog remains open. Default implementation returns true.
  80. virtual bool OnDialogCloseRequested();
  81. // Called when the dialog's window is certainly about to close, but teardown
  82. // has not started yet. This differs from OnDialogCloseRequested in that
  83. // OnDialogCloseRequested is part of the process of deciding whether to close
  84. // a window, while OnDialogWillClose is called as soon as it is known for
  85. // certain that the window is about to be closed.
  86. virtual void OnDialogWillClose() {}
  87. // A callback to notify the delegate that the dialog is about to close due to
  88. // the user pressing the ESC key.
  89. virtual void OnDialogClosingFromKeyEvent() {}
  90. // A callback to notify the delegate that the dialog closed.
  91. // IMPORTANT: Implementations should delete |this| here (unless they've
  92. // arranged for the delegate to be deleted in some other way, e.g. by
  93. // registering it as a message handler in the WebUI object).
  94. virtual void OnDialogClosed(const std::string& json_retval) = 0;
  95. // A callback to notify the delegate that the dialog is being closed in
  96. // response to a "dialogClose" message from WebUI.
  97. virtual void OnDialogCloseFromWebUI(const std::string& json_retval);
  98. // A callback to notify the delegate that the contents are requesting
  99. // to be closed. This could be in response to a number of events
  100. // that are handled by the WebContents. If the output parameter
  101. // is set to true, then the dialog is closed. The default is false.
  102. // |out_close_dialog| is never NULL.
  103. virtual void OnCloseContents(content::WebContents* source,
  104. bool* out_close_dialog) = 0;
  105. // Returns true if escape should immediately close the dialog. Default is
  106. // true.
  107. virtual bool ShouldCloseDialogOnEscape() const;
  108. // A callback to allow the delegate to dictate that the window should not
  109. // have a title bar. This is useful when presenting branded interfaces.
  110. virtual bool ShouldShowDialogTitle() const = 0;
  111. // A callback to allow the delegate to center title text. Default is
  112. // false.
  113. virtual bool ShouldCenterDialogTitleText() const;
  114. // Returns true if the dialog should show a close button in the title bar.
  115. // Default implementation returns true.
  116. virtual bool ShouldShowCloseButton() const;
  117. // A callback to allow the delegate to inhibit context menu or show
  118. // customized menu.
  119. //
  120. // The `render_frame_host` represents the frame that requests the context menu
  121. // (typically this frame is focused, but this is not necessarily the case -
  122. // see https://crbug.com/1257907#c14).
  123. //
  124. // Returns true iff you do NOT want the standard context menu to be
  125. // shown (because you want to handle it yourself).
  126. virtual bool HandleContextMenu(content::RenderFrameHost& render_frame_host,
  127. const content::ContextMenuParams& params);
  128. // A callback to allow the delegate to open a new URL inside |source|.
  129. // On return |out_new_contents| should contain the WebContents the URL
  130. // is opened in. Return false to use the default handler.
  131. virtual bool HandleOpenURLFromTab(content::WebContents* source,
  132. const content::OpenURLParams& params,
  133. content::WebContents** out_new_contents);
  134. // A callback to control whether a WebContents will be created. Returns
  135. // true to disallow the creation. Return false to use the default handler.
  136. virtual bool HandleShouldOverrideWebContentsCreation();
  137. // Stores the dialog bounds.
  138. virtual void StoreDialogSize(const gfx::Size& dialog_size) {}
  139. // Returns the accelerators handled by the delegate.
  140. virtual std::vector<Accelerator> GetAccelerators();
  141. // Returns true if |accelerator| is processed, otherwise false.
  142. virtual bool AcceleratorPressed(const Accelerator& accelerator);
  143. virtual void OnWebContentsFinishedLoad() {}
  144. virtual void RequestMediaAccessPermission(
  145. content::WebContents* web_contents,
  146. const content::MediaStreamRequest& request,
  147. content::MediaResponseCallback callback) {}
  148. virtual bool CheckMediaAccessPermission(
  149. content::RenderFrameHost* render_frame_host,
  150. const GURL& security_origin,
  151. blink::mojom::MediaStreamType type);
  152. // Whether to use dialog frame view for non client frame view.
  153. virtual FrameKind GetWebDialogFrameKind() const;
  154. virtual ~WebDialogDelegate() = default;
  155. private:
  156. bool can_minimize_ = false;
  157. bool can_resize_ = true;
  158. };
  159. } // namespace ui
  160. #endif // UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_