window_tree_host_manager.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 ASH_DISPLAY_WINDOW_TREE_HOST_MANAGER_H_
  5. #define ASH_DISPLAY_WINDOW_TREE_HOST_MANAGER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <vector>
  10. #include "ash/ash_export.h"
  11. #include "ash/host/ash_window_tree_host_delegate.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/gtest_prod_util.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/observer_list.h"
  16. #include "base/timer/timer.h"
  17. #include "ui/aura/window.h"
  18. #include "ui/aura/window_tree_host_observer.h"
  19. #include "ui/base/ime/ime_key_event_dispatcher.h"
  20. #include "ui/base/ime/input_method.h"
  21. #include "ui/display/display_observer.h"
  22. #include "ui/display/manager/content_protection_manager.h"
  23. #include "ui/display/manager/display_manager.h"
  24. #include "ui/gfx/geometry/point.h"
  25. namespace aura {
  26. class WindowTreeHost;
  27. }
  28. namespace gfx {
  29. class Insets;
  30. }
  31. namespace ash {
  32. class AshWindowTreeHost;
  33. struct AshWindowTreeHostInitParams;
  34. class CursorWindowController;
  35. class FocusActivationStore;
  36. class MirrorWindowController;
  37. class RootWindowController;
  38. // WindowTreeHostManager owns and maintains RootWindows for each attached
  39. // display, keeping them in sync with display configuration changes.
  40. class ASH_EXPORT WindowTreeHostManager
  41. : public display::DisplayObserver,
  42. public aura::WindowTreeHostObserver,
  43. public display::ContentProtectionManager::Observer,
  44. public display::DisplayManager::Delegate,
  45. public ui::ImeKeyEventDispatcher,
  46. public AshWindowTreeHostDelegate {
  47. public:
  48. // TODO(oshima): Consider moving this to display::DisplayObserver.
  49. class ASH_EXPORT Observer {
  50. public:
  51. virtual ~Observer() {}
  52. // Invoked only once after all displays are initialized
  53. // after startup.
  54. virtual void OnDisplaysInitialized() {}
  55. // Invoked when the display configuration change is requested,
  56. // but before the change is applied to aura/ash.
  57. virtual void OnDisplayConfigurationChanging() {}
  58. // Invoked when the all display configuration changes
  59. // have been applied.
  60. virtual void OnDisplayConfigurationChanged() {}
  61. // Invoked in WindowTreeHostManager::Shutdown().
  62. virtual void OnWindowTreeHostManagerShutdown() {}
  63. };
  64. WindowTreeHostManager();
  65. WindowTreeHostManager(const WindowTreeHostManager&) = delete;
  66. WindowTreeHostManager& operator=(const WindowTreeHostManager&) = delete;
  67. ~WindowTreeHostManager() override;
  68. void Start();
  69. void Shutdown();
  70. // Returns primary display's ID.
  71. // TODO(oshima): Move this out from WindowTreeHostManager;
  72. static int64_t GetPrimaryDisplayId();
  73. // Returns true if the current primary display ID is valid.
  74. static bool HasValidPrimaryDisplayId();
  75. CursorWindowController* cursor_window_controller() {
  76. return cursor_window_controller_.get();
  77. }
  78. MirrorWindowController* mirror_window_controller() {
  79. return mirror_window_controller_.get();
  80. }
  81. // Create a WindowTreeHost for the primary display. This replaces
  82. // |initial_bounds| in |init_params|.
  83. void CreatePrimaryHost(const AshWindowTreeHostInitParams& init_params);
  84. // Initializes all WindowTreeHosts.
  85. void InitHosts();
  86. // Add/Remove observers.
  87. void AddObserver(Observer* observer);
  88. void RemoveObserver(Observer* observer);
  89. // Returns the root window for primary display.
  90. aura::Window* GetPrimaryRootWindow();
  91. // Returns the root window for |display_id|.
  92. aura::Window* GetRootWindowForDisplayId(int64_t id);
  93. // Returns AshWTH for given display |id|. Returns nullptr if the WTH does not
  94. // exist.
  95. AshWindowTreeHost* GetAshWindowTreeHostForDisplayId(int64_t id);
  96. // Returns all root windows. In non extended desktop mode, this
  97. // returns the primary root window only.
  98. aura::Window::Windows GetAllRootWindows();
  99. // Returns all root window controllers. In non extended desktop
  100. // mode, this return a RootWindowController for the primary root window only.
  101. std::vector<RootWindowController*> GetAllRootWindowControllers();
  102. // Gets/Sets/Clears the overscan insets for the specified |display_id|. See
  103. // display_manager.h for the details.
  104. gfx::Insets GetOverscanInsets(int64_t display_id) const;
  105. void SetOverscanInsets(int64_t display_id, const gfx::Insets& insets_in_dip);
  106. // Checks if the mouse pointer is on one of displays, and moves to
  107. // the center of the nearest display if it's outside of all displays.
  108. void UpdateMouseLocationAfterDisplayChange();
  109. // Sets the work area's |insets| to the display assigned to |window|.
  110. bool UpdateWorkAreaOfDisplayNearestWindow(const aura::Window* window,
  111. const gfx::Insets& insets);
  112. ui::InputMethod* input_method() { return input_method_.get(); }
  113. // display::DisplayObserver overrides:
  114. void OnDisplayAdded(const display::Display& display) override;
  115. void OnDisplayRemoved(const display::Display& display) override;
  116. void OnDisplayMetricsChanged(const display::Display& display,
  117. uint32_t metrics) override;
  118. // aura::WindowTreeHostObserver overrides:
  119. void OnHostResized(aura::WindowTreeHost* host) override;
  120. // display::ContentProtectionManager::Observer overrides:
  121. void OnDisplaySecurityChanged(int64_t display_id, bool secure) override;
  122. // display::DisplayManager::Delegate overrides:
  123. void CreateOrUpdateMirroringDisplay(
  124. const display::DisplayInfoList& info_list) override;
  125. void CloseMirroringDisplayIfNotNecessary() override;
  126. void SetPrimaryDisplayId(int64_t id) override;
  127. void PreDisplayConfigurationChange(bool clear_focus) override;
  128. void PostDisplayConfigurationChange() override;
  129. // ui::ImeKeyEventDispatcher overrides:
  130. ui::EventDispatchDetails DispatchKeyEventPostIME(
  131. ui::KeyEvent* event) override;
  132. // ash::AshWindowTreeHostDelegate overrides:
  133. const display::Display* GetDisplayById(int64_t display_id) const override;
  134. void SetCurrentEventTargeterSourceHost(
  135. aura::WindowTreeHost* targeter_src_host) override;
  136. private:
  137. FRIEND_TEST_ALL_PREFIXES(WindowTreeHostManagerTest, BoundsUpdated);
  138. FRIEND_TEST_ALL_PREFIXES(WindowTreeHostManagerTest, SecondaryDisplayLayout);
  139. friend class MirrorWindowController;
  140. // Creates a WindowTreeHost for |display| and stores it in the
  141. // |window_tree_hosts_| map.
  142. AshWindowTreeHost* AddWindowTreeHostForDisplay(
  143. const display::Display& display,
  144. const AshWindowTreeHostInitParams& params);
  145. // Delete the AsWindowTreeHost. This does not remove the entry from
  146. // |window_tree_hosts_|. Caller has to explicitly remove it.
  147. void DeleteHost(AshWindowTreeHost* host_to_delete);
  148. typedef std::map<int64_t, AshWindowTreeHost*> WindowTreeHostMap;
  149. // The mapping from display ID to its window tree host.
  150. WindowTreeHostMap window_tree_hosts_;
  151. base::ObserverList<Observer, true>::Unchecked observers_;
  152. // Store the primary window tree host temporarily while replacing
  153. // display.
  154. AshWindowTreeHost* primary_tree_host_for_replace_;
  155. std::unique_ptr<FocusActivationStore> focus_activation_store_;
  156. std::unique_ptr<CursorWindowController> cursor_window_controller_;
  157. std::unique_ptr<MirrorWindowController> mirror_window_controller_;
  158. std::unique_ptr<ui::InputMethod> input_method_;
  159. // Stores the current cursor location (in native coordinates and screen
  160. // coordinates respectively). The locations are used to restore the cursor
  161. // location when the display configuration changes and to determine whether
  162. // the mouse should be moved after a display configuration change.
  163. gfx::Point cursor_location_in_native_coords_for_restore_;
  164. gfx::Point cursor_location_in_screen_coords_for_restore_;
  165. // Stores the cursor's display. The id is used to determine whether the mouse
  166. // should be moved after a display configuration change.
  167. int64_t cursor_display_id_for_restore_;
  168. // Receive DisplayObserver callbacks between Start and Shutdown.
  169. absl::optional<display::ScopedDisplayObserver> display_observer_;
  170. // A repeating timer to trigger sending UMA metrics for primary display's
  171. // effective resolution at fixed intervals.
  172. std::unique_ptr<base::RepeatingTimer> effective_resolution_UMA_timer_;
  173. base::WeakPtrFactory<WindowTreeHostManager> weak_ptr_factory_{this};
  174. };
  175. } // namespace ash
  176. #endif // ASH_DISPLAY_WINDOW_TREE_HOST_MANAGER_H_