Chromium Code Reviews| Index: content/browser/web_contents/web_contents_view_aura.cc |
| diff --git a/content/browser/web_contents/web_contents_view_aura.cc b/content/browser/web_contents/web_contents_view_aura.cc |
| index 3f520db8d6efc07e5558fa6a1d2e21adfa794c28..466394e2063f5d78236dced9983b05a0aeccf5c8 100644 |
| --- a/content/browser/web_contents/web_contents_view_aura.cc |
| +++ b/content/browser/web_contents/web_contents_view_aura.cc |
| @@ -4,6 +4,8 @@ |
| #include "content/browser/web_contents/web_contents_view_aura.h" |
| +#include <set> |
| + |
| #include "base/auto_reset.h" |
| #include "base/command_line.h" |
| #include "base/metrics/histogram.h" |
| @@ -652,29 +654,25 @@ class WebContentsViewAura::WindowObserver |
| : public aura::WindowObserver, public aura::RootWindowObserver { |
| public: |
| explicit WindowObserver(WebContentsViewAura* view) |
| - : view_(view), |
| - parent_(NULL) { |
| + : view_(view) { |
| view_->window_->AddObserver(this); |
| + std::set<aura::Window*> ancestors = GenerateAncestors(view_->window_.get()); |
| + AddToObserved(ancestors); |
| } |
| virtual ~WindowObserver() { |
| view_->window_->RemoveObserver(this); |
| if (view_->window_->GetDispatcher()) |
| view_->window_->GetDispatcher()->RemoveRootWindowObserver(this); |
| - if (parent_) |
| - parent_->RemoveObserver(this); |
| + RemoveFromObserved(observed_); |
| } |
| // Overridden from aura::WindowObserver: |
| virtual void OnWindowParentChanged(aura::Window* window, |
| aura::Window* parent) OVERRIDE { |
| - if (window == parent_) |
| - return; |
| - if (parent_) |
| - parent_->RemoveObserver(this); |
| - parent_ = parent; |
| - if (parent) |
| - parent->AddObserver(this); |
| + std::set<aura::Window*> ancestors = GenerateAncestors(view_->window_.get()); |
| + RemoveFromObserved(RelativeComplement(ancestors, observed_)); |
| + AddToObserved(RelativeComplement(observed_, ancestors)); |
| } |
| virtual void OnWindowBoundsChanged(aura::Window* window, |
| @@ -686,12 +684,12 @@ class WebContentsViewAura::WindowObserver |
| } |
| virtual void OnWindowAddedToRootWindow(aura::Window* window) OVERRIDE { |
| - if (window != parent_) |
| + if (window == view_->window_.get()) |
| window->GetDispatcher()->AddRootWindowObserver(this); |
| } |
| virtual void OnWindowRemovingFromRootWindow(aura::Window* window) OVERRIDE { |
| - if (window != parent_) |
| + if (window == view_->window_.get()) |
| window->GetDispatcher()->RemoveRootWindowObserver(this); |
| } |
| @@ -708,11 +706,52 @@ class WebContentsViewAura::WindowObserver |
| SendScreenRects(); |
| } |
| - WebContentsViewAura* view_; |
| + std::set<aura::Window*> GenerateAncestors(aura::Window* window) { |
| + std::set<aura::Window*> ret_val; |
| + while (window->parent()) { |
| + ret_val.insert(window->parent()); |
| + window = window->parent(); |
| + } |
| + return ret_val; |
| + } |
| + |
| + void AddToObserved(std::set<aura::Window*> windows) { |
|
sky
2013/11/04 21:34:01
const std::set&
rharrison
2013/11/06 19:26:17
Done.
|
| + for (std::set<aura::Window*>::iterator iter = windows.begin(); |
| + iter != windows.end(); |
| + ++iter) { |
| + if (observed_.end() == observed_.find(*iter)) { |
| + (*iter)->AddObserver(this); |
| + observed_.insert(*iter); |
| + } |
| + } |
| + } |
| - // We cache the old parent so that we can unregister when it's not the parent |
| - // anymore. |
| - aura::Window* parent_; |
| + void RemoveFromObserved(std::set<aura::Window*> windows) { |
|
sky
2013/11/04 21:34:01
const std::set&
rharrison
2013/11/06 19:26:17
Done.
|
| + for (std::set<aura::Window*>::iterator iter = windows.begin(); |
| + iter != windows.end(); |
| + ++iter) { |
| + if (observed_.end() != observed_.find(*iter)) { |
| + (*iter)->RemoveObserver(this); |
| + observed_.erase(*iter); |
| + } |
| + } |
| + } |
| + |
| + // Calculates the set of items in |set_b|, but not in |set_a|. |
| + std::set<aura::Window*> RelativeComplement(std::set<aura::Window*> set_a, |
|
sky
2013/11/04 21:33:23
Does std offer some function that does this?
sky
2013/11/04 21:34:01
const std::set&
rharrison
2013/11/06 19:26:17
Apparently it is std::set_difference... I probably
|
| + std::set<aura::Window*> set_b) { |
| + std::set<aura::Window*> ret_val; |
| + for (std::set<aura::Window*>::iterator iter = set_b.begin(); |
| + iter != set_b.end(); |
| + ++iter) { |
| + if (set_a.end() == set_a.find(*iter)) |
| + ret_val.insert(*iter); |
| + } |
| + return ret_val; |
| + } |
| + |
| + WebContentsViewAura* view_; |
| + std::set<aura::Window*> observed_; |
| DISALLOW_COPY_AND_ASSIGN(WindowObserver); |
| }; |