Update V8 to version 4.4.65.
[chromium-blink-merge.git] / ui / aura / window_event_dispatcher.cc
blob8b7db5d1eb1ec68d96d3cbcaee170b8c5cfee7f4
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.
5 #include "ui/aura/window_event_dispatcher.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/aura/client/capture_client.h"
12 #include "ui/aura/client/cursor_client.h"
13 #include "ui/aura/client/event_client.h"
14 #include "ui/aura/client/focus_client.h"
15 #include "ui/aura/client/screen_position_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_delegate.h"
19 #include "ui/aura/window_targeter.h"
20 #include "ui/aura/window_tracker.h"
21 #include "ui/aura/window_tree_host.h"
22 #include "ui/base/hit_test.h"
23 #include "ui/compositor/dip_util.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h"
26 #include "ui/events/gestures/gesture_recognizer.h"
27 #include "ui/events/gestures/gesture_types.h"
29 typedef ui::EventDispatchDetails DispatchDetails;
31 namespace aura {
33 namespace {
35 // Returns true if |target| has a non-client (frame) component at |location|,
36 // in window coordinates.
37 bool IsNonClientLocation(Window* target, const gfx::Point& location) {
38 if (!target->delegate())
39 return false;
40 int hit_test_code = target->delegate()->GetNonClientComponent(location);
41 return hit_test_code != HTCLIENT && hit_test_code != HTNOWHERE;
44 Window* ConsumerToWindow(ui::GestureConsumer* consumer) {
45 return consumer ? static_cast<Window*>(consumer) : NULL;
48 void SetLastMouseLocation(const Window* root_window,
49 const gfx::Point& location_in_root) {
50 client::ScreenPositionClient* client =
51 client::GetScreenPositionClient(root_window);
52 if (client) {
53 gfx::Point location_in_screen = location_in_root;
54 client->ConvertPointToScreen(root_window, &location_in_screen);
55 Env::GetInstance()->set_last_mouse_location(location_in_screen);
56 } else {
57 Env::GetInstance()->set_last_mouse_location(location_in_root);
61 bool IsEventCandidateForHold(const ui::Event& event) {
62 if (event.type() == ui::ET_TOUCH_MOVED)
63 return true;
64 if (event.type() == ui::ET_MOUSE_DRAGGED)
65 return true;
66 if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED))
67 return true;
68 return false;
71 } // namespace
73 ////////////////////////////////////////////////////////////////////////////////
74 // WindowEventDispatcher, public:
76 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host)
77 : host_(host),
78 touch_ids_down_(0),
79 mouse_pressed_handler_(NULL),
80 mouse_moved_handler_(NULL),
81 event_dispatch_target_(NULL),
82 old_dispatch_target_(NULL),
83 synthesize_mouse_move_(false),
84 move_hold_count_(0),
85 dispatching_held_event_(nullptr),
86 observer_manager_(this),
87 repost_event_factory_(this),
88 held_event_factory_(this) {
89 ui::GestureRecognizer::Get()->AddGestureEventHelper(this);
90 Env::GetInstance()->AddObserver(this);
93 WindowEventDispatcher::~WindowEventDispatcher() {
94 TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor");
95 Env::GetInstance()->RemoveObserver(this);
96 ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this);
99 void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent& event) {
100 DCHECK(event.type() == ui::ET_MOUSE_PRESSED ||
101 event.type() == ui::ET_GESTURE_TAP_DOWN);
102 // We allow for only one outstanding repostable event. This is used
103 // in exiting context menus. A dropped repost request is allowed.
104 if (event.type() == ui::ET_MOUSE_PRESSED) {
105 held_repostable_event_.reset(
106 new ui::MouseEvent(
107 static_cast<const ui::MouseEvent&>(event),
108 static_cast<aura::Window*>(event.target()),
109 window()));
110 base::MessageLoop::current()->PostNonNestableTask(
111 FROM_HERE, base::Bind(
112 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
113 repost_event_factory_.GetWeakPtr()));
114 } else {
115 DCHECK(event.type() == ui::ET_GESTURE_TAP_DOWN);
116 held_repostable_event_.reset();
117 // TODO(rbyers): Reposing of gestures is tricky to get
118 // right, so it's not yet supported. crbug.com/170987.
122 void WindowEventDispatcher::OnMouseEventsEnableStateChanged(bool enabled) {
123 // Send entered / exited so that visual state can be updated to match
124 // mouse events state.
125 PostSynthesizeMouseMove();
126 // TODO(mazda): Add code to disable mouse events when |enabled| == false.
129 void WindowEventDispatcher::DispatchCancelModeEvent() {
130 ui::CancelModeEvent event;
131 Window* focused_window = client::GetFocusClient(window())->GetFocusedWindow();
132 if (focused_window && !window()->Contains(focused_window))
133 focused_window = NULL;
134 DispatchDetails details =
135 DispatchEvent(focused_window ? focused_window : window(), &event);
136 if (details.dispatcher_destroyed)
137 return;
140 void WindowEventDispatcher::DispatchGestureEvent(ui::GestureEvent* event) {
141 DispatchDetails details = DispatchHeldEvents();
142 if (details.dispatcher_destroyed)
143 return;
144 Window* target = GetGestureTarget(event);
145 if (target) {
146 event->ConvertLocationToTarget(window(), target);
147 DispatchDetails details = DispatchEvent(target, event);
148 if (details.dispatcher_destroyed)
149 return;
153 DispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint(
154 Window* window,
155 const gfx::Point& point) {
156 ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EventTimeForNow(),
157 ui::EF_NONE, ui::EF_NONE);
158 return DispatchMouseEnterOrExit(window, event, ui::ET_MOUSE_EXITED);
161 void WindowEventDispatcher::ProcessedTouchEvent(Window* window,
162 ui::EventResult result) {
163 scoped_ptr<ui::GestureRecognizer::Gestures> gestures(
164 ui::GestureRecognizer::Get()->AckAsyncTouchEvent(result, window));
165 DispatchDetails details = ProcessGestures(gestures.get());
166 if (details.dispatcher_destroyed)
167 return;
170 void WindowEventDispatcher::HoldPointerMoves() {
171 if (!move_hold_count_)
172 held_event_factory_.InvalidateWeakPtrs();
173 ++move_hold_count_;
174 TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
175 this);
178 void WindowEventDispatcher::ReleasePointerMoves() {
179 --move_hold_count_;
180 DCHECK_GE(move_hold_count_, 0);
181 if (!move_hold_count_ && held_move_event_) {
182 // We don't want to call DispatchHeldEvents directly, because this might be
183 // called from a deep stack while another event, in which case dispatching
184 // another one may not be safe/expected. Instead we post a task, that we
185 // may cancel if HoldPointerMoves is called again before it executes.
186 base::MessageLoop::current()->PostNonNestableTask(
187 FROM_HERE, base::Bind(
188 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
189 held_event_factory_.GetWeakPtr()));
191 TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
194 gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
195 gfx::Point location = Env::GetInstance()->last_mouse_location();
196 client::ScreenPositionClient* client =
197 client::GetScreenPositionClient(window());
198 if (client)
199 client->ConvertPointFromScreen(window(), &location);
200 return location;
203 void WindowEventDispatcher::OnHostLostMouseGrab() {
204 mouse_pressed_handler_ = NULL;
205 mouse_moved_handler_ = NULL;
208 void WindowEventDispatcher::OnCursorMovedToRootLocation(
209 const gfx::Point& root_location) {
210 SetLastMouseLocation(window(), root_location);
212 // Synthesize a mouse move in case the cursor's location in root coordinates
213 // changed but its position in WindowTreeHost coordinates did not.
214 PostSynthesizeMouseMove();
217 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
218 OnWindowHidden(window, WINDOW_DESTROYED);
221 ////////////////////////////////////////////////////////////////////////////////
222 // WindowEventDispatcher, private:
224 Window* WindowEventDispatcher::window() {
225 return host_->window();
228 const Window* WindowEventDispatcher::window() const {
229 return host_->window();
232 void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
233 ui::LocatedEvent* event) {
234 event->UpdateForRootTransform(host_->GetInverseRootTransform());
237 void WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
238 // The mouse capture is intentionally ignored. Think that a mouse enters
239 // to a window, the window sets the capture, the mouse exits the window,
240 // and then it releases the capture. In that case OnMouseExited won't
241 // be called. So it is natural not to emit OnMouseExited even though
242 // |window| is the capture window.
243 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
244 if (window->Contains(mouse_moved_handler_) &&
245 window->ContainsPointInRoot(last_mouse_location)) {
246 DispatchDetails details =
247 DispatchMouseExitAtPoint(window, last_mouse_location);
248 if (details.dispatcher_destroyed)
249 return;
253 ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
254 Window* target,
255 const ui::MouseEvent& event,
256 ui::EventType type) {
257 if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
258 !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
259 SetLastMouseLocation(window(), event.root_location());
262 if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
263 !window()->Contains(mouse_moved_handler_))
264 return DispatchDetails();
266 // |event| may be an event in the process of being dispatched to a target (in
267 // which case its locations will be in the event's target's coordinate
268 // system), or a synthetic event created in root-window (in which case, the
269 // event's target will be NULL, and the event will be in the root-window's
270 // coordinate system.
271 if (!target)
272 target = window();
273 ui::MouseEvent translated_event(event,
274 target,
275 mouse_moved_handler_,
276 type,
277 event.flags() | ui::EF_IS_SYNTHESIZED);
278 return DispatchEvent(mouse_moved_handler_, &translated_event);
281 ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
282 ui::GestureRecognizer::Gestures* gestures) {
283 DispatchDetails details;
284 if (!gestures || gestures->empty())
285 return details;
287 Window* target = GetGestureTarget(gestures->get().at(0));
288 if (!target)
289 return details;
291 for (size_t i = 0; i < gestures->size(); ++i) {
292 ui::GestureEvent* event = gestures->get().at(i);
293 event->ConvertLocationToTarget(window(), target);
294 details = DispatchEvent(target, event);
295 if (details.dispatcher_destroyed || details.target_destroyed)
296 break;
298 return details;
301 void WindowEventDispatcher::OnWindowHidden(Window* invisible,
302 WindowHiddenReason reason) {
303 // If the window the mouse was pressed in becomes invisible, it should no
304 // longer receive mouse events.
305 if (invisible->Contains(mouse_pressed_handler_))
306 mouse_pressed_handler_ = NULL;
307 if (invisible->Contains(mouse_moved_handler_))
308 mouse_moved_handler_ = NULL;
310 // If events are being dispatched from a nested message-loop, and the target
311 // of the outer loop is hidden or moved to another dispatcher during
312 // dispatching events in the inner loop, then reset the target for the outer
313 // loop.
314 if (invisible->Contains(old_dispatch_target_))
315 old_dispatch_target_ = NULL;
317 invisible->CleanupGestureState();
319 // Do not clear the capture, and the |event_dispatch_target_| if the
320 // window is moving across hosts, because the target itself is actually still
321 // visible and clearing them stops further event processing, which can cause
322 // unexpected behaviors. See crbug.com/157583
323 if (reason != WINDOW_MOVING) {
324 // We don't ask |invisible| here, because invisible may have been removed
325 // from the window hierarchy already by the time this function is called
326 // (OnWindowDestroyed).
327 client::CaptureClient* capture_client =
328 client::GetCaptureClient(host_->window());
329 Window* capture_window =
330 capture_client ? capture_client->GetCaptureWindow() : NULL;
332 if (invisible->Contains(event_dispatch_target_))
333 event_dispatch_target_ = NULL;
335 // If the ancestor of the capture window is hidden, release the capture.
336 // Note that this may delete the window so do not use capture_window
337 // after this.
338 if (invisible->Contains(capture_window) && invisible != window())
339 capture_window->ReleaseCapture();
343 Window* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
344 return ConsumerToWindow(
345 ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
348 bool WindowEventDispatcher::is_dispatched_held_event(
349 const ui::Event& event) const {
350 return dispatching_held_event_ == &event;
353 ////////////////////////////////////////////////////////////////////////////////
354 // WindowEventDispatcher, aura::client::CaptureDelegate implementation:
356 void WindowEventDispatcher::UpdateCapture(Window* old_capture,
357 Window* new_capture) {
358 // |mouse_moved_handler_| may have been set to a Window in a different root
359 // (see below). Clear it here to ensure we don't end up referencing a stale
360 // Window.
361 if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
362 mouse_moved_handler_ = NULL;
364 if (old_capture && old_capture->GetRootWindow() == window() &&
365 old_capture->delegate()) {
366 // Send a capture changed event with bogus location data.
367 ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
368 gfx::Point(), ui::EventTimeForNow(), 0, 0);
370 DispatchDetails details = DispatchEvent(old_capture, &event);
371 if (details.dispatcher_destroyed)
372 return;
374 if (!details.target_destroyed)
375 old_capture->delegate()->OnCaptureLost();
378 if (new_capture) {
379 // Make all subsequent mouse events go to the capture window. We shouldn't
380 // need to send an event here as OnCaptureLost() should take care of that.
381 if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
382 mouse_moved_handler_ = new_capture;
383 } else {
384 // Make sure mouse_moved_handler gets updated.
385 DispatchDetails details = SynthesizeMouseMoveEvent();
386 if (details.dispatcher_destroyed)
387 return;
389 mouse_pressed_handler_ = NULL;
392 void WindowEventDispatcher::OnOtherRootGotCapture() {
393 // Windows provides the TrackMouseEvents API which allows us to rely on the
394 // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on
395 // desktop Windows, every top level window could potentially have its own
396 // root window, in which case this function will get called whenever those
397 // windows grab mouse capture. Sending mouse exit messages in these cases
398 // causes subtle bugs like (crbug.com/394672).
399 #if !defined(OS_WIN)
400 if (mouse_moved_handler_) {
401 // Dispatch a mouse exit to reset any state associated with hover. This is
402 // important when going from no window having capture to a window having
403 // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
404 DispatchDetails details =
405 DispatchMouseExitAtPoint(nullptr, GetLastMouseLocationInRoot());
406 if (details.dispatcher_destroyed)
407 return;
409 #endif
411 mouse_moved_handler_ = NULL;
412 mouse_pressed_handler_ = NULL;
415 void WindowEventDispatcher::SetNativeCapture() {
416 host_->SetCapture();
419 void WindowEventDispatcher::ReleaseNativeCapture() {
420 host_->ReleaseCapture();
423 ////////////////////////////////////////////////////////////////////////////////
424 // WindowEventDispatcher, ui::EventProcessor implementation:
425 ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
426 return window();
429 void WindowEventDispatcher::OnEventProcessingStarted(ui::Event* event) {
430 // The held events are already in |window()|'s coordinate system. So it is
431 // not necessary to apply the transform to convert from the host's
432 // coordinate system to |window()|'s coordinate system.
433 if (event->IsLocatedEvent() && !is_dispatched_held_event(*event))
434 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
437 ////////////////////////////////////////////////////////////////////////////////
438 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
440 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
441 return event_dispatch_target_ == target;
444 ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
445 ui::EventTarget* target,
446 ui::Event* event) {
447 Window* target_window = static_cast<Window*>(target);
448 CHECK(window()->Contains(target_window));
450 if (!dispatching_held_event_) {
451 bool can_be_held = IsEventCandidateForHold(*event);
452 if (!move_hold_count_ || !can_be_held) {
453 if (can_be_held)
454 held_move_event_.reset();
455 DispatchDetails details = DispatchHeldEvents();
456 if (details.dispatcher_destroyed || details.target_destroyed)
457 return details;
461 if (event->IsMouseEvent()) {
462 PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
463 } else if (event->IsScrollEvent()) {
464 PreDispatchLocatedEvent(target_window,
465 static_cast<ui::ScrollEvent*>(event));
466 } else if (event->IsTouchEvent()) {
467 PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
469 old_dispatch_target_ = event_dispatch_target_;
470 event_dispatch_target_ = static_cast<Window*>(target);
471 return DispatchDetails();
474 ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
475 ui::EventTarget* target,
476 const ui::Event& event) {
477 DispatchDetails details;
478 if (!target || target != event_dispatch_target_)
479 details.target_destroyed = true;
480 event_dispatch_target_ = old_dispatch_target_;
481 old_dispatch_target_ = NULL;
482 #ifndef NDEBUG
483 DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
484 #endif
486 if (event.IsTouchEvent() && !details.target_destroyed) {
487 // Do not let 'held' touch events contribute to any gestures unless it is
488 // being dispatched.
489 if (is_dispatched_held_event(event) || !held_move_event_ ||
490 !held_move_event_->IsTouchEvent()) {
491 const ui::TouchEvent& touchevent =
492 static_cast<const ui::TouchEvent&>(event);
494 if (!touchevent.synchronous_handling_disabled()) {
495 scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
497 // Once we've fully migrated to the eager gesture detector, we won't
498 // need to pass an event here.
499 gestures.reset(ui::GestureRecognizer::Get()->AckSyncTouchEvent(
500 touchevent.unique_event_id(), event.result(),
501 static_cast<Window*>(target)));
503 return ProcessGestures(gestures.get());
508 return details;
511 ////////////////////////////////////////////////////////////////////////////////
512 // WindowEventDispatcher, ui::GestureEventHelper implementation:
514 bool WindowEventDispatcher::CanDispatchToConsumer(
515 ui::GestureConsumer* consumer) {
516 Window* consumer_window = ConsumerToWindow(consumer);
517 return (consumer_window && consumer_window->GetRootWindow() == window());
520 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
521 // The touchcancel event's location is based on the last known location of
522 // the pointer, in dips. OnEventFromSource expects events with co-ordinates
523 // in raw pixels, so we convert back to raw pixels here.
524 event->UpdateForRootTransform(host_->GetRootTransform());
525 DispatchDetails details = OnEventFromSource(event);
526 if (details.dispatcher_destroyed)
527 return;
530 ////////////////////////////////////////////////////////////////////////////////
531 // WindowEventDispatcher, WindowObserver implementation:
533 void WindowEventDispatcher::OnWindowDestroying(Window* window) {
534 if (!host_->window()->Contains(window))
535 return;
537 SynthesizeMouseMoveAfterChangeToWindow(window);
540 void WindowEventDispatcher::OnWindowDestroyed(Window* window) {
541 // We observe all windows regardless of what root Window (if any) they're
542 // attached to.
543 observer_manager_.Remove(window);
546 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
547 if (!observer_manager_.IsObserving(attached))
548 observer_manager_.Add(attached);
550 if (!host_->window()->Contains(attached))
551 return;
553 SynthesizeMouseMoveAfterChangeToWindow(attached);
556 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
557 Window* new_root) {
558 if (!host_->window()->Contains(detached))
559 return;
561 DCHECK(client::GetCaptureWindow(window()) != window());
563 DispatchMouseExitToHidingWindow(detached);
564 SynthesizeMouseMoveAfterChangeToWindow(detached);
566 // Hiding the window releases capture which can implicitly destroy the window
567 // so the window may no longer be valid after this call.
568 OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
571 void WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
572 bool visible) {
573 if (!host_->window()->Contains(window))
574 return;
576 DispatchMouseExitToHidingWindow(window);
579 void WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
580 bool visible) {
581 if (!host_->window()->Contains(window))
582 return;
584 if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
585 PostSynthesizeMouseMove();
587 // Hiding the window releases capture which can implicitly destroy the window
588 // so the window may no longer be valid after this call.
589 if (!visible)
590 OnWindowHidden(window, WINDOW_HIDDEN);
593 void WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
594 const gfx::Rect& old_bounds,
595 const gfx::Rect& new_bounds) {
596 if (!host_->window()->Contains(window))
597 return;
599 if (window == host_->window()) {
600 TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
601 "size", new_bounds.size().ToString());
603 DispatchDetails details = DispatchHeldEvents();
604 if (details.dispatcher_destroyed)
605 return;
607 synthesize_mouse_move_ = false;
610 if (window->IsVisible() && !window->ignore_events()) {
611 gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
612 Window::ConvertRectToTarget(window->parent(), host_->window(),
613 &old_bounds_in_root);
614 Window::ConvertRectToTarget(window->parent(), host_->window(),
615 &new_bounds_in_root);
616 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
617 if (old_bounds_in_root.Contains(last_mouse_location) !=
618 new_bounds_in_root.Contains(last_mouse_location)) {
619 PostSynthesizeMouseMove();
624 void WindowEventDispatcher::OnWindowTransforming(Window* window) {
625 if (!host_->window()->Contains(window))
626 return;
628 SynthesizeMouseMoveAfterChangeToWindow(window);
631 void WindowEventDispatcher::OnWindowTransformed(Window* window) {
632 if (!host_->window()->Contains(window))
633 return;
635 SynthesizeMouseMoveAfterChangeToWindow(window);
638 ///////////////////////////////////////////////////////////////////////////////
639 // WindowEventDispatcher, EnvObserver implementation:
641 void WindowEventDispatcher::OnWindowInitialized(Window* window) {
642 observer_manager_.Add(window);
645 ////////////////////////////////////////////////////////////////////////////////
646 // WindowEventDispatcher, private:
648 ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
649 if (!held_repostable_event_ && !held_move_event_)
650 return DispatchDetails();
652 CHECK(!dispatching_held_event_);
654 DispatchDetails dispatch_details;
655 if (held_repostable_event_) {
656 if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
657 scoped_ptr<ui::MouseEvent> mouse_event(
658 static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
659 dispatching_held_event_ = mouse_event.get();
660 dispatch_details = OnEventFromSource(mouse_event.get());
661 } else {
662 // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
663 NOTREACHED();
665 if (dispatch_details.dispatcher_destroyed)
666 return dispatch_details;
669 if (held_move_event_) {
670 // If a mouse move has been synthesized, the target location is suspect,
671 // so drop the held mouse event.
672 if (held_move_event_->IsTouchEvent() ||
673 (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
674 dispatching_held_event_ = held_move_event_.get();
675 dispatch_details = OnEventFromSource(held_move_event_.get());
677 if (!dispatch_details.dispatcher_destroyed)
678 held_move_event_.reset();
681 if (!dispatch_details.dispatcher_destroyed)
682 dispatching_held_event_ = nullptr;
683 return dispatch_details;
686 void WindowEventDispatcher::PostSynthesizeMouseMove() {
687 if (synthesize_mouse_move_)
688 return;
689 synthesize_mouse_move_ = true;
690 base::MessageLoop::current()->PostNonNestableTask(
691 FROM_HERE,
692 base::Bind(base::IgnoreResult(
693 &WindowEventDispatcher::SynthesizeMouseMoveEvent),
694 held_event_factory_.GetWeakPtr()));
697 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
698 Window* window) {
699 if (window->IsVisible() &&
700 window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
701 PostSynthesizeMouseMove();
705 ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
706 DispatchDetails details;
707 if (!synthesize_mouse_move_)
708 return details;
709 synthesize_mouse_move_ = false;
711 // If one of the mouse buttons is currently down, then do not synthesize a
712 // mouse-move event. In such cases, aura could synthesize a DRAGGED event
713 // instead of a MOVED event, but in multi-display/multi-host scenarios, the
714 // DRAGGED event can be synthesized in the incorrect host. So avoid
715 // synthesizing any events at all.
716 if (Env::GetInstance()->mouse_button_flags())
717 return details;
719 gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
720 if (!window()->bounds().Contains(root_mouse_location))
721 return details;
722 gfx::Point host_mouse_location = root_mouse_location;
723 host_->ConvertPointToHost(&host_mouse_location);
724 ui::MouseEvent event(ui::ET_MOUSE_MOVED, host_mouse_location,
725 host_mouse_location, ui::EventTimeForNow(),
726 ui::EF_IS_SYNTHESIZED, 0);
727 return OnEventFromSource(&event);
730 void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
731 ui::LocatedEvent* event) {
732 int flags = event->flags();
733 if (IsNonClientLocation(target, event->location()))
734 flags |= ui::EF_IS_NON_CLIENT;
735 event->set_flags(flags);
737 if (!is_dispatched_held_event(*event) &&
738 (event->IsMouseEvent() || event->IsScrollEvent()) &&
739 !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
740 if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
741 SetLastMouseLocation(window(), event->root_location());
742 synthesize_mouse_move_ = false;
746 void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
747 ui::MouseEvent* event) {
748 client::CursorClient* cursor_client = client::GetCursorClient(window());
749 // We allow synthesized mouse exit events through even if mouse events are
750 // disabled. This ensures that hover state, etc on controls like buttons is
751 // cleared.
752 if (cursor_client &&
753 !cursor_client->IsMouseEventsEnabled() &&
754 (event->flags() & ui::EF_IS_SYNTHESIZED) &&
755 (event->type() != ui::ET_MOUSE_EXITED)) {
756 event->SetHandled();
757 return;
760 if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
761 if (move_hold_count_) {
762 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
763 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
764 SetLastMouseLocation(window(), event->root_location());
766 held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
767 event->SetHandled();
768 return;
769 } else {
770 // We may have a held event for a period between the time move_hold_count_
771 // fell to 0 and the DispatchHeldEvents executes. Since we're going to
772 // dispatch the new event directly below, we can reset the old one.
773 held_move_event_.reset();
777 const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
778 ui::EF_MIDDLE_MOUSE_BUTTON |
779 ui::EF_RIGHT_MOUSE_BUTTON |
780 ui::EF_BACK_MOUSE_BUTTON |
781 ui::EF_FORWARD_MOUSE_BUTTON;
782 switch (event->type()) {
783 case ui::ET_MOUSE_EXITED:
784 if (!target || target == window()) {
785 DispatchDetails details =
786 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_EXITED);
787 if (details.dispatcher_destroyed) {
788 event->SetHandled();
789 return;
791 mouse_moved_handler_ = NULL;
793 break;
794 case ui::ET_MOUSE_MOVED:
795 // Send an exit to the current |mouse_moved_handler_| and an enter to
796 // |target|. Take care that both us and |target| aren't destroyed during
797 // dispatch.
798 if (target != mouse_moved_handler_) {
799 aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
800 WindowTracker live_window;
801 live_window.Add(target);
802 DispatchDetails details =
803 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_EXITED);
804 if (details.dispatcher_destroyed) {
805 event->SetHandled();
806 return;
808 // If the |mouse_moved_handler_| changes out from under us, assume a
809 // nested message loop ran and we don't need to do anything.
810 if (mouse_moved_handler_ != old_mouse_moved_handler) {
811 event->SetHandled();
812 return;
814 if (!live_window.Contains(target) || details.target_destroyed) {
815 mouse_moved_handler_ = NULL;
816 event->SetHandled();
817 return;
819 live_window.Remove(target);
821 mouse_moved_handler_ = target;
822 details =
823 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_ENTERED);
824 if (details.dispatcher_destroyed || details.target_destroyed) {
825 event->SetHandled();
826 return;
829 break;
830 case ui::ET_MOUSE_PRESSED:
831 // Don't set the mouse pressed handler for non client mouse down events.
832 // These are only sent by Windows and are not always followed with non
833 // client mouse up events which causes subsequent mouse events to be
834 // sent to the wrong target.
835 if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
836 mouse_pressed_handler_ = target;
837 Env::GetInstance()->set_mouse_button_flags(
838 event->flags() & kMouseButtonFlagMask);
839 break;
840 case ui::ET_MOUSE_RELEASED:
841 mouse_pressed_handler_ = NULL;
842 Env::GetInstance()->set_mouse_button_flags(event->flags() &
843 kMouseButtonFlagMask & ~event->changed_button_flags());
844 break;
845 default:
846 break;
849 PreDispatchLocatedEvent(target, event);
852 void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
853 ui::TouchEvent* event) {
854 switch (event->type()) {
855 case ui::ET_TOUCH_PRESSED:
856 touch_ids_down_ |= (1 << event->touch_id());
857 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
858 break;
860 // Handle ET_TOUCH_CANCELLED only if it has a native event.
861 case ui::ET_TOUCH_CANCELLED:
862 if (!event->HasNativeEvent())
863 break;
864 // fallthrough
865 case ui::ET_TOUCH_RELEASED:
866 touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
867 (1 << event->touch_id());
868 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
869 break;
871 case ui::ET_TOUCH_MOVED:
872 if (move_hold_count_ && !dispatching_held_event_) {
873 held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
874 event->SetHandled();
875 return;
877 break;
879 default:
880 NOTREACHED();
881 break;
884 ui::TouchEvent orig_event(*event, target, window());
885 if (!ui::GestureRecognizer::Get()->ProcessTouchEventPreDispatch(&orig_event,
886 target)) {
887 // The event is invalid - ignore it.
888 event->StopPropagation();
889 event->DisableSynchronousHandling();
890 return;
893 // This flag is set depending on the gestures recognized in the call above,
894 // and needs to propagate with the forwarded event.
895 event->set_may_cause_scrolling(orig_event.may_cause_scrolling());
897 PreDispatchLocatedEvent(target, event);
900 } // namespace aura