Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / aura / window.cc
index 4645659..992642e 100644 (file)
 #include "ui/aura/client/window_stacking_client.h"
 #include "ui/aura/env.h"
 #include "ui/aura/layout_manager.h"
-#include "ui/aura/root_window.h"
 #include "ui/aura/window_delegate.h"
+#include "ui/aura/window_event_dispatcher.h"
 #include "ui/aura/window_observer.h"
 #include "ui/aura/window_tracker.h"
 #include "ui/aura/window_tree_host.h"
 #include "ui/compositor/compositor.h"
 #include "ui/compositor/layer.h"
 #include "ui/events/event_target_iterator.h"
-#include "ui/gfx/animation/multi_animation.h"
 #include "ui/gfx/canvas.h"
 #include "ui/gfx/path.h"
 #include "ui/gfx/scoped_canvas.h"
@@ -162,7 +161,7 @@ class ScopedCursorHider {
   explicit ScopedCursorHider(Window* window)
       : window_(window),
         hid_cursor_(false) {
-    if (!window_->HasDispatcher())
+    if (!window_->IsRootWindow())
       return;
     const bool cursor_is_in_bounds = window_->GetBoundsInScreen().Contains(
         Env::GetInstance()->last_mouse_location());
@@ -174,7 +173,7 @@ class ScopedCursorHider {
     }
   }
   ~ScopedCursorHider() {
-    if (!window_->HasDispatcher())
+    if (!window_->IsRootWindow())
       return;
 
     // Update the device scale factor of the cursor client only when the last
@@ -199,7 +198,7 @@ class ScopedCursorHider {
 };
 
 Window::Window(WindowDelegate* delegate)
-    : dispatcher_(NULL),
+    : host_(NULL),
       type_(ui::wm::WINDOW_TYPE_UNKNOWN),
       owned_by_parent_(true),
       delegate_(delegate),
@@ -217,19 +216,35 @@ Window::Window(WindowDelegate* delegate)
 }
 
 Window::~Window() {
-  // |layer_| can be NULL during tests, or if this Window is layerless.
-  if (layer_)
-    layer_->SuppressPaint();
+  // |layer()| can be NULL during tests, or if this Window is layerless.
+  if (layer()) {
+    if (layer()->owner() == this)
+      layer()->CompleteAllAnimations();
+    layer()->SuppressPaint();
+  }
 
   // Let the delegate know we're in the processing of destroying.
   if (delegate_)
-    delegate_->OnWindowDestroying();
+    delegate_->OnWindowDestroying(this);
   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowDestroying(this));
 
-  // Let the root know so that it can remove any references to us.
-  WindowEventDispatcher* dispatcher = GetDispatcher();
-  if (dispatcher)
-    dispatcher->OnWindowDestroying(this);
+  // TODO(beng): See comment in window_event_dispatcher.h. This shouldn't be
+  //             necessary but unfortunately is right now due to ordering
+  //             peculiarities. WED must be notified _after_ other observers
+  //             are notified of pending teardown but before the hierarchy
+  //             is actually torn down.
+  WindowTreeHost* host = GetHost();
+  if (host)
+    host->dispatcher()->OnPostNotifiedWindowDestroying(this);
+
+  // The window should have already had its state cleaned up in
+  // WindowEventDispatcher::OnWindowHidden(), but there have been some crashes
+  // involving windows being destroyed without being hidden first. See
+  // crbug.com/342040. This should help us debug the issue. TODO(tdresser):
+  // remove this once we determine why we have windows that are destroyed
+  // without being hidden.
+  bool window_incorrectly_cleaned_up = CleanupGestureState();
+  CHECK(!window_incorrectly_cleaned_up);
 
   // Then destroy the children.
   RemoveOrDestroyChildren();
@@ -240,10 +255,10 @@ Window::~Window() {
     parent_->RemoveChild(this);
 
   if (delegate_)
-    delegate_->OnWindowDestroyed();
+    delegate_->OnWindowDestroyed(this);
   ObserverListBase<WindowObserver>::Iterator iter(observers_);
-  WindowObserver* observer;
-  while ((observer = iter.GetNext())) {
+  for (WindowObserver* observer = iter.GetNext(); observer;
+       observer = iter.GetNext()) {
     RemoveObserver(observer);
     observer->OnWindowDestroyed(this);
   }
@@ -260,81 +275,52 @@ Window::~Window() {
   // If we have layer it will either be destroyed by |layer_owner_|'s dtor, or
   // by whoever acquired it. We don't have a layer if Init() wasn't invoked or
   // we are layerless.
-  if (layer_) {
-    layer_->set_delegate(NULL);
-    layer_ = NULL;
-  }
+  if (layer())
+    layer()->set_delegate(NULL);
+  DestroyLayer();
 }
 
 void Window::Init(WindowLayerType window_layer_type) {
   if (window_layer_type != WINDOW_LAYER_NONE) {
-    layer_ = new ui::Layer(WindowLayerTypeToUILayerType(window_layer_type));
-    layer_owner_.reset(layer_);
-    layer_->SetVisible(false);
-    layer_->set_delegate(this);
-    UpdateLayerName(name_);
-    layer_->SetFillsBoundsOpaquely(!transparent_);
+    SetLayer(new ui::Layer(WindowLayerTypeToUILayerType(window_layer_type)));
+    layer()->SetVisible(false);
+    layer()->set_delegate(this);
+    UpdateLayerName();
+    layer()->SetFillsBoundsOpaquely(!transparent_);
   }
 
   Env::GetInstance()->NotifyWindowInitialized(this);
 }
 
-ui::Layer* Window::RecreateLayer() {
-  // Disconnect the old layer, but don't delete it.
-  ui::Layer* old_layer = AcquireLayer();
-  if (!old_layer)
-    return NULL;
-
-  bounds_.SetRect(0, 0, 0, 0);
-
-  old_layer->set_delegate(NULL);
-
-  layer_ = new ui::Layer(old_layer->type());
-  layer_owner_.reset(layer_);
-  layer_->SetVisible(old_layer->visible());
-  layer_->set_scale_content(old_layer->scale_content());
-  layer_->set_delegate(this);
-  layer_->SetMasksToBounds(old_layer->GetMasksToBounds());
-
-  if (delegate_)
-    delegate_->DidRecreateLayer(old_layer, layer_);
-
-  UpdateLayerName(name_);
-  layer_->SetFillsBoundsOpaquely(!transparent_);
-  // Install new layer as a sibling of the old layer, stacked below it.
-  if (old_layer->parent()) {
-    old_layer->parent()->Add(layer_);
-    old_layer->parent()->StackBelow(layer_, old_layer);
-  }
-  // Migrate all the child layers over to the new layer. Copy the list because
-  // the items are removed during iteration.
-  std::vector<ui::Layer*> children_copy = old_layer->children();
-  for (std::vector<ui::Layer*>::const_iterator it = children_copy.begin();
-       it != children_copy.end();
-       ++it) {
-    ui::Layer* child = *it;
-    layer_->Add(child);
-  }
-  return old_layer;
-}
-
 void Window::SetType(ui::wm::WindowType type) {
   // Cannot change type after the window is initialized.
-  DCHECK(!layer_);
+  DCHECK(!layer());
   type_ = type;
 }
 
 void Window::SetName(const std::string& name) {
   name_ = name;
 
-  if (layer_)
-    UpdateLayerName(name_);
+  if (layer())
+    UpdateLayerName();
+}
+
+void Window::SetTitle(const base::string16& title) {
+  title_ = title;
+  FOR_EACH_OBSERVER(WindowObserver,
+                    observers_,
+                    OnWindowTitleChanged(this));
 }
 
 void Window::SetTransparent(bool transparent) {
   transparent_ = transparent;
-  if (layer_)
-    layer_->SetFillsBoundsOpaquely(!transparent_);
+  if (layer())
+    layer()->SetFillsBoundsOpaquely(!transparent_);
+}
+
+void Window::SetFillsBoundsCompletely(bool fills_bounds) {
+  if (layer())
+    layer()->SetFillsBoundsCompletely(fills_bounds);
 }
 
 Window* Window::GetRootWindow() {
@@ -343,26 +329,33 @@ Window* Window::GetRootWindow() {
 }
 
 const Window* Window::GetRootWindow() const {
-  return dispatcher_ ? this : parent_ ? parent_->GetRootWindow() : NULL;
+  return IsRootWindow() ? this : parent_ ? parent_->GetRootWindow() : NULL;
 }
 
-WindowEventDispatcher* Window::GetDispatcher() {
-  return const_cast<WindowEventDispatcher*>(const_cast<const Window*>(this)->
-      GetDispatcher());
+WindowTreeHost* Window::GetHost() {
+  return const_cast<WindowTreeHost*>(const_cast<const Window*>(this)->
+      GetHost());
 }
 
-const WindowEventDispatcher* Window::GetDispatcher() const {
+const WindowTreeHost* Window::GetHost() const {
   const Window* root_window = GetRootWindow();
-  return root_window ? root_window->dispatcher_ : NULL;
+  return root_window ? root_window->host_ : NULL;
 }
 
 void Window::Show() {
+  if (layer()) {
+    DCHECK_EQ(visible_, layer()->GetTargetVisibility());
+    // It is not allowed that a window is visible but the layers alpha is fully
+    // transparent since the window would still be considered to be active but
+    // could not be seen.
+    DCHECK(!(visible_ && layer()->GetTargetOpacity() == 0.0f));
+  }
   SetVisible(true);
 }
 
 void Window::Hide() {
+  // RootWindow::OnVisibilityChanged will call ReleaseCapture.
   SetVisible(false);
-  ReleaseCapture();
 }
 
 bool Window::IsVisible() const {
@@ -373,8 +366,8 @@ bool Window::IsVisible() const {
   for (const Window* window = this; window; window = window->parent()) {
     if (!window->visible_)
       return false;
-    if (window->layer_)
-      return window->layer_->IsDrawn();
+    if (window->layer())
+      return window->layer()->IsDrawn();
   }
   return false;
 }
@@ -406,17 +399,16 @@ gfx::Rect Window::GetBoundsInScreen() const {
 }
 
 void Window::SetTransform(const gfx::Transform& transform) {
-  if (!layer_) {
+  if (!layer()) {
     // Transforms aren't supported on layerless windows.
     NOTREACHED();
     return;
   }
-  WindowEventDispatcher* dispatcher = GetDispatcher();
-  bool contained_mouse = IsVisible() && dispatcher &&
-      ContainsPointInRoot(dispatcher->GetLastMouseLocationInRoot());
-  layer_->SetTransform(transform);
-  if (dispatcher)
-    dispatcher->OnWindowTransformed(this, contained_mouse);
+  FOR_EACH_OBSERVER(WindowObserver, observers_,
+                    OnWindowTransforming(this));
+  layer()->SetTransform(transform);
+  FOR_EACH_OBSERVER(WindowObserver, observers_,
+                    OnWindowTransformed(this));
 }
 
 void Window::SetLayoutManager(LayoutManager* layout_manager) {
@@ -433,11 +425,27 @@ void Window::SetLayoutManager(LayoutManager* layout_manager) {
     layout_manager_->OnWindowAddedToLayout(*it);
 }
 
+scoped_ptr<ui::EventTargeter>
+Window::SetEventTargeter(scoped_ptr<ui::EventTargeter> targeter) {
+  scoped_ptr<ui::EventTargeter> old_targeter = targeter_.Pass();
+  targeter_ = targeter.Pass();
+  return old_targeter.Pass();
+}
+
 void Window::SetBounds(const gfx::Rect& new_bounds) {
   if (parent_ && parent_->layout_manager())
     parent_->layout_manager()->SetChildBounds(this, new_bounds);
-  else
-    SetBoundsInternal(new_bounds);
+  else {
+    // Ensure we don't go smaller than our minimum bounds.
+    gfx::Rect final_bounds(new_bounds);
+    if (delegate_) {
+      const gfx::Size& min_size = delegate_->GetMinimumSize();
+      final_bounds.set_width(std::max(min_size.width(), final_bounds.width()));
+      final_bounds.set_height(std::max(min_size.height(),
+                                       final_bounds.height()));
+    }
+    SetBoundsInternal(final_bounds);
+  }
 }
 
 void Window::SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen,
@@ -454,11 +462,11 @@ void Window::SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen,
 }
 
 gfx::Rect Window::GetTargetBounds() const {
-  if (!layer_)
+  if (!layer())
     return bounds();
 
-  if (!parent_ || parent_->layer_)
-    return layer_->GetTargetBounds();
+  if (!parent_ || parent_->layer())
+    return layer()->GetTargetBounds();
 
   // We have a layer but our parent (who is valid) doesn't. This means the
   // coordinates of the layer are relative to the first ancestor with a layer;
@@ -467,15 +475,15 @@ gfx::Rect Window::GetTargetBounds() const {
   const aura::Window* ancestor_with_layer =
       parent_->GetAncestorWithLayer(&offset);
   if (!ancestor_with_layer)
-    return layer_->GetTargetBounds();
+    return layer()->GetTargetBounds();
 
-  gfx::Rect layer_target_bounds = layer_->GetTargetBounds();
+  gfx::Rect layer_target_bounds = layer()->GetTargetBounds();
   layer_target_bounds -= offset;
   return layer_target_bounds;
 }
 
 void Window::SchedulePaintInRect(const gfx::Rect& rect) {
-  if (!layer_ && parent_) {
+  if (!layer() && parent_) {
     // Notification of paint scheduled happens for the window with a layer.
     gfx::Rect parent_rect(bounds().size());
     parent_rect.Intersect(rect);
@@ -483,7 +491,7 @@ void Window::SchedulePaintInRect(const gfx::Rect& rect) {
       parent_rect.Offset(bounds().origin().OffsetFromOrigin());
       parent_->SchedulePaintInRect(parent_rect);
     }
-  } else if (layer_ && layer_->SchedulePaint(rect)) {
+  } else if (layer() && layer()->SchedulePaint(rect)) {
     FOR_EACH_OBSERVER(
         WindowObserver, observers_, OnWindowPaintScheduled(this, rect));
   }
@@ -526,13 +534,14 @@ void Window::AddChild(Window* child) {
 
   gfx::Vector2d offset;
   aura::Window* ancestor_with_layer = GetAncestorWithLayer(&offset);
+
+  child->parent_ = this;
+
   if (ancestor_with_layer) {
     offset += child->bounds().OffsetFromOrigin();
     child->ReparentLayers(ancestor_with_layer->layer(), offset);
   }
 
-  child->parent_ = this;
-
   children_.push_back(child);
   if (layout_manager_)
     layout_manager_->OnWindowAddedToLayout(child);
@@ -541,7 +550,7 @@ void Window::AddChild(Window* child) {
 
   Window* root_window = GetRootWindow();
   if (root_window && old_root != root_window) {
-    root_window->GetDispatcher()->OnWindowAddedToRootWindow(child);
+    root_window->GetHost()->dispatcher()->OnWindowAddedToRootWindow(child);
     child->NotifyAddedToRootWindow();
   }
 
@@ -596,53 +605,61 @@ void Window::ConvertPointToTarget(const Window* source,
   if (source->GetRootWindow() != target->GetRootWindow()) {
     client::ScreenPositionClient* source_client =
         client::GetScreenPositionClient(source->GetRootWindow());
-    source_client->ConvertPointToScreen(source, point);
+    // |source_client| can be NULL in tests.
+    if (source_client)
+      source_client->ConvertPointToScreen(source, point);
 
     client::ScreenPositionClient* target_client =
         client::GetScreenPositionClient(target->GetRootWindow());
-    target_client->ConvertPointFromScreen(target, point);
-  } else if ((source != target) && (!source->layer_ || !target->layer_)) {
-    if (!source->layer_) {
+    // |target_client| can be NULL in tests.
+    if (target_client)
+      target_client->ConvertPointFromScreen(target, point);
+  } else if ((source != target) && (!source->layer() || !target->layer())) {
+    if (!source->layer()) {
       gfx::Vector2d offset_to_layer;
       source = source->GetAncestorWithLayer(&offset_to_layer);
       *point += offset_to_layer;
     }
-    if (!target->layer_) {
+    if (!target->layer()) {
       gfx::Vector2d offset_to_layer;
       target = target->GetAncestorWithLayer(&offset_to_layer);
       *point -= offset_to_layer;
     }
-    ui::Layer::ConvertPointToLayer(source->layer_, target->layer_, point);
+    ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
   } else {
-    ui::Layer::ConvertPointToLayer(source->layer_, target->layer_, point);
+    ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
   }
 }
 
+// static
+void Window::ConvertRectToTarget(const Window* source,
+                                 const Window* target,
+                                 gfx::Rect* rect) {
+  DCHECK(rect);
+  gfx::Point origin = rect->origin();
+  ConvertPointToTarget(source, target, &origin);
+  rect->set_origin(origin);
+}
+
 void Window::MoveCursorTo(const gfx::Point& point_in_window) {
   Window* root_window = GetRootWindow();
   DCHECK(root_window);
   gfx::Point point_in_root(point_in_window);
   ConvertPointToTarget(this, root_window, &point_in_root);
-  root_window->GetDispatcher()->MoveCursorTo(point_in_root);
+  root_window->GetHost()->MoveCursorTo(point_in_root);
 }
 
 gfx::NativeCursor Window::GetCursor(const gfx::Point& point) const {
   return delegate_ ? delegate_->GetCursor(point) : gfx::kNullCursor;
 }
 
-void Window::SetEventFilter(ui::EventHandler* event_filter) {
-  if (event_filter_)
-    RemovePreTargetHandler(event_filter_.get());
-  event_filter_.reset(event_filter);
-  if (event_filter)
-    AddPreTargetHandler(event_filter);
-}
-
 void Window::AddObserver(WindowObserver* observer) {
+  observer->OnObservingWindow(this);
   observers_.AddObserver(observer);
 }
 
 void Window::RemoveObserver(WindowObserver* observer) {
+  observer->OnUnobservingWindow(this);
   observers_.RemoveObserver(observer);
 }
 
@@ -663,28 +680,6 @@ bool Window::ContainsPoint(const gfx::Point& local_point) const {
   return gfx::Rect(bounds().size()).Contains(local_point);
 }
 
-bool Window::HitTest(const gfx::Point& local_point) {
-  // Expand my bounds for hit testing (override is usually zero but it's
-  // probably cheaper to do the math every time than to branch).
-  gfx::Rect local_bounds(gfx::Point(), bounds().size());
-  local_bounds.Inset(aura::Env::GetInstance()->is_touch_down() ?
-      hit_test_bounds_override_outer_touch_ :
-      hit_test_bounds_override_outer_mouse_);
-
-  if (!delegate_ || !delegate_->HasHitTestMask())
-    return local_bounds.Contains(local_point);
-
-  gfx::Path mask;
-  delegate_->GetHitTestMask(&mask);
-
-  SkRegion clip_region;
-  clip_region.setRect(local_bounds.x(), local_bounds.y(),
-                      local_bounds.width(), local_bounds.height());
-  SkRegion mask_region;
-  return mask_region.setPath(mask, clip_region) &&
-      mask_region.contains(local_point.x(), local_point.y());
-}
-
 Window* Window::GetEventHandlerForPoint(const gfx::Point& local_point) {
   return GetWindowForPoint(local_point, true, true);
 }
@@ -720,7 +715,7 @@ bool Window::HasFocus() const {
 }
 
 bool Window::CanFocus() const {
-  if (dispatcher_)
+  if (IsRootWindow())
     return IsVisible();
 
   // NOTE: as part of focusing the window the ActivationClient may make the
@@ -739,7 +734,7 @@ bool Window::CanFocus() const {
 }
 
 bool Window::CanReceiveEvents() const {
-  if (dispatcher_)
+  if (IsRootWindow())
     return IsVisible();
 
   // The client may forbid certain windows from receiving events at a given
@@ -758,6 +753,9 @@ void Window::SetCapture() {
   Window* root_window = GetRootWindow();
   if (!root_window)
     return;
+  client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
+  if (!capture_client)
+    return;
   client::GetCaptureClient(root_window)->SetCapture(this);
 }
 
@@ -765,6 +763,9 @@ void Window::ReleaseCapture() {
   Window* root_window = GetRootWindow();
   if (!root_window)
     return;
+  client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
+  if (!capture_client)
+    return;
   client::GetCaptureClient(root_window)->ReleaseCapture(this);
 }
 
@@ -777,8 +778,8 @@ bool Window::HasCapture() {
 }
 
 void Window::SuppressPaint() {
-  if (layer_)
-    layer_->SuppressPaint();
+  if (layer())
+    layer()->SuppressPaint();
 }
 
 // {Set,Get,Clear}Property are implemented in window_property.h.
@@ -794,8 +795,6 @@ void* Window::GetNativeWindowProperty(const char* key) const {
 
 void Window::OnDeviceScaleFactorChanged(float device_scale_factor) {
   ScopedCursorHider hider(this);
-  if (dispatcher_)
-    dispatcher_->host()->OnDeviceScaleFactorChanged(device_scale_factor);
   if (delegate_)
     delegate_->OnDeviceScaleFactorChanged(device_scale_factor);
 }
@@ -807,10 +806,10 @@ std::string Window::GetDebugInfo() const {
       name().empty() ? "Unknown" : name().c_str(), id(),
       bounds().x(), bounds().y(), bounds().width(), bounds().height(),
       visible_ ? "WindowVisible" : "WindowHidden",
-      layer_ ?
-          (layer_->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
+      layer() ?
+          (layer()->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
           "NoLayer",
-      layer_ ? layer_->opacity() : 1.0f);
+      layer() ? layer()->opacity() : 1.0f);
 }
 
 void Window::PrintWindowHierarchy(int depth) const {
@@ -872,63 +871,65 @@ int64 Window::GetPropertyInternal(const void* key,
   return iter->second.value;
 }
 
-void Window::SetBoundsInternal(const gfx::Rect& new_bounds) {
-  gfx::Rect actual_new_bounds(new_bounds);
+bool Window::HitTest(const gfx::Point& local_point) {
+  gfx::Rect local_bounds(bounds().size());
+  if (!delegate_ || !delegate_->HasHitTestMask())
+    return local_bounds.Contains(local_point);
 
-  // Ensure we don't go smaller than our minimum bounds.
-  if (delegate_) {
-    const gfx::Size& min_size = delegate_->GetMinimumSize();
-    actual_new_bounds.set_width(
-        std::max(min_size.width(), actual_new_bounds.width()));
-    actual_new_bounds.set_height(
-        std::max(min_size.height(), actual_new_bounds.height()));
-  }
+  gfx::Path mask;
+  delegate_->GetHitTestMask(&mask);
+
+  SkRegion clip_region;
+  clip_region.setRect(local_bounds.x(), local_bounds.y(),
+                      local_bounds.width(), local_bounds.height());
+  SkRegion mask_region;
+  return mask_region.setPath(mask, clip_region) &&
+      mask_region.contains(local_point.x(), local_point.y());
+}
 
+void Window::SetBoundsInternal(const gfx::Rect& new_bounds) {
+  gfx::Rect actual_new_bounds(new_bounds);
   gfx::Rect old_bounds = GetTargetBounds();
 
   // Always need to set the layer's bounds -- even if it is to the same thing.
   // This may cause important side effects such as stopping animation.
-  if (!layer_) {
+  if (!layer()) {
     const gfx::Vector2d origin_delta = new_bounds.OffsetFromOrigin() -
         bounds_.OffsetFromOrigin();
     bounds_ = new_bounds;
     OffsetLayerBounds(origin_delta);
   } else {
-    if (parent_ && !parent_->layer_) {
+    if (parent_ && !parent_->layer()) {
       gfx::Vector2d offset;
       const aura::Window* ancestor_with_layer =
           parent_->GetAncestorWithLayer(&offset);
       if (ancestor_with_layer)
         actual_new_bounds.Offset(offset);
     }
-    layer_->SetBounds(actual_new_bounds);
+    layer()->SetBounds(actual_new_bounds);
   }
 
   // If we are currently not the layer's delegate, we will not get bounds
   // changed notification from the layer (this typically happens after animating
   // hidden). We must notify ourselves.
-  if (!layer_ || layer_->delegate() != this)
-    OnWindowBoundsChanged(old_bounds, ContainsMouse());
+  if (!layer() || layer()->delegate() != this)
+    OnWindowBoundsChanged(old_bounds);
 }
 
 void Window::SetVisible(bool visible) {
-  if ((layer_ && visible == layer_->GetTargetVisibility()) ||
-      (!layer_ && visible == visible_))
+  if ((layer() && visible == layer()->GetTargetVisibility()) ||
+      (!layer() && visible == visible_))
     return;  // No change.
 
   FOR_EACH_OBSERVER(WindowObserver, observers_,
                     OnWindowVisibilityChanging(this, visible));
 
-  WindowEventDispatcher* dispatcher = GetDispatcher();
-  if (dispatcher)
-    dispatcher->DispatchMouseExitToHidingWindow(this);
-
   client::VisibilityClient* visibility_client =
       client::GetVisibilityClient(this);
   if (visibility_client)
     visibility_client->UpdateLayerVisibility(this, visible);
-  else if (layer_)
-    layer_->SetVisible(visible);
+  else if (layer())
+    layer()->SetVisible(visible);
   visible_ = visible;
   SchedulePaint();
   if (parent_ && parent_->layout_manager_)
@@ -938,9 +939,6 @@ void Window::SetVisible(bool visible) {
     delegate_->OnWindowTargetVisibilityChanged(visible);
 
   NotifyWindowVisibilityChanged(this, visible);
-
-  if (dispatcher)
-    dispatcher->OnWindowVisibilityChanged(this, visible);
 }
 
 void Window::SchedulePaint() {
@@ -956,9 +954,10 @@ void Window::Paint(gfx::Canvas* canvas) {
 void Window::PaintLayerlessChildren(gfx::Canvas* canvas) {
   for (size_t i = 0, count = children_.size(); i < count; ++i) {
     Window* child = children_[i];
-    if (!child->layer_ && child->visible_) {
+    if (!child->layer() && child->visible_) {
       gfx::ScopedCanvas scoped_canvas(canvas);
-      if (canvas->ClipRect(child->bounds())) {
+      canvas->ClipRect(child->bounds());
+      if (!canvas->IsClipEmpty()) {
         canvas->Translate(child->bounds().OffsetFromOrigin());
         child->Paint(canvas);
       }
@@ -1027,15 +1026,12 @@ void Window::RemoveChildImpl(Window* child, Window* new_parent) {
   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWillRemoveWindow(child));
   Window* root_window = child->GetRootWindow();
   Window* new_root_window = new_parent ? new_parent->GetRootWindow() : NULL;
-  if (root_window && root_window != new_root_window) {
-    root_window->GetDispatcher()->OnWindowRemovedFromRootWindow(
-        child, new_root_window);
-    child->NotifyRemovingFromRootWindow();
-  }
+  if (root_window && root_window != new_root_window)
+    child->NotifyRemovingFromRootWindow(new_root_window);
 
   gfx::Vector2d offset;
   GetAncestorWithLayer(&offset);
-  child->UnparentLayers(!layer_, offset);
+  child->UnparentLayers(!layer(), offset);
   child->parent_ = NULL;
   Windows::iterator i = std::find(children_.begin(), children_.end(), child);
   DCHECK(i != children_.end());
@@ -1047,7 +1043,7 @@ void Window::RemoveChildImpl(Window* child, Window* new_parent) {
 
 void Window::UnparentLayers(bool has_layerless_ancestor,
                             const gfx::Vector2d& offset) {
-  if (!layer_) {
+  if (!layer()) {
     const gfx::Vector2d new_offset = offset + bounds().OffsetFromOrigin();
     for (size_t i = 0; i < children_.size(); ++i) {
       children_[i]->UnparentLayers(true, new_offset);
@@ -1056,14 +1052,14 @@ void Window::UnparentLayers(bool has_layerless_ancestor,
     // Only remove the layer if we still own it.  Someone else may have acquired
     // ownership of it via AcquireLayer() and may expect the hierarchy to go
     // unchanged as the Window is destroyed.
-    if (layer_owner_) {
-      if (layer_->parent())
-        layer_->parent()->Remove(layer_);
+    if (OwnsLayer()) {
+      if (layer()->parent())
+        layer()->parent()->Remove(layer());
       if (has_layerless_ancestor) {
         const gfx::Rect real_bounds(bounds_);
-        gfx::Rect layer_bounds(layer_->bounds());
+        gfx::Rect layer_bounds(layer()->bounds());
         layer_bounds.Offset(-offset);
-        layer_->SetBounds(layer_bounds);
+        layer()->SetBounds(layer_bounds);
         bounds_ = real_bounds;
       }
     }
@@ -1072,7 +1068,7 @@ void Window::UnparentLayers(bool has_layerless_ancestor,
 
 void Window::ReparentLayers(ui::Layer* parent_layer,
                             const gfx::Vector2d& offset) {
-  if (!layer_) {
+  if (!layer()) {
     for (size_t i = 0; i < children_.size(); ++i) {
       children_[i]->ReparentLayers(
           parent_layer,
@@ -1080,22 +1076,22 @@ void Window::ReparentLayers(ui::Layer* parent_layer,
     }
   } else {
     const gfx::Rect real_bounds(bounds());
-    parent_layer->Add(layer_);
-    gfx::Rect layer_bounds(layer_->bounds().size());
+    parent_layer->Add(layer());
+    gfx::Rect layer_bounds(layer()->bounds().size());
     layer_bounds += offset;
-    layer_->SetBounds(layer_bounds);
+    layer()->SetBounds(layer_bounds);
     bounds_ = real_bounds;
   }
 }
 
 void Window::OffsetLayerBounds(const gfx::Vector2d& offset) {
-  if (!layer_) {
+  if (!layer()) {
     for (size_t i = 0; i < children_.size(); ++i)
       children_[i]->OffsetLayerBounds(offset);
   } else {
-    gfx::Rect layer_bounds(layer_->bounds());
+    gfx::Rect layer_bounds(layer()->bounds());
     layer_bounds += offset;
-    layer_->SetBounds(layer_bounds);
+    layer()->SetBounds(layer_bounds);
   }
 }
 
@@ -1150,11 +1146,11 @@ void Window::StackChildLayerRelativeTo(Window* child,
   if (!ancestor_layer)
     return;
 
-  if (child->layer_ && target->layer_) {
+  if (child->layer() && target->layer()) {
     if (direction == STACK_ABOVE)
-      ancestor_layer->StackAbove(child->layer_, target->layer_);
+      ancestor_layer->StackAbove(child->layer(), target->layer());
     else
-      ancestor_layer->StackBelow(child->layer_, target->layer_);
+      ancestor_layer->StackBelow(child->layer(), target->layer());
     return;
   }
   typedef std::vector<ui::Layer*> Layers;
@@ -1200,12 +1196,12 @@ void Window::OnStackingChanged() {
   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowStackingChanged(this));
 }
 
-void Window::NotifyRemovingFromRootWindow() {
+void Window::NotifyRemovingFromRootWindow(Window* new_root) {
   FOR_EACH_OBSERVER(WindowObserver, observers_,
-                    OnWindowRemovingFromRootWindow(this));
+                    OnWindowRemovingFromRootWindow(this, new_root));
   for (Window::Windows::const_iterator it = children_.begin();
        it != children_.end(); ++it) {
-    (*it)->NotifyRemovingFromRootWindow();
+    (*it)->NotifyRemovingFromRootWindow(new_root);
   }
 }
 
@@ -1322,11 +1318,10 @@ void Window::NotifyWindowVisibilityChangedUp(aura::Window* target,
   }
 }
 
-void Window::OnWindowBoundsChanged(const gfx::Rect& old_bounds,
-                                   bool contained_mouse) {
-  if (layer_) {
-    bounds_ = layer_->bounds();
-    if (parent_ && !parent_->layer_) {
+void Window::OnWindowBoundsChanged(const gfx::Rect& old_bounds) {
+  if (layer()) {
+    bounds_ = layer()->bounds();
+    if (parent_ && !parent_->layer()) {
       gfx::Vector2d offset;
       aura::Window* ancestor_with_layer =
           parent_->GetAncestorWithLayer(&offset);
@@ -1342,9 +1337,19 @@ void Window::OnWindowBoundsChanged(const gfx::Rect& old_bounds,
   FOR_EACH_OBSERVER(WindowObserver,
                     observers_,
                     OnWindowBoundsChanged(this, old_bounds, bounds()));
-  WindowEventDispatcher* dispatcher = GetDispatcher();
-  if (dispatcher)
-    dispatcher->OnWindowBoundsChanged(this, contained_mouse);
+}
+
+bool Window::CleanupGestureState() {
+  bool state_modified = false;
+  state_modified |= ui::GestureRecognizer::Get()->CancelActiveTouches(this);
+  state_modified |=
+      ui::GestureRecognizer::Get()->CleanupStateForConsumer(this);
+  for (Window::Windows::iterator iter = children_.begin();
+       iter != children_.end();
+       ++iter) {
+    state_modified |= (*iter)->CleanupGestureState();
+  }
+  return state_modified;
 }
 
 void Window::OnPaintLayer(gfx::Canvas* canvas) {
@@ -1353,7 +1358,7 @@ void Window::OnPaintLayer(gfx::Canvas* canvas) {
 
 base::Closure Window::PrepareForLayerBoundsChange() {
   return base::Bind(&Window::OnWindowBoundsChanged, base::Unretained(this),
-                    bounds(), ContainsMouse());
+                    bounds());
 }
 
 bool Window::CanAcceptEvent(const ui::Event& event) {
@@ -1377,13 +1382,14 @@ bool Window::CanAcceptEvent(const ui::Event& event) {
     return true;
 
   // For located events (i.e. mouse, touch etc.), an assumption is made that
-  // windows that don't have a delegate cannot process the event (see more in
-  // GetWindowForPoint()). This assumption is not made for key events.
-  return event.IsKeyEvent() || delegate_;
+  // windows that don't have a default event-handler cannot process the event
+  // (see more in GetWindowForPoint()). This assumption is not made for key
+  // events.
+  return event.IsKeyEvent() || target_handler();
 }
 
 ui::EventTarget* Window::GetParentTarget() {
-  if (dispatcher_) {
+  if (IsRootWindow()) {
     return client::GetEventClient(this) ?
         client::GetEventClient(this)->GetToplevelEventTarget() :
             Env::GetInstance();
@@ -1406,9 +1412,9 @@ void Window::ConvertEventToTarget(ui::EventTarget* target,
                                  static_cast<Window*>(target));
 }
 
-void Window::UpdateLayerName(const std::string& name) {
+void Window::UpdateLayerName() {
 #if !defined(NDEBUG)
-  DCHECK(layer_);
+  DCHECK(layer());
 
   std::string layer_name(name_);
   if (layer_name.empty())
@@ -1417,23 +1423,23 @@ void Window::UpdateLayerName(const std::string& name) {
   if (id_ != -1)
     layer_name += " " + base::IntToString(id_);
 
-  layer_->set_name(layer_name);
+  layer()->set_name(layer_name);
 #endif
 }
 
 bool Window::ContainsMouse() {
   bool contains_mouse = false;
   if (IsVisible()) {
-    WindowEventDispatcher* dispatcher = GetDispatcher();
-    contains_mouse = dispatcher &&
-        ContainsPointInRoot(dispatcher->GetLastMouseLocationInRoot());
+    WindowTreeHost* host = GetHost();
+    contains_mouse = host &&
+        ContainsPointInRoot(host->dispatcher()->GetLastMouseLocationInRoot());
   }
   return contains_mouse;
 }
 
 const Window* Window::GetAncestorWithLayer(gfx::Vector2d* offset) const {
   for (const aura::Window* window = this; window; window = window->parent()) {
-    if (window->layer_)
+    if (window->layer())
       return window;
     if (offset)
       *offset += window->bounds().OffsetFromOrigin();