window_event_dispatcher.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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) {
1636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ui::TouchEvent orig_event(*event, window, this->window());
1646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Once we've fully migrated to the eager gesture detector, we won't need to
1656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // pass an event here.
1666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<ui::GestureRecognizer::Gestures> gestures(
1676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ui::GestureRecognizer::Get()->ProcessTouchEventOnAsyncAck(
1686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          orig_event, result, window));
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details = ProcessGestures(gestures.get());
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (details.dispatcher_destroyed)
171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::HoldPointerMoves() {
1757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_)
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    held_event_factory_.InvalidateWeakPtrs();
1777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ++move_hold_count_;
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           this);
1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleasePointerMoves() {
1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  --move_hold_count_;
1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DCHECK_GE(move_hold_count_, 0);
1857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_ && held_move_event_) {
1867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // We don't want to call DispatchHeldEvents directly, because this might be
1877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // called from a deep stack while another event, in which case dispatching
1887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // another one may not be safe/expected.  Instead we post a task, that we
1897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // may cancel if HoldPointerMoves is called again before it executes.
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostNonNestableTask(
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        FROM_HERE, base::Bind(
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          held_event_factory_.GetWeakPtr()));
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point location = Env::GetInstance()->last_mouse_location();
200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::ScreenPositionClient* client =
201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      client::GetScreenPositionClient(window());
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (client)
203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    client->ConvertPointFromScreen(window(), &location);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return location;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WindowEventDispatcher::OnHostLostMouseGrab() {
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  mouse_pressed_handler_ = NULL;
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  mouse_moved_handler_ = NULL;
2105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnCursorMovedToRootLocation(
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const gfx::Point& root_location) {
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetLastMouseLocation(window(), root_location);
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  synthesize_mouse_move_ = false;
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
218effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
219effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(window, WINDOW_DESTROYED);
220effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
221effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)Window* WindowEventDispatcher::window() {
22623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
22723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
22823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
22923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)const Window* WindowEventDispatcher::window() const {
23023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
23123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
23223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::LocatedEvent* event) {
23523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  event->UpdateForRootTransform(host_->GetInverseRootTransform());
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
238effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
239effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The mouse capture is intentionally ignored. Think that a mouse enters
240effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // to a window, the window sets the capture, the mouse exits the window,
241effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // and then it releases the capture. In that case OnMouseExited won't
242effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // be called. So it is natural not to emit OnMouseExited even though
243effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |window| is the capture window.
244effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
245effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->Contains(mouse_moved_handler_) &&
246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      window->ContainsPointInRoot(last_mouse_location)) {
247116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DispatchDetails details = DispatchMouseExitAtPoint(last_mouse_location);
248116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (details.dispatcher_destroyed)
249116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
250116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
251effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
252effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
254f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ui::MouseEvent& event,
255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::EventType type) {
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SetLastMouseLocation(window(), event.root_location());
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
26123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
26223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      !window()->Contains(mouse_moved_handler_))
263f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return DispatchDetails();
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |event| may be an event in the process of being dispatched to a target (in
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // which case its locations will be in the event's target's coordinate
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // system), or a synthetic event created in root-window (in which case, the
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // event's target will be NULL, and the event will be in the root-window's
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // coordinate system.
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::Window* target = static_cast<Window*>(event.target());
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!target)
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    target = window();
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::MouseEvent translated_event(event,
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  target,
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  mouse_moved_handler_,
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  type,
277eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  event.flags() | ui::EF_IS_SYNTHESIZED);
278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchEvent(mouse_moved_handler_, &translated_event);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
282f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::GestureRecognizer::Gestures* gestures) {
283f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!gestures || gestures->empty())
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return details;
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Window* target = GetGestureTarget(gestures->get().at(0));
2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!target)
2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return details;
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < gestures->size(); ++i) {
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::GestureEvent* event = gestures->get().at(i);
293f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    event->ConvertLocationToTarget(window(), target);
294a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details = DispatchEvent(target, event);
295f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed || details.target_destroyed)
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return details;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnWindowHidden(Window* invisible,
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                           WindowHiddenReason reason) {
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the window the mouse was pressed in becomes invisible, it should no
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // longer receive mouse events.
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_pressed_handler_))
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_pressed_handler_ = NULL;
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_moved_handler_))
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_moved_handler_ = NULL;
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If events are being dispatched from a nested message-loop, and the target
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // of the outer loop is hidden or moved to another dispatcher during
312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // dispatching events in the inner loop, then reset the target for the outer
313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // loop.
314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (invisible->Contains(old_dispatch_target_))
315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    old_dispatch_target_ = NULL;
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
317c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  invisible->CleanupGestureState();
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Do not clear the capture, and the |event_dispatch_target_| if the
320effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // window is moving across hosts, because the target itself is actually still
321effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // visible and clearing them stops further event processing, which can cause
322effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // unexpected behaviors. See crbug.com/157583
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reason != WINDOW_MOVING) {
324effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // We don't ask |invisible| here, because invisible may have been removed
325effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // from the window hierarchy already by the time this function is called
326effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // (OnWindowDestroyed).
327effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    client::CaptureClient* capture_client =
328effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        client::GetCaptureClient(host_->window());
329effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* capture_window =
330effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        capture_client ? capture_client->GetCaptureWindow() : NULL;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (invisible->Contains(event_dispatch_target_))
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      event_dispatch_target_ = NULL;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If the ancestor of the capture window is hidden, release the capture.
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Note that this may delete the window so do not use capture_window
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // after this.
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (invisible->Contains(capture_window) && invisible != window())
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      capture_window->ReleaseCapture();
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
343effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochWindow* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
344effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Window* target = NULL;
345effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!event->IsEndingEvent()) {
346effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // The window that received the start event (e.g. scroll begin) needs to
347effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // receive the end event (e.g. scroll end).
348effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = client::GetCaptureWindow(window());
349effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
350effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!target) {
351effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = ConsumerToWindow(
352effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
353effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
354effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
355effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return target;
356effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
357effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
3588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
359a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, aura::client::CaptureDelegate implementation:
3608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::UpdateCapture(Window* old_capture,
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                          Window* new_capture) {
3638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // |mouse_moved_handler_| may have been set to a Window in a different root
3648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // (see below). Clear it here to ensure we don't end up referencing a stale
3658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Window.
366f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
3678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mouse_moved_handler_ = NULL;
3688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
369f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (old_capture && old_capture->GetRootWindow() == window() &&
3708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      old_capture->delegate()) {
3718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Send a capture changed event with bogus location data.
3728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         gfx::Point(), 0, 0);
3748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
375a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DispatchDetails details = DispatchEvent(old_capture, &event);
376f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
377f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    old_capture->delegate()->OnCaptureLost();
3808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (new_capture) {
3838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make all subsequent mouse events go to the capture window. We shouldn't
3848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // need to send an event here as OnCaptureLost() should take care of that.
3858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
3868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      mouse_moved_handler_ = new_capture;
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make sure mouse_moved_handler gets updated.
3895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    DispatchDetails details = SynthesizeMouseMoveEvent();
390f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
391f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  mouse_pressed_handler_ = NULL;
3948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnOtherRootGotCapture() {
3976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Windows provides the TrackMouseEvents API which allows us to rely on the
3986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on
3996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // desktop Windows, every top level window could potentially have its own
4006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // root window, in which case this function will get called whenever those
4016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // windows grab mouse capture. Sending mouse exit messages in these cases
4026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // causes subtle bugs like (crbug.com/394672).
4035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if !defined(OS_WIN)
404116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (mouse_moved_handler_) {
405116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Dispatch a mouse exit to reset any state associated with hover. This is
406116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // important when going from no window having capture to a window having
407116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
408116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DispatchDetails details = DispatchMouseExitAtPoint(
409116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        GetLastMouseLocationInRoot());
410116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (details.dispatcher_destroyed)
411116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
412116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
4135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
414116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
4151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_moved_handler_ = NULL;
4161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_pressed_handler_ = NULL;
4171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
4181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::SetNativeCapture() {
4208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->SetCapture();
4218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleaseNativeCapture() {
4248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->ReleaseCapture();
4258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventProcessor implementation:
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
430a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return window();
431a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
432a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PrepareEventForDispatch(ui::Event* event) {
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (dispatching_held_event_) {
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The held events are already in |window()|'s coordinate system. So it is
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // not necessary to apply the transform to convert from the host's
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // coordinate system to |window()|'s coordinate system.
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent() ||
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsScrollEvent() ||
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsTouchEvent() ||
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsGestureEvent()) {
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
448a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
4508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
451a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return event_dispatch_target_ == target;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
457a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::Event* event) {
458a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* target_window = static_cast<Window*>(target);
459a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(window()->Contains(target_window));
460a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_) {
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool can_be_held = IsEventCandidateForHold(*event);
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!move_hold_count_ || !can_be_held) {
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (can_be_held)
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset();
4665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DispatchDetails details = DispatchHeldEvents();
4675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (details.dispatcher_destroyed || details.target_destroyed)
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return details;
4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent()) {
4735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
4745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsScrollEvent()) {
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchLocatedEvent(target_window,
4765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            static_cast<ui::ScrollEvent*>(event));
4775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsTouchEvent()) {
4785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = event_dispatch_target_;
481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = static_cast<Window*>(target);
482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchDetails();
483a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
484a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
485a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
486a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
487a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const ui::Event& event) {
488a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DispatchDetails details;
489a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!target || target != event_dispatch_target_)
490a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details.target_destroyed = true;
491a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = old_dispatch_target_;
492a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = NULL;
493a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef NDEBUG
494a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
495a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.IsTouchEvent() && !details.target_destroyed) {
498f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // Do not let 'held' touch events contribute to any gestures unless it is
499f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // being dispatched.
500f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (dispatching_held_event_ || !held_move_event_ ||
501f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        !held_move_event_->IsTouchEvent()) {
5026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // If the event is being handled asynchronously, ignore it.
5036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      if(event.result() & ui::ER_CONSUMED)
5046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        return details;
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
5066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
5076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Once we've fully migrated to the eager gesture detector, we won't
5086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // need to pass an event here.
5096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event),
5106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                static_cast<Window*>(event.target()),
5116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                window());
5126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      gestures.reset(
5136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          ui::GestureRecognizer::Get()->ProcessTouchEventPostDispatch(
5146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)              orig_event, event.result(), static_cast<Window*>(target)));
5156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return ProcessGestures(gestures.get());
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return details;
521a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
522a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::GestureEventHelper implementation:
5258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToConsumer(
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::GestureConsumer* consumer) {
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* consumer_window = ConsumerToWindow(consumer);
529f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return (consumer_window && consumer_window->GetRootWindow() == window());
5308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
5335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details = OnEventFromSource(event);
5345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (details.dispatcher_destroyed)
5355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
539effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, WindowObserver implementation:
540effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
541effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroying(Window* window) {
542effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
543effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
544effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
545effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
546effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
547effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
548effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
549effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroyed(Window* window) {
550effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // We observe all windows regardless of what root Window (if any) they're
551effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // attached to.
552effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Remove(window);
553effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
554effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
555effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
556effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!observer_manager_.IsObserving(attached))
557effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    observer_manager_.Add(attached);
558effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
559effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(attached))
560effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
561effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
562effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(attached);
563effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
564effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
565effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
566effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                           Window* new_root) {
567effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(detached))
568effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
569effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
570effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(client::GetCaptureWindow(window()) != window());
571effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
572effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(detached);
573effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(detached);
574effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
575effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
576effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
577effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
578effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
579effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
580effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
581effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                       bool visible) {
582effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
583effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
584effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
585effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
586effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
587effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
588effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
589effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                      bool visible) {
590effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
591effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
592effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
593effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
5945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PostSynthesizeMouseMove();
595effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
596effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
597effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
598effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!visible)
599effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    OnWindowHidden(window, WINDOW_HIDDEN);
600effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
601effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
602effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
603effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& old_bounds,
604effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& new_bounds) {
605effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
606effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
607effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
608effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window == host_->window()) {
609effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
610effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 "size", new_bounds.size().ToString());
611effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
612effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DispatchDetails details = DispatchHeldEvents();
613effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (details.dispatcher_destroyed)
614effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
615effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
616effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    synthesize_mouse_move_ = false;
617effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
618effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
6190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (window->IsVisible() && !window->ignore_events()) {
620effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
621effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
622effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &old_bounds_in_root);
623effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
624effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &new_bounds_in_root);
625effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
626effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (old_bounds_in_root.Contains(last_mouse_location) !=
627effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        new_bounds_in_root.Contains(last_mouse_location)) {
6285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      PostSynthesizeMouseMove();
629effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
630effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
631effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
632effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
633effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransforming(Window* window) {
634effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
635effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
636effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
637effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
638effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
639effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
640effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransformed(Window* window) {
641effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
642effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
643effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
644effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
645effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
646effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
647effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///////////////////////////////////////////////////////////////////////////////
648effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, EnvObserver implementation:
649effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
650effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowInitialized(Window* window) {
651effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Add(window);
652effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
653effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
654effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch////////////////////////////////////////////////////////////////////////////////
655a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
657a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
6585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!held_repostable_event_ && !held_move_event_)
6595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return DispatchDetails();
6605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CHECK(!dispatching_held_event_);
6625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dispatching_held_event_ = true;
6635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails dispatch_details;
6655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_repostable_event_) {
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
6675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::MouseEvent> mouse_event(
6685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
6695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(mouse_event.get());
670f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else {
6715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
6725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      NOTREACHED();
673f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
6745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (dispatch_details.dispatcher_destroyed)
6755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return dispatch_details;
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
6775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_move_event_) {
6795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If a mouse move has been synthesized, the target location is suspect,
6805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // so drop the held mouse event.
6815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_move_event_->IsTouchEvent() ||
6825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
6835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(held_move_event_.get());
6845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
6855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!dispatch_details.dispatcher_destroyed)
6865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatch_details.dispatcher_destroyed)
6905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    dispatching_held_event_ = false;
6915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return dispatch_details;
692f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
693f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid WindowEventDispatcher::PostSynthesizeMouseMove() {
6955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (synthesize_mouse_move_)
6965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
6975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = true;
6985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::MessageLoop::current()->PostNonNestableTask(
6995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
700a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(base::IgnoreResult(
701a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          &WindowEventDispatcher::SynthesizeMouseMoveEvent),
7025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu          held_event_factory_.GetWeakPtr()));
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
706effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* window) {
707effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->IsVisible() &&
708effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
7095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    PostSynthesizeMouseMove();
710effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
711effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
712effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
7135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
7145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details;
7155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!synthesize_mouse_move_)
7165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = false;
718116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
719116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // If one of the mouse buttons is currently down, then do not synthesize a
720116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // mouse-move event. In such cases, aura could synthesize a DRAGGED event
721116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // instead of a MOVED event, but in multi-display/multi-host scenarios, the
722116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // DRAGGED event can be synthesized in the incorrect host. So avoid
723116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // synthesizing any events at all.
724116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (Env::GetInstance()->mouse_button_flags())
725116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return details;
726116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
7285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!window()->bounds().Contains(root_mouse_location))
7295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point host_mouse_location = root_mouse_location;
73123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  host_->ConvertPointToHost(&host_mouse_location);
7325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ui::MouseEvent event(ui::ET_MOUSE_MOVED,
7335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       host_mouse_location,
7345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       host_mouse_location,
7355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       ui::EF_IS_SYNTHESIZED,
7365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                       0);
7375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return OnEventFromSource(&event);
7385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
740a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
741a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                    ui::LocatedEvent* event) {
7425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int flags = event->flags();
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsNonClientLocation(target, event->location()))
7445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    flags |= ui::EF_IS_NON_CLIENT;
7455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  event->set_flags(flags);
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_ &&
7485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (event->IsMouseEvent() || event->IsScrollEvent()) &&
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
7505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
7515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      SetLastMouseLocation(window(), event->root_location());
7525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    synthesize_mouse_move_ = false;
7534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
756a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
757a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::MouseEvent* event) {
758f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::CursorClient* cursor_client = client::GetCursorClient(window());
759c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // We allow synthesized mouse exit events through even if mouse events are
760c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // disabled. This ensures that hover state, etc on controls like buttons is
761c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // cleared.
762eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (cursor_client &&
763eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !cursor_client->IsMouseEventsEnabled() &&
764c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (event->flags() & ui::EF_IS_SYNTHESIZED) &&
765c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      (event->type() != ui::ET_MOUSE_EXITED)) {
7665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    event->SetHandled();
7675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
769eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (move_hold_count_) {
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SetLastMouseLocation(window(), event->root_location());
7755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
7765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
7775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->SetHandled();
7785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
7795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
7805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // We may have a held event for a period between the time move_hold_count_
7815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // fell to 0 and the DispatchHeldEvents executes. Since we're going to
7825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // dispatch the new event directly below, we can reset the old one.
7835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
7885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_MIDDLE_MOUSE_BUTTON |
7895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_RIGHT_MOUSE_BUTTON;
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (event->type()) {
7912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ui::ET_MOUSE_EXITED:
7925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!target || target == window()) {
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)        }
7998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mouse_moved_handler_ = NULL;
8002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
8012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_MOVED:
80358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // Send an exit to the current |mouse_moved_handler_| and an enter to
80458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // |target|. Take care that both us and |target| aren't destroyed during
80558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // dispatch.
80658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (target != mouse_moved_handler_) {
80758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
8085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        WindowTracker live_window;
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Add(target);
810f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DispatchDetails details =
811f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
8125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed) {
8135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
81658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // If the |mouse_moved_handler_| changes out from under us, assume a
81758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // nested message loop ran and we don't need to do anything.
8185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (mouse_moved_handler_ != old_mouse_moved_handler) {
8195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
8225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (!live_window.Contains(target) || details.target_destroyed) {
8238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          mouse_moved_handler_ = NULL;
8245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
8275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Remove(target);
8285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        mouse_moved_handler_ = target;
8305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED);
8315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed || details.target_destroyed) {
8325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
83458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        }
83558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_PRESSED:
8382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't set the mouse pressed handler for non client mouse down events.
8392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // These are only sent by Windows and are not always followed with non
8402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // client mouse up events which causes subsequent mouse events to be
8412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // sent to the wrong target.
8422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mouse_pressed_handler_ = target;
844eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(
845eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          event->flags() & kMouseButtonFlagMask);
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_RELEASED:
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_pressed_handler_ = NULL;
849eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(event->flags() &
850eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          kMouseButtonFlagMask & ~event->changed_button_flags());
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
859a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
860a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::TouchEvent* event) {
8617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (event->type()) {
8627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_PRESSED:
8637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ |= (1 << event->touch_id());
8647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // Handle ET_TOUCH_CANCELLED only if it has a native event.
8687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_CANCELLED:
8697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!event->HasNativeEvent())
8707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        break;
8717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // fallthrough
8727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_RELEASED:
8737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
8747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            (1 << event->touch_id());
8757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case ui::ET_TOUCH_MOVED:
8795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (move_hold_count_ && !dispatching_held_event_) {
8805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
8815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        event->SetHandled();
8825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return;
8835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
8847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    default:
8871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      NOTREACHED();
8885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (dispatching_held_event_ || !held_move_event_ ||
8926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      !held_move_event_->IsTouchEvent()) {
8936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ui::TouchEvent orig_event(*event, target, window());
8946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
8956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // If the touch event is invalid in some way, the gesture recognizer will
8966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // reject it. In this case, stop the touch from reaching the next event
8976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // phase.
8986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (!ui::GestureRecognizer::Get()->ProcessTouchEventPreDispatch(orig_event,
8996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                                                    target)) {
9006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      event->SetHandled();
9016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
9026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
9036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
9045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
908