Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / view_manager / server_view.cc
bloba852c151914686c3c77b9df255d0c57619fe6bba
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 "components/view_manager/server_view.h"
7 #include <inttypes.h>
9 #include "base/strings/stringprintf.h"
10 #include "components/view_manager/server_view_delegate.h"
11 #include "components/view_manager/server_view_observer.h"
13 namespace view_manager {
15 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id)
16 : delegate_(delegate),
17 id_(id),
18 parent_(nullptr),
19 visible_(false),
20 opacity_(1),
21 allows_reembed_(false),
22 // Don't notify newly added observers during notification. This causes
23 // problems for code that adds an observer as part of an observer
24 // notification (such as ServerViewDrawTracker).
25 observers_(base::ObserverList<ServerViewObserver>::NOTIFY_EXISTING_ONLY) {
26 DCHECK(delegate); // Must provide a delegate.
29 ServerView::~ServerView() {
30 delegate_->PrepareToDestroyView(this);
31 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnWillDestroyView(this));
33 while (!children_.empty())
34 children_.front()->parent()->Remove(children_.front());
36 if (parent_)
37 parent_->Remove(this);
39 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnViewDestroyed(this));
42 void ServerView::AddObserver(ServerViewObserver* observer) {
43 observers_.AddObserver(observer);
46 void ServerView::RemoveObserver(ServerViewObserver* observer) {
47 observers_.RemoveObserver(observer);
50 void ServerView::Add(ServerView* child) {
51 // We assume validation checks happened already.
52 DCHECK(child);
53 DCHECK(child != this);
54 DCHECK(!child->Contains(this));
55 if (child->parent() == this) {
56 if (children_.size() == 1)
57 return; // Already in the right position.
58 Reorder(child, children_.back(), mojo::ORDER_DIRECTION_ABOVE);
59 return;
62 ServerView* old_parent = child->parent();
63 child->delegate_->PrepareToChangeViewHierarchy(child, this, old_parent);
64 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
65 OnWillChangeViewHierarchy(child, this, old_parent));
67 if (child->parent())
68 child->parent()->RemoveImpl(child);
70 child->parent_ = this;
71 children_.push_back(child);
72 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
73 OnViewHierarchyChanged(child, this, old_parent));
76 void ServerView::Remove(ServerView* child) {
77 // We assume validation checks happened else where.
78 DCHECK(child);
79 DCHECK(child != this);
80 DCHECK(child->parent() == this);
82 child->delegate_->PrepareToChangeViewHierarchy(child, NULL, this);
83 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
84 OnWillChangeViewHierarchy(child, nullptr, this));
85 RemoveImpl(child);
86 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
87 OnViewHierarchyChanged(child, nullptr, this));
90 void ServerView::Reorder(ServerView* child,
91 ServerView* relative,
92 mojo::OrderDirection direction) {
93 // We assume validation checks happened else where.
94 DCHECK(child);
95 DCHECK(child->parent() == this);
96 DCHECK_GT(children_.size(), 1u);
97 children_.erase(std::find(children_.begin(), children_.end(), child));
98 Views::iterator i = std::find(children_.begin(), children_.end(), relative);
99 if (direction == mojo::ORDER_DIRECTION_ABOVE) {
100 DCHECK(i != children_.end());
101 children_.insert(++i, child);
102 } else if (direction == mojo::ORDER_DIRECTION_BELOW) {
103 DCHECK(i != children_.end());
104 children_.insert(i, child);
106 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
107 OnViewReordered(this, relative, direction));
110 void ServerView::SetBounds(const gfx::Rect& bounds) {
111 if (bounds_ == bounds)
112 return;
114 const gfx::Rect old_bounds = bounds_;
115 bounds_ = bounds;
116 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
117 OnViewBoundsChanged(this, old_bounds, bounds));
120 const ServerView* ServerView::GetRoot() const {
121 const ServerView* view = this;
122 while (view && view->parent())
123 view = view->parent();
124 return view;
127 std::vector<const ServerView*> ServerView::GetChildren() const {
128 std::vector<const ServerView*> children;
129 children.reserve(children_.size());
130 for (size_t i = 0; i < children_.size(); ++i)
131 children.push_back(children_[i]);
132 return children;
135 std::vector<ServerView*> ServerView::GetChildren() {
136 // TODO(sky): rename to children() and fix return type.
137 return children_;
140 bool ServerView::Contains(const ServerView* view) const {
141 for (const ServerView* parent = view; parent; parent = parent->parent_) {
142 if (parent == this)
143 return true;
145 return false;
148 void ServerView::SetVisible(bool value) {
149 if (visible_ == value)
150 return;
152 delegate_->PrepareToChangeViewVisibility(this);
153 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
154 OnWillChangeViewVisibility(this));
155 visible_ = value;
156 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
157 OnViewVisibilityChanged(this));
160 void ServerView::SetOpacity(float value) {
161 if (value == opacity_)
162 return;
163 opacity_ = value;
164 delegate_->OnScheduleViewPaint(this);
167 void ServerView::SetTransform(const gfx::Transform& transform) {
168 if (transform_ == transform)
169 return;
171 transform_ = transform;
172 delegate_->OnScheduleViewPaint(this);
175 void ServerView::SetProperty(const std::string& name,
176 const std::vector<uint8_t>* value) {
177 auto it = properties_.find(name);
178 if (it != properties_.end()) {
179 if (value && it->second == *value)
180 return;
181 } else if (!value) {
182 // This property isn't set in |properties_| and |value| is NULL, so there's
183 // no change.
184 return;
187 if (value) {
188 properties_[name] = *value;
189 } else if (it != properties_.end()) {
190 properties_.erase(it);
193 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
194 OnViewSharedPropertyChanged(this, name, value));
197 bool ServerView::IsDrawn() const {
198 return delegate_->IsViewDrawn(this);
201 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) {
202 surface_id_ = surface_id;
203 delegate_->OnScheduleViewPaint(this);
206 #if !defined(NDEBUG)
207 std::string ServerView::GetDebugWindowHierarchy() const {
208 std::string result;
209 BuildDebugInfo(std::string(), &result);
210 return result;
213 void ServerView::BuildDebugInfo(const std::string& depth,
214 std::string* result) const {
215 *result += base::StringPrintf(
216 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64 "\n",
217 depth.c_str(), static_cast<int>(id_.connection_id),
218 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(),
219 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id);
220 for (const ServerView* child : children_)
221 child->BuildDebugInfo(depth + " ", result);
223 #endif
225 void ServerView::RemoveImpl(ServerView* view) {
226 view->parent_ = NULL;
227 children_.erase(std::find(children_.begin(), children_.end(), view));
230 } // namespace view_manager