| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef COMPONENTS_MUS_WS_WINDOW_SERVER_H_ | |
| 6 #define COMPONENTS_MUS_WS_WINDOW_SERVER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/macros.h" | |
| 16 #include "components/mus/public/interfaces/window_manager_window_tree_factory.mo
jom.h" | |
| 17 #include "components/mus/public/interfaces/window_tree.mojom.h" | |
| 18 #include "components/mus/public/interfaces/window_tree_host.mojom.h" | |
| 19 #include "components/mus/surfaces/surfaces_state.h" | |
| 20 #include "components/mus/ws/display.h" | |
| 21 #include "components/mus/ws/display_manager_delegate.h" | |
| 22 #include "components/mus/ws/ids.h" | |
| 23 #include "components/mus/ws/operation.h" | |
| 24 #include "components/mus/ws/server_window_delegate.h" | |
| 25 #include "components/mus/ws/server_window_observer.h" | |
| 26 #include "components/mus/ws/user_id_tracker.h" | |
| 27 #include "components/mus/ws/user_id_tracker_observer.h" | |
| 28 #include "components/mus/ws/window_manager_window_tree_factory_set.h" | |
| 29 #include "mojo/public/cpp/bindings/array.h" | |
| 30 #include "mojo/public/cpp/bindings/binding.h" | |
| 31 | |
| 32 namespace mus { | |
| 33 namespace ws { | |
| 34 | |
| 35 class AccessPolicy; | |
| 36 class DisplayManager; | |
| 37 class ServerWindow; | |
| 38 class UserActivityMonitor; | |
| 39 class WindowManagerState; | |
| 40 class WindowServerDelegate; | |
| 41 class WindowTree; | |
| 42 class WindowTreeBinding; | |
| 43 | |
| 44 // WindowServer manages the set of clients of the window server (all the | |
| 45 // WindowTrees) as well as providing the root of the hierarchy. | |
| 46 class WindowServer : public ServerWindowDelegate, | |
| 47 public ServerWindowObserver, | |
| 48 public DisplayManagerDelegate, | |
| 49 public UserIdTrackerObserver { | |
| 50 public: | |
| 51 WindowServer(WindowServerDelegate* delegate, | |
| 52 const scoped_refptr<mus::SurfacesState>& surfaces_state); | |
| 53 ~WindowServer() override; | |
| 54 | |
| 55 WindowServerDelegate* delegate() { return delegate_; } | |
| 56 | |
| 57 UserIdTracker* user_id_tracker() { return &user_id_tracker_; } | |
| 58 const UserIdTracker* user_id_tracker() const { return &user_id_tracker_; } | |
| 59 | |
| 60 DisplayManager* display_manager() { return display_manager_.get(); } | |
| 61 const DisplayManager* display_manager() const { | |
| 62 return display_manager_.get(); | |
| 63 } | |
| 64 | |
| 65 // Creates a new ServerWindow. The return value is owned by the caller, but | |
| 66 // must be destroyed before WindowServer. | |
| 67 ServerWindow* CreateServerWindow( | |
| 68 const WindowId& id, | |
| 69 const std::map<std::string, std::vector<uint8_t>>& properties); | |
| 70 | |
| 71 // Returns the id for the next WindowTree. | |
| 72 ClientSpecificId GetAndAdvanceNextClientId(); | |
| 73 | |
| 74 // See description of WindowTree::Embed() for details. This assumes | |
| 75 // |transport_window_id| is valid. | |
| 76 WindowTree* EmbedAtWindow(ServerWindow* root, | |
| 77 const UserId& user_id, | |
| 78 mojom::WindowTreeClientPtr client, | |
| 79 uint32_t flags, | |
| 80 std::unique_ptr<AccessPolicy> access_policy); | |
| 81 | |
| 82 // Adds |tree_impl_ptr| to the set of known trees. Use DestroyTree() to | |
| 83 // destroy the tree. | |
| 84 void AddTree(std::unique_ptr<WindowTree> tree_impl_ptr, | |
| 85 std::unique_ptr<WindowTreeBinding> binding, | |
| 86 mojom::WindowTreePtr tree_ptr); | |
| 87 WindowTree* CreateTreeForWindowManager( | |
| 88 const UserId& user_id, | |
| 89 mojom::WindowTreeRequest window_tree_request, | |
| 90 mojom::WindowTreeClientPtr window_tree_client); | |
| 91 // Invoked when a WindowTree's connection encounters an error. | |
| 92 void DestroyTree(WindowTree* tree); | |
| 93 | |
| 94 // Returns the tree by client id. | |
| 95 WindowTree* GetTreeWithId(ClientSpecificId client_id); | |
| 96 | |
| 97 WindowTree* GetTreeWithClientName(const std::string& client_name); | |
| 98 | |
| 99 size_t num_trees() const { return tree_map_.size(); } | |
| 100 | |
| 101 // Returns the Window identified by |id|. | |
| 102 ServerWindow* GetWindow(const WindowId& id); | |
| 103 | |
| 104 // Schedules a paint for the specified region in the coordinates of |window|. | |
| 105 void SchedulePaint(ServerWindow* window, const gfx::Rect& bounds); | |
| 106 | |
| 107 OperationType current_operation_type() const { | |
| 108 return current_operation_ ? current_operation_->type() | |
| 109 : OperationType::NONE; | |
| 110 } | |
| 111 | |
| 112 // Returns true if the specified client issued the current operation. | |
| 113 bool IsOperationSource(ClientSpecificId client_id) const { | |
| 114 return current_operation_ && | |
| 115 current_operation_->source_tree_id() == client_id; | |
| 116 } | |
| 117 | |
| 118 // Invoked when a client messages a client about the change. This is used | |
| 119 // to avoid sending ServerChangeIdAdvanced() unnecessarily. | |
| 120 void OnTreeMessagedClient(ClientSpecificId id); | |
| 121 | |
| 122 // Returns true if OnTreeMessagedClient() was invoked for id. | |
| 123 bool DidTreeMessageClient(ClientSpecificId id) const; | |
| 124 | |
| 125 // Returns the WindowTree that has |id| as a root. | |
| 126 WindowTree* GetTreeWithRoot(const ServerWindow* window) { | |
| 127 return const_cast<WindowTree*>( | |
| 128 const_cast<const WindowServer*>(this)->GetTreeWithRoot(window)); | |
| 129 } | |
| 130 const WindowTree* GetTreeWithRoot(const ServerWindow* window) const; | |
| 131 | |
| 132 void OnFirstWindowManagerWindowTreeFactoryReady(); | |
| 133 | |
| 134 UserActivityMonitor* GetUserActivityMonitorForUser(const UserId& user_id); | |
| 135 | |
| 136 WindowManagerWindowTreeFactorySet* window_manager_window_tree_factory_set() { | |
| 137 return &window_manager_window_tree_factory_set_; | |
| 138 } | |
| 139 | |
| 140 // Sets focus to |window|. Returns true if |window| already has focus, or | |
| 141 // focus was successfully changed. Returns |false| if |window| is not a valid | |
| 142 // window to receive focus. | |
| 143 bool SetFocusedWindow(ServerWindow* window); | |
| 144 ServerWindow* GetFocusedWindow(); | |
| 145 | |
| 146 // Returns a change id for the window manager that is associated with | |
| 147 // |source| and |client_change_id|. When the window manager replies | |
| 148 // WindowManagerChangeCompleted() is called to obtain the original source | |
| 149 // and client supplied change_id that initiated the called. | |
| 150 uint32_t GenerateWindowManagerChangeId(WindowTree* source, | |
| 151 uint32_t client_change_id); | |
| 152 | |
| 153 // Called when a response from the window manager is obtained. Calls to | |
| 154 // the client that initiated the change with the change id originally | |
| 155 // supplied by the client. | |
| 156 void WindowManagerChangeCompleted(uint32_t window_manager_change_id, | |
| 157 bool success); | |
| 158 void WindowManagerCreatedTopLevelWindow(WindowTree* wm_tree, | |
| 159 uint32_t window_manager_change_id, | |
| 160 const ServerWindow* window); | |
| 161 | |
| 162 // Called when we get an unexpected message from the WindowManager. | |
| 163 // TODO(sky): decide what we want to do here. | |
| 164 void WindowManagerSentBogusMessage() {} | |
| 165 | |
| 166 // These functions trivially delegate to all WindowTrees, which in | |
| 167 // term notify their clients. | |
| 168 void ProcessWindowBoundsChanged(const ServerWindow* window, | |
| 169 const gfx::Rect& old_bounds, | |
| 170 const gfx::Rect& new_bounds); | |
| 171 void ProcessClientAreaChanged( | |
| 172 const ServerWindow* window, | |
| 173 const gfx::Insets& new_client_area, | |
| 174 const std::vector<gfx::Rect>& new_additional_client_areas); | |
| 175 void ProcessLostCapture(const ServerWindow* window); | |
| 176 void ProcessWillChangeWindowHierarchy(const ServerWindow* window, | |
| 177 const ServerWindow* new_parent, | |
| 178 const ServerWindow* old_parent); | |
| 179 void ProcessWindowHierarchyChanged(const ServerWindow* window, | |
| 180 const ServerWindow* new_parent, | |
| 181 const ServerWindow* old_parent); | |
| 182 void ProcessWindowReorder(const ServerWindow* window, | |
| 183 const ServerWindow* relative_window, | |
| 184 const mojom::OrderDirection direction); | |
| 185 void ProcessWindowDeleted(const ServerWindow* window); | |
| 186 void ProcessWillChangeWindowPredefinedCursor(ServerWindow* window, | |
| 187 int32_t cursor_id); | |
| 188 | |
| 189 // Sends an |event| to all WindowTrees belonging to |user_id| that might be | |
| 190 // observing events. Skips |ignore_tree| if it is non-null. | |
| 191 void SendToEventObservers(const ui::Event& event, | |
| 192 const UserId& user_id, | |
| 193 WindowTree* ignore_tree); | |
| 194 | |
| 195 // Sets a callback to be called whenever a ServerWindow is scheduled for | |
| 196 // a [re]paint. This should only be called in a test configuration. | |
| 197 void SetPaintCallback(const base::Callback<void(ServerWindow*)>& callback); | |
| 198 | |
| 199 private: | |
| 200 friend class Operation; | |
| 201 | |
| 202 using WindowTreeMap = | |
| 203 std::map<ClientSpecificId, std::unique_ptr<WindowTree>>; | |
| 204 using UserActivityMonitorMap = | |
| 205 std::map<UserId, std::unique_ptr<UserActivityMonitor>>; | |
| 206 | |
| 207 struct InFlightWindowManagerChange { | |
| 208 // Identifies the client that initiated the change. | |
| 209 ClientSpecificId client_id; | |
| 210 | |
| 211 // Change id supplied by the client. | |
| 212 uint32_t client_change_id; | |
| 213 }; | |
| 214 | |
| 215 using InFlightWindowManagerChangeMap = | |
| 216 std::map<uint32_t, InFlightWindowManagerChange>; | |
| 217 | |
| 218 bool GetAndClearInFlightWindowManagerChange( | |
| 219 uint32_t window_manager_change_id, | |
| 220 InFlightWindowManagerChange* change); | |
| 221 | |
| 222 // Invoked when a client is about to execute a window server operation. | |
| 223 // Subsequently followed by FinishOperation() once the change is done. | |
| 224 // | |
| 225 // Changes should never nest, meaning each PrepareForOperation() must be | |
| 226 // balanced with a call to FinishOperation() with no PrepareForOperation() | |
| 227 // in between. | |
| 228 void PrepareForOperation(Operation* op); | |
| 229 | |
| 230 // Balances a call to PrepareForOperation(). | |
| 231 void FinishOperation(); | |
| 232 | |
| 233 // Updates the native cursor by figuring out what window is under the mouse | |
| 234 // cursor. This is run in response to events that change the bounds or window | |
| 235 // hierarchy. | |
| 236 void UpdateNativeCursorFromMouseLocation(ServerWindow* window); | |
| 237 | |
| 238 // Updates the native cursor if the cursor is currently inside |window|. This | |
| 239 // is run in response to events that change the mouse cursor properties of | |
| 240 // |window|. | |
| 241 void UpdateNativeCursorIfOver(ServerWindow* window); | |
| 242 | |
| 243 // Overridden from ServerWindowDelegate: | |
| 244 mus::SurfacesState* GetSurfacesState() override; | |
| 245 void OnScheduleWindowPaint(ServerWindow* window) override; | |
| 246 const ServerWindow* GetRootWindow(const ServerWindow* window) const override; | |
| 247 void ScheduleSurfaceDestruction(ServerWindow* window) override; | |
| 248 | |
| 249 // Overridden from ServerWindowObserver: | |
| 250 void OnWindowDestroyed(ServerWindow* window) override; | |
| 251 void OnWillChangeWindowHierarchy(ServerWindow* window, | |
| 252 ServerWindow* new_parent, | |
| 253 ServerWindow* old_parent) override; | |
| 254 void OnWindowHierarchyChanged(ServerWindow* window, | |
| 255 ServerWindow* new_parent, | |
| 256 ServerWindow* old_parent) override; | |
| 257 void OnWindowBoundsChanged(ServerWindow* window, | |
| 258 const gfx::Rect& old_bounds, | |
| 259 const gfx::Rect& new_bounds) override; | |
| 260 void OnWindowClientAreaChanged( | |
| 261 ServerWindow* window, | |
| 262 const gfx::Insets& new_client_area, | |
| 263 const std::vector<gfx::Rect>& new_additional_client_areas) override; | |
| 264 void OnWindowReordered(ServerWindow* window, | |
| 265 ServerWindow* relative, | |
| 266 mojom::OrderDirection direction) override; | |
| 267 void OnWillChangeWindowVisibility(ServerWindow* window) override; | |
| 268 void OnWindowVisibilityChanged(ServerWindow* window) override; | |
| 269 void OnWindowOpacityChanged(ServerWindow* window, | |
| 270 float old_opacity, | |
| 271 float new_opacity) override; | |
| 272 void OnWindowSharedPropertyChanged( | |
| 273 ServerWindow* window, | |
| 274 const std::string& name, | |
| 275 const std::vector<uint8_t>* new_data) override; | |
| 276 void OnWindowPredefinedCursorChanged(ServerWindow* window, | |
| 277 int32_t cursor_id) override; | |
| 278 void OnWindowNonClientCursorChanged(ServerWindow* window, | |
| 279 int32_t cursor_id) override; | |
| 280 void OnWindowTextInputStateChanged(ServerWindow* window, | |
| 281 const ui::TextInputState& state) override; | |
| 282 void OnTransientWindowAdded(ServerWindow* window, | |
| 283 ServerWindow* transient_child) override; | |
| 284 void OnTransientWindowRemoved(ServerWindow* window, | |
| 285 ServerWindow* transient_child) override; | |
| 286 | |
| 287 // DisplayManagerDelegate: | |
| 288 void OnFirstDisplayReady() override; | |
| 289 void OnNoMoreDisplays() override; | |
| 290 bool GetFrameDecorationsForUser( | |
| 291 const UserId& user_id, | |
| 292 mojom::FrameDecorationValuesPtr* values) override; | |
| 293 WindowManagerState* GetWindowManagerStateForUser( | |
| 294 const UserId& user_id) override; | |
| 295 | |
| 296 // UserIdTrackerObserver: | |
| 297 void OnActiveUserIdChanged(const UserId& previously_active_id, | |
| 298 const UserId& active_id) override; | |
| 299 void OnUserIdAdded(const UserId& id) override; | |
| 300 void OnUserIdRemoved(const UserId& id) override; | |
| 301 | |
| 302 UserIdTracker user_id_tracker_; | |
| 303 | |
| 304 WindowServerDelegate* delegate_; | |
| 305 | |
| 306 // State for rendering into a Surface. | |
| 307 scoped_refptr<mus::SurfacesState> surfaces_state_; | |
| 308 | |
| 309 // ID to use for next WindowTree. | |
| 310 ClientSpecificId next_client_id_; | |
| 311 | |
| 312 std::unique_ptr<DisplayManager> display_manager_; | |
| 313 | |
| 314 // Set of WindowTrees. | |
| 315 WindowTreeMap tree_map_; | |
| 316 | |
| 317 // If non-null then we're processing a client operation. The Operation is | |
| 318 // not owned by us (it's created on the stack by WindowTree). | |
| 319 Operation* current_operation_; | |
| 320 | |
| 321 bool in_destructor_; | |
| 322 | |
| 323 // Maps from window manager change id to the client that initiated the | |
| 324 // request. | |
| 325 InFlightWindowManagerChangeMap in_flight_wm_change_map_; | |
| 326 | |
| 327 // Next id supplied to the window manager. | |
| 328 uint32_t next_wm_change_id_; | |
| 329 | |
| 330 base::Callback<void(ServerWindow*)> window_paint_callback_; | |
| 331 | |
| 332 UserActivityMonitorMap activity_monitor_map_; | |
| 333 | |
| 334 WindowManagerWindowTreeFactorySet window_manager_window_tree_factory_set_; | |
| 335 | |
| 336 DISALLOW_COPY_AND_ASSIGN(WindowServer); | |
| 337 }; | |
| 338 | |
| 339 } // namespace ws | |
| 340 } // namespace mus | |
| 341 | |
| 342 #endif // COMPONENTS_MUS_WS_WINDOW_SERVER_H_ | |
| OLD | NEW |