[Sync] Fix invalidations on Android.
[chromium-blink-merge.git] / mojo / services / view_manager / server_view.cc
blob6459f9adcd84c72cf5fe4ad3d323a60e9b215f96
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.
5 #include "mojo/services/view_manager/server_view.h"
7 #include <inttypes.h>
9 #include "base/strings/stringprintf.h"
10 #include "mojo/services/view_manager/server_view_delegate.h"
12 namespace view_manager {
14 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id)
15 : delegate_(delegate),
16 id_(id),
17 parent_(nullptr),
18 visible_(false),
19 opacity_(1) {
20 DCHECK(delegate); // Must provide a delegate.
23 ServerView::~ServerView() {
24 delegate_->OnWillDestroyView(this);
26 while (!children_.empty())
27 children_.front()->parent()->Remove(children_.front());
29 if (parent_)
30 parent_->Remove(this);
32 delegate_->OnViewDestroyed(this);
35 void ServerView::Add(ServerView* child) {
36 // We assume validation checks happened already.
37 DCHECK(child);
38 DCHECK(child != this);
39 DCHECK(!child->Contains(this));
40 if (child->parent() == this) {
41 if (children_.size() == 1)
42 return; // Already in the right position.
43 Reorder(child, children_.back(), mojo::ORDER_DIRECTION_ABOVE);
44 return;
47 ServerView* old_parent = child->parent();
48 child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent);
49 if (child->parent())
50 child->parent()->RemoveImpl(child);
52 child->parent_ = this;
53 children_.push_back(child);
54 child->delegate_->OnViewHierarchyChanged(child, this, old_parent);
57 void ServerView::Remove(ServerView* child) {
58 // We assume validation checks happened else where.
59 DCHECK(child);
60 DCHECK(child != this);
61 DCHECK(child->parent() == this);
63 child->delegate_->OnWillChangeViewHierarchy(child, NULL, this);
64 RemoveImpl(child);
65 child->delegate_->OnViewHierarchyChanged(child, NULL, this);
68 void ServerView::Reorder(ServerView* child,
69 ServerView* relative,
70 mojo::OrderDirection direction) {
71 // We assume validation checks happened else where.
72 DCHECK(child);
73 DCHECK(child->parent() == this);
74 DCHECK_GT(children_.size(), 1u);
75 children_.erase(std::find(children_.begin(), children_.end(), child));
76 Views::iterator i = std::find(children_.begin(), children_.end(), relative);
77 if (direction == mojo::ORDER_DIRECTION_ABOVE) {
78 DCHECK(i != children_.end());
79 children_.insert(++i, child);
80 } else if (direction == mojo::ORDER_DIRECTION_BELOW) {
81 DCHECK(i != children_.end());
82 children_.insert(i, child);
84 delegate_->OnViewReordered(this, relative, direction);
87 void ServerView::SetBounds(const gfx::Rect& bounds) {
88 if (bounds_ == bounds)
89 return;
91 const gfx::Rect old_bounds = bounds_;
92 bounds_ = bounds;
93 delegate_->OnViewBoundsChanged(this, old_bounds, bounds);
96 const ServerView* ServerView::GetRoot() const {
97 const ServerView* view = this;
98 while (view && view->parent())
99 view = view->parent();
100 return view;
103 std::vector<const ServerView*> ServerView::GetChildren() const {
104 std::vector<const ServerView*> children;
105 children.reserve(children_.size());
106 for (size_t i = 0; i < children_.size(); ++i)
107 children.push_back(children_[i]);
108 return children;
111 std::vector<ServerView*> ServerView::GetChildren() {
112 // TODO(sky): rename to children() and fix return type.
113 return children_;
116 bool ServerView::Contains(const ServerView* view) const {
117 for (const ServerView* parent = view; parent; parent = parent->parent_) {
118 if (parent == this)
119 return true;
121 return false;
124 void ServerView::SetVisible(bool value) {
125 if (visible_ == value)
126 return;
128 delegate_->OnWillChangeViewVisibility(this);
129 visible_ = value;
132 void ServerView::SetOpacity(float value) {
133 if (value == opacity_)
134 return;
135 opacity_ = value;
136 delegate_->OnScheduleViewPaint(this);
139 void ServerView::SetTransform(const gfx::Transform& transform) {
140 if (transform_ == transform)
141 return;
143 transform_ = transform;
144 delegate_->OnScheduleViewPaint(this);
147 void ServerView::SetProperty(const std::string& name,
148 const std::vector<uint8_t>* value) {
149 auto it = properties_.find(name);
150 if (it != properties_.end()) {
151 if (value && it->second == *value)
152 return;
153 } else if (!value) {
154 // This property isn't set in |properties_| and |value| is NULL, so there's
155 // no change.
156 return;
159 if (value) {
160 properties_[name] = *value;
161 } else if (it != properties_.end()) {
162 properties_.erase(it);
165 delegate_->OnViewSharedPropertyChanged(this, name, value);
168 bool ServerView::IsDrawn(const ServerView* root) const {
169 if (!root->visible_)
170 return false;
171 const ServerView* view = this;
172 while (view && view != root && view->visible_)
173 view = view->parent_;
174 return view == root;
177 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) {
178 surface_id_ = surface_id;
179 delegate_->OnViewSurfaceIdChanged(this);
182 #if !defined(NDEBUG)
183 std::string ServerView::GetDebugWindowHierarchy() const {
184 std::string result;
185 BuildDebugInfo(std::string(), &result);
186 return result;
189 void ServerView::BuildDebugInfo(const std::string& depth,
190 std::string* result) const {
191 *result += base::StringPrintf(
192 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64 "\n",
193 depth.c_str(), static_cast<int>(id_.connection_id),
194 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(),
195 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id);
196 for (const ServerView* child : children_)
197 child->BuildDebugInfo(depth + " ", result);
199 #endif
201 void ServerView::RemoveImpl(ServerView* view) {
202 view->parent_ = NULL;
203 children_.erase(std::find(children_.begin(), children_.end(), view));
206 } // namespace view_manager