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