shell_desktop_controller_aura.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2014 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 EXTENSIONS_SHELL_BROWSER_SHELL_DESKTOP_CONTROLLER_AURA_H_
  5. #define EXTENSIONS_SHELL_BROWSER_SHELL_DESKTOP_CONTROLLER_AURA_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "components/keep_alive_registry/keep_alive_state_observer.h"
  14. #include "extensions/shell/browser/desktop_controller.h"
  15. #include "extensions/shell/browser/root_window_controller.h"
  16. #include "ui/aura/window.h"
  17. #include "ui/base/ime/ime_key_event_dispatcher.h"
  18. #include "ui/display/display.h"
  19. #if BUILDFLAG(IS_CHROMEOS_ASH)
  20. #include "chromeos/dbus/power/power_manager_client.h"
  21. #include "ui/display/manager/display_configurator.h"
  22. #endif
  23. namespace aura {
  24. class WindowTreeHost;
  25. } // namespace aura
  26. namespace content {
  27. class BrowserContext;
  28. } // namespace content
  29. namespace display {
  30. class Screen;
  31. } // namespace display
  32. namespace gfx {
  33. class Size;
  34. } // namespace gfx
  35. namespace ui {
  36. class InputMethod;
  37. #if BUILDFLAG(IS_CHROMEOS_ASH)
  38. class UserActivityDetector;
  39. class UserActivityPowerManagerNotifier;
  40. #endif
  41. } // namespace ui
  42. namespace wm {
  43. class CompoundEventFilter;
  44. class CursorManager;
  45. class FocusController;
  46. } // namespace wm
  47. namespace extensions {
  48. class AppWindowClient;
  49. // Simple desktop controller for app_shell. Associates each display with a
  50. // RootWindowController. Adds AppWindows by passing them to the nearest
  51. // RootWindowController.
  52. class ShellDesktopControllerAura
  53. : public DesktopController,
  54. public RootWindowController::DesktopDelegate,
  55. #if BUILDFLAG(IS_CHROMEOS_ASH)
  56. public chromeos::PowerManagerClient::Observer,
  57. public display::DisplayConfigurator::Observer,
  58. #endif
  59. public ui::ImeKeyEventDispatcher,
  60. public KeepAliveStateObserver {
  61. public:
  62. explicit ShellDesktopControllerAura(content::BrowserContext* browser_context);
  63. ShellDesktopControllerAura(const ShellDesktopControllerAura&) = delete;
  64. ShellDesktopControllerAura& operator=(const ShellDesktopControllerAura&) =
  65. delete;
  66. ~ShellDesktopControllerAura() override;
  67. // DesktopController:
  68. void PreMainMessageLoopRun() override;
  69. void WillRunMainMessageLoop(
  70. std::unique_ptr<base::RunLoop>& run_loop) override;
  71. void PostMainMessageLoopRun() override;
  72. void AddAppWindow(AppWindow* app_window, gfx::NativeWindow window) override;
  73. void CloseAppWindows() override;
  74. // RootWindowController::DesktopDelegate:
  75. void CloseRootWindowController(
  76. RootWindowController* root_window_controller) override;
  77. #if BUILDFLAG(IS_CHROMEOS_ASH)
  78. // chromeos::PowerManagerClient::Observer:
  79. void PowerButtonEventReceived(bool down, base::TimeTicks timestamp) override;
  80. // display::DisplayConfigurator::Observer:
  81. void OnDisplayModeChanged(
  82. const display::DisplayConfigurator::DisplayStateList& displays) override;
  83. #endif
  84. // ui::ImeKeyEventDispatcher:
  85. ui::EventDispatchDetails DispatchKeyEventPostIME(
  86. ui::KeyEvent* key_event) override;
  87. // KeepAliveStateObserver:
  88. void OnKeepAliveStateChanged(bool is_keeping_alive) override;
  89. void OnKeepAliveRestartStateChanged(bool can_restart) override;
  90. // Returns the WindowTreeHost for the primary display.
  91. aura::WindowTreeHost* GetPrimaryHost();
  92. // Returns all root windows managed by RootWindowControllers.
  93. aura::Window::Windows GetAllRootWindows();
  94. // Updates the bounds of |app_window|. This may involve reparenting the window
  95. // to a different root window if the new bounds are in a different display.
  96. void SetWindowBoundsInScreen(AppWindow* app_window, const gfx::Rect& bounds);
  97. protected:
  98. // Creates and sets the aura clients and window manager stuff. Subclass may
  99. // initialize different sets of the clients.
  100. virtual void InitWindowManager();
  101. // Removes all RootWindowControllers and tears down our aura clients.
  102. virtual void TearDownWindowManager();
  103. private:
  104. // Creates a RootWindowController to host AppWindows.
  105. std::unique_ptr<RootWindowController> CreateRootWindowControllerForDisplay(
  106. const display::Display& display);
  107. // Removes handlers from the RootWindowController so it can be destroyed.
  108. void TearDownRootWindowController(RootWindowController* root);
  109. // Quits if there are no app windows, and no keep-alives waiting for apps to
  110. // relaunch.
  111. void MaybeQuit();
  112. #if BUILDFLAG(IS_CHROMEOS_ASH)
  113. // Returns the desired dimensions of the RootWindowController from the command
  114. // line, or falls back to a default size.
  115. gfx::Size GetStartingWindowSize();
  116. // Returns the dimensions (in pixels) of the primary display, or an empty size
  117. // if the dimensions can't be determined or no display is connected.
  118. gfx::Size GetPrimaryDisplaySize();
  119. #endif
  120. const raw_ptr<content::BrowserContext> browser_context_;
  121. #if BUILDFLAG(IS_CHROMEOS_ASH)
  122. std::unique_ptr<display::DisplayConfigurator> display_configurator_;
  123. #endif
  124. std::unique_ptr<display::Screen> screen_;
  125. std::unique_ptr<wm::CompoundEventFilter> root_window_event_filter_;
  126. // Mapping from display ID to the RootWindowController created for that
  127. // display.
  128. std::map<int64_t, std::unique_ptr<RootWindowController>>
  129. root_window_controllers_;
  130. std::unique_ptr<ui::InputMethod> input_method_;
  131. std::unique_ptr<wm::FocusController> focus_controller_;
  132. std::unique_ptr<wm::CursorManager> cursor_manager_;
  133. #if BUILDFLAG(IS_CHROMEOS_ASH)
  134. std::unique_ptr<ui::UserActivityDetector> user_activity_detector_;
  135. std::unique_ptr<ui::UserActivityPowerManagerNotifier> user_activity_notifier_;
  136. #endif
  137. std::unique_ptr<AppWindowClient> app_window_client_;
  138. // NativeAppWindow::Close() deletes the AppWindow.
  139. std::list<AppWindow*> app_windows_;
  140. // Non-null between WillRunMainMessageLoop() and MaybeQuit().
  141. base::OnceClosure quit_when_idle_closure_;
  142. };
  143. } // namespace extensions
  144. #endif // EXTENSIONS_SHELL_BROWSER_SHELL_DESKTOP_CONTROLLER_AURA_H_