window_event_dispatcher.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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.
124effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  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)
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchMouseExitAtPoint(const gfx::Point& point) {
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE,
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       ui::EF_NONE);
156f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details =
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      DispatchMouseEnterOrExit(event, ui::ET_MOUSE_EXITED);
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (details.dispatcher_destroyed)
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event,
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                Window* window,
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                ui::EventResult result) {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  gestures.reset(ui::GestureRecognizer::Get()->
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ProcessTouchEventForGesture(*event, result, window));
168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details = ProcessGestures(gestures.get());
169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (details.dispatcher_destroyed)
170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::HoldPointerMoves() {
1747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    held_event_factory_.InvalidateWeakPtrs();
1767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ++move_hold_count_;
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           this);
1797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleasePointerMoves() {
1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  --move_hold_count_;
1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DCHECK_GE(move_hold_count_, 0);
1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!move_hold_count_ && held_move_event_) {
1857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // We don't want to call DispatchHeldEvents directly, because this might be
1867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // called from a deep stack while another event, in which case dispatching
1877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // another one may not be safe/expected.  Instead we post a task, that we
1887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // may cancel if HoldPointerMoves is called again before it executes.
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostNonNestableTask(
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        FROM_HERE, base::Bind(
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          held_event_factory_.GetWeakPtr()));
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point location = Env::GetInstance()->last_mouse_location();
199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::ScreenPositionClient* client =
200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      client::GetScreenPositionClient(window());
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (client)
202f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    client->ConvertPointFromScreen(window(), &location);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return location;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnHostLostMouseGrab() {
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  mouse_pressed_handler_ = NULL;
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  mouse_moved_handler_ = NULL;
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnCursorMovedToRootLocation(
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const gfx::Point& root_location) {
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetLastMouseLocation(window(), root_location);
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  synthesize_mouse_move_ = false;
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
217effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
218effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(window, WINDOW_DESTROYED);
219effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
220effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
22423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)Window* WindowEventDispatcher::window() {
22523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
22623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
22723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
22823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)const Window* WindowEventDispatcher::window() const {
22923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return host_->window();
23023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
23123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::LocatedEvent* event) {
23423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  event->UpdateForRootTransform(host_->GetInverseRootTransform());
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
237effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
238effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The mouse capture is intentionally ignored. Think that a mouse enters
239effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // to a window, the window sets the capture, the mouse exits the window,
240effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // and then it releases the capture. In that case OnMouseExited won't
241effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // be called. So it is natural not to emit OnMouseExited even though
242effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |window| is the capture window.
243effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
244effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->Contains(mouse_moved_handler_) &&
245effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      window->ContainsPointInRoot(last_mouse_location))
246effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DispatchMouseExitAtPoint(last_mouse_location);
247effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
248effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
250f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const ui::MouseEvent& event,
251f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::EventType type) {
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SetLastMouseLocation(window(), event.root_location());
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
25723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
25823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      !window()->Contains(mouse_moved_handler_))
259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return DispatchDetails();
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |event| may be an event in the process of being dispatched to a target (in
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // which case its locations will be in the event's target's coordinate
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // system), or a synthetic event created in root-window (in which case, the
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // event's target will be NULL, and the event will be in the root-window's
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // coordinate system.
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::Window* target = static_cast<Window*>(event.target());
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!target)
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    target = window();
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::MouseEvent translated_event(event,
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  target,
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  mouse_moved_handler_,
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  type,
273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                  event.flags() | ui::EF_IS_SYNTHESIZED);
274a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchEvent(mouse_moved_handler_, &translated_event);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
278f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    ui::GestureRecognizer::Gestures* gestures) {
279f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DispatchDetails details;
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!gestures || gestures->empty())
281f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return details;
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Window* target = GetGestureTarget(gestures->get().at(0));
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < gestures->size(); ++i) {
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::GestureEvent* event = gestures->get().at(i);
286f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    event->ConvertLocationToTarget(window(), target);
287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details = DispatchEvent(target, event);
288f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed || details.target_destroyed)
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
291f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return details;
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnWindowHidden(Window* invisible,
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                           WindowHiddenReason reason) {
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the window the mouse was pressed in becomes invisible, it should no
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // longer receive mouse events.
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_pressed_handler_))
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_pressed_handler_ = NULL;
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (invisible->Contains(mouse_moved_handler_))
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mouse_moved_handler_ = NULL;
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If events are being dispatched from a nested message-loop, and the target
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // of the outer loop is hidden or moved to another dispatcher during
305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // dispatching events in the inner loop, then reset the target for the outer
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // loop.
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (invisible->Contains(old_dispatch_target_))
308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    old_dispatch_target_ = NULL;
309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CleanupGestureState(invisible);
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Do not clear the capture, and the |event_dispatch_target_| if the
313effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // window is moving across hosts, because the target itself is actually still
314effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // visible and clearing them stops further event processing, which can cause
315effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // unexpected behaviors. See crbug.com/157583
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reason != WINDOW_MOVING) {
317effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // We don't ask |invisible| here, because invisible may have been removed
318effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // from the window hierarchy already by the time this function is called
319effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // (OnWindowDestroyed).
320effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    client::CaptureClient* capture_client =
321effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        client::GetCaptureClient(host_->window());
322effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* capture_window =
323effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        capture_client ? capture_client->GetCaptureWindow() : NULL;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (invisible->Contains(event_dispatch_target_))
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      event_dispatch_target_ = NULL;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If the ancestor of the capture window is hidden, release the capture.
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Note that this may delete the window so do not use capture_window
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // after this.
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (invisible->Contains(capture_window) && invisible != window())
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      capture_window->ReleaseCapture();
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
336a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::CleanupGestureState(Window* window) {
337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ui::GestureRecognizer::Get()->CancelActiveTouches(window);
3388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ui::GestureRecognizer::Get()->CleanupStateForConsumer(window);
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const Window::Windows& windows = window->children();
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (Window::Windows::const_iterator iter = windows.begin();
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      iter != windows.end();
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++iter) {
343f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    CleanupGestureState(*iter);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
347effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochWindow* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
348effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  Window* target = NULL;
349effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!event->IsEndingEvent()) {
350effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // The window that received the start event (e.g. scroll begin) needs to
351effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // receive the end event (e.g. scroll end).
352effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = client::GetCaptureWindow(window());
353effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
354effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!target) {
355effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    target = ConsumerToWindow(
356effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
357effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
358effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
359effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return target;
360effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
361effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
3628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, aura::client::CaptureDelegate implementation:
3648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
365a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::UpdateCapture(Window* old_capture,
366a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                          Window* new_capture) {
3678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // |mouse_moved_handler_| may have been set to a Window in a different root
3688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // (see below). Clear it here to ensure we don't end up referencing a stale
3698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Window.
370f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
3718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    mouse_moved_handler_ = NULL;
3728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
373f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (old_capture && old_capture->GetRootWindow() == window() &&
3748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      old_capture->delegate()) {
3758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Send a capture changed event with bogus location data.
3768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         gfx::Point(), 0, 0);
3788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
379a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DispatchDetails details = DispatchEvent(old_capture, &event);
380f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
381f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    old_capture->delegate()->OnCaptureLost();
3848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (new_capture) {
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make all subsequent mouse events go to the capture window. We shouldn't
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // need to send an event here as OnCaptureLost() should take care of that.
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
3908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      mouse_moved_handler_ = new_capture;
3918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
3928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Make sure mouse_moved_handler gets updated.
393f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DispatchDetails details = SynthesizeMouseMoveEvent();
394f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (details.dispatcher_destroyed)
395f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
3968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  mouse_pressed_handler_ = NULL;
3988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::OnOtherRootGotCapture() {
4011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_moved_handler_ = NULL;
4021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  mouse_pressed_handler_ = NULL;
4031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
4041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::SetNativeCapture() {
4068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->SetCapture();
4078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::ReleaseNativeCapture() {
4108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  host_->ReleaseCapture();
4118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventProcessor implementation:
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
416a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return window();
417a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
418a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PrepareEventForDispatch(ui::Event* event) {
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (dispatching_held_event_) {
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The held events are already in |window()|'s coordinate system. So it is
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // not necessary to apply the transform to convert from the host's
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // coordinate system to |window()|'s coordinate system.
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent() ||
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsScrollEvent() ||
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsTouchEvent() ||
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->IsGestureEvent()) {
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
434a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
4368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return event_dispatch_target_ == target;
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::Event* event) {
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* target_window = static_cast<Window*>(target);
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CHECK(window()->Contains(target_window));
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_) {
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool can_be_held = IsEventCandidateForHold(*event);
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!move_hold_count_ || !can_be_held) {
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (can_be_held)
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset();
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DispatchDetails details = DispatchHeldEvents();
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (details.dispatcher_destroyed || details.target_destroyed)
4545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return details;
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
4565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event->IsMouseEvent()) {
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
4605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsScrollEvent()) {
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchLocatedEvent(target_window,
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            static_cast<ui::ScrollEvent*>(event));
4635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (event->IsTouchEvent()) {
4645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
4655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = event_dispatch_target_;
467a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = static_cast<Window*>(target);
468a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return DispatchDetails();
469a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
470a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
471a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
472a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::EventTarget* target,
473a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const ui::Event& event) {
474a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DispatchDetails details;
475a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!target || target != event_dispatch_target_)
476a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    details.target_destroyed = true;
477a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  event_dispatch_target_ = old_dispatch_target_;
478a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  old_dispatch_target_ = NULL;
479a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef NDEBUG
480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (event.IsTouchEvent() && !details.target_destroyed) {
4845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Do not let 'held' touch events contribute to any gestures.
4855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!held_move_event_ || !held_move_event_->IsTouchEvent()) {
4865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event),
4875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                static_cast<Window*>(event.target()), window());
4885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // Get the list of GestureEvents from GestureRecognizer.
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
4905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      gestures.reset(ui::GestureRecognizer::Get()->
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ProcessTouchEventForGesture(orig_event, event.result(),
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      static_cast<Window*>(target)));
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return ProcessGestures(gestures.get());
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
497a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return details;
498a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
499a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
501a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, ui::GestureEventHelper implementation:
5028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool WindowEventDispatcher::CanDispatchToConsumer(
504a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::GestureConsumer* consumer) {
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Window* consumer_window = ConsumerToWindow(consumer);
506f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return (consumer_window && consumer_window->GetRootWindow() == window());
5078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchPostponedGestureEvent(
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::GestureEvent* event) {
5118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DispatchGestureEvent(event);
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details = OnEventFromSource(event);
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (details.dispatcher_destroyed)
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
521effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, WindowObserver implementation:
522effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
523effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroying(Window* window) {
524effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
525effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
526effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
527effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
528effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
529effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
530effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
531effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowDestroyed(Window* window) {
532effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // We observe all windows regardless of what root Window (if any) they're
533effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // attached to.
534effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Remove(window);
535effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
536effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
537effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
538effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!observer_manager_.IsObserving(attached))
539effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    observer_manager_.Add(attached);
540effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
541effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(attached))
542effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
543effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
544effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(attached);
545effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
546effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
547effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
548effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                           Window* new_root) {
549effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(detached))
550effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
551effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
552effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(client::GetCaptureWindow(window()) != window());
553effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
554effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(detached);
555effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(detached);
556effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
557effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
558effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
559effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
560effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
561effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
562effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
563effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                       bool visible) {
564effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
565effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
566effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
567effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DispatchMouseExitToHidingWindow(window);
568effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
569effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
570effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
571effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                      bool visible) {
572effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
573effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
574effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
575effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
576effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    PostSynthesizeMouseMove();
577effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
578effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Hiding the window releases capture which can implicitly destroy the window
579effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // so the window may no longer be valid after this call.
580effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!visible)
581effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    OnWindowHidden(window, WINDOW_HIDDEN);
582effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
583effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
584effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
585effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& old_bounds,
586effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  const gfx::Rect& new_bounds) {
587effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
588effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
589effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
590effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window == host_->window()) {
591effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
592effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                 "size", new_bounds.size().ToString());
593effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
594effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DispatchDetails details = DispatchHeldEvents();
595effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (details.dispatcher_destroyed)
596effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
597effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
598effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Constrain the mouse position within the new root Window size.
599effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Point point;
600effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (host_->QueryMouseLocation(&point)) {
601effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      SetLastMouseLocation(
602effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          host_->window(),
603effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          ui::ConvertPointToDIP(host_->window()->layer(), point));
604effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
605effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    synthesize_mouse_move_ = false;
606effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
607effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
608effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->IsVisible()) {
609effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
610effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
611effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &old_bounds_in_root);
612effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window::ConvertRectToTarget(window->parent(), host_->window(),
613effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                &new_bounds_in_root);
614effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
615effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (old_bounds_in_root.Contains(last_mouse_location) !=
616effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        new_bounds_in_root.Contains(last_mouse_location)) {
617effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      PostSynthesizeMouseMove();
618effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
619effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
620effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
621effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
622effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransforming(Window* window) {
623effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
624effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
625effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
626effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
627effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
628effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
629effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowTransformed(Window* window) {
630effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!host_->window()->Contains(window))
631effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
632effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
633effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SynthesizeMouseMoveAfterChangeToWindow(window);
634effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
635effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
636effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///////////////////////////////////////////////////////////////////////////////
637effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// WindowEventDispatcher, EnvObserver implementation:
638effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
639effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::OnWindowInitialized(Window* window) {
640effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  observer_manager_.Add(window);
641effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
642effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
643effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch////////////////////////////////////////////////////////////////////////////////
644a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WindowEventDispatcher, private:
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
646a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
6475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!held_repostable_event_ && !held_move_event_)
6485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return DispatchDetails();
6495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CHECK(!dispatching_held_event_);
6515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dispatching_held_event_ = true;
6525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails dispatch_details;
6545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_repostable_event_) {
6555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
6565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<ui::MouseEvent> mouse_event(
6575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
6585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(mouse_event.get());
659f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    } else {
6605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
6615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      NOTREACHED();
662f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
6635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (dispatch_details.dispatcher_destroyed)
6645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return dispatch_details;
665f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
6665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (held_move_event_) {
6685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // If a mouse move has been synthesized, the target location is suspect,
6695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // so drop the held mouse event.
6705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (held_move_event_->IsTouchEvent() ||
6715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
6725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      dispatch_details = OnEventFromSource(held_move_event_.get());
6735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
6745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!dispatch_details.dispatcher_destroyed)
6755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
6765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
6775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatch_details.dispatcher_destroyed)
6795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    dispatching_held_event_ = false;
6805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return dispatch_details;
681f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
682f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
683effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::PostSynthesizeMouseMove() {
6845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (synthesize_mouse_move_)
6855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
6865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = true;
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::MessageLoop::current()->PostNonNestableTask(
6885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      FROM_HERE,
689a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::Bind(base::IgnoreResult(
690a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          &WindowEventDispatcher::SynthesizeMouseMoveEvent),
691a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          held_event_factory_.GetWeakPtr()));
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
694effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
695effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    Window* window) {
696effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (window->IsVisible() &&
697effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
698effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    PostSynthesizeMouseMove();
699effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
700effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
701effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
702a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
7035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DispatchDetails details;
7045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!synthesize_mouse_move_)
7055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synthesize_mouse_move_ = false;
7075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
7085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!window()->bounds().Contains(root_mouse_location))
7095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return details;
7105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::Point host_mouse_location = root_mouse_location;
71123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  host_->ConvertPointToHost(&host_mouse_location);
7125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ui::MouseEvent event(ui::ET_MOUSE_MOVED,
7135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       host_mouse_location,
7145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       host_mouse_location,
7155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       ui::EF_IS_SYNTHESIZED,
7165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       0);
7175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return OnEventFromSource(&event);
7185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
7195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
720a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
721a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                    ui::LocatedEvent* event) {
7225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int flags = event->flags();
7235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsNonClientLocation(target, event->location()))
7245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    flags |= ui::EF_IS_NON_CLIENT;
7255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  event->set_flags(flags);
7265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dispatching_held_event_ &&
7285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (event->IsMouseEvent() || event->IsScrollEvent()) &&
7295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
7305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
7315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      SetLastMouseLocation(window(), event->root_location());
7325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    synthesize_mouse_move_ = false;
7334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
736a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
737a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::MouseEvent* event) {
738f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  client::CursorClient* cursor_client = client::GetCursorClient(window());
739eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (cursor_client &&
740eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      !cursor_client->IsMouseEventsEnabled() &&
7415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (event->flags() & ui::EF_IS_SYNTHESIZED)) {
7425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    event->SetHandled();
7435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
7445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
745eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
7475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (move_hold_count_) {
7485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
7495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
7505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SetLastMouseLocation(window(), event->root_location());
7515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
7525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
7535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      event->SetHandled();
7545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
7555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
7565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // We may have a held event for a period between the time move_hold_count_
7575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // fell to 0 and the DispatchHeldEvents executes. Since we're going to
7585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // dispatch the new event directly below, we can reset the old one.
7595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      held_move_event_.reset();
7605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
7615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
7645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_MIDDLE_MOUSE_BUTTON |
7655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                   ui::EF_RIGHT_MOUSE_BUTTON;
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (event->type()) {
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case ui::ET_MOUSE_EXITED:
7685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (!target || target == window()) {
769f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DispatchDetails details =
770f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
7715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed) {
7725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
7735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
7745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
7758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        mouse_moved_handler_ = NULL;
7762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
7772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_MOVED:
77958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // Send an exit to the current |mouse_moved_handler_| and an enter to
78058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // |target|. Take care that both us and |target| aren't destroyed during
78158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      // dispatch.
78258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (target != mouse_moved_handler_) {
78358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
7845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        WindowTracker live_window;
7855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Add(target);
786f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        DispatchDetails details =
787f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
7885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed) {
7895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
7915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
79258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // If the |mouse_moved_handler_| changes out from under us, assume a
79358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // nested message loop ran and we don't need to do anything.
7945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (mouse_moved_handler_ != old_mouse_moved_handler) {
7955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
7965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
7975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
7985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (!live_window.Contains(target) || details.target_destroyed) {
7998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          mouse_moved_handler_ = NULL;
8005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
8025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
8035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        live_window.Remove(target);
8045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        mouse_moved_handler_ = target;
8065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED);
8075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (details.dispatcher_destroyed || details.target_destroyed) {
8085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          event->SetHandled();
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          return;
81058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        }
81158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_PRESSED:
8142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Don't set the mouse pressed handler for non client mouse down events.
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // These are only sent by Windows and are not always followed with non
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // client mouse up events which causes subsequent mouse events to be
8172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // sent to the wrong target.
8182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mouse_pressed_handler_ = target;
820eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(
821eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          event->flags() & kMouseButtonFlagMask);
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case ui::ET_MOUSE_RELEASED:
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_pressed_handler_ = NULL;
825eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Env::GetInstance()->set_mouse_button_flags(event->flags() &
826eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          kMouseButtonFlagMask & ~event->changed_button_flags());
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
836a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                                  ui::TouchEvent* event) {
8377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  switch (event->type()) {
8387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_PRESSED:
8397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ |= (1 << event->touch_id());
8407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // Handle ET_TOUCH_CANCELLED only if it has a native event.
8447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_CANCELLED:
8457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!event->HasNativeEvent())
8467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        break;
8477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // fallthrough
8487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    case ui::ET_TOUCH_RELEASED:
8497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
8507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            (1 << event->touch_id());
8517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
8527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case ui::ET_TOUCH_MOVED:
8555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (move_hold_count_ && !dispatching_held_event_) {
8565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
8575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        event->SetHandled();
8585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return;
8595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
8607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      break;
8617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
8625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    default:
8631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      NOTREACHED();
8645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PreDispatchLocatedEvent(target, event);
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
870