Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / ui / aura / window_event_dispatcher.cc
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 #include "ui/aura/window_event_dispatcher.h"
6
7 #include "base/bind.h"
8 #include "base/debug/trace_event.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.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/gestures/gesture_recognizer.h"
26 #include "ui/events/gestures/gesture_types.h"
27
28 typedef ui::EventDispatchDetails DispatchDetails;
29
30 namespace aura {
31
32 namespace {
33
34 // Returns true if |target| has a non-client (frame) component at |location|,
35 // in window coordinates.
36 bool IsNonClientLocation(Window* target, const gfx::Point& location) {
37   if (!target->delegate())
38     return false;
39   int hit_test_code = target->delegate()->GetNonClientComponent(location);
40   return hit_test_code != HTCLIENT && hit_test_code != HTNOWHERE;
41 }
42
43 Window* ConsumerToWindow(ui::GestureConsumer* consumer) {
44   return consumer ? static_cast<Window*>(consumer) : NULL;
45 }
46
47 void SetLastMouseLocation(const Window* root_window,
48                           const gfx::Point& location_in_root) {
49   client::ScreenPositionClient* client =
50       client::GetScreenPositionClient(root_window);
51   if (client) {
52     gfx::Point location_in_screen = location_in_root;
53     client->ConvertPointToScreen(root_window, &location_in_screen);
54     Env::GetInstance()->set_last_mouse_location(location_in_screen);
55   } else {
56     Env::GetInstance()->set_last_mouse_location(location_in_root);
57   }
58 }
59
60 bool IsEventCandidateForHold(const ui::Event& event) {
61   if (event.type() == ui::ET_TOUCH_MOVED)
62     return true;
63   if (event.type() == ui::ET_MOUSE_DRAGGED)
64     return true;
65   if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED))
66     return true;
67   return false;
68 }
69
70 }  // namespace
71
72 ////////////////////////////////////////////////////////////////////////////////
73 // WindowEventDispatcher, public:
74
75 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host)
76     : host_(host),
77       touch_ids_down_(0),
78       mouse_pressed_handler_(NULL),
79       mouse_moved_handler_(NULL),
80       event_dispatch_target_(NULL),
81       old_dispatch_target_(NULL),
82       synthesize_mouse_move_(false),
83       move_hold_count_(0),
84       dispatching_held_event_(false),
85       observer_manager_(this),
86       repost_event_factory_(this),
87       held_event_factory_(this) {
88   ui::GestureRecognizer::Get()->AddGestureEventHelper(this);
89   Env::GetInstance()->AddObserver(this);
90 }
91
92 WindowEventDispatcher::~WindowEventDispatcher() {
93   TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor");
94   Env::GetInstance()->RemoveObserver(this);
95   ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this);
96 }
97
98 void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent& event) {
99   DCHECK(event.type() == ui::ET_MOUSE_PRESSED ||
100          event.type() == ui::ET_GESTURE_TAP_DOWN);
101   // We allow for only one outstanding repostable event. This is used
102   // in exiting context menus.  A dropped repost request is allowed.
103   if (event.type() == ui::ET_MOUSE_PRESSED) {
104     held_repostable_event_.reset(
105         new ui::MouseEvent(
106             static_cast<const ui::MouseEvent&>(event),
107             static_cast<aura::Window*>(event.target()),
108             window()));
109     base::MessageLoop::current()->PostNonNestableTask(
110         FROM_HERE, base::Bind(
111             base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
112             repost_event_factory_.GetWeakPtr()));
113   } else {
114     DCHECK(event.type() == ui::ET_GESTURE_TAP_DOWN);
115     held_repostable_event_.reset();
116     // TODO(rbyers): Reposing of gestures is tricky to get
117     // right, so it's not yet supported.  crbug.com/170987.
118   }
119 }
120
121 void WindowEventDispatcher::OnMouseEventsEnableStateChanged(bool enabled) {
122   // Send entered / exited so that visual state can be updated to match
123   // mouse events state.
124   PostSynthesizeMouseMove();
125   // TODO(mazda): Add code to disable mouse events when |enabled| == false.
126 }
127
128 void WindowEventDispatcher::DispatchCancelModeEvent() {
129   ui::CancelModeEvent event;
130   Window* focused_window = client::GetFocusClient(window())->GetFocusedWindow();
131   if (focused_window && !window()->Contains(focused_window))
132     focused_window = NULL;
133   DispatchDetails details =
134       DispatchEvent(focused_window ? focused_window : window(), &event);
135   if (details.dispatcher_destroyed)
136     return;
137 }
138
139 void WindowEventDispatcher::DispatchGestureEvent(ui::GestureEvent* event) {
140   DispatchDetails details = DispatchHeldEvents();
141   if (details.dispatcher_destroyed)
142     return;
143
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;
150   }
151 }
152
153 DispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint(
154     const gfx::Point& point) {
155   ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE,
156                        ui::EF_NONE);
157   return DispatchMouseEnterOrExit(event, ui::ET_MOUSE_EXITED);
158 }
159
160 void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event,
161                                                 Window* window,
162                                                 ui::EventResult result) {
163   // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager
164   // gesture detection. See crbug.com/410280.
165   if (!ui::GestureRecognizer::Get()
166            ->ProcessTouchEventPreDispatch(*event, window)) {
167     return;
168   }
169
170   // Once we've fully migrated to the eager gesture detector, we won't need to
171   // pass an event here.
172   scoped_ptr<ui::GestureRecognizer::Gestures> gestures(
173       ui::GestureRecognizer::Get()->ProcessTouchEventOnAsyncAck(
174           *event, result, window));
175   DispatchDetails details = ProcessGestures(gestures.get());
176   if (details.dispatcher_destroyed)
177     return;
178 }
179
180 void WindowEventDispatcher::HoldPointerMoves() {
181   if (!move_hold_count_)
182     held_event_factory_.InvalidateWeakPtrs();
183   ++move_hold_count_;
184   TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
185                            this);
186 }
187
188 void WindowEventDispatcher::ReleasePointerMoves() {
189   --move_hold_count_;
190   DCHECK_GE(move_hold_count_, 0);
191   if (!move_hold_count_ && held_move_event_) {
192     // We don't want to call DispatchHeldEvents directly, because this might be
193     // called from a deep stack while another event, in which case dispatching
194     // another one may not be safe/expected.  Instead we post a task, that we
195     // may cancel if HoldPointerMoves is called again before it executes.
196     base::MessageLoop::current()->PostNonNestableTask(
197         FROM_HERE, base::Bind(
198           base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
199           held_event_factory_.GetWeakPtr()));
200   }
201   TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
202 }
203
204 gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
205   gfx::Point location = Env::GetInstance()->last_mouse_location();
206   client::ScreenPositionClient* client =
207       client::GetScreenPositionClient(window());
208   if (client)
209     client->ConvertPointFromScreen(window(), &location);
210   return location;
211 }
212
213 void WindowEventDispatcher::OnHostLostMouseGrab() {
214   mouse_pressed_handler_ = NULL;
215   mouse_moved_handler_ = NULL;
216 }
217
218 void WindowEventDispatcher::OnCursorMovedToRootLocation(
219     const gfx::Point& root_location) {
220   SetLastMouseLocation(window(), root_location);
221   synthesize_mouse_move_ = false;
222 }
223
224 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
225   OnWindowHidden(window, WINDOW_DESTROYED);
226 }
227
228 ////////////////////////////////////////////////////////////////////////////////
229 // WindowEventDispatcher, private:
230
231 Window* WindowEventDispatcher::window() {
232   return host_->window();
233 }
234
235 const Window* WindowEventDispatcher::window() const {
236   return host_->window();
237 }
238
239 void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
240     ui::LocatedEvent* event) {
241   event->UpdateForRootTransform(host_->GetInverseRootTransform());
242 }
243
244 void WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
245   // The mouse capture is intentionally ignored. Think that a mouse enters
246   // to a window, the window sets the capture, the mouse exits the window,
247   // and then it releases the capture. In that case OnMouseExited won't
248   // be called. So it is natural not to emit OnMouseExited even though
249   // |window| is the capture window.
250   gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
251   if (window->Contains(mouse_moved_handler_) &&
252       window->ContainsPointInRoot(last_mouse_location)) {
253     DispatchDetails details = DispatchMouseExitAtPoint(last_mouse_location);
254     if (details.dispatcher_destroyed)
255       return;
256   }
257 }
258
259 ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
260     const ui::MouseEvent& event,
261     ui::EventType type) {
262   if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
263       !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
264     SetLastMouseLocation(window(), event.root_location());
265   }
266
267   if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
268       !window()->Contains(mouse_moved_handler_))
269     return DispatchDetails();
270
271   // |event| may be an event in the process of being dispatched to a target (in
272   // which case its locations will be in the event's target's coordinate
273   // system), or a synthetic event created in root-window (in which case, the
274   // event's target will be NULL, and the event will be in the root-window's
275   // coordinate system.
276   aura::Window* target = static_cast<Window*>(event.target());
277   if (!target)
278     target = window();
279   ui::MouseEvent translated_event(event,
280                                   target,
281                                   mouse_moved_handler_,
282                                   type,
283                                   event.flags() | ui::EF_IS_SYNTHESIZED);
284   return DispatchEvent(mouse_moved_handler_, &translated_event);
285 }
286
287 ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
288     ui::GestureRecognizer::Gestures* gestures) {
289   DispatchDetails details;
290   if (!gestures || gestures->empty())
291     return details;
292
293   Window* target = GetGestureTarget(gestures->get().at(0));
294   if (!target)
295     return details;
296
297   for (size_t i = 0; i < gestures->size(); ++i) {
298     ui::GestureEvent* event = gestures->get().at(i);
299     event->ConvertLocationToTarget(window(), target);
300     details = DispatchEvent(target, event);
301     if (details.dispatcher_destroyed || details.target_destroyed)
302       break;
303   }
304   return details;
305 }
306
307 void WindowEventDispatcher::OnWindowHidden(Window* invisible,
308                                            WindowHiddenReason reason) {
309   // If the window the mouse was pressed in becomes invisible, it should no
310   // longer receive mouse events.
311   if (invisible->Contains(mouse_pressed_handler_))
312     mouse_pressed_handler_ = NULL;
313   if (invisible->Contains(mouse_moved_handler_))
314     mouse_moved_handler_ = NULL;
315
316   // If events are being dispatched from a nested message-loop, and the target
317   // of the outer loop is hidden or moved to another dispatcher during
318   // dispatching events in the inner loop, then reset the target for the outer
319   // loop.
320   if (invisible->Contains(old_dispatch_target_))
321     old_dispatch_target_ = NULL;
322
323   invisible->CleanupGestureState();
324
325   // Do not clear the capture, and the |event_dispatch_target_| if the
326   // window is moving across hosts, because the target itself is actually still
327   // visible and clearing them stops further event processing, which can cause
328   // unexpected behaviors. See crbug.com/157583
329   if (reason != WINDOW_MOVING) {
330     // We don't ask |invisible| here, because invisible may have been removed
331     // from the window hierarchy already by the time this function is called
332     // (OnWindowDestroyed).
333     client::CaptureClient* capture_client =
334         client::GetCaptureClient(host_->window());
335     Window* capture_window =
336         capture_client ? capture_client->GetCaptureWindow() : NULL;
337
338     if (invisible->Contains(event_dispatch_target_))
339       event_dispatch_target_ = NULL;
340
341     // If the ancestor of the capture window is hidden, release the capture.
342     // Note that this may delete the window so do not use capture_window
343     // after this.
344     if (invisible->Contains(capture_window) && invisible != window())
345       capture_window->ReleaseCapture();
346   }
347 }
348
349 Window* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
350   Window* target = NULL;
351   if (!event->IsEndingEvent()) {
352     // The window that received the start event (e.g. scroll begin) needs to
353     // receive the end event (e.g. scroll end).
354     target = client::GetCaptureWindow(window());
355   }
356   if (!target) {
357     target = ConsumerToWindow(
358         ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
359   }
360
361   return target;
362 }
363
364 ////////////////////////////////////////////////////////////////////////////////
365 // WindowEventDispatcher, aura::client::CaptureDelegate implementation:
366
367 void WindowEventDispatcher::UpdateCapture(Window* old_capture,
368                                           Window* new_capture) {
369   // |mouse_moved_handler_| may have been set to a Window in a different root
370   // (see below). Clear it here to ensure we don't end up referencing a stale
371   // Window.
372   if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
373     mouse_moved_handler_ = NULL;
374
375   if (old_capture && old_capture->GetRootWindow() == window() &&
376       old_capture->delegate()) {
377     // Send a capture changed event with bogus location data.
378     ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
379                          gfx::Point(), 0, 0);
380
381     DispatchDetails details = DispatchEvent(old_capture, &event);
382     if (details.dispatcher_destroyed)
383       return;
384
385     old_capture->delegate()->OnCaptureLost();
386   }
387
388   if (new_capture) {
389     // Make all subsequent mouse events go to the capture window. We shouldn't
390     // need to send an event here as OnCaptureLost() should take care of that.
391     if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
392       mouse_moved_handler_ = new_capture;
393   } else {
394     // Make sure mouse_moved_handler gets updated.
395     DispatchDetails details = SynthesizeMouseMoveEvent();
396     if (details.dispatcher_destroyed)
397       return;
398   }
399   mouse_pressed_handler_ = NULL;
400 }
401
402 void WindowEventDispatcher::OnOtherRootGotCapture() {
403   // Windows provides the TrackMouseEvents API which allows us to rely on the
404   // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on
405   // desktop Windows, every top level window could potentially have its own
406   // root window, in which case this function will get called whenever those
407   // windows grab mouse capture. Sending mouse exit messages in these cases
408   // causes subtle bugs like (crbug.com/394672).
409 #if !defined(OS_WIN)
410   if (mouse_moved_handler_) {
411     // Dispatch a mouse exit to reset any state associated with hover. This is
412     // important when going from no window having capture to a window having
413     // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
414     DispatchDetails details = DispatchMouseExitAtPoint(
415         GetLastMouseLocationInRoot());
416     if (details.dispatcher_destroyed)
417       return;
418   }
419 #endif
420
421   mouse_moved_handler_ = NULL;
422   mouse_pressed_handler_ = NULL;
423 }
424
425 void WindowEventDispatcher::SetNativeCapture() {
426   host_->SetCapture();
427 }
428
429 void WindowEventDispatcher::ReleaseNativeCapture() {
430   host_->ReleaseCapture();
431 }
432
433 ////////////////////////////////////////////////////////////////////////////////
434 // WindowEventDispatcher, ui::EventProcessor implementation:
435 ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
436   return window();
437 }
438
439 void WindowEventDispatcher::PrepareEventForDispatch(ui::Event* event) {
440   if (dispatching_held_event_) {
441     // The held events are already in |window()|'s coordinate system. So it is
442     // not necessary to apply the transform to convert from the host's
443     // coordinate system to |window()|'s coordinate system.
444     return;
445   }
446   if (event->IsMouseEvent() ||
447       event->IsScrollEvent() ||
448       event->IsTouchEvent() ||
449       event->IsGestureEvent()) {
450     TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
451   }
452 }
453
454 ////////////////////////////////////////////////////////////////////////////////
455 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
456
457 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
458   return event_dispatch_target_ == target;
459 }
460
461 ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
462     ui::EventTarget* target,
463     ui::Event* event) {
464   Window* target_window = static_cast<Window*>(target);
465   CHECK(window()->Contains(target_window));
466
467   if (!dispatching_held_event_) {
468     bool can_be_held = IsEventCandidateForHold(*event);
469     if (!move_hold_count_ || !can_be_held) {
470       if (can_be_held)
471         held_move_event_.reset();
472       DispatchDetails details = DispatchHeldEvents();
473       if (details.dispatcher_destroyed || details.target_destroyed)
474         return details;
475     }
476   }
477
478   if (event->IsMouseEvent()) {
479     PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
480   } else if (event->IsScrollEvent()) {
481     PreDispatchLocatedEvent(target_window,
482                             static_cast<ui::ScrollEvent*>(event));
483   } else if (event->IsTouchEvent()) {
484     PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
485   }
486   old_dispatch_target_ = event_dispatch_target_;
487   event_dispatch_target_ = static_cast<Window*>(target);
488   return DispatchDetails();
489 }
490
491 ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
492     ui::EventTarget* target,
493     const ui::Event& event) {
494   DispatchDetails details;
495   if (!target || target != event_dispatch_target_)
496     details.target_destroyed = true;
497   event_dispatch_target_ = old_dispatch_target_;
498   old_dispatch_target_ = NULL;
499 #ifndef NDEBUG
500   DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
501 #endif
502
503   if (event.IsTouchEvent() && !details.target_destroyed) {
504     // Do not let 'held' touch events contribute to any gestures unless it is
505     // being dispatched.
506     if (dispatching_held_event_ || !held_move_event_ ||
507         !held_move_event_->IsTouchEvent()) {
508
509       // Once we've fully migrated to the eager gesture detector, we won't
510       // need to pass an event here.
511       ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event),
512                                 static_cast<Window*>(event.target()),
513                                 window());
514
515       if (event.result() & ui::ER_CONSUMED)
516         orig_event.StopPropagation();
517
518       // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager
519       // gesture detection. See crbug.com/410280.
520       if (!ui::GestureRecognizer::Get()
521                ->ProcessTouchEventPreDispatch(orig_event,
522                                               static_cast<Window*>(target))) {
523         return details;
524       }
525
526       scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
527
528       gestures.reset(
529           ui::GestureRecognizer::Get()->ProcessTouchEventPostDispatch(
530               orig_event, event.result(), static_cast<Window*>(target)));
531
532       return ProcessGestures(gestures.get());
533     }
534   }
535
536   return details;
537 }
538
539 ////////////////////////////////////////////////////////////////////////////////
540 // WindowEventDispatcher, ui::GestureEventHelper implementation:
541
542 bool WindowEventDispatcher::CanDispatchToConsumer(
543     ui::GestureConsumer* consumer) {
544   Window* consumer_window = ConsumerToWindow(consumer);
545   return (consumer_window && consumer_window->GetRootWindow() == window());
546 }
547
548 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
549   DispatchDetails details = OnEventFromSource(event);
550   if (details.dispatcher_destroyed)
551     return;
552 }
553
554 ////////////////////////////////////////////////////////////////////////////////
555 // WindowEventDispatcher, WindowObserver implementation:
556
557 void WindowEventDispatcher::OnWindowDestroying(Window* window) {
558   if (!host_->window()->Contains(window))
559     return;
560
561   DispatchMouseExitToHidingWindow(window);
562   SynthesizeMouseMoveAfterChangeToWindow(window);
563 }
564
565 void WindowEventDispatcher::OnWindowDestroyed(Window* window) {
566   // We observe all windows regardless of what root Window (if any) they're
567   // attached to.
568   observer_manager_.Remove(window);
569 }
570
571 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
572   if (!observer_manager_.IsObserving(attached))
573     observer_manager_.Add(attached);
574
575   if (!host_->window()->Contains(attached))
576     return;
577
578   SynthesizeMouseMoveAfterChangeToWindow(attached);
579 }
580
581 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
582                                                            Window* new_root) {
583   if (!host_->window()->Contains(detached))
584     return;
585
586   DCHECK(client::GetCaptureWindow(window()) != window());
587
588   DispatchMouseExitToHidingWindow(detached);
589   SynthesizeMouseMoveAfterChangeToWindow(detached);
590
591   // Hiding the window releases capture which can implicitly destroy the window
592   // so the window may no longer be valid after this call.
593   OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
594 }
595
596 void WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
597                                                        bool visible) {
598   if (!host_->window()->Contains(window))
599     return;
600
601   DispatchMouseExitToHidingWindow(window);
602 }
603
604 void WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
605                                                       bool visible) {
606   if (!host_->window()->Contains(window))
607     return;
608
609   if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
610     PostSynthesizeMouseMove();
611
612   // Hiding the window releases capture which can implicitly destroy the window
613   // so the window may no longer be valid after this call.
614   if (!visible)
615     OnWindowHidden(window, WINDOW_HIDDEN);
616 }
617
618 void WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
619                                                   const gfx::Rect& old_bounds,
620                                                   const gfx::Rect& new_bounds) {
621   if (!host_->window()->Contains(window))
622     return;
623
624   if (window == host_->window()) {
625     TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
626                  "size", new_bounds.size().ToString());
627
628     DispatchDetails details = DispatchHeldEvents();
629     if (details.dispatcher_destroyed)
630       return;
631
632     synthesize_mouse_move_ = false;
633   }
634
635   if (window->IsVisible() && !window->ignore_events()) {
636     gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
637     Window::ConvertRectToTarget(window->parent(), host_->window(),
638                                 &old_bounds_in_root);
639     Window::ConvertRectToTarget(window->parent(), host_->window(),
640                                 &new_bounds_in_root);
641     gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
642     if (old_bounds_in_root.Contains(last_mouse_location) !=
643         new_bounds_in_root.Contains(last_mouse_location)) {
644       PostSynthesizeMouseMove();
645     }
646   }
647 }
648
649 void WindowEventDispatcher::OnWindowTransforming(Window* window) {
650   if (!host_->window()->Contains(window))
651     return;
652
653   SynthesizeMouseMoveAfterChangeToWindow(window);
654 }
655
656 void WindowEventDispatcher::OnWindowTransformed(Window* window) {
657   if (!host_->window()->Contains(window))
658     return;
659
660   SynthesizeMouseMoveAfterChangeToWindow(window);
661 }
662
663 ///////////////////////////////////////////////////////////////////////////////
664 // WindowEventDispatcher, EnvObserver implementation:
665
666 void WindowEventDispatcher::OnWindowInitialized(Window* window) {
667   observer_manager_.Add(window);
668 }
669
670 ////////////////////////////////////////////////////////////////////////////////
671 // WindowEventDispatcher, private:
672
673 ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
674   if (!held_repostable_event_ && !held_move_event_)
675     return DispatchDetails();
676
677   CHECK(!dispatching_held_event_);
678   dispatching_held_event_ = true;
679
680   DispatchDetails dispatch_details;
681   if (held_repostable_event_) {
682     if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
683       scoped_ptr<ui::MouseEvent> mouse_event(
684           static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
685       dispatch_details = OnEventFromSource(mouse_event.get());
686     } else {
687       // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
688       NOTREACHED();
689     }
690     if (dispatch_details.dispatcher_destroyed)
691       return dispatch_details;
692   }
693
694   if (held_move_event_) {
695     // If a mouse move has been synthesized, the target location is suspect,
696     // so drop the held mouse event.
697     if (held_move_event_->IsTouchEvent() ||
698         (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
699       dispatch_details = OnEventFromSource(held_move_event_.get());
700     }
701     if (!dispatch_details.dispatcher_destroyed)
702       held_move_event_.reset();
703   }
704
705   if (!dispatch_details.dispatcher_destroyed)
706     dispatching_held_event_ = false;
707   return dispatch_details;
708 }
709
710 void WindowEventDispatcher::PostSynthesizeMouseMove() {
711   if (synthesize_mouse_move_)
712     return;
713   synthesize_mouse_move_ = true;
714   base::MessageLoop::current()->PostNonNestableTask(
715       FROM_HERE,
716       base::Bind(base::IgnoreResult(
717           &WindowEventDispatcher::SynthesizeMouseMoveEvent),
718           held_event_factory_.GetWeakPtr()));
719 }
720
721 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
722     Window* window) {
723   if (window->IsVisible() &&
724       window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
725     PostSynthesizeMouseMove();
726   }
727 }
728
729 ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
730   DispatchDetails details;
731   if (!synthesize_mouse_move_)
732     return details;
733   synthesize_mouse_move_ = false;
734
735   // If one of the mouse buttons is currently down, then do not synthesize a
736   // mouse-move event. In such cases, aura could synthesize a DRAGGED event
737   // instead of a MOVED event, but in multi-display/multi-host scenarios, the
738   // DRAGGED event can be synthesized in the incorrect host. So avoid
739   // synthesizing any events at all.
740   if (Env::GetInstance()->mouse_button_flags())
741     return details;
742
743   gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
744   if (!window()->bounds().Contains(root_mouse_location))
745     return details;
746   gfx::Point host_mouse_location = root_mouse_location;
747   host_->ConvertPointToHost(&host_mouse_location);
748   ui::MouseEvent event(ui::ET_MOUSE_MOVED,
749                        host_mouse_location,
750                        host_mouse_location,
751                        ui::EF_IS_SYNTHESIZED,
752                        0);
753   return OnEventFromSource(&event);
754 }
755
756 void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
757                                                     ui::LocatedEvent* event) {
758   int flags = event->flags();
759   if (IsNonClientLocation(target, event->location()))
760     flags |= ui::EF_IS_NON_CLIENT;
761   event->set_flags(flags);
762
763   if (!dispatching_held_event_ &&
764       (event->IsMouseEvent() || event->IsScrollEvent()) &&
765       !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
766     if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
767       SetLastMouseLocation(window(), event->root_location());
768     synthesize_mouse_move_ = false;
769   }
770 }
771
772 void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
773                                                   ui::MouseEvent* event) {
774   client::CursorClient* cursor_client = client::GetCursorClient(window());
775   // We allow synthesized mouse exit events through even if mouse events are
776   // disabled. This ensures that hover state, etc on controls like buttons is
777   // cleared.
778   if (cursor_client &&
779       !cursor_client->IsMouseEventsEnabled() &&
780       (event->flags() & ui::EF_IS_SYNTHESIZED) &&
781       (event->type() != ui::ET_MOUSE_EXITED)) {
782     event->SetHandled();
783     return;
784   }
785
786   if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
787     if (move_hold_count_) {
788       if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
789           event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
790         SetLastMouseLocation(window(), event->root_location());
791       }
792       held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
793       event->SetHandled();
794       return;
795     } else {
796       // We may have a held event for a period between the time move_hold_count_
797       // fell to 0 and the DispatchHeldEvents executes. Since we're going to
798       // dispatch the new event directly below, we can reset the old one.
799       held_move_event_.reset();
800     }
801   }
802
803   const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
804                                    ui::EF_MIDDLE_MOUSE_BUTTON |
805                                    ui::EF_RIGHT_MOUSE_BUTTON;
806   switch (event->type()) {
807     case ui::ET_MOUSE_EXITED:
808       if (!target || target == window()) {
809         DispatchDetails details =
810             DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
811         if (details.dispatcher_destroyed) {
812           event->SetHandled();
813           return;
814         }
815         mouse_moved_handler_ = NULL;
816       }
817       break;
818     case ui::ET_MOUSE_MOVED:
819       // Send an exit to the current |mouse_moved_handler_| and an enter to
820       // |target|. Take care that both us and |target| aren't destroyed during
821       // dispatch.
822       if (target != mouse_moved_handler_) {
823         aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
824         WindowTracker live_window;
825         live_window.Add(target);
826         DispatchDetails details =
827             DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
828         if (details.dispatcher_destroyed) {
829           event->SetHandled();
830           return;
831         }
832         // If the |mouse_moved_handler_| changes out from under us, assume a
833         // nested message loop ran and we don't need to do anything.
834         if (mouse_moved_handler_ != old_mouse_moved_handler) {
835           event->SetHandled();
836           return;
837         }
838         if (!live_window.Contains(target) || details.target_destroyed) {
839           mouse_moved_handler_ = NULL;
840           event->SetHandled();
841           return;
842         }
843         live_window.Remove(target);
844
845         mouse_moved_handler_ = target;
846         details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED);
847         if (details.dispatcher_destroyed || details.target_destroyed) {
848           event->SetHandled();
849           return;
850         }
851       }
852       break;
853     case ui::ET_MOUSE_PRESSED:
854       // Don't set the mouse pressed handler for non client mouse down events.
855       // These are only sent by Windows and are not always followed with non
856       // client mouse up events which causes subsequent mouse events to be
857       // sent to the wrong target.
858       if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
859         mouse_pressed_handler_ = target;
860       Env::GetInstance()->set_mouse_button_flags(
861           event->flags() & kMouseButtonFlagMask);
862       break;
863     case ui::ET_MOUSE_RELEASED:
864       mouse_pressed_handler_ = NULL;
865       Env::GetInstance()->set_mouse_button_flags(event->flags() &
866           kMouseButtonFlagMask & ~event->changed_button_flags());
867       break;
868     default:
869       break;
870   }
871
872   PreDispatchLocatedEvent(target, event);
873 }
874
875 void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
876                                                   ui::TouchEvent* event) {
877   switch (event->type()) {
878     case ui::ET_TOUCH_PRESSED:
879       touch_ids_down_ |= (1 << event->touch_id());
880       Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
881       break;
882
883       // Handle ET_TOUCH_CANCELLED only if it has a native event.
884     case ui::ET_TOUCH_CANCELLED:
885       if (!event->HasNativeEvent())
886         break;
887       // fallthrough
888     case ui::ET_TOUCH_RELEASED:
889       touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
890             (1 << event->touch_id());
891       Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
892       break;
893
894     case ui::ET_TOUCH_MOVED:
895       if (move_hold_count_ && !dispatching_held_event_) {
896         held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
897         event->SetHandled();
898         return;
899       }
900       break;
901
902     default:
903       NOTREACHED();
904       break;
905   }
906
907   PreDispatchLocatedEvent(target, event);
908 }
909
910 }  // namespace aura