web_dialog_web_contents_delegate.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_WEB_CONTENTS_DELEGATE_H_
  5. #define UI_WEB_DIALOGS_WEB_DIALOG_WEB_CONTENTS_DELEGATE_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "content/public/browser/web_contents_delegate.h"
  9. #include "ui/web_dialogs/web_dialogs_export.h"
  10. namespace blink {
  11. namespace mojom {
  12. class FileChooserParams;
  13. }
  14. } // namespace blink
  15. namespace content {
  16. class BrowserContext;
  17. class FileSelectListener;
  18. class RenderFrameHost;
  19. }
  20. namespace ui {
  21. // This class implements (and mostly ignores) most of
  22. // content::WebContentsDelegate for use in a Web dialog. Subclasses need only
  23. // override a few methods instead of the everything from
  24. // content::WebContentsDelegate; this way, implementations on all platforms
  25. // behave consistently.
  26. class WEB_DIALOGS_EXPORT WebDialogWebContentsDelegate
  27. : public content::WebContentsDelegate {
  28. public:
  29. // Handles OpenURLFromTab and AddNewContents for WebDialogWebContentsDelegate.
  30. class WebContentsHandler {
  31. public:
  32. virtual ~WebContentsHandler() {}
  33. virtual content::WebContents* OpenURLFromTab(
  34. content::BrowserContext* context,
  35. content::WebContents* source,
  36. const content::OpenURLParams& params) = 0;
  37. virtual void AddNewContents(
  38. content::BrowserContext* context,
  39. content::WebContents* source,
  40. std::unique_ptr<content::WebContents> new_contents,
  41. const GURL& target_url,
  42. WindowOpenDisposition disposition,
  43. const gfx::Rect& initial_rect,
  44. bool user_gesture) = 0;
  45. // This is added to allow the injection of a file chooser handler.
  46. // The WebDialogWebContentsDelegate's original implementation does not
  47. // do anything for file chooser request
  48. virtual void RunFileChooser(
  49. content::RenderFrameHost* render_frame_host,
  50. scoped_refptr<content::FileSelectListener> listener,
  51. const blink::mojom::FileChooserParams& params) = 0;
  52. };
  53. // |context| and |handler| must be non-NULL.
  54. WebDialogWebContentsDelegate(content::BrowserContext* context,
  55. std::unique_ptr<WebContentsHandler> handler);
  56. WebDialogWebContentsDelegate(const WebDialogWebContentsDelegate&) = delete;
  57. WebDialogWebContentsDelegate& operator=(const WebDialogWebContentsDelegate&) =
  58. delete;
  59. ~WebDialogWebContentsDelegate() override;
  60. // The returned browser context is guaranteed to be original if non-NULL.
  61. content::BrowserContext* browser_context() const {
  62. return browser_context_;
  63. }
  64. // Calling this causes all following events sent from the
  65. // WebContents object to be ignored. It also makes all following
  66. // calls to browser_context() return NULL.
  67. void Detach();
  68. // content::WebContentsDelegate declarations.
  69. content::WebContents* OpenURLFromTab(
  70. content::WebContents* source,
  71. const content::OpenURLParams& params) override;
  72. void AddNewContents(content::WebContents* source,
  73. std::unique_ptr<content::WebContents> new_contents,
  74. const GURL& target_url,
  75. WindowOpenDisposition disposition,
  76. const gfx::Rect& initial_rect,
  77. bool user_gesture,
  78. bool* was_blocked) override;
  79. bool PreHandleGestureEvent(content::WebContents* source,
  80. const blink::WebGestureEvent& event) override;
  81. void RunFileChooser(content::RenderFrameHost* render_frame_host,
  82. scoped_refptr<content::FileSelectListener> listener,
  83. const blink::mojom::FileChooserParams& params) override;
  84. private:
  85. // Weak pointer. Always an original profile.
  86. raw_ptr<content::BrowserContext> browser_context_;
  87. std::unique_ptr<WebContentsHandler> const handler_;
  88. };
  89. } // namespace ui
  90. #endif // UI_WEB_DIALOGS_WEB_DIALOG_WEB_CONTENTS_DELEGATE_H_