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