window_event_dispatcher.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/trace_event.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
10ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/capture_client.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/cursor_client.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/event_client.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/client/focus_client.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/client/screen_position_client.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/env.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window_delegate.h"
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/aura/window_targeter.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window_tracker.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ui/aura/window_tree_host.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/hit_test.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/compositor/dip_util.h"
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h"
2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/gestures/gesture_recognizer.h"
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/gestures/gesture_types.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)typedef ui::EventDispatchDetails DispatchDetails;
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace aura {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |target| has a non-client (frame) component at |location|,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in window coordinates.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsNonClientLocation(Window* target, const gfx::Point& location) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!target->delegate())
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int hit_test_code = target->delegate()->GetNonClientComponent(location);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return hit_test_code != HTCLIENT && hit_test_code != HTNOWHERE;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Window* ConsumerToWindow(ui::GestureConsumer* consumer) {
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return consumer ? static_cast<Window*>(consumer) : NULL;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void SetLastMouseLocation(const Window* root_window,
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          const gfx::Point& location_in_root) {
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  client::ScreenPositionClient* client =
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      client::GetScreenPositionClient(root_window);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (client) {
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Point location_in_screen = location_in_root;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    client->ConvertPointToScreen(root_window, &location_in_screen);
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Env::GetInstance()->set_last_mouse_location(location_in_screen);
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Env::GetInstance()->set_last_mouse_location(location_in_root);
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsEventCandidateForHold(const ui::Event& event) {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.type() == ui::ET_TOUCH_MOVED)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.type() == ui::ET_MOUSE_DRAGGED)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED))
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, public:
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : host_(host),
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      touch_ids_down_(0),
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_pressed_handler_(NULL),
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_moved_handler_(NULL),
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      event_dispatch_target_(NULL),
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      old_dispatch_target_(NULL),
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      synthesize_mouse_move_(false),
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      move_hold_count_(0),
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatching_held_event_(false),
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      observer_manager_(this),
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      repost_event_factory_(this),
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      held_event_factory_(this) {
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ui::GestureRecognizer::Get()->AddGestureEventHelper(this);
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Env::GetInstance()->AddObserver(this);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)WindowEventDispatcher::~WindowEventDispatcher() {
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor");
94effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Env::GetInstance()->RemoveObserver(this);
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent& event) {
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(event.type() == ui::ET_MOUSE_PRESSED ||
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)         event.type() == ui::ET_GESTURE_TAP_DOWN);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We allow for only one outstanding repostable event. This is used
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in exiting context menus.  A dropped repost request is allowed.
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (event.type() == ui::ET_MOUSE_PRESSED) {
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    held_repostable_event_.reset(
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        new ui::MouseEvent(
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            static_cast<const ui::MouseEvent&>(event),
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            static_cast<aura::Window*>(event.target()),
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            window()));
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostNonNestableTask(
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        FROM_HERE, base::Bind(
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            repost_event_factory_.GetWeakPtr()));
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK(event.type() == ui::ET_GESTURE_TAP_DOWN);
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    held_repostable_event_.reset();
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // TODO(rbyers): Reposing of gestures is tricky to get
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // right, so it's not yet supported.  crbug.com/170987.
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnMouseEventsEnableStateChanged(bool enabled) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Send entered / exited so that visual state can be updated to match
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // mouse events state.
1245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  PostSynthesizeMouseMove();
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(mazda): Add code to disable mouse events when |enabled| == false.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchCancelModeEvent() {
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ui::CancelModeEvent event;
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* focused_window = client::GetFocusClient(window())->GetFocusedWindow();
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (focused_window && !window()->Contains(focused_window))
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    focused_window = NULL;
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DispatchDetails details =
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      DispatchEvent(focused_window ? focused_window : window(), &event);
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (details.dispatcher_destroyed)
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchGestureEvent(ui::GestureEvent* event) {
140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details = DispatchHeldEvents();
141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (details.dispatcher_destroyed)
142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Window* target = GetGestureTarget(event);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (target) {
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    event->ConvertLocationToTarget(window(), target);
147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DispatchDetails details = DispatchEvent(target, event);
148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
149f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
153116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint(
154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const gfx::Point& point) {
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE,
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       ui::EF_NONE);
157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return DispatchMouseEnterOrExit(event, ui::ET_MOUSE_EXITED);
15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event,
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                Window* window,
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                ui::EventResult result) {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  gestures.reset(ui::GestureRecognizer::Get()->
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ProcessTouchEventForGesture(*event, result, window));
166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details = ProcessGestures(gestures.get());
167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (details.dispatcher_destroyed)
168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::HoldPointerMoves() {
1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    held_event_factory_.InvalidateWeakPtrs();
1747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ++move_hold_count_;
175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           this);
1777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleasePointerMoves() {
1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  --move_hold_count_;
1817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DCHECK_GE(move_hold_count_, 0);
1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_ && held_move_event_) {
1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // We don't want to call DispatchHeldEvents directly, because this might be
1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // called from a deep stack while another event, in which case dispatching
1857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // another one may not be safe/expected.  Instead we post a task, that we
1867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // may cancel if HoldPointerMoves is called again before it executes.
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostNonNestableTask(
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        FROM_HERE, base::Bind(
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          held_event_factory_.GetWeakPtr()));
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point location = Env::GetInstance()->last_mouse_location();
197f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::ScreenPositionClient* client =
198f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      client::GetScreenPositionClient(window());
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (client)
200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    client->ConvertPointFromScreen(window(), &location);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return location;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WindowEventDispatcher::OnHostLostMouseGrab() {
2055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  mouse_pressed_handler_ = NULL;
2065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  mouse_moved_handler_ = NULL;
2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnCursorMovedToRootLocation(
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const gfx::Point& root_location) {
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetLastMouseLocation(window(), root_location);
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  synthesize_mouse_move_ = false;
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
215effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
216effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(window, WINDOW_DESTROYED);
217effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
218effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)Window* WindowEventDispatcher::window() {
22323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
22423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
22523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
22623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)const Window* WindowEventDispatcher::window() const {
22723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
22823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
22923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::LocatedEvent* event) {
23223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  event->UpdateForRootTransform(host_->GetInverseRootTransform());
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
235effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
236effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The mouse capture is intentionally ignored. Think that a mouse enters
237effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // to a window, the window sets the capture, the mouse exits the window,
238effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // and then it releases the capture. In that case OnMouseExited won't
239effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // be called. So it is natural not to emit OnMouseExited even though
240effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |window| is the capture window.
241effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
242effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->Contains(mouse_moved_handler_) &&
243116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      window->ContainsPointInRoot(last_mouse_location)) {
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DispatchDetails details = DispatchMouseExitAtPoint(last_mouse_location);
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (details.dispatcher_destroyed)
246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
247116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
248effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
249effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
251f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ui::MouseEvent& event,
252f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::EventType type) {
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SetLastMouseLocation(window(), event.root_location());
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
25823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
25923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      !window()->Contains(mouse_moved_handler_))
260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return DispatchDetails();
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |event| may be an event in the process of being dispatched to a target (in
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // which case its locations will be in the event's target's coordinate
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // system), or a synthetic event created in root-window (in which case, the
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // event's target will be NULL, and the event will be in the root-window's
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // coordinate system.
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::Window* target = static_cast<Window*>(event.target());
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!target)
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    target = window();
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::MouseEvent translated_event(event,
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  target,
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  mouse_moved_handler_,
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  type,
274eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  event.flags() | ui::EF_IS_SYNTHESIZED);
275a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchEvent(mouse_moved_handler_, &translated_event);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
279f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::GestureRecognizer::Gestures* gestures) {
280f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details;
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!gestures || gestures->empty())
282f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return details;
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Window* target = GetGestureTarget(gestures->get().at(0));
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < gestures->size(); ++i) {
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::GestureEvent* event = gestures->get().at(i);
287f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    event->ConvertLocationToTarget(window(), target);
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details = DispatchEvent(target, event);
289f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed || details.target_destroyed)
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
292f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return details;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnWindowHidden(Window* invisible,
296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                           WindowHiddenReason reason) {
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the window the mouse was pressed in becomes invisible, it should no
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // longer receive mouse events.
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_pressed_handler_))
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_pressed_handler_ = NULL;
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_moved_handler_))
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_moved_handler_ = NULL;
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If events are being dispatched from a nested message-loop, and the target
305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // of the outer loop is hidden or moved to another dispatcher during
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // dispatching events in the inner loop, then reset the target for the outer
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // loop.
308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (invisible->Contains(old_dispatch_target_))
309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    old_dispatch_target_ = NULL;
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
311c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  invisible->CleanupGestureState();
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Do not clear the capture, and the |event_dispatch_target_| if the
314effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // window is moving across hosts, because the target itself is actually still
315effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // visible and clearing them stops further event processing, which can cause
316effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // unexpected behaviors. See crbug.com/157583
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reason != WINDOW_MOVING) {
318effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // We don't ask |invisible| here, because invisible may have been removed
319effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // from the window hierarchy already by the time this function is called
320effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // (OnWindowDestroyed).
321effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    client::CaptureClient* capture_client =
322effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        client::GetCaptureClient(host_->window());
323effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* capture_window =
324effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        capture_client ? capture_client->GetCaptureWindow() : NULL;
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (invisible->Contains(event_dispatch_target_))
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      event_dispatch_target_ = NULL;
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If the ancestor of the capture window is hidden, release the capture.
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Note that this may delete the window so do not use capture_window
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // after this.
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (invisible->Contains(capture_window) && invisible != window())
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      capture_window->ReleaseCapture();
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
337effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochWindow* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
338effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Window* target = NULL;
339effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!event->IsEndingEvent()) {
340effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // The window that received the start event (e.g. scroll begin) needs to
341effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // receive the end event (e.g. scroll end).
342effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = client::GetCaptureWindow(window());
343effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
344effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!target) {
345effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = ConsumerToWindow(
346effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
347effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
348effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
349effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return target;
350effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
351effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
3528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
353a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, aura::client::CaptureDelegate implementation:
3548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
355a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::UpdateCapture(Window* old_capture,
356a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                          Window* new_capture) {
3578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // |mouse_moved_handler_| may have been set to a Window in a different root
3588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // (see below). Clear it here to ensure we don't end up referencing a stale
3598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Window.
360f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
3618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mouse_moved_handler_ = NULL;
3628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
363f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (old_capture && old_capture->GetRootWindow() == window() &&
3648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      old_capture->delegate()) {
3658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Send a capture changed event with bogus location data.
3668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         gfx::Point(), 0, 0);
3688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
369a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DispatchDetails details = DispatchEvent(old_capture, &event);
370f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    old_capture->delegate()->OnCaptureLost();
3748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (new_capture) {
3778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make all subsequent mouse events go to the capture window. We shouldn't
3788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // need to send an event here as OnCaptureLost() should take care of that.
3798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
3808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      mouse_moved_handler_ = new_capture;
3818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
3828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make sure mouse_moved_handler gets updated.
3835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    DispatchDetails details = SynthesizeMouseMoveEvent();
384f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
385f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  mouse_pressed_handler_ = NULL;
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnOtherRootGotCapture() {
3915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Sending the mouse exit causes bugs on Windows (e.g. crbug.com/394672).
3925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // TODO(pkotwicz): Fix the bugs and send mouse exit on Windows too.
3935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if !defined(OS_WIN)
394116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (mouse_moved_handler_) {
395116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Dispatch a mouse exit to reset any state associated with hover. This is
396116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // important when going from no window having capture to a window having
397116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
398116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DispatchDetails details = DispatchMouseExitAtPoint(
399116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        GetLastMouseLocationInRoot());
400116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (details.dispatcher_destroyed)
401116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
402116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
4035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
404116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
4051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_moved_handler_ = NULL;
4061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_pressed_handler_ = NULL;
4071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
4081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::SetNativeCapture() {
4108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->SetCapture();
4118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleaseNativeCapture() {
4148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->ReleaseCapture();
4158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventProcessor implementation:
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
420a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return window();
421a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
422a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PrepareEventForDispatch(ui::Event* event) {
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (dispatching_held_event_) {
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The held events are already in |window()|'s coordinate system. So it is
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // not necessary to apply the transform to convert from the host's
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // coordinate system to |window()|'s coordinate system.
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent() ||
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsScrollEvent() ||
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsTouchEvent() ||
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsGestureEvent()) {
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
438a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
4408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return event_dispatch_target_ == target;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::Event* event) {
448a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* target_window = static_cast<Window*>(target);
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(window()->Contains(target_window));
450a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_) {
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool can_be_held = IsEventCandidateForHold(*event);
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!move_hold_count_ || !can_be_held) {
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (can_be_held)
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset();
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DispatchDetails details = DispatchHeldEvents();
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (details.dispatcher_destroyed || details.target_destroyed)
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return details;
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent()) {
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsScrollEvent()) {
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchLocatedEvent(target_window,
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            static_cast<ui::ScrollEvent*>(event));
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsTouchEvent()) {
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
470a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = event_dispatch_target_;
471a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = static_cast<Window*>(target);
472a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchDetails();
473a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
474a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
475a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
476a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
477a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const ui::Event& event) {
478a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DispatchDetails details;
479a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!target || target != event_dispatch_target_)
480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details.target_destroyed = true;
481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = old_dispatch_target_;
482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = NULL;
483a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef NDEBUG
484a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
485a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.IsTouchEvent() && !details.target_destroyed) {
488f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Do not let 'held' touch events contribute to any gestures unless it is
489f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // being dispatched.
490f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (dispatching_held_event_ || !held_move_event_ ||
491f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        !held_move_event_->IsTouchEvent()) {
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event),
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                static_cast<Window*>(event.target()), window());
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // Get the list of GestureEvents from GestureRecognizer.
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      gestures.reset(ui::GestureRecognizer::Get()->
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ProcessTouchEventForGesture(orig_event, event.result(),
4985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      static_cast<Window*>(target)));
4995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return ProcessGestures(gestures.get());
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
5015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
5025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
503a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return details;
504a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
505a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::GestureEventHelper implementation:
5088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToConsumer(
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::GestureConsumer* consumer) {
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* consumer_window = ConsumerToWindow(consumer);
512f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return (consumer_window && consumer_window->GetRootWindow() == window());
5138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details = OnEventFromSource(event);
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (details.dispatcher_destroyed)
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
522effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, WindowObserver implementation:
523effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
524effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroying(Window* window) {
525effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
526effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
527effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
528effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
529effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
530effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
531effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
532effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroyed(Window* window) {
533effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // We observe all windows regardless of what root Window (if any) they're
534effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // attached to.
535effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Remove(window);
536effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
537effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
538effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
539effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!observer_manager_.IsObserving(attached))
540effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    observer_manager_.Add(attached);
541effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
542effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(attached))
543effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
544effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
545effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(attached);
546effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
547effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
548effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
549effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                           Window* new_root) {
550effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(detached))
551effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
552effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
553effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(client::GetCaptureWindow(window()) != window());
554effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
555effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(detached);
556effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(detached);
557effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
558effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
559effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
560effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
561effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
562effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
563effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
564effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                       bool visible) {
565effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
566effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
567effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
568effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
569effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
570effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
571effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
572effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                      bool visible) {
573effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
574effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
575effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
576effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
5775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PostSynthesizeMouseMove();
578effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
579effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
580effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
581effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!visible)
582effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    OnWindowHidden(window, WINDOW_HIDDEN);
583effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
584effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
585effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
586effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& old_bounds,
587effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& new_bounds) {
588effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
589effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
590effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
591effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window == host_->window()) {
592effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
593effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 "size", new_bounds.size().ToString());
594effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
595effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DispatchDetails details = DispatchHeldEvents();
596effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (details.dispatcher_destroyed)
597effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
598effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
599effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    synthesize_mouse_move_ = false;
600effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
601effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
6020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (window->IsVisible() && !window->ignore_events()) {
603effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
604effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
605effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &old_bounds_in_root);
606effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
607effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &new_bounds_in_root);
608effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
609effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (old_bounds_in_root.Contains(last_mouse_location) !=
610effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        new_bounds_in_root.Contains(last_mouse_location)) {
6115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      PostSynthesizeMouseMove();
612effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
613effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
614effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
615effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
616effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransforming(Window* window) {
617effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
618effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
619effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
620effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
621effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
622effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
623effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransformed(Window* window) {
624effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
625effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
626effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
627effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
628effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
629effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
630effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///////////////////////////////////////////////////////////////////////////////
631effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, EnvObserver implementation:
632effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
633effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowInitialized(Window* window) {
634effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Add(window);
635effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
636effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
637effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch////////////////////////////////////////////////////////////////////////////////
638a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
640a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
6415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!held_repostable_event_ && !held_move_event_)
6425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return DispatchDetails();
6435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CHECK(!dispatching_held_event_);
6455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dispatching_held_event_ = true;
6465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails dispatch_details;
6485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_repostable_event_) {
6495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
6505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::MouseEvent> mouse_event(
6515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(mouse_event.get());
653f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else {
6545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
6555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      NOTREACHED();
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
6575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (dispatch_details.dispatcher_destroyed)
6585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return dispatch_details;
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
6605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_move_event_) {
6625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If a mouse move has been synthesized, the target location is suspect,
6635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // so drop the held mouse event.
6645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_move_event_->IsTouchEvent() ||
6655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(held_move_event_.get());
6675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
6685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!dispatch_details.dispatcher_destroyed)
6695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
6705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatch_details.dispatcher_destroyed)
6735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    dispatching_held_event_ = false;
6745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return dispatch_details;
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid WindowEventDispatcher::PostSynthesizeMouseMove() {
6785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (synthesize_mouse_move_)
6795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
6805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = true;
6815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::MessageLoop::current()->PostNonNestableTask(
6825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
683a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(base::IgnoreResult(
684a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          &WindowEventDispatcher::SynthesizeMouseMoveEvent),
6855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          held_event_factory_.GetWeakPtr()));
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
688effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
689effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* window) {
690effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->IsVisible() &&
691effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
6925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PostSynthesizeMouseMove();
693effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
694effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
695effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
6965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
6975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details;
6985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!synthesize_mouse_move_)
6995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = false;
701116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
702116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // If one of the mouse buttons is currently down, then do not synthesize a
703116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // mouse-move event. In such cases, aura could synthesize a DRAGGED event
704116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // instead of a MOVED event, but in multi-display/multi-host scenarios, the
705116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // DRAGGED event can be synthesized in the incorrect host. So avoid
706116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // synthesizing any events at all.
707116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (Env::GetInstance()->mouse_button_flags())
708116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return details;
709116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
7115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!window()->bounds().Contains(root_mouse_location))
7125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point host_mouse_location = root_mouse_location;
71423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  host_->ConvertPointToHost(&host_mouse_location);
7155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ui::MouseEvent event(ui::ET_MOUSE_MOVED,
7165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       host_mouse_location,
7175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       host_mouse_location,
7185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       ui::EF_IS_SYNTHESIZED,
7195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       0);
7205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return OnEventFromSource(&event);
7215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
723a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
724a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                    ui::LocatedEvent* event) {
7255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int flags = event->flags();
7265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsNonClientLocation(target, event->location()))
7275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    flags |= ui::EF_IS_NON_CLIENT;
7285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  event->set_flags(flags);
7295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_ &&
7315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (event->IsMouseEvent() || event->IsScrollEvent()) &&
7325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
7335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
7345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      SetLastMouseLocation(window(), event->root_location());
7355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    synthesize_mouse_move_ = false;
7364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
739a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
740a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::MouseEvent* event) {
741f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::CursorClient* cursor_client = client::GetCursorClient(window());
742c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // We allow synthesized mouse exit events through even if mouse events are
743c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // disabled. This ensures that hover state, etc on controls like buttons is
744c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // cleared.
745eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (cursor_client &&
746eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !cursor_client->IsMouseEventsEnabled() &&
747c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (event->flags() & ui::EF_IS_SYNTHESIZED) &&
748c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (event->type() != ui::ET_MOUSE_EXITED)) {
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    event->SetHandled();
7505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
7515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
752eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
7545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (move_hold_count_) {
7555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
7565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
7575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SetLastMouseLocation(window(), event->root_location());
7585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
7595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
7605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->SetHandled();
7615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
7625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // We may have a held event for a period between the time move_hold_count_
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // fell to 0 and the DispatchHeldEvents executes. Since we're going to
7655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // dispatch the new event directly below, we can reset the old one.
7665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_MIDDLE_MOUSE_BUTTON |
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_RIGHT_MOUSE_BUTTON;
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (event->type()) {
7742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ui::ET_MOUSE_EXITED:
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!target || target == window()) {
776f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DispatchDetails details =
777f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed) {
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
7828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mouse_moved_handler_ = NULL;
7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_MOVED:
78658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // Send an exit to the current |mouse_moved_handler_| and an enter to
78758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // |target|. Take care that both us and |target| aren't destroyed during
78858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // dispatch.
78958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (target != mouse_moved_handler_) {
79058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
7915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        WindowTracker live_window;
7925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Add(target);
793f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DispatchDetails details =
794f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
7955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed) {
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
7975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
7985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
79958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // If the |mouse_moved_handler_| changes out from under us, assume a
80058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // nested message loop ran and we don't need to do anything.
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (mouse_moved_handler_ != old_mouse_moved_handler) {
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (!live_window.Contains(target) || details.target_destroyed) {
8068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          mouse_moved_handler_ = NULL;
8075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
8105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Remove(target);
8115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        mouse_moved_handler_ = target;
8135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED);
8145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed || details.target_destroyed) {
8155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        }
81858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_PRESSED:
8212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't set the mouse pressed handler for non client mouse down events.
8222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // These are only sent by Windows and are not always followed with non
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // client mouse up events which causes subsequent mouse events to be
8242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // sent to the wrong target.
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mouse_pressed_handler_ = target;
827eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(
828eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          event->flags() & kMouseButtonFlagMask);
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_RELEASED:
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_pressed_handler_ = NULL;
832eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(event->flags() &
833eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          kMouseButtonFlagMask & ~event->changed_button_flags());
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
842a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
843a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::TouchEvent* event) {
8447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (event->type()) {
8457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_PRESSED:
8467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ |= (1 << event->touch_id());
8477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // Handle ET_TOUCH_CANCELLED only if it has a native event.
8517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_CANCELLED:
8527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!event->HasNativeEvent())
8537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        break;
8547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // fallthrough
8557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_RELEASED:
8567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
8577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            (1 << event->touch_id());
8587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case ui::ET_TOUCH_MOVED:
8625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (move_hold_count_ && !dispatching_held_event_) {
8635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
8645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        event->SetHandled();
8655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return;
8665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
8677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    default:
8701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      NOTREACHED();
8715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
877