Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / services / view_manager / server_view.cc
1 // Copyright 2014 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 "mojo/services/view_manager/server_view.h"
6
7 #include "mojo/services/view_manager/server_view_delegate.h"
8
9 namespace mojo {
10 namespace service {
11
12 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id)
13     : delegate_(delegate), id_(id), parent_(NULL), visible_(true) {
14   DCHECK(delegate);  // Must provide a delegate.
15 }
16
17 ServerView::~ServerView() {
18   while (!children_.empty())
19     children_.front()->parent()->Remove(children_.front());
20
21   if (parent_)
22     parent_->Remove(this);
23
24   delegate_->OnViewDestroyed(this);
25 }
26
27 void ServerView::Add(ServerView* child) {
28   // We assume validation checks happened already.
29   DCHECK(child);
30   DCHECK(child != this);
31   DCHECK(!child->Contains(this));
32   if (child->parent() == this) {
33     if (children_.size() == 1)
34       return;  // Already in the right position.
35     Reorder(child, children_.back(), ORDER_DIRECTION_ABOVE);
36     return;
37   }
38
39   const ServerView* old_parent = child->parent();
40   child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent);
41   if (child->parent())
42     child->parent()->RemoveImpl(child);
43
44   child->parent_ = this;
45   children_.push_back(child);
46   child->delegate_->OnViewHierarchyChanged(child, this, old_parent);
47 }
48
49 void ServerView::Remove(ServerView* child) {
50   // We assume validation checks happened else where.
51   DCHECK(child);
52   DCHECK(child != this);
53   DCHECK(child->parent() == this);
54
55   child->delegate_->OnWillChangeViewHierarchy(child, NULL, this);
56   RemoveImpl(child);
57   child->delegate_->OnViewHierarchyChanged(child, NULL, this);
58 }
59
60 void ServerView::Reorder(ServerView* child,
61                          ServerView* relative,
62                          OrderDirection direction) {
63   // We assume validation checks happened else where.
64   DCHECK(child);
65   DCHECK(child->parent() == this);
66   DCHECK_GT(children_.size(), 1u);
67   children_.erase(std::find(children_.begin(), children_.end(), child));
68   Views::iterator i = std::find(children_.begin(), children_.end(), relative);
69   if (direction == ORDER_DIRECTION_ABOVE) {
70     DCHECK(i != children_.end());
71     children_.insert(++i, child);
72   } else if (direction == ORDER_DIRECTION_BELOW) {
73     DCHECK(i != children_.end());
74     children_.insert(i, child);
75   }
76 }
77
78 void ServerView::SetBounds(const gfx::Rect& bounds) {
79   if (bounds_ == bounds)
80     return;
81
82   const gfx::Rect old_bounds = bounds_;
83   bounds_ = bounds;
84   delegate_->OnViewBoundsChanged(this, old_bounds, bounds);
85 }
86
87 const ServerView* ServerView::GetRoot() const {
88   const ServerView* view = this;
89   while (view && view->parent())
90     view = view->parent();
91   return view;
92 }
93
94 std::vector<const ServerView*> ServerView::GetChildren() const {
95   std::vector<const ServerView*> children;
96   children.reserve(children_.size());
97   for (size_t i = 0; i < children_.size(); ++i)
98     children.push_back(children_[i]);
99   return children;
100 }
101
102 std::vector<ServerView*> ServerView::GetChildren() {
103   // TODO(sky): rename to children() and fix return type.
104   return children_;
105 }
106
107 bool ServerView::Contains(const ServerView* view) const {
108   for (const ServerView* parent = view; parent; parent = parent->parent_) {
109     if (parent == this)
110       return true;
111   }
112   return false;
113 }
114
115 void ServerView::SetVisible(bool value) {
116   if (visible_ == value)
117     return;
118
119   delegate_->OnWillChangeViewVisibility(this);
120   visible_ = value;
121 }
122
123 bool ServerView::IsDrawn(const ServerView* root) const {
124   if (!root->visible_)
125     return false;
126   const ServerView* view = this;
127   while (view && view != root && view->visible_)
128     view = view->parent_;
129   return view == root;
130 }
131
132 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) {
133   surface_id_ = surface_id;
134   delegate_->OnViewSurfaceIdChanged(this);
135 }
136
137 void ServerView::RemoveImpl(ServerView* view) {
138   view->parent_ = NULL;
139   children_.erase(std::find(children_.begin(), children_.end(), view));
140 }
141
142 }  // namespace service
143 }  // namespace mojo