Chromium Code Reviews| Index: mojo/services/public/cpp/view_manager/lib/view_manager_private.cc |
| diff --git a/mojo/services/public/cpp/view_manager/lib/view_manager_private.cc b/mojo/services/public/cpp/view_manager/lib/view_manager_private.cc |
| index e350f3426dfd42481358e7edf35b12b38c697308..c17fed358a2347be48359f45dd202e438b4ed579 100644 |
| --- a/mojo/services/public/cpp/view_manager/lib/view_manager_private.cc |
| +++ b/mojo/services/public/cpp/view_manager/lib/view_manager_private.cc |
| @@ -4,13 +4,62 @@ |
| #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" |
| +#include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" |
| +#include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" |
| + |
| namespace mojo { |
| namespace view_manager { |
| +namespace { |
| + |
| +// Responsible for removing a root from the ViewManager when that node is |
| +// destroyed. |
| +class RootObserver : public ViewTreeNodeObserver { |
| + public: |
| + explicit RootObserver(ViewTreeNode* root) : root_(root) {} |
| + virtual ~RootObserver() {} |
| + |
| + private: |
| + // Overridden from ViewTreeNodeObserver: |
| + virtual void OnNodeDestroy(ViewTreeNode* node, |
| + DispositionChangePhase phase) OVERRIDE { |
| + DCHECK_EQ(node, root_); |
| + if (phase != ViewTreeNodeObserver::DISPOSITION_CHANGED) |
| + return; |
| + ViewManagerPrivate( |
| + ViewTreeNodePrivate(root_).view_manager()).RemoveRoot(root_); |
|
sky
2014/06/05 18:18:52
nit: indent 1 more.
|
| + } |
| + |
| + ViewTreeNode* root_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RootObserver); |
| +}; |
| + |
| +} // namespace |
| ViewManagerPrivate::ViewManagerPrivate(ViewManager* manager) |
| : manager_(manager) {} |
| ViewManagerPrivate::~ViewManagerPrivate() {} |
| +void ViewManagerPrivate::AddRoot(ViewTreeNode* root) { |
| + // A new root must not already exist as a root or be contained by an existing |
| + // hierarchy visible to this view manager. |
| + std::vector<ViewTreeNode*>::const_iterator it = manager_->roots().begin(); |
| + for (; it != manager_->roots().end(); ++it) { |
| + if (*it == root || (*it)->Contains(root)) |
| + return; |
| + } |
| + manager_->roots_.push_back(root); |
| + root->AddObserver(new RootObserver(root)); |
| + manager_->root_added_callback_.Run(manager_); |
| +} |
| + |
| +void ViewManagerPrivate::RemoveRoot(ViewTreeNode* root) { |
| + std::vector<ViewTreeNode*>::iterator it = |
| + std::find(manager_->roots_.begin(), manager_->roots_.end(), root); |
| + if (it != manager_->roots_.end()) |
| + manager_->roots_.erase(it); |
| +} |
| + |
| void ViewManagerPrivate::AddNode(TransportNodeId node_id, ViewTreeNode* node) { |
| DCHECK(!manager_->nodes_[node_id]); |
| manager_->nodes_[node_id] = node; |