web_dialog_ui.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_UI_H_
  5. #define UI_WEB_DIALOGS_WEB_DIALOG_UI_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "content/public/browser/web_contents_delegate.h"
  8. #include "content/public/browser/web_ui_controller.h"
  9. #include "ui/base/ui_base_types.h"
  10. #include "ui/web_dialogs/web_dialogs_export.h"
  11. #include "ui/webui/mojo_web_ui_controller.h"
  12. #include "url/gurl.h"
  13. namespace content {
  14. class WebContents;
  15. }
  16. namespace ui {
  17. class WebDialogDelegate;
  18. class WEB_DIALOGS_EXPORT WebDialogUIBase {
  19. public:
  20. // Sets the delegate on the WebContents.
  21. static void SetDelegate(content::WebContents* web_contents,
  22. WebDialogDelegate* delegate);
  23. WebDialogUIBase(content::WebUI* web_ui);
  24. WebDialogUIBase(const WebDialogUIBase&) = delete;
  25. WebDialogUIBase& operator=(const WebDialogUIBase&) = delete;
  26. // Close the dialog, passing the specified arguments to the close handler.
  27. void CloseDialog(const base::Value::List& args);
  28. protected:
  29. virtual ~WebDialogUIBase();
  30. // Prepares |render_frame_host| to host a dialog.
  31. void HandleRenderFrameCreated(content::RenderFrameHost* render_frame_host);
  32. private:
  33. // Gets the delegate for the WebContent set with SetDelegate.
  34. static WebDialogDelegate* GetDelegate(content::WebContents* web_contents);
  35. // JS message handler.
  36. void OnDialogClosed(const base::Value::List& args);
  37. raw_ptr<content::WebUI> web_ui_;
  38. };
  39. // Displays file URL contents inside a modal web dialog.
  40. //
  41. // This application really should not use WebContents + WebUI. It should instead
  42. // just embed a RenderView in a dialog and be done with it.
  43. //
  44. // Before loading a URL corresponding to this WebUI, the caller should set its
  45. // delegate as user data on the WebContents by calling SetDelegate(). This WebUI
  46. // will pick it up from there and call it back. This is a bit of a hack to allow
  47. // the dialog to pass its delegate to the Web UI without having nasty accessors
  48. // on the WebContents. The correct design using RVH directly would avoid all of
  49. // this.
  50. class WEB_DIALOGS_EXPORT WebDialogUI : public WebDialogUIBase,
  51. public content::WebUIController {
  52. public:
  53. // When created, the delegate should already be set as user data on the
  54. // WebContents.
  55. explicit WebDialogUI(content::WebUI* web_ui);
  56. ~WebDialogUI() override;
  57. WebDialogUI(const WebDialogUI&) = delete;
  58. WebDialogUI& operator=(const WebDialogUI&) = delete;
  59. private:
  60. // content::WebUIController:
  61. void WebUIRenderFrameCreated(
  62. content::RenderFrameHost* render_frame_host) override;
  63. };
  64. // Displays file URL contents inside a modal web dialog while also enabling
  65. // Mojo calls to be made from within the dialog.
  66. class WEB_DIALOGS_EXPORT MojoWebDialogUI : public WebDialogUIBase,
  67. public MojoWebUIController {
  68. public:
  69. // When created, the delegate should already be set as user data on the
  70. // WebContents.
  71. explicit MojoWebDialogUI(content::WebUI* web_ui);
  72. ~MojoWebDialogUI() override;
  73. MojoWebDialogUI(const MojoWebDialogUI&) = delete;
  74. MojoWebDialogUI& operator=(const MojoWebDialogUI&) = delete;
  75. private:
  76. // content::WebUIController:
  77. void WebUIRenderFrameCreated(
  78. content::RenderFrameHost* render_frame_host) override;
  79. };
  80. } // namespace ui
  81. #endif // UI_WEB_DIALOGS_WEB_DIALOG_UI_H_