Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / ui / aura / window.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/aura/window.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "ui/aura/client/capture_client.h"
17 #include "ui/aura/client/cursor_client.h"
18 #include "ui/aura/client/event_client.h"
19 #include "ui/aura/client/focus_client.h"
20 #include "ui/aura/client/screen_position_client.h"
21 #include "ui/aura/client/visibility_client.h"
22 #include "ui/aura/client/window_stacking_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/layout_manager.h"
25 #include "ui/aura/root_window.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/aura/window_observer.h"
28 #include "ui/aura/window_tracker.h"
29 #include "ui/aura/window_tree_host.h"
30 #include "ui/compositor/compositor.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/events/event_target_iterator.h"
33 #include "ui/gfx/animation/multi_animation.h"
34 #include "ui/gfx/canvas.h"
35 #include "ui/gfx/path.h"
36 #include "ui/gfx/scoped_canvas.h"
37 #include "ui/gfx/screen.h"
38
39 namespace aura {
40
41 namespace {
42
43 ui::LayerType WindowLayerTypeToUILayerType(WindowLayerType window_layer_type) {
44   switch (window_layer_type) {
45     case WINDOW_LAYER_NONE:
46       break;
47     case WINDOW_LAYER_NOT_DRAWN:
48       return ui::LAYER_NOT_DRAWN;
49     case WINDOW_LAYER_TEXTURED:
50       return ui::LAYER_TEXTURED;
51     case WINDOW_LAYER_SOLID_COLOR:
52       return ui::LAYER_SOLID_COLOR;
53   }
54   NOTREACHED();
55   return ui::LAYER_NOT_DRAWN;
56 }
57
58 // Used when searching for a Window to stack relative to.
59 template <class T>
60 T IteratorForDirectionBegin(aura::Window* window);
61
62 template <>
63 Window::Windows::const_iterator IteratorForDirectionBegin(
64     aura::Window* window) {
65   return window->children().begin();
66 }
67
68 template <>
69 Window::Windows::const_reverse_iterator IteratorForDirectionBegin(
70     aura::Window* window) {
71   return window->children().rbegin();
72 }
73
74 template <class T>
75 T IteratorForDirectionEnd(aura::Window* window);
76
77 template <>
78 Window::Windows::const_iterator IteratorForDirectionEnd(aura::Window* window) {
79   return window->children().end();
80 }
81
82 template <>
83 Window::Windows::const_reverse_iterator IteratorForDirectionEnd(
84     aura::Window* window) {
85   return window->children().rend();
86 }
87
88 // Depth first search for the first Window with a layer to stack relative
89 // to. Starts at target. Does not descend into |ignore|.
90 template <class T>
91 ui::Layer* FindStackingTargetLayerDown(aura::Window* target,
92                                        aura::Window* ignore) {
93   if (target == ignore)
94     return NULL;
95
96   if (target->layer())
97     return target->layer();
98
99   for (T i = IteratorForDirectionBegin<T>(target);
100        i != IteratorForDirectionEnd<T>(target); ++i) {
101     ui::Layer* layer = FindStackingTargetLayerDown<T>(*i, ignore);
102     if (layer)
103       return layer;
104   }
105   return NULL;
106 }
107
108 // Depth first search through the siblings of |target||. This does not search
109 // all the siblings, only those before/after |target| (depening upon the
110 // template type) and ignoring |ignore|. Returns the Layer of the first Window
111 // encountered with a Layer.
112 template <class T>
113 ui::Layer* FindStackingLayerInSiblings(aura::Window* target,
114                                        aura::Window* ignore) {
115   aura::Window* parent = target->parent();
116   for (T i = std::find(IteratorForDirectionBegin<T>(parent),
117                   IteratorForDirectionEnd<T>(parent), target);
118        i != IteratorForDirectionEnd<T>(parent); ++i) {
119     ui::Layer* layer = FindStackingTargetLayerDown<T>(*i, ignore);
120     if (layer)
121       return layer;
122   }
123   return NULL;
124 }
125
126 // Returns the first Window that has a Layer. This does a depth first search
127 // through the descendants of |target| first, then ascends up doing a depth
128 // first search through siblings of all ancestors until a Layer is found or an
129 // ancestor with a layer is found. This is intended to locate a layer to stack
130 // other layers relative to.
131 template <class T>
132 ui::Layer* FindStackingTargetLayer(aura::Window* target, aura::Window* ignore) {
133   ui::Layer* result = FindStackingTargetLayerDown<T>(target, ignore);
134   if (result)
135     return result;
136   while (target->parent()) {
137     ui::Layer* result = FindStackingLayerInSiblings<T>(target, ignore);
138     if (result)
139       return result;
140     target = target->parent();
141     if (target->layer())
142       return NULL;
143   }
144   return NULL;
145 }
146
147 // Does a depth first search for all descendants of |child| that have layers.
148 // This stops at any descendants that have layers (and adds them to |layers|).
149 void GetLayersToStack(aura::Window* child, std::vector<ui::Layer*>* layers) {
150   if (child->layer()) {
151     layers->push_back(child->layer());
152     return;
153   }
154   for (size_t i = 0; i < child->children().size(); ++i)
155     GetLayersToStack(child->children()[i], layers);
156 }
157
158 }  // namespace
159
160 class ScopedCursorHider {
161  public:
162   explicit ScopedCursorHider(Window* window)
163       : window_(window),
164         hid_cursor_(false) {
165     if (!window_->HasDispatcher())
166       return;
167     const bool cursor_is_in_bounds = window_->GetBoundsInScreen().Contains(
168         Env::GetInstance()->last_mouse_location());
169     client::CursorClient* cursor_client = client::GetCursorClient(window_);
170     if (cursor_is_in_bounds && cursor_client &&
171         cursor_client->IsCursorVisible()) {
172       cursor_client->HideCursor();
173       hid_cursor_ = true;
174     }
175   }
176   ~ScopedCursorHider() {
177     if (!window_->HasDispatcher())
178       return;
179
180     // Update the device scale factor of the cursor client only when the last
181     // mouse location is on this root window.
182     if (hid_cursor_) {
183       client::CursorClient* cursor_client = client::GetCursorClient(window_);
184       if (cursor_client) {
185         const gfx::Display& display =
186             gfx::Screen::GetScreenFor(window_)->GetDisplayNearestWindow(
187                 window_);
188         cursor_client->SetDisplay(display);
189         cursor_client->ShowCursor();
190       }
191     }
192   }
193
194  private:
195   Window* window_;
196   bool hid_cursor_;
197
198   DISALLOW_COPY_AND_ASSIGN(ScopedCursorHider);
199 };
200
201 Window::Window(WindowDelegate* delegate)
202     : dispatcher_(NULL),
203       type_(ui::wm::WINDOW_TYPE_UNKNOWN),
204       owned_by_parent_(true),
205       delegate_(delegate),
206       parent_(NULL),
207       visible_(false),
208       id_(-1),
209       transparent_(false),
210       user_data_(NULL),
211       ignore_events_(false),
212       // Don't notify newly added observers during notification. This causes
213       // problems for code that adds an observer as part of an observer
214       // notification (such as the workspace code).
215       observers_(ObserverList<WindowObserver>::NOTIFY_EXISTING_ONLY) {
216   set_target_handler(delegate_);
217 }
218
219 Window::~Window() {
220   // |layer_| can be NULL during tests, or if this Window is layerless.
221   if (layer_)
222     layer_->SuppressPaint();
223
224   // Let the delegate know we're in the processing of destroying.
225   if (delegate_)
226     delegate_->OnWindowDestroying();
227   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowDestroying(this));
228
229   // Let the root know so that it can remove any references to us.
230   WindowEventDispatcher* dispatcher = GetDispatcher();
231   if (dispatcher)
232     dispatcher->OnWindowDestroying(this);
233
234   // Then destroy the children.
235   RemoveOrDestroyChildren();
236
237   // The window needs to be removed from the parent before calling the
238   // WindowDestroyed callbacks of delegate and the observers.
239   if (parent_)
240     parent_->RemoveChild(this);
241
242   if (delegate_)
243     delegate_->OnWindowDestroyed();
244   ObserverListBase<WindowObserver>::Iterator iter(observers_);
245   WindowObserver* observer;
246   while ((observer = iter.GetNext())) {
247     RemoveObserver(observer);
248     observer->OnWindowDestroyed(this);
249   }
250
251   // Clear properties.
252   for (std::map<const void*, Value>::const_iterator iter = prop_map_.begin();
253        iter != prop_map_.end();
254        ++iter) {
255     if (iter->second.deallocator)
256       (*iter->second.deallocator)(iter->second.value);
257   }
258   prop_map_.clear();
259
260   // If we have layer it will either be destroyed by |layer_owner_|'s dtor, or
261   // by whoever acquired it. We don't have a layer if Init() wasn't invoked or
262   // we are layerless.
263   if (layer_) {
264     layer_->set_delegate(NULL);
265     layer_ = NULL;
266   }
267 }
268
269 void Window::Init(WindowLayerType window_layer_type) {
270   if (window_layer_type != WINDOW_LAYER_NONE) {
271     layer_ = new ui::Layer(WindowLayerTypeToUILayerType(window_layer_type));
272     layer_owner_.reset(layer_);
273     layer_->SetVisible(false);
274     layer_->set_delegate(this);
275     UpdateLayerName(name_);
276     layer_->SetFillsBoundsOpaquely(!transparent_);
277   }
278
279   Env::GetInstance()->NotifyWindowInitialized(this);
280 }
281
282 ui::Layer* Window::RecreateLayer() {
283   // Disconnect the old layer, but don't delete it.
284   ui::Layer* old_layer = AcquireLayer();
285   if (!old_layer)
286     return NULL;
287
288   bounds_.SetRect(0, 0, 0, 0);
289
290   old_layer->set_delegate(NULL);
291
292   layer_ = new ui::Layer(old_layer->type());
293   layer_owner_.reset(layer_);
294   layer_->SetVisible(old_layer->visible());
295   layer_->set_scale_content(old_layer->scale_content());
296   layer_->set_delegate(this);
297   layer_->SetMasksToBounds(old_layer->GetMasksToBounds());
298
299   if (delegate_)
300     delegate_->DidRecreateLayer(old_layer, layer_);
301
302   UpdateLayerName(name_);
303   layer_->SetFillsBoundsOpaquely(!transparent_);
304   // Install new layer as a sibling of the old layer, stacked below it.
305   if (old_layer->parent()) {
306     old_layer->parent()->Add(layer_);
307     old_layer->parent()->StackBelow(layer_, old_layer);
308   }
309   // Migrate all the child layers over to the new layer. Copy the list because
310   // the items are removed during iteration.
311   std::vector<ui::Layer*> children_copy = old_layer->children();
312   for (std::vector<ui::Layer*>::const_iterator it = children_copy.begin();
313        it != children_copy.end();
314        ++it) {
315     ui::Layer* child = *it;
316     layer_->Add(child);
317   }
318   return old_layer;
319 }
320
321 void Window::SetType(ui::wm::WindowType type) {
322   // Cannot change type after the window is initialized.
323   DCHECK(!layer_);
324   type_ = type;
325 }
326
327 void Window::SetName(const std::string& name) {
328   name_ = name;
329
330   if (layer_)
331     UpdateLayerName(name_);
332 }
333
334 void Window::SetTransparent(bool transparent) {
335   transparent_ = transparent;
336   if (layer_)
337     layer_->SetFillsBoundsOpaquely(!transparent_);
338 }
339
340 Window* Window::GetRootWindow() {
341   return const_cast<Window*>(
342       static_cast<const Window*>(this)->GetRootWindow());
343 }
344
345 const Window* Window::GetRootWindow() const {
346   return dispatcher_ ? this : parent_ ? parent_->GetRootWindow() : NULL;
347 }
348
349 WindowEventDispatcher* Window::GetDispatcher() {
350   return const_cast<WindowEventDispatcher*>(const_cast<const Window*>(this)->
351       GetDispatcher());
352 }
353
354 const WindowEventDispatcher* Window::GetDispatcher() const {
355   const Window* root_window = GetRootWindow();
356   return root_window ? root_window->dispatcher_ : NULL;
357 }
358
359 void Window::Show() {
360   SetVisible(true);
361 }
362
363 void Window::Hide() {
364   SetVisible(false);
365   ReleaseCapture();
366 }
367
368 bool Window::IsVisible() const {
369   // Layer visibility can be inconsistent with window visibility, for example
370   // when a Window is hidden, we want this function to return false immediately
371   // after, even though the client may decide to animate the hide effect (and
372   // so the layer will be visible for some time after Hide() is called).
373   for (const Window* window = this; window; window = window->parent()) {
374     if (!window->visible_)
375       return false;
376     if (window->layer_)
377       return window->layer_->IsDrawn();
378   }
379   return false;
380 }
381
382 gfx::Rect Window::GetBoundsInRootWindow() const {
383   // TODO(beng): There may be a better way to handle this, and the existing code
384   //             is likely wrong anyway in a multi-display world, but this will
385   //             do for now.
386   if (!GetRootWindow())
387     return bounds();
388   gfx::Point origin = bounds().origin();
389   ConvertPointToTarget(parent_, GetRootWindow(), &origin);
390   return gfx::Rect(origin, bounds().size());
391 }
392
393 gfx::Rect Window::GetBoundsInScreen() const {
394   gfx::Rect bounds(GetBoundsInRootWindow());
395   const Window* root = GetRootWindow();
396   if (root) {
397     aura::client::ScreenPositionClient* screen_position_client =
398         aura::client::GetScreenPositionClient(root);
399     if (screen_position_client) {
400       gfx::Point origin = bounds.origin();
401       screen_position_client->ConvertPointToScreen(root, &origin);
402       bounds.set_origin(origin);
403     }
404   }
405   return bounds;
406 }
407
408 void Window::SetTransform(const gfx::Transform& transform) {
409   if (!layer_) {
410     // Transforms aren't supported on layerless windows.
411     NOTREACHED();
412     return;
413   }
414   WindowEventDispatcher* dispatcher = GetDispatcher();
415   bool contained_mouse = IsVisible() && dispatcher &&
416       ContainsPointInRoot(dispatcher->GetLastMouseLocationInRoot());
417   layer_->SetTransform(transform);
418   if (dispatcher)
419     dispatcher->OnWindowTransformed(this, contained_mouse);
420 }
421
422 void Window::SetLayoutManager(LayoutManager* layout_manager) {
423   if (layout_manager == layout_manager_)
424     return;
425   layout_manager_.reset(layout_manager);
426   if (!layout_manager)
427     return;
428   // If we're changing to a new layout manager, ensure it is aware of all the
429   // existing child windows.
430   for (Windows::const_iterator it = children_.begin();
431        it != children_.end();
432        ++it)
433     layout_manager_->OnWindowAddedToLayout(*it);
434 }
435
436 void Window::SetBounds(const gfx::Rect& new_bounds) {
437   if (parent_ && parent_->layout_manager())
438     parent_->layout_manager()->SetChildBounds(this, new_bounds);
439   else
440     SetBoundsInternal(new_bounds);
441 }
442
443 void Window::SetBoundsInScreen(const gfx::Rect& new_bounds_in_screen,
444                                const gfx::Display& dst_display) {
445   Window* root = GetRootWindow();
446   if (root) {
447     gfx::Point origin = new_bounds_in_screen.origin();
448     aura::client::ScreenPositionClient* screen_position_client =
449         aura::client::GetScreenPositionClient(root);
450     screen_position_client->SetBounds(this, new_bounds_in_screen, dst_display);
451     return;
452   }
453   SetBounds(new_bounds_in_screen);
454 }
455
456 gfx::Rect Window::GetTargetBounds() const {
457   if (!layer_)
458     return bounds();
459
460   if (!parent_ || parent_->layer_)
461     return layer_->GetTargetBounds();
462
463   // We have a layer but our parent (who is valid) doesn't. This means the
464   // coordinates of the layer are relative to the first ancestor with a layer;
465   // convert to be relative to parent.
466   gfx::Vector2d offset;
467   const aura::Window* ancestor_with_layer =
468       parent_->GetAncestorWithLayer(&offset);
469   if (!ancestor_with_layer)
470     return layer_->GetTargetBounds();
471
472   gfx::Rect layer_target_bounds = layer_->GetTargetBounds();
473   layer_target_bounds -= offset;
474   return layer_target_bounds;
475 }
476
477 void Window::SchedulePaintInRect(const gfx::Rect& rect) {
478   if (!layer_ && parent_) {
479     // Notification of paint scheduled happens for the window with a layer.
480     gfx::Rect parent_rect(bounds().size());
481     parent_rect.Intersect(rect);
482     if (!parent_rect.IsEmpty()) {
483       parent_rect.Offset(bounds().origin().OffsetFromOrigin());
484       parent_->SchedulePaintInRect(parent_rect);
485     }
486   } else if (layer_ && layer_->SchedulePaint(rect)) {
487     FOR_EACH_OBSERVER(
488         WindowObserver, observers_, OnWindowPaintScheduled(this, rect));
489   }
490 }
491
492 void Window::StackChildAtTop(Window* child) {
493   if (children_.size() <= 1 || child == children_.back())
494     return;  // In the front already.
495   StackChildAbove(child, children_.back());
496 }
497
498 void Window::StackChildAbove(Window* child, Window* target) {
499   StackChildRelativeTo(child, target, STACK_ABOVE);
500 }
501
502 void Window::StackChildAtBottom(Window* child) {
503   if (children_.size() <= 1 || child == children_.front())
504     return;  // At the bottom already.
505   StackChildBelow(child, children_.front());
506 }
507
508 void Window::StackChildBelow(Window* child, Window* target) {
509   StackChildRelativeTo(child, target, STACK_BELOW);
510 }
511
512 void Window::AddChild(Window* child) {
513   WindowObserver::HierarchyChangeParams params;
514   params.target = child;
515   params.new_parent = this;
516   params.old_parent = child->parent();
517   params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
518   NotifyWindowHierarchyChange(params);
519
520   Window* old_root = child->GetRootWindow();
521
522   DCHECK(std::find(children_.begin(), children_.end(), child) ==
523       children_.end());
524   if (child->parent())
525     child->parent()->RemoveChildImpl(child, this);
526
527   gfx::Vector2d offset;
528   aura::Window* ancestor_with_layer = GetAncestorWithLayer(&offset);
529   if (ancestor_with_layer) {
530     offset += child->bounds().OffsetFromOrigin();
531     child->ReparentLayers(ancestor_with_layer->layer(), offset);
532   }
533
534   child->parent_ = this;
535
536   children_.push_back(child);
537   if (layout_manager_)
538     layout_manager_->OnWindowAddedToLayout(child);
539   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowAdded(child));
540   child->OnParentChanged();
541
542   Window* root_window = GetRootWindow();
543   if (root_window && old_root != root_window) {
544     root_window->GetDispatcher()->OnWindowAddedToRootWindow(child);
545     child->NotifyAddedToRootWindow();
546   }
547
548   params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
549   NotifyWindowHierarchyChange(params);
550 }
551
552 void Window::RemoveChild(Window* child) {
553   WindowObserver::HierarchyChangeParams params;
554   params.target = child;
555   params.new_parent = NULL;
556   params.old_parent = this;
557   params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING;
558   NotifyWindowHierarchyChange(params);
559
560   RemoveChildImpl(child, NULL);
561
562   params.phase = WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED;
563   NotifyWindowHierarchyChange(params);
564 }
565
566 bool Window::Contains(const Window* other) const {
567   for (const Window* parent = other; parent; parent = parent->parent_) {
568     if (parent == this)
569       return true;
570   }
571   return false;
572 }
573
574 Window* Window::GetChildById(int id) {
575   return const_cast<Window*>(const_cast<const Window*>(this)->GetChildById(id));
576 }
577
578 const Window* Window::GetChildById(int id) const {
579   Windows::const_iterator i;
580   for (i = children_.begin(); i != children_.end(); ++i) {
581     if ((*i)->id() == id)
582       return *i;
583     const Window* result = (*i)->GetChildById(id);
584     if (result)
585       return result;
586   }
587   return NULL;
588 }
589
590 // static
591 void Window::ConvertPointToTarget(const Window* source,
592                                   const Window* target,
593                                   gfx::Point* point) {
594   if (!source)
595     return;
596   if (source->GetRootWindow() != target->GetRootWindow()) {
597     client::ScreenPositionClient* source_client =
598         client::GetScreenPositionClient(source->GetRootWindow());
599     source_client->ConvertPointToScreen(source, point);
600
601     client::ScreenPositionClient* target_client =
602         client::GetScreenPositionClient(target->GetRootWindow());
603     target_client->ConvertPointFromScreen(target, point);
604   } else if ((source != target) && (!source->layer_ || !target->layer_)) {
605     if (!source->layer_) {
606       gfx::Vector2d offset_to_layer;
607       source = source->GetAncestorWithLayer(&offset_to_layer);
608       *point += offset_to_layer;
609     }
610     if (!target->layer_) {
611       gfx::Vector2d offset_to_layer;
612       target = target->GetAncestorWithLayer(&offset_to_layer);
613       *point -= offset_to_layer;
614     }
615     ui::Layer::ConvertPointToLayer(source->layer_, target->layer_, point);
616   } else {
617     ui::Layer::ConvertPointToLayer(source->layer_, target->layer_, point);
618   }
619 }
620
621 void Window::MoveCursorTo(const gfx::Point& point_in_window) {
622   Window* root_window = GetRootWindow();
623   DCHECK(root_window);
624   gfx::Point point_in_root(point_in_window);
625   ConvertPointToTarget(this, root_window, &point_in_root);
626   root_window->GetDispatcher()->MoveCursorTo(point_in_root);
627 }
628
629 gfx::NativeCursor Window::GetCursor(const gfx::Point& point) const {
630   return delegate_ ? delegate_->GetCursor(point) : gfx::kNullCursor;
631 }
632
633 void Window::SetEventFilter(ui::EventHandler* event_filter) {
634   if (event_filter_)
635     RemovePreTargetHandler(event_filter_.get());
636   event_filter_.reset(event_filter);
637   if (event_filter)
638     AddPreTargetHandler(event_filter);
639 }
640
641 void Window::AddObserver(WindowObserver* observer) {
642   observers_.AddObserver(observer);
643 }
644
645 void Window::RemoveObserver(WindowObserver* observer) {
646   observers_.RemoveObserver(observer);
647 }
648
649 bool Window::HasObserver(WindowObserver* observer) {
650   return observers_.HasObserver(observer);
651 }
652
653 bool Window::ContainsPointInRoot(const gfx::Point& point_in_root) const {
654   const Window* root_window = GetRootWindow();
655   if (!root_window)
656     return false;
657   gfx::Point local_point(point_in_root);
658   ConvertPointToTarget(root_window, this, &local_point);
659   return gfx::Rect(GetTargetBounds().size()).Contains(local_point);
660 }
661
662 bool Window::ContainsPoint(const gfx::Point& local_point) const {
663   return gfx::Rect(bounds().size()).Contains(local_point);
664 }
665
666 bool Window::HitTest(const gfx::Point& local_point) {
667   // Expand my bounds for hit testing (override is usually zero but it's
668   // probably cheaper to do the math every time than to branch).
669   gfx::Rect local_bounds(gfx::Point(), bounds().size());
670   local_bounds.Inset(aura::Env::GetInstance()->is_touch_down() ?
671       hit_test_bounds_override_outer_touch_ :
672       hit_test_bounds_override_outer_mouse_);
673
674   if (!delegate_ || !delegate_->HasHitTestMask())
675     return local_bounds.Contains(local_point);
676
677   gfx::Path mask;
678   delegate_->GetHitTestMask(&mask);
679
680   SkRegion clip_region;
681   clip_region.setRect(local_bounds.x(), local_bounds.y(),
682                       local_bounds.width(), local_bounds.height());
683   SkRegion mask_region;
684   return mask_region.setPath(mask, clip_region) &&
685       mask_region.contains(local_point.x(), local_point.y());
686 }
687
688 Window* Window::GetEventHandlerForPoint(const gfx::Point& local_point) {
689   return GetWindowForPoint(local_point, true, true);
690 }
691
692 Window* Window::GetTopWindowContainingPoint(const gfx::Point& local_point) {
693   return GetWindowForPoint(local_point, false, false);
694 }
695
696 Window* Window::GetToplevelWindow() {
697   Window* topmost_window_with_delegate = NULL;
698   for (aura::Window* window = this; window != NULL; window = window->parent()) {
699     if (window->delegate())
700       topmost_window_with_delegate = window;
701   }
702   return topmost_window_with_delegate;
703 }
704
705 void Window::Focus() {
706   client::FocusClient* client = client::GetFocusClient(this);
707   DCHECK(client);
708   client->FocusWindow(this);
709 }
710
711 void Window::Blur() {
712   client::FocusClient* client = client::GetFocusClient(this);
713   DCHECK(client);
714   client->FocusWindow(NULL);
715 }
716
717 bool Window::HasFocus() const {
718   client::FocusClient* client = client::GetFocusClient(this);
719   return client && client->GetFocusedWindow() == this;
720 }
721
722 bool Window::CanFocus() const {
723   if (dispatcher_)
724     return IsVisible();
725
726   // NOTE: as part of focusing the window the ActivationClient may make the
727   // window visible (by way of making a hidden ancestor visible). For this
728   // reason we can't check visibility here and assume the client is doing it.
729   if (!parent_ || (delegate_ && !delegate_->CanFocus()))
730     return false;
731
732   // The client may forbid certain windows from receiving focus at a given point
733   // in time.
734   client::EventClient* client = client::GetEventClient(GetRootWindow());
735   if (client && !client->CanProcessEventsWithinSubtree(this))
736     return false;
737
738   return parent_->CanFocus();
739 }
740
741 bool Window::CanReceiveEvents() const {
742   if (dispatcher_)
743     return IsVisible();
744
745   // The client may forbid certain windows from receiving events at a given
746   // point in time.
747   client::EventClient* client = client::GetEventClient(GetRootWindow());
748   if (client && !client->CanProcessEventsWithinSubtree(this))
749     return false;
750
751   return parent_ && IsVisible() && parent_->CanReceiveEvents();
752 }
753
754 void Window::SetCapture() {
755   if (!IsVisible())
756     return;
757
758   Window* root_window = GetRootWindow();
759   if (!root_window)
760     return;
761   client::GetCaptureClient(root_window)->SetCapture(this);
762 }
763
764 void Window::ReleaseCapture() {
765   Window* root_window = GetRootWindow();
766   if (!root_window)
767     return;
768   client::GetCaptureClient(root_window)->ReleaseCapture(this);
769 }
770
771 bool Window::HasCapture() {
772   Window* root_window = GetRootWindow();
773   if (!root_window)
774     return false;
775   client::CaptureClient* capture_client = client::GetCaptureClient(root_window);
776   return capture_client && capture_client->GetCaptureWindow() == this;
777 }
778
779 void Window::SuppressPaint() {
780   if (layer_)
781     layer_->SuppressPaint();
782 }
783
784 // {Set,Get,Clear}Property are implemented in window_property.h.
785
786 void Window::SetNativeWindowProperty(const char* key, void* value) {
787   SetPropertyInternal(
788       key, key, NULL, reinterpret_cast<int64>(value), 0);
789 }
790
791 void* Window::GetNativeWindowProperty(const char* key) const {
792   return reinterpret_cast<void*>(GetPropertyInternal(key, 0));
793 }
794
795 void Window::OnDeviceScaleFactorChanged(float device_scale_factor) {
796   ScopedCursorHider hider(this);
797   if (dispatcher_)
798     dispatcher_->host()->OnDeviceScaleFactorChanged(device_scale_factor);
799   if (delegate_)
800     delegate_->OnDeviceScaleFactorChanged(device_scale_factor);
801 }
802
803 #if !defined(NDEBUG)
804 std::string Window::GetDebugInfo() const {
805   return base::StringPrintf(
806       "%s<%d> bounds(%d, %d, %d, %d) %s %s opacity=%.1f",
807       name().empty() ? "Unknown" : name().c_str(), id(),
808       bounds().x(), bounds().y(), bounds().width(), bounds().height(),
809       visible_ ? "WindowVisible" : "WindowHidden",
810       layer_ ?
811           (layer_->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
812           "NoLayer",
813       layer_ ? layer_->opacity() : 1.0f);
814 }
815
816 void Window::PrintWindowHierarchy(int depth) const {
817   VLOG(0) << base::StringPrintf(
818       "%*s%s", depth * 2, "", GetDebugInfo().c_str());
819   for (Windows::const_iterator it = children_.begin();
820        it != children_.end(); ++it) {
821     Window* child = *it;
822     child->PrintWindowHierarchy(depth + 1);
823   }
824 }
825 #endif
826
827 void Window::RemoveOrDestroyChildren() {
828   while (!children_.empty()) {
829     Window* child = children_[0];
830     if (child->owned_by_parent_) {
831       delete child;
832       // Deleting the child so remove it from out children_ list.
833       DCHECK(std::find(children_.begin(), children_.end(), child) ==
834              children_.end());
835     } else {
836       // Even if we can't delete the child, we still need to remove it from the
837       // parent so that relevant bookkeeping (parent_ back-pointers etc) are
838       // updated.
839       RemoveChild(child);
840     }
841   }
842 }
843
844 ///////////////////////////////////////////////////////////////////////////////
845 // Window, private:
846
847 int64 Window::SetPropertyInternal(const void* key,
848                                   const char* name,
849                                   PropertyDeallocator deallocator,
850                                   int64 value,
851                                   int64 default_value) {
852   int64 old = GetPropertyInternal(key, default_value);
853   if (value == default_value) {
854     prop_map_.erase(key);
855   } else {
856     Value prop_value;
857     prop_value.name = name;
858     prop_value.value = value;
859     prop_value.deallocator = deallocator;
860     prop_map_[key] = prop_value;
861   }
862   FOR_EACH_OBSERVER(WindowObserver, observers_,
863                     OnWindowPropertyChanged(this, key, old));
864   return old;
865 }
866
867 int64 Window::GetPropertyInternal(const void* key,
868                                   int64 default_value) const {
869   std::map<const void*, Value>::const_iterator iter = prop_map_.find(key);
870   if (iter == prop_map_.end())
871     return default_value;
872   return iter->second.value;
873 }
874
875 void Window::SetBoundsInternal(const gfx::Rect& new_bounds) {
876   gfx::Rect actual_new_bounds(new_bounds);
877
878   // Ensure we don't go smaller than our minimum bounds.
879   if (delegate_) {
880     const gfx::Size& min_size = delegate_->GetMinimumSize();
881     actual_new_bounds.set_width(
882         std::max(min_size.width(), actual_new_bounds.width()));
883     actual_new_bounds.set_height(
884         std::max(min_size.height(), actual_new_bounds.height()));
885   }
886
887   gfx::Rect old_bounds = GetTargetBounds();
888
889   // Always need to set the layer's bounds -- even if it is to the same thing.
890   // This may cause important side effects such as stopping animation.
891   if (!layer_) {
892     const gfx::Vector2d origin_delta = new_bounds.OffsetFromOrigin() -
893         bounds_.OffsetFromOrigin();
894     bounds_ = new_bounds;
895     OffsetLayerBounds(origin_delta);
896   } else {
897     if (parent_ && !parent_->layer_) {
898       gfx::Vector2d offset;
899       const aura::Window* ancestor_with_layer =
900           parent_->GetAncestorWithLayer(&offset);
901       if (ancestor_with_layer)
902         actual_new_bounds.Offset(offset);
903     }
904     layer_->SetBounds(actual_new_bounds);
905   }
906
907   // If we are currently not the layer's delegate, we will not get bounds
908   // changed notification from the layer (this typically happens after animating
909   // hidden). We must notify ourselves.
910   if (!layer_ || layer_->delegate() != this)
911     OnWindowBoundsChanged(old_bounds, ContainsMouse());
912 }
913
914 void Window::SetVisible(bool visible) {
915   if ((layer_ && visible == layer_->GetTargetVisibility()) ||
916       (!layer_ && visible == visible_))
917     return;  // No change.
918
919   FOR_EACH_OBSERVER(WindowObserver, observers_,
920                     OnWindowVisibilityChanging(this, visible));
921
922   WindowEventDispatcher* dispatcher = GetDispatcher();
923   if (dispatcher)
924     dispatcher->DispatchMouseExitToHidingWindow(this);
925
926   client::VisibilityClient* visibility_client =
927       client::GetVisibilityClient(this);
928   if (visibility_client)
929     visibility_client->UpdateLayerVisibility(this, visible);
930   else if (layer_)
931     layer_->SetVisible(visible);
932   visible_ = visible;
933   SchedulePaint();
934   if (parent_ && parent_->layout_manager_)
935     parent_->layout_manager_->OnChildWindowVisibilityChanged(this, visible);
936
937   if (delegate_)
938     delegate_->OnWindowTargetVisibilityChanged(visible);
939
940   NotifyWindowVisibilityChanged(this, visible);
941
942   if (dispatcher)
943     dispatcher->OnWindowVisibilityChanged(this, visible);
944 }
945
946 void Window::SchedulePaint() {
947   SchedulePaintInRect(gfx::Rect(0, 0, bounds().width(), bounds().height()));
948 }
949
950 void Window::Paint(gfx::Canvas* canvas) {
951   if (delegate_)
952     delegate_->OnPaint(canvas);
953   PaintLayerlessChildren(canvas);
954 }
955
956 void Window::PaintLayerlessChildren(gfx::Canvas* canvas) {
957   for (size_t i = 0, count = children_.size(); i < count; ++i) {
958     Window* child = children_[i];
959     if (!child->layer_ && child->visible_) {
960       gfx::ScopedCanvas scoped_canvas(canvas);
961       if (canvas->ClipRect(child->bounds())) {
962         canvas->Translate(child->bounds().OffsetFromOrigin());
963         child->Paint(canvas);
964       }
965     }
966   }
967 }
968
969 Window* Window::GetWindowForPoint(const gfx::Point& local_point,
970                                   bool return_tightest,
971                                   bool for_event_handling) {
972   if (!IsVisible())
973     return NULL;
974
975   if ((for_event_handling && !HitTest(local_point)) ||
976       (!for_event_handling && !ContainsPoint(local_point)))
977     return NULL;
978
979   // Check if I should claim this event and not pass it to my children because
980   // the location is inside my hit test override area.  For details, see
981   // set_hit_test_bounds_override_inner().
982   if (for_event_handling && !hit_test_bounds_override_inner_.empty()) {
983     gfx::Rect inset_local_bounds(gfx::Point(), bounds().size());
984     inset_local_bounds.Inset(hit_test_bounds_override_inner_);
985     // We know we're inside the normal local bounds, so if we're outside the
986     // inset bounds we must be in the special hit test override area.
987     DCHECK(HitTest(local_point));
988     if (!inset_local_bounds.Contains(local_point))
989       return delegate_ ? this : NULL;
990   }
991
992   if (!return_tightest && delegate_)
993     return this;
994
995   for (Windows::const_reverse_iterator it = children_.rbegin(),
996            rend = children_.rend();
997        it != rend; ++it) {
998     Window* child = *it;
999
1000     if (for_event_handling) {
1001       if (child->ignore_events_)
1002         continue;
1003       // The client may not allow events to be processed by certain subtrees.
1004       client::EventClient* client = client::GetEventClient(GetRootWindow());
1005       if (client && !client->CanProcessEventsWithinSubtree(child))
1006         continue;
1007       if (delegate_ && !delegate_->ShouldDescendIntoChildForEventHandling(
1008               child, local_point))
1009         continue;
1010     }
1011
1012     gfx::Point point_in_child_coords(local_point);
1013     ConvertPointToTarget(this, child, &point_in_child_coords);
1014     Window* match = child->GetWindowForPoint(point_in_child_coords,
1015                                              return_tightest,
1016                                              for_event_handling);
1017     if (match)
1018       return match;
1019   }
1020
1021   return delegate_ ? this : NULL;
1022 }
1023
1024 void Window::RemoveChildImpl(Window* child, Window* new_parent) {
1025   if (layout_manager_)
1026     layout_manager_->OnWillRemoveWindowFromLayout(child);
1027   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWillRemoveWindow(child));
1028   Window* root_window = child->GetRootWindow();
1029   Window* new_root_window = new_parent ? new_parent->GetRootWindow() : NULL;
1030   if (root_window && root_window != new_root_window) {
1031     root_window->GetDispatcher()->OnWindowRemovedFromRootWindow(
1032         child, new_root_window);
1033     child->NotifyRemovingFromRootWindow();
1034   }
1035
1036   gfx::Vector2d offset;
1037   GetAncestorWithLayer(&offset);
1038   child->UnparentLayers(!layer_, offset);
1039   child->parent_ = NULL;
1040   Windows::iterator i = std::find(children_.begin(), children_.end(), child);
1041   DCHECK(i != children_.end());
1042   children_.erase(i);
1043   child->OnParentChanged();
1044   if (layout_manager_)
1045     layout_manager_->OnWindowRemovedFromLayout(child);
1046 }
1047
1048 void Window::UnparentLayers(bool has_layerless_ancestor,
1049                             const gfx::Vector2d& offset) {
1050   if (!layer_) {
1051     const gfx::Vector2d new_offset = offset + bounds().OffsetFromOrigin();
1052     for (size_t i = 0; i < children_.size(); ++i) {
1053       children_[i]->UnparentLayers(true, new_offset);
1054     }
1055   } else {
1056     // Only remove the layer if we still own it.  Someone else may have acquired
1057     // ownership of it via AcquireLayer() and may expect the hierarchy to go
1058     // unchanged as the Window is destroyed.
1059     if (layer_owner_) {
1060       if (layer_->parent())
1061         layer_->parent()->Remove(layer_);
1062       if (has_layerless_ancestor) {
1063         const gfx::Rect real_bounds(bounds_);
1064         gfx::Rect layer_bounds(layer_->bounds());
1065         layer_bounds.Offset(-offset);
1066         layer_->SetBounds(layer_bounds);
1067         bounds_ = real_bounds;
1068       }
1069     }
1070   }
1071 }
1072
1073 void Window::ReparentLayers(ui::Layer* parent_layer,
1074                             const gfx::Vector2d& offset) {
1075   if (!layer_) {
1076     for (size_t i = 0; i < children_.size(); ++i) {
1077       children_[i]->ReparentLayers(
1078           parent_layer,
1079           offset + children_[i]->bounds().OffsetFromOrigin());
1080     }
1081   } else {
1082     const gfx::Rect real_bounds(bounds());
1083     parent_layer->Add(layer_);
1084     gfx::Rect layer_bounds(layer_->bounds().size());
1085     layer_bounds += offset;
1086     layer_->SetBounds(layer_bounds);
1087     bounds_ = real_bounds;
1088   }
1089 }
1090
1091 void Window::OffsetLayerBounds(const gfx::Vector2d& offset) {
1092   if (!layer_) {
1093     for (size_t i = 0; i < children_.size(); ++i)
1094       children_[i]->OffsetLayerBounds(offset);
1095   } else {
1096     gfx::Rect layer_bounds(layer_->bounds());
1097     layer_bounds += offset;
1098     layer_->SetBounds(layer_bounds);
1099   }
1100 }
1101
1102 void Window::OnParentChanged() {
1103   FOR_EACH_OBSERVER(
1104       WindowObserver, observers_, OnWindowParentChanged(this, parent_));
1105 }
1106
1107 void Window::StackChildRelativeTo(Window* child,
1108                                   Window* target,
1109                                   StackDirection direction) {
1110   DCHECK_NE(child, target);
1111   DCHECK(child);
1112   DCHECK(target);
1113   DCHECK_EQ(this, child->parent());
1114   DCHECK_EQ(this, target->parent());
1115
1116   client::WindowStackingClient* stacking_client =
1117       client::GetWindowStackingClient();
1118   if (stacking_client &&
1119       !stacking_client->AdjustStacking(&child, &target, &direction))
1120     return;
1121
1122   const size_t child_i =
1123       std::find(children_.begin(), children_.end(), child) - children_.begin();
1124   const size_t target_i =
1125       std::find(children_.begin(), children_.end(), target) - children_.begin();
1126
1127   // Don't move the child if it is already in the right place.
1128   if ((direction == STACK_ABOVE && child_i == target_i + 1) ||
1129       (direction == STACK_BELOW && child_i + 1 == target_i))
1130     return;
1131
1132   const size_t dest_i =
1133       direction == STACK_ABOVE ?
1134       (child_i < target_i ? target_i : target_i + 1) :
1135       (child_i < target_i ? target_i - 1 : target_i);
1136   children_.erase(children_.begin() + child_i);
1137   children_.insert(children_.begin() + dest_i, child);
1138
1139   StackChildLayerRelativeTo(child, target, direction);
1140
1141   child->OnStackingChanged();
1142 }
1143
1144 void Window::StackChildLayerRelativeTo(Window* child,
1145                                        Window* target,
1146                                        StackDirection direction) {
1147   Window* ancestor_with_layer = GetAncestorWithLayer(NULL);
1148   ui::Layer* ancestor_layer =
1149       ancestor_with_layer ? ancestor_with_layer->layer() : NULL;
1150   if (!ancestor_layer)
1151     return;
1152
1153   if (child->layer_ && target->layer_) {
1154     if (direction == STACK_ABOVE)
1155       ancestor_layer->StackAbove(child->layer_, target->layer_);
1156     else
1157       ancestor_layer->StackBelow(child->layer_, target->layer_);
1158     return;
1159   }
1160   typedef std::vector<ui::Layer*> Layers;
1161   Layers layers;
1162   GetLayersToStack(child, &layers);
1163   if (layers.empty())
1164     return;
1165
1166   ui::Layer* target_layer;
1167   if (direction == STACK_ABOVE) {
1168     target_layer =
1169         FindStackingTargetLayer<Windows::const_reverse_iterator>(target, child);
1170   } else {
1171     target_layer =
1172         FindStackingTargetLayer<Windows::const_iterator>(target, child);
1173   }
1174
1175   if (!target_layer) {
1176     if (direction == STACK_ABOVE) {
1177       for (Layers::const_reverse_iterator i = layers.rbegin(),
1178                rend = layers.rend(); i != rend; ++i) {
1179         ancestor_layer->StackAtBottom(*i);
1180       }
1181     } else {
1182       for (Layers::const_iterator i = layers.begin(); i != layers.end(); ++i)
1183         ancestor_layer->StackAtTop(*i);
1184     }
1185     return;
1186   }
1187
1188   if (direction == STACK_ABOVE) {
1189     for (Layers::const_reverse_iterator i = layers.rbegin(),
1190              rend = layers.rend(); i != rend; ++i) {
1191       ancestor_layer->StackAbove(*i, target_layer);
1192     }
1193   } else {
1194     for (Layers::const_iterator i = layers.begin(); i != layers.end(); ++i)
1195       ancestor_layer->StackBelow(*i, target_layer);
1196   }
1197 }
1198
1199 void Window::OnStackingChanged() {
1200   FOR_EACH_OBSERVER(WindowObserver, observers_, OnWindowStackingChanged(this));
1201 }
1202
1203 void Window::NotifyRemovingFromRootWindow() {
1204   FOR_EACH_OBSERVER(WindowObserver, observers_,
1205                     OnWindowRemovingFromRootWindow(this));
1206   for (Window::Windows::const_iterator it = children_.begin();
1207        it != children_.end(); ++it) {
1208     (*it)->NotifyRemovingFromRootWindow();
1209   }
1210 }
1211
1212 void Window::NotifyAddedToRootWindow() {
1213   FOR_EACH_OBSERVER(WindowObserver, observers_,
1214                     OnWindowAddedToRootWindow(this));
1215   for (Window::Windows::const_iterator it = children_.begin();
1216        it != children_.end(); ++it) {
1217     (*it)->NotifyAddedToRootWindow();
1218   }
1219 }
1220
1221 void Window::NotifyWindowHierarchyChange(
1222     const WindowObserver::HierarchyChangeParams& params) {
1223   params.target->NotifyWindowHierarchyChangeDown(params);
1224   switch (params.phase) {
1225   case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING:
1226     if (params.old_parent)
1227       params.old_parent->NotifyWindowHierarchyChangeUp(params);
1228     break;
1229   case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED:
1230     if (params.new_parent)
1231       params.new_parent->NotifyWindowHierarchyChangeUp(params);
1232     break;
1233   default:
1234     NOTREACHED();
1235     break;
1236   }
1237 }
1238
1239 void Window::NotifyWindowHierarchyChangeDown(
1240     const WindowObserver::HierarchyChangeParams& params) {
1241   NotifyWindowHierarchyChangeAtReceiver(params);
1242   for (Window::Windows::const_iterator it = children_.begin();
1243        it != children_.end(); ++it) {
1244     (*it)->NotifyWindowHierarchyChangeDown(params);
1245   }
1246 }
1247
1248 void Window::NotifyWindowHierarchyChangeUp(
1249     const WindowObserver::HierarchyChangeParams& params) {
1250   for (Window* window = this; window; window = window->parent())
1251     window->NotifyWindowHierarchyChangeAtReceiver(params);
1252 }
1253
1254 void Window::NotifyWindowHierarchyChangeAtReceiver(
1255     const WindowObserver::HierarchyChangeParams& params) {
1256   WindowObserver::HierarchyChangeParams local_params = params;
1257   local_params.receiver = this;
1258
1259   switch (params.phase) {
1260   case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING:
1261     FOR_EACH_OBSERVER(WindowObserver, observers_,
1262                       OnWindowHierarchyChanging(local_params));
1263     break;
1264   case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED:
1265     FOR_EACH_OBSERVER(WindowObserver, observers_,
1266                       OnWindowHierarchyChanged(local_params));
1267     break;
1268   default:
1269     NOTREACHED();
1270     break;
1271   }
1272 }
1273
1274 void Window::NotifyWindowVisibilityChanged(aura::Window* target,
1275                                            bool visible) {
1276   if (!NotifyWindowVisibilityChangedDown(target, visible)) {
1277     return; // |this| has been deleted.
1278   }
1279   NotifyWindowVisibilityChangedUp(target, visible);
1280 }
1281
1282 bool Window::NotifyWindowVisibilityChangedAtReceiver(aura::Window* target,
1283                                                      bool visible) {
1284   // |this| may be deleted during a call to OnWindowVisibilityChanged() on one
1285   // of the observers. We create an local observer for that. In that case we
1286   // exit without further access to any members.
1287   WindowTracker tracker;
1288   tracker.Add(this);
1289   FOR_EACH_OBSERVER(WindowObserver, observers_,
1290                     OnWindowVisibilityChanged(target, visible));
1291   return tracker.Contains(this);
1292 }
1293
1294 bool Window::NotifyWindowVisibilityChangedDown(aura::Window* target,
1295                                                bool visible) {
1296   if (!NotifyWindowVisibilityChangedAtReceiver(target, visible))
1297     return false; // |this| was deleted.
1298   std::set<const Window*> child_already_processed;
1299   bool child_destroyed = false;
1300   do {
1301     child_destroyed = false;
1302     for (Window::Windows::const_iterator it = children_.begin();
1303          it != children_.end(); ++it) {
1304       if (!child_already_processed.insert(*it).second)
1305         continue;
1306       if (!(*it)->NotifyWindowVisibilityChangedDown(target, visible)) {
1307         // |*it| was deleted, |it| is invalid and |children_| has changed.
1308         // We exit the current for-loop and enter a new one.
1309         child_destroyed = true;
1310         break;
1311       }
1312     }
1313   } while (child_destroyed);
1314   return true;
1315 }
1316
1317 void Window::NotifyWindowVisibilityChangedUp(aura::Window* target,
1318                                              bool visible) {
1319   for (Window* window = this; window; window = window->parent()) {
1320     bool ret = window->NotifyWindowVisibilityChangedAtReceiver(target, visible);
1321     DCHECK(ret);
1322   }
1323 }
1324
1325 void Window::OnWindowBoundsChanged(const gfx::Rect& old_bounds,
1326                                    bool contained_mouse) {
1327   if (layer_) {
1328     bounds_ = layer_->bounds();
1329     if (parent_ && !parent_->layer_) {
1330       gfx::Vector2d offset;
1331       aura::Window* ancestor_with_layer =
1332           parent_->GetAncestorWithLayer(&offset);
1333       if (ancestor_with_layer)
1334         bounds_.Offset(-offset);
1335     }
1336   }
1337
1338   if (layout_manager_)
1339     layout_manager_->OnWindowResized();
1340   if (delegate_)
1341     delegate_->OnBoundsChanged(old_bounds, bounds());
1342   FOR_EACH_OBSERVER(WindowObserver,
1343                     observers_,
1344                     OnWindowBoundsChanged(this, old_bounds, bounds()));
1345   WindowEventDispatcher* dispatcher = GetDispatcher();
1346   if (dispatcher)
1347     dispatcher->OnWindowBoundsChanged(this, contained_mouse);
1348 }
1349
1350 void Window::OnPaintLayer(gfx::Canvas* canvas) {
1351   Paint(canvas);
1352 }
1353
1354 base::Closure Window::PrepareForLayerBoundsChange() {
1355   return base::Bind(&Window::OnWindowBoundsChanged, base::Unretained(this),
1356                     bounds(), ContainsMouse());
1357 }
1358
1359 bool Window::CanAcceptEvent(const ui::Event& event) {
1360   // The client may forbid certain windows from receiving events at a given
1361   // point in time.
1362   client::EventClient* client = client::GetEventClient(GetRootWindow());
1363   if (client && !client->CanProcessEventsWithinSubtree(this))
1364     return false;
1365
1366   // We need to make sure that a touch cancel event and any gesture events it
1367   // creates can always reach the window. This ensures that we receive a valid
1368   // touch / gesture stream.
1369   if (event.IsEndingEvent())
1370     return true;
1371
1372   if (!IsVisible())
1373     return false;
1374
1375   // The top-most window can always process an event.
1376   if (!parent_)
1377     return true;
1378
1379   // For located events (i.e. mouse, touch etc.), an assumption is made that
1380   // windows that don't have a delegate cannot process the event (see more in
1381   // GetWindowForPoint()). This assumption is not made for key events.
1382   return event.IsKeyEvent() || delegate_;
1383 }
1384
1385 ui::EventTarget* Window::GetParentTarget() {
1386   if (dispatcher_) {
1387     return client::GetEventClient(this) ?
1388         client::GetEventClient(this)->GetToplevelEventTarget() :
1389             Env::GetInstance();
1390   }
1391   return parent_;
1392 }
1393
1394 scoped_ptr<ui::EventTargetIterator> Window::GetChildIterator() const {
1395   return scoped_ptr<ui::EventTargetIterator>(
1396       new ui::EventTargetIteratorImpl<Window>(children()));
1397 }
1398
1399 ui::EventTargeter* Window::GetEventTargeter() {
1400   return targeter_.get();
1401 }
1402
1403 void Window::ConvertEventToTarget(ui::EventTarget* target,
1404                                   ui::LocatedEvent* event) {
1405   event->ConvertLocationToTarget(this,
1406                                  static_cast<Window*>(target));
1407 }
1408
1409 void Window::UpdateLayerName(const std::string& name) {
1410 #if !defined(NDEBUG)
1411   DCHECK(layer_);
1412
1413   std::string layer_name(name_);
1414   if (layer_name.empty())
1415     layer_name = "Unnamed Window";
1416
1417   if (id_ != -1)
1418     layer_name += " " + base::IntToString(id_);
1419
1420   layer_->set_name(layer_name);
1421 #endif
1422 }
1423
1424 bool Window::ContainsMouse() {
1425   bool contains_mouse = false;
1426   if (IsVisible()) {
1427     WindowEventDispatcher* dispatcher = GetDispatcher();
1428     contains_mouse = dispatcher &&
1429         ContainsPointInRoot(dispatcher->GetLastMouseLocationInRoot());
1430   }
1431   return contains_mouse;
1432 }
1433
1434 const Window* Window::GetAncestorWithLayer(gfx::Vector2d* offset) const {
1435   for (const aura::Window* window = this; window; window = window->parent()) {
1436     if (window->layer_)
1437       return window;
1438     if (offset)
1439       *offset += window->bounds().OffsetFromOrigin();
1440   }
1441   if (offset)
1442     *offset = gfx::Vector2d();
1443   return NULL;
1444 }
1445
1446 }  // namespace aura