Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: services/ui/ws/event_dispatcher.h

Issue 2884463002: Make event-targeting asynchronous in window server. (Closed)
Patch Set: WeakPtrFactory; const &; etc Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef SERVICES_UI_WS_EVENT_DISPATCHER_H_ 5 #ifndef SERVICES_UI_WS_EVENT_DISPATCHER_H_
6 #define SERVICES_UI_WS_EVENT_DISPATCHER_H_ 6 #define SERVICES_UI_WS_EVENT_DISPATCHER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
15 #include "services/ui/common/types.h" 16 #include "services/ui/common/types.h"
16 #include "services/ui/public/interfaces/cursor/cursor.mojom.h" 17 #include "services/ui/public/interfaces/cursor/cursor.mojom.h"
17 #include "services/ui/public/interfaces/window_manager.mojom.h" 18 #include "services/ui/public/interfaces/window_manager.mojom.h"
18 #include "services/ui/ws/drag_cursor_updater.h" 19 #include "services/ui/ws/drag_cursor_updater.h"
19 #include "services/ui/ws/modal_window_controller.h" 20 #include "services/ui/ws/modal_window_controller.h"
20 #include "services/ui/ws/server_window_observer.h" 21 #include "services/ui/ws/server_window_observer.h"
21 #include "ui/gfx/geometry/rect_f.h" 22 #include "ui/gfx/geometry/rect_f.h"
22 23
23 namespace ui { 24 namespace ui {
24 class Event; 25 class Event;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 return mouse_cursor_source_window_; 116 return mouse_cursor_source_window_;
116 } 117 }
117 118
118 // Returns the window the mouse cursor is taken from. This does not take 119 // Returns the window the mouse cursor is taken from. This does not take
119 // into account drags. In other words if there is a drag on going the mouse 120 // into account drags. In other words if there is a drag on going the mouse
120 // comes comes from a different window. 121 // comes comes from a different window.
121 const ServerWindow* GetWindowForMouseCursor() const; 122 const ServerWindow* GetWindowForMouseCursor() const;
122 123
123 // If the mouse cursor is still over |mouse_cursor_source_window_|, updates 124 // If the mouse cursor is still over |mouse_cursor_source_window_|, updates
124 // whether we are in the non-client area. Used when 125 // whether we are in the non-client area. Used when
125 // |mouse_cursor_source_window_| has changed its properties. 126 // |mouse_cursor_source_window_| has changed its properties. |callback| is
126 void UpdateNonClientAreaForCurrentWindow(); 127 // called after the update is finished.
128 void UpdateNonClientAreaForCurrentWindow(const base::Closure& callback);
sky 2017/05/15 21:20:23 Having any of these functions take callbacks makes
riajiang 2017/05/17 02:01:59 Makes sense! With erg@'s CL https://codereview.chr
127 129
128 // Possibly updates the cursor. If we aren't in an implicit capture, we take 130 // Possibly updates the cursor. If we aren't in an implicit capture, we take
129 // the last known location of the mouse pointer, and look for the 131 // the last known location of the mouse pointer, and look for the
130 // ServerWindow* under it. 132 // ServerWindow* under it. |callback| is called after the update is finished.
131 void UpdateCursorProviderByLastKnownLocation(); 133 void UpdateCursorProviderByLastKnownLocation(const base::Closure& callback);
132 134
133 // Adds an accelerator with the given id and event-matcher. If an accelerator 135 // Adds an accelerator with the given id and event-matcher. If an accelerator
134 // already exists with the same id or the same matcher, then the accelerator 136 // already exists with the same id or the same matcher, then the accelerator
135 // is not added. Returns whether adding the accelerator was successful or not. 137 // is not added. Returns whether adding the accelerator was successful or not.
136 bool AddAccelerator(uint32_t id, mojom::EventMatcherPtr event_matcher); 138 bool AddAccelerator(uint32_t id, mojom::EventMatcherPtr event_matcher);
137 139
138 void RemoveAccelerator(uint32_t id); 140 void RemoveAccelerator(uint32_t id);
139 141
140 // Processes the supplied event, informing the delegate as approriate. This 142 // Processes the supplied event, informing the delegate as approriate. This
141 // may result in generating any number of events. If |match_phase| is 143 // may result in generating any number of events. If |match_phase| is
142 // ANY and there is a matching accelerator with PRE_TARGET found, than only 144 // ANY and there is a matching accelerator with PRE_TARGET found, than only
143 // OnAccelerator() is called. The expectation is after the PRE_TARGET has been 145 // OnAccelerator() is called. The expectation is after the PRE_TARGET has been
144 // handled this is again called with an AcceleratorMatchPhase of POST_ONLY. 146 // handled this is again called with an AcceleratorMatchPhase of POST_ONLY.
145 void ProcessEvent(const ui::Event& event, AcceleratorMatchPhase match_phase); 147 void ProcessEvent(const ui::Event& event, AcceleratorMatchPhase match_phase);
146 148
149 bool HittestInFlight();
sky 2017/05/15 21:20:23 IsHitTestInFlight() and const. Also, use HitTest e
riajiang 2017/05/17 02:01:59 Done.
150
147 private: 151 private:
148 friend class test::EventDispatcherTestApi; 152 friend class test::EventDispatcherTestApi;
149 153
150 // Keeps track of state associated with an active pointer. 154 // Keeps track of state associated with an active pointer.
151 struct PointerTarget { 155 struct PointerTarget {
152 PointerTarget() 156 PointerTarget()
153 : window(nullptr), 157 : window(nullptr),
154 is_mouse_event(false), 158 is_mouse_event(false),
155 in_nonclient_area(false), 159 in_nonclient_area(false),
156 is_pointer_down(false) {} 160 is_pointer_down(false) {}
157 161
158 // The target window, which may be null. null is used in two situations: 162 // The target window, which may be null. null is used in two situations:
159 // when there is no valid window target, or there was a target but the 163 // when there is no valid window target, or there was a target but the
160 // window is destroyed before a corresponding release/cancel. 164 // window is destroyed before a corresponding release/cancel.
161 ServerWindow* window; 165 ServerWindow* window;
162 166
163 bool is_mouse_event; 167 bool is_mouse_event;
164 168
165 // Did the pointer event start in the non-client area. 169 // Did the pointer event start in the non-client area.
166 bool in_nonclient_area; 170 bool in_nonclient_area;
167 171
168 bool is_pointer_down; 172 bool is_pointer_down;
169 }; 173 };
170 174
175 using PointerTargetForEventCallback = base::OnceCallback<void(PointerTarget)>;
176 using HittestCallback = base::OnceCallback<void(DeepestWindow)>;
177
178 struct HittestRequest {
179 HittestRequest(const gfx::Point& location,
180 HittestCallback hittest_callback);
181 ~HittestRequest();
182
183 gfx::Point hittest_location;
184 HittestCallback hittest_callback;
185 };
186
171 void SetMouseCursorSourceWindow(ServerWindow* window); 187 void SetMouseCursorSourceWindow(ServerWindow* window);
172 188
173 void ProcessKeyEvent(const ui::KeyEvent& event, 189 void ProcessKeyEvent(const ui::KeyEvent& event,
174 AcceleratorMatchPhase match_phase); 190 AcceleratorMatchPhase match_phase);
175 191
176 bool IsTrackingPointer(int32_t pointer_id) const { 192 bool IsTrackingPointer(int32_t pointer_id) const {
177 return pointer_targets_.count(pointer_id) > 0; 193 return pointer_targets_.count(pointer_id) > 0;
178 } 194 }
179 195
180 // EventDispatcher provides the following logic for pointer events: 196 // EventDispatcher provides the following logic for pointer events:
181 // . wheel events go to the current target of the associated pointer. If 197 // . wheel events go to the current target of the associated pointer. If
182 // there is no target, they go to the deepest window. 198 // there is no target, they go to the deepest window.
183 // . move (not drag) events go to the deepest window. 199 // . move (not drag) events go to the deepest window.
184 // . when a pointer goes down all events until the corresponding up or 200 // . when a pointer goes down all events until the corresponding up or
185 // cancel go to the deepest target. For mouse events the up only occurs 201 // cancel go to the deepest target. For mouse events the up only occurs
186 // when no buttons on the mouse are down. 202 // when no buttons on the mouse are down.
187 // This also generates exit events as appropriate. For example, if the mouse 203 // This also generates exit events as appropriate. For example, if the mouse
188 // moves between one window to another an exit is generated on the first. 204 // moves between one window to another an exit is generated on the first.
189 void ProcessPointerEvent(const ui::PointerEvent& event); 205 void ProcessPointerEvent(const ui::PointerEvent& event);
206 void ProcessPointerEventOnFoundTarget(const ui::PointerEvent& event,
207 PointerTarget pointer_target_found);
190 208
191 // Adds |pointer_target| to |pointer_targets_|. 209 // Adds |pointer_target| to |pointer_targets_|.
192 void StartTrackingPointer(int32_t pointer_id, 210 void StartTrackingPointer(int32_t pointer_id,
193 const PointerTarget& pointer_target); 211 const PointerTarget& pointer_target);
194 212
195 // Removes a PointerTarget from |pointer_targets_|. 213 // Removes a PointerTarget from |pointer_targets_|.
196 void StopTrackingPointer(int32_t pointer_id); 214 void StopTrackingPointer(int32_t pointer_id);
197 215
198 // Starts tracking the pointer for |event|, or if already tracking the 216 // Starts tracking the pointer for |event|, or if already tracking the
199 // pointer sends the appropriate event to the delegate and updates the 217 // pointer sends the appropriate event to the delegate and updates the
200 // currently tracked PointerTarget appropriately. 218 // currently tracked PointerTarget appropriately.
201 void UpdateTargetForPointer(int32_t pointer_id, 219 void UpdateTargetForPointer(int32_t pointer_id,
202 const ui::LocatedEvent& event); 220 const ui::PointerEvent& event,
221 const PointerTarget& pointer_target_found);
203 222
204 // Returns a PointerTarget for the supplied event. If there is no valid 223 // |callback| is called with a PointerTarget for the supplied event. If there
205 // event target for the specified location |window| in the returned value is 224 // is no valid event target for the specified location |window| in the
206 // null. 225 // returned value is null.
207 PointerTarget PointerTargetForEvent(const ui::LocatedEvent& event); 226 void PointerTargetForEvent(const ui::PointerEvent& event,
227 PointerTargetForEventCallback callback);
228 void PointerTargetForEventOnFoundWindow(
229 const ui::PointerEvent& event,
230 PointerTargetForEventCallback callback,
231 DeepestWindow deepest_window);
208 232
209 // Returns true if any pointers are in the pressed/down state. 233 // Returns true if any pointers are in the pressed/down state.
210 bool AreAnyPointersDown() const; 234 bool AreAnyPointersDown() const;
211 235
212 // If |target->window| is valid, then passes the event to the delegate. 236 // If |target->window| is valid, then passes the event to the delegate.
213 void DispatchToPointerTarget(const PointerTarget& target, 237 void DispatchToPointerTarget(const PointerTarget& target,
214 const ui::LocatedEvent& event); 238 const ui::LocatedEvent& event);
215 239
216 // Dispatch |event| to the delegate. 240 // Dispatch |event| to the delegate.
217 void DispatchToClient(ServerWindow* window, 241 void DispatchToClient(ServerWindow* window,
218 ClientSpecificId client_id, 242 ClientSpecificId client_id,
219 const ui::LocatedEvent& event); 243 const ui::LocatedEvent& event);
220 244
221 // Stops sending pointer events to |window|. This does not remove the entry 245 // Stops sending pointer events to |window|. This does not remove the entry
222 // for |window| from |pointer_targets_|, rather it nulls out the window. This 246 // for |window| from |pointer_targets_|, rather it nulls out the window. This
223 // way we continue to eat events until the up/cancel is received. 247 // way we continue to eat events until the up/cancel is received.
224 void CancelPointerEventsToTarget(ServerWindow* window); 248 void CancelPointerEventsToTarget(ServerWindow* window);
225 249
226 // Used to observe a window. Can be called multiple times on a window. To 250 // Used to observe a window. Can be called multiple times on a window. To
227 // unobserve a window, UnobserveWindow() should be called the same number of 251 // unobserve a window, UnobserveWindow() should be called the same number of
228 // times. 252 // times.
229 void ObserveWindow(ServerWindow* winodw); 253 void ObserveWindow(ServerWindow* winodw);
230 void UnobserveWindow(ServerWindow* winodw); 254 void UnobserveWindow(ServerWindow* winodw);
231 255
232 // Returns an Accelerator bound to the specified code/flags, and of the 256 // Returns an Accelerator bound to the specified code/flags, and of the
233 // matching |phase|. Otherwise returns null. 257 // matching |phase|. Otherwise returns null.
234 Accelerator* FindAccelerator(const ui::KeyEvent& event, 258 Accelerator* FindAccelerator(const ui::KeyEvent& event,
235 const ui::mojom::AcceleratorPhase phase); 259 const ui::mojom::AcceleratorPhase phase);
236 260
237 DeepestWindow FindDeepestVisibleWindowForEvents(const gfx::Point& location); 261 // Call WindowFinder to find the deepest visible window for |location|.
262 // |callback| is called with the window found.
263 void FindDeepestVisibleWindowForEvents(const gfx::Point& location,
264 HittestCallback callback);
265 void FindDeepestVisibleWindowForEventsImpl(const gfx::Point& location,
266 HittestCallback callback);
267 void FindDeepestVisibleWindowForEventsAsync(const gfx::Point& location,
268 HittestCallback callback);
269
270 void UpdateNonClientAreaForCurrentWindowOnFoundWindow(
271 const base::Closure& callback,
272 DeepestWindow deepest_window);
273 void UpdateCursorProviderByLastKnownLocationOnFoundWindow(
274 const base::Closure& callback,
275 DeepestWindow deepest_window);
276
277 void ProcessNextHittesetRequestFromQueue();
238 278
239 // Clears the implicit captures in |pointer_targets_|, with the exception of 279 // Clears the implicit captures in |pointer_targets_|, with the exception of
240 // |window|. |window| may be null. |client_id| is the target client of 280 // |window|. |window| may be null. |client_id| is the target client of
241 // |window|. 281 // |window|.
242 void CancelImplicitCaptureExcept(ServerWindow* window, 282 void CancelImplicitCaptureExcept(ServerWindow* window,
243 ClientSpecificId client_id); 283 ClientSpecificId client_id);
244 284
245 // ServerWindowObserver: 285 // ServerWindowObserver:
246 void OnWillChangeWindowHierarchy(ServerWindow* window, 286 void OnWillChangeWindowHierarchy(ServerWindow* window,
247 ServerWindow* new_parent, 287 ServerWindow* new_parent,
(...skipping 16 matching lines...) Expand all
264 bool mouse_button_down_; 304 bool mouse_button_down_;
265 ServerWindow* mouse_cursor_source_window_; 305 ServerWindow* mouse_cursor_source_window_;
266 bool mouse_cursor_in_non_client_area_; 306 bool mouse_cursor_in_non_client_area_;
267 307
268 // The on screen location of the mouse pointer. This can be outside the 308 // The on screen location of the mouse pointer. This can be outside the
269 // bounds of |mouse_cursor_source_window_|, which can capture the cursor. 309 // bounds of |mouse_cursor_source_window_|, which can capture the cursor.
270 gfx::Point mouse_pointer_last_location_; 310 gfx::Point mouse_pointer_last_location_;
271 311
272 std::map<uint32_t, std::unique_ptr<Accelerator>> accelerators_; 312 std::map<uint32_t, std::unique_ptr<Accelerator>> accelerators_;
273 313
314 // True if we are waiting for the result of a hit-test. False otherwise.
315 bool hittest_in_flight_;
316 std::queue<std::unique_ptr<HittestRequest>> hittest_request_queue_;
317
274 using PointerIdToTargetMap = std::map<int32_t, PointerTarget>; 318 using PointerIdToTargetMap = std::map<int32_t, PointerTarget>;
275 // |pointer_targets_| contains the active pointers. For a mouse based pointer 319 // |pointer_targets_| contains the active pointers. For a mouse based pointer
276 // a PointerTarget is always active (and present in |pointer_targets_|). For 320 // a PointerTarget is always active (and present in |pointer_targets_|). For
277 // touch based pointers the pointer is active while down and removed on 321 // touch based pointers the pointer is active while down and removed on
278 // cancel or up. 322 // cancel or up.
279 PointerIdToTargetMap pointer_targets_; 323 PointerIdToTargetMap pointer_targets_;
280 324
281 // Keeps track of number of observe requests for each observed window. 325 // Keeps track of number of observe requests for each observed window.
282 std::map<const ServerWindow*, uint8_t> observed_windows_; 326 std::map<const ServerWindow*, uint8_t> observed_windows_;
283 327
284 #if !defined(NDEBUG) 328 #if !defined(NDEBUG)
285 std::unique_ptr<ui::Event> previous_event_; 329 std::unique_ptr<ui::Event> previous_event_;
286 AcceleratorMatchPhase previous_accelerator_match_phase_ = 330 AcceleratorMatchPhase previous_accelerator_match_phase_ =
287 AcceleratorMatchPhase::ANY; 331 AcceleratorMatchPhase::ANY;
288 #endif 332 #endif
289 333
334 base::WeakPtrFactory<EventDispatcher> weak_ptr_factory_;
335
290 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); 336 DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
291 }; 337 };
292 338
293 } // namespace ws 339 } // namespace ws
294 } // namespace ui 340 } // namespace ui
295 341
296 #endif // SERVICES_UI_WS_EVENT_DISPATCHER_H_ 342 #endif // SERVICES_UI_WS_EVENT_DISPATCHER_H_
OLDNEW
« no previous file with comments | « no previous file | services/ui/ws/event_dispatcher.cc » ('j') | services/ui/ws/window_manager_state.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698