web_contents_modal_dialog_manager.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 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_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
  5. #define COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_
  6. #include <memory>
  7. #include "base/containers/circular_deque.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "build/build_config.h"
  10. #include "components/web_modal/single_web_contents_dialog_manager.h"
  11. #include "components/web_modal/web_modal_export.h"
  12. #include "content/public/browser/web_contents_observer.h"
  13. #include "content/public/browser/web_contents_user_data.h"
  14. #include "ui/gfx/native_widget_types.h"
  15. namespace web_modal {
  16. class WebContentsModalDialogManagerDelegate;
  17. // Per-WebContents class to manage WebContents-modal dialogs.
  18. class WEB_MODAL_EXPORT WebContentsModalDialogManager
  19. : public SingleWebContentsDialogManagerDelegate,
  20. public content::WebContentsObserver,
  21. public content::WebContentsUserData<WebContentsModalDialogManager> {
  22. public:
  23. WebContentsModalDialogManager(const WebContentsModalDialogManager&) = delete;
  24. WebContentsModalDialogManager& operator=(
  25. const WebContentsModalDialogManager&) = delete;
  26. ~WebContentsModalDialogManager() override;
  27. WebContentsModalDialogManagerDelegate* delegate() const { return delegate_; }
  28. void SetDelegate(WebContentsModalDialogManagerDelegate* d);
  29. // Allow clients to supply their own native dialog manager. Suitable for
  30. // bubble clients.
  31. void ShowDialogWithManager(
  32. gfx::NativeWindow dialog,
  33. std::unique_ptr<SingleWebContentsDialogManager> manager);
  34. // Returns true if any dialogs are active and not closed.
  35. bool IsDialogActive() const;
  36. // Focus the topmost modal dialog. IsDialogActive() must be true when calling
  37. // this function.
  38. void FocusTopmostDialog() const;
  39. // SingleWebContentsDialogManagerDelegate:
  40. content::WebContents* GetWebContents() const override;
  41. void WillClose(gfx::NativeWindow dialog) override;
  42. // For testing.
  43. class TestApi {
  44. public:
  45. explicit TestApi(WebContentsModalDialogManager* manager)
  46. : manager_(manager) {}
  47. TestApi(const TestApi&) = delete;
  48. TestApi& operator=(const TestApi&) = delete;
  49. void CloseAllDialogs() { manager_->CloseAllDialogs(); }
  50. void WebContentsVisibilityChanged(content::Visibility visibility) {
  51. manager_->OnVisibilityChanged(visibility);
  52. }
  53. private:
  54. raw_ptr<WebContentsModalDialogManager> manager_;
  55. };
  56. // Closes all WebContentsModalDialogs.
  57. void CloseAllDialogs();
  58. private:
  59. explicit WebContentsModalDialogManager(content::WebContents* web_contents);
  60. friend class content::WebContentsUserData<WebContentsModalDialogManager>;
  61. struct DialogState {
  62. DialogState(gfx::NativeWindow dialog,
  63. std::unique_ptr<SingleWebContentsDialogManager> manager);
  64. DialogState(DialogState&& state);
  65. ~DialogState();
  66. gfx::NativeWindow dialog;
  67. std::unique_ptr<SingleWebContentsDialogManager> manager;
  68. };
  69. // Blocks/unblocks interaction with renderer process.
  70. void BlockWebContentsInteraction(bool blocked);
  71. bool IsWebContentsVisible() const;
  72. // Overridden from content::WebContentsObserver:
  73. void DidFinishNavigation(
  74. content::NavigationHandle* navigation_handle) override;
  75. void DidGetIgnoredUIEvent() override;
  76. void OnVisibilityChanged(content::Visibility visibility) override;
  77. void WebContentsDestroyed() override;
  78. // Delegate for notifying our owner about stuff. Not owned by us.
  79. raw_ptr<WebContentsModalDialogManagerDelegate> delegate_ = nullptr;
  80. // All active dialogs.
  81. base::circular_deque<DialogState> child_dialogs_;
  82. // Whether the WebContents' visibility is content::Visibility::HIDDEN.
  83. bool web_contents_is_hidden_;
  84. // True while closing the dialogs on WebContents close.
  85. bool closing_all_dialogs_ = false;
  86. WEB_CONTENTS_USER_DATA_KEY_DECL();
  87. };
  88. } // namespace web_modal
  89. #endif // COMPONENTS_WEB_MODAL_WEB_CONTENTS_MODAL_DIALOG_MANAGER_H_