Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(847)

Side by Side Diff: mojo/services/view_manager/public/cpp/lib/view.cc

Issue 861683003: Move services code brought in from Mojo to live under //third_party. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "view_manager/public/cpp/view.h"
6
7 #include <set>
8 #include <string>
9
10 #include "mojo/public/cpp/application/service_provider_impl.h"
11 #include "view_manager/public/cpp/lib/view_manager_client_impl.h"
12 #include "view_manager/public/cpp/lib/view_private.h"
13 #include "view_manager/public/cpp/view_observer.h"
14 #include "view_manager/public/cpp/view_tracker.h"
15
16 namespace mojo {
17
18 namespace {
19
20 void NotifyViewTreeChangeAtReceiver(
21 View* receiver,
22 const ViewObserver::TreeChangeParams& params,
23 bool change_applied) {
24 ViewObserver::TreeChangeParams local_params = params;
25 local_params.receiver = receiver;
26 if (change_applied) {
27 FOR_EACH_OBSERVER(ViewObserver,
28 *ViewPrivate(receiver).observers(),
29 OnTreeChanged(local_params));
30 } else {
31 FOR_EACH_OBSERVER(ViewObserver,
32 *ViewPrivate(receiver).observers(),
33 OnTreeChanging(local_params));
34 }
35 }
36
37 void NotifyViewTreeChangeUp(
38 View* start_at,
39 const ViewObserver::TreeChangeParams& params,
40 bool change_applied) {
41 for (View* current = start_at; current; current = current->parent())
42 NotifyViewTreeChangeAtReceiver(current, params, change_applied);
43 }
44
45 void NotifyViewTreeChangeDown(
46 View* start_at,
47 const ViewObserver::TreeChangeParams& params,
48 bool change_applied) {
49 NotifyViewTreeChangeAtReceiver(start_at, params, change_applied);
50 View::Children::const_iterator it = start_at->children().begin();
51 for (; it != start_at->children().end(); ++it)
52 NotifyViewTreeChangeDown(*it, params, change_applied);
53 }
54
55 void NotifyViewTreeChange(
56 const ViewObserver::TreeChangeParams& params,
57 bool change_applied) {
58 NotifyViewTreeChangeDown(params.target, params, change_applied);
59 if (params.old_parent)
60 NotifyViewTreeChangeUp(params.old_parent, params, change_applied);
61 if (params.new_parent)
62 NotifyViewTreeChangeUp(params.new_parent, params, change_applied);
63 }
64
65 class ScopedTreeNotifier {
66 public:
67 ScopedTreeNotifier(View* target, View* old_parent, View* new_parent) {
68 params_.target = target;
69 params_.old_parent = old_parent;
70 params_.new_parent = new_parent;
71 NotifyViewTreeChange(params_, false);
72 }
73 ~ScopedTreeNotifier() {
74 NotifyViewTreeChange(params_, true);
75 }
76
77 private:
78 ViewObserver::TreeChangeParams params_;
79
80 DISALLOW_COPY_AND_ASSIGN(ScopedTreeNotifier);
81 };
82
83 void RemoveChildImpl(View* child, View::Children* children) {
84 View::Children::iterator it =
85 std::find(children->begin(), children->end(), child);
86 if (it != children->end()) {
87 children->erase(it);
88 ViewPrivate(child).ClearParent();
89 }
90 }
91
92 class ScopedOrderChangedNotifier {
93 public:
94 ScopedOrderChangedNotifier(View* view,
95 View* relative_view,
96 OrderDirection direction)
97 : view_(view),
98 relative_view_(relative_view),
99 direction_(direction) {
100 FOR_EACH_OBSERVER(ViewObserver,
101 *ViewPrivate(view_).observers(),
102 OnViewReordering(view_, relative_view_, direction_));
103 }
104 ~ScopedOrderChangedNotifier() {
105 FOR_EACH_OBSERVER(ViewObserver,
106 *ViewPrivate(view_).observers(),
107 OnViewReordered(view_, relative_view_, direction_));
108 }
109
110 private:
111 View* view_;
112 View* relative_view_;
113 OrderDirection direction_;
114
115 DISALLOW_COPY_AND_ASSIGN(ScopedOrderChangedNotifier);
116 };
117
118 // Returns true if the order actually changed.
119 bool ReorderImpl(View::Children* children,
120 View* view,
121 View* relative,
122 OrderDirection direction) {
123 DCHECK(relative);
124 DCHECK_NE(view, relative);
125 DCHECK_EQ(view->parent(), relative->parent());
126
127 const size_t child_i =
128 std::find(children->begin(), children->end(), view) - children->begin();
129 const size_t target_i =
130 std::find(children->begin(), children->end(), relative) -
131 children->begin();
132 if ((direction == ORDER_DIRECTION_ABOVE && child_i == target_i + 1) ||
133 (direction == ORDER_DIRECTION_BELOW && child_i + 1 == target_i)) {
134 return false;
135 }
136
137 ScopedOrderChangedNotifier notifier(view, relative, direction);
138
139 const size_t dest_i = direction == ORDER_DIRECTION_ABOVE
140 ? (child_i < target_i ? target_i : target_i + 1)
141 : (child_i < target_i ? target_i - 1 : target_i);
142 children->erase(children->begin() + child_i);
143 children->insert(children->begin() + dest_i, view);
144
145 return true;
146 }
147
148 class ScopedSetBoundsNotifier {
149 public:
150 ScopedSetBoundsNotifier(View* view,
151 const Rect& old_bounds,
152 const Rect& new_bounds)
153 : view_(view),
154 old_bounds_(old_bounds),
155 new_bounds_(new_bounds) {
156 FOR_EACH_OBSERVER(ViewObserver,
157 *ViewPrivate(view_).observers(),
158 OnViewBoundsChanging(view_, old_bounds_, new_bounds_));
159 }
160 ~ScopedSetBoundsNotifier() {
161 FOR_EACH_OBSERVER(ViewObserver,
162 *ViewPrivate(view_).observers(),
163 OnViewBoundsChanged(view_, old_bounds_, new_bounds_));
164 }
165
166 private:
167 View* view_;
168 const Rect old_bounds_;
169 const Rect new_bounds_;
170
171 DISALLOW_COPY_AND_ASSIGN(ScopedSetBoundsNotifier);
172 };
173
174 // Some operations are only permitted in the connection that created the view.
175 bool OwnsView(ViewManager* manager, View* view) {
176 return !manager ||
177 static_cast<ViewManagerClientImpl*>(manager)->OwnsView(view->id());
178 }
179
180 } // namespace
181
182 ////////////////////////////////////////////////////////////////////////////////
183 // View, public:
184
185 void View::Destroy() {
186 if (!OwnsView(manager_, this))
187 return;
188
189 if (manager_)
190 static_cast<ViewManagerClientImpl*>(manager_)->DestroyView(id_);
191 while (!children_.empty()) {
192 View* child = children_.front();
193 if (!OwnsView(manager_, child)) {
194 ViewPrivate(child).ClearParent();
195 children_.erase(children_.begin());
196 } else {
197 child->Destroy();
198 DCHECK(std::find(children_.begin(), children_.end(), child) ==
199 children_.end());
200 }
201 }
202 LocalDestroy();
203 }
204
205 void View::SetBounds(const Rect& bounds) {
206 if (!OwnsView(manager_, this))
207 return;
208
209 if (bounds_.Equals(bounds))
210 return;
211
212 if (manager_)
213 static_cast<ViewManagerClientImpl*>(manager_)->SetBounds(id_, bounds);
214 LocalSetBounds(bounds_, bounds);
215 }
216
217 void View::SetVisible(bool value) {
218 if (visible_ == value)
219 return;
220
221 if (manager_)
222 static_cast<ViewManagerClientImpl*>(manager_)->SetVisible(id_, value);
223 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanging(this));
224 visible_ = value;
225 NotifyViewVisibilityChanged(this);
226 }
227
228 void View::SetSharedProperty(const std::string& name,
229 const std::vector<uint8_t>* value) {
230 std::vector<uint8_t> old_value;
231 std::vector<uint8_t>* old_value_ptr = nullptr;
232 auto it = properties_.find(name);
233 if (it != properties_.end()) {
234 old_value = it->second;
235 old_value_ptr = &old_value;
236
237 if (value && old_value == *value)
238 return;
239 } else if (!value) {
240 // This property isn't set in |properties_| and |value| is NULL, so there's
241 // no change.
242 return;
243 }
244
245 if (value) {
246 properties_[name] = *value;
247 } else if (it != properties_.end()) {
248 properties_.erase(it);
249 }
250
251 // TODO: add test coverage of this (450303).
252 if (manager_) {
253 Array<uint8_t> transport_value;
254 if (value) {
255 transport_value.resize(value->size());
256 if (value->size())
257 memcpy(&transport_value.front(), &(value->front()), value->size());
258 }
259 static_cast<ViewManagerClientImpl*>(manager_)->SetProperty(
260 id_, name, transport_value.Pass());
261 }
262
263 FOR_EACH_OBSERVER(
264 ViewObserver, observers_,
265 OnViewSharedPropertyChanged(this, name, old_value_ptr, value));
266 }
267
268 bool View::IsDrawn() const {
269 if (!visible_)
270 return false;
271 return parent_ ? parent_->IsDrawn() : drawn_;
272 }
273
274 void View::AddObserver(ViewObserver* observer) {
275 observers_.AddObserver(observer);
276 }
277
278 void View::RemoveObserver(ViewObserver* observer) {
279 observers_.RemoveObserver(observer);
280 }
281
282 const View* View::GetRoot() const {
283 const View* root = this;
284 for (const View* parent = this; parent; parent = parent->parent())
285 root = parent;
286 return root;
287 }
288
289 void View::AddChild(View* child) {
290 // TODO(beng): not necessarily valid to all connections, but possibly to the
291 // embeddee in an embedder-embeddee relationship.
292 if (manager_)
293 CHECK_EQ(child->view_manager(), manager_);
294 LocalAddChild(child);
295 if (manager_)
296 static_cast<ViewManagerClientImpl*>(manager_)->AddChild(child->id(), id_);
297 }
298
299 void View::RemoveChild(View* child) {
300 // TODO(beng): not necessarily valid to all connections, but possibly to the
301 // embeddee in an embedder-embeddee relationship.
302 if (manager_)
303 CHECK_EQ(child->view_manager(), manager_);
304 LocalRemoveChild(child);
305 if (manager_) {
306 static_cast<ViewManagerClientImpl*>(manager_)->RemoveChild(child->id(),
307 id_);
308 }
309 }
310
311 void View::MoveToFront() {
312 if (!parent_ || parent_->children_.back() == this)
313 return;
314 Reorder(parent_->children_.back(), ORDER_DIRECTION_ABOVE);
315 }
316
317 void View::MoveToBack() {
318 if (!parent_ || parent_->children_.front() == this)
319 return;
320 Reorder(parent_->children_.front(), ORDER_DIRECTION_BELOW);
321 }
322
323 void View::Reorder(View* relative, OrderDirection direction) {
324 if (!LocalReorder(relative, direction))
325 return;
326 if (manager_) {
327 static_cast<ViewManagerClientImpl*>(manager_)->Reorder(id_,
328 relative->id(),
329 direction);
330 }
331 }
332
333 bool View::Contains(View* child) const {
334 if (!child)
335 return false;
336 if (child == this)
337 return true;
338 if (manager_)
339 CHECK_EQ(child->view_manager(), manager_);
340 for (View* p = child->parent(); p; p = p->parent()) {
341 if (p == this)
342 return true;
343 }
344 return false;
345 }
346
347 View* View::GetChildById(Id id) {
348 if (id == id_)
349 return this;
350 // TODO(beng): this could be improved depending on how we decide to own views.
351 Children::const_iterator it = children_.begin();
352 for (; it != children_.end(); ++it) {
353 View* view = (*it)->GetChildById(id);
354 if (view)
355 return view;
356 }
357 return NULL;
358 }
359
360 void View::SetSurfaceId(SurfaceIdPtr id) {
361 if (manager_) {
362 static_cast<ViewManagerClientImpl*>(manager_)->SetSurfaceId(id_, id.Pass());
363 }
364 }
365
366 void View::SetFocus() {
367 if (manager_)
368 static_cast<ViewManagerClientImpl*>(manager_)->SetFocus(id_);
369 }
370
371 void View::Embed(const String& url) {
372 static_cast<ViewManagerClientImpl*>(manager_)->Embed(url, id_);
373 }
374
375 void View::Embed(const String& url,
376 InterfaceRequest<ServiceProvider> services,
377 ServiceProviderPtr exposed_services) {
378 static_cast<ViewManagerClientImpl*>(manager_)
379 ->Embed(url, id_, services.Pass(), exposed_services.Pass());
380 }
381
382 ////////////////////////////////////////////////////////////////////////////////
383 // View, protected:
384
385 namespace {
386
387 ViewportMetricsPtr CreateEmptyViewportMetrics() {
388 ViewportMetricsPtr metrics = ViewportMetrics::New();
389 metrics->size = Size::New();
390 return metrics;
391 }
392 }
393
394 View::View()
395 : manager_(NULL),
396 id_(static_cast<Id>(-1)),
397 parent_(NULL),
398 viewport_metrics_(CreateEmptyViewportMetrics()),
399 visible_(true),
400 drawn_(false) {
401 }
402
403 View::~View() {
404 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroying(this));
405 if (parent_)
406 parent_->LocalRemoveChild(this);
407
408 // We may still have children. This can happen if the embedder destroys the
409 // root while we're still alive.
410 while (!children_.empty()) {
411 View* child = children_.front();
412 LocalRemoveChild(child);
413 DCHECK(children_.empty() || children_.front() != child);
414 }
415
416 // TODO(beng): It'd be better to do this via a destruction observer in the
417 // ViewManagerClientImpl.
418 if (manager_)
419 static_cast<ViewManagerClientImpl*>(manager_)->RemoveView(id_);
420
421 // Clear properties.
422 for (auto& pair : prop_map_) {
423 if (pair.second.deallocator)
424 (*pair.second.deallocator)(pair.second.value);
425 }
426 prop_map_.clear();
427
428 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroyed(this));
429 }
430
431 ////////////////////////////////////////////////////////////////////////////////
432 // View, private:
433
434 View::View(ViewManager* manager, Id id)
435 : manager_(manager),
436 id_(id),
437 parent_(nullptr),
438 viewport_metrics_(CreateEmptyViewportMetrics()),
439 visible_(false),
440 drawn_(false) {
441 }
442
443 int64 View::SetLocalPropertyInternal(const void* key,
444 const char* name,
445 PropertyDeallocator deallocator,
446 int64 value,
447 int64 default_value) {
448 int64 old = GetLocalPropertyInternal(key, default_value);
449 if (value == default_value) {
450 prop_map_.erase(key);
451 } else {
452 Value prop_value;
453 prop_value.name = name;
454 prop_value.value = value;
455 prop_value.deallocator = deallocator;
456 prop_map_[key] = prop_value;
457 }
458 FOR_EACH_OBSERVER(ViewObserver, observers_,
459 OnViewLocalPropertyChanged(this, key, old));
460 return old;
461 }
462
463 int64 View::GetLocalPropertyInternal(const void* key,
464 int64 default_value) const {
465 std::map<const void*, Value>::const_iterator iter = prop_map_.find(key);
466 if (iter == prop_map_.end())
467 return default_value;
468 return iter->second.value;
469 }
470
471 void View::LocalDestroy() {
472 delete this;
473 }
474
475 void View::LocalAddChild(View* child) {
476 ScopedTreeNotifier notifier(child, child->parent(), this);
477 if (child->parent())
478 RemoveChildImpl(child, &child->parent_->children_);
479 children_.push_back(child);
480 child->parent_ = this;
481 }
482
483 void View::LocalRemoveChild(View* child) {
484 DCHECK_EQ(this, child->parent());
485 ScopedTreeNotifier notifier(child, this, NULL);
486 RemoveChildImpl(child, &children_);
487 }
488
489 bool View::LocalReorder(View* relative, OrderDirection direction) {
490 return ReorderImpl(&parent_->children_, this, relative, direction);
491 }
492
493 void View::LocalSetBounds(const Rect& old_bounds,
494 const Rect& new_bounds) {
495 DCHECK(old_bounds.x == bounds_.x);
496 DCHECK(old_bounds.y == bounds_.y);
497 DCHECK(old_bounds.width == bounds_.width);
498 DCHECK(old_bounds.height == bounds_.height);
499 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds);
500 bounds_ = new_bounds;
501 }
502
503 void View::LocalSetViewportMetrics(const ViewportMetrics& old_metrics,
504 const ViewportMetrics& new_metrics) {
505 // TODO(eseidel): We could check old_metrics against viewport_metrics_.
506 viewport_metrics_ = new_metrics.Clone();
507 FOR_EACH_OBSERVER(
508 ViewObserver, observers_,
509 OnViewViewportMetricsChanged(this, old_metrics, new_metrics));
510 }
511
512 void View::LocalSetDrawn(bool value) {
513 if (drawn_ == value)
514 return;
515
516 // As IsDrawn() is derived from |visible_| and |drawn_|, only send drawn
517 // notification is the value of IsDrawn() is really changing.
518 if (IsDrawn() == value) {
519 drawn_ = value;
520 return;
521 }
522 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanging(this));
523 drawn_ = value;
524 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanged(this));
525 }
526
527 void View::NotifyViewVisibilityChanged(View* target) {
528 if (!NotifyViewVisibilityChangedDown(target)) {
529 return; // |this| has been deleted.
530 }
531 NotifyViewVisibilityChangedUp(target);
532 }
533
534 bool View::NotifyViewVisibilityChangedAtReceiver(View* target) {
535 // |this| may be deleted during a call to OnViewVisibilityChanged() on one
536 // of the observers. We create an local observer for that. In that case we
537 // exit without further access to any members.
538 ViewTracker tracker;
539 tracker.Add(this);
540 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanged(target));
541 return tracker.Contains(this);
542 }
543
544 bool View::NotifyViewVisibilityChangedDown(View* target) {
545 if (!NotifyViewVisibilityChangedAtReceiver(target))
546 return false; // |this| was deleted.
547 std::set<const View*> child_already_processed;
548 bool child_destroyed = false;
549 do {
550 child_destroyed = false;
551 for (View::Children::const_iterator it = children_.begin();
552 it != children_.end(); ++it) {
553 if (!child_already_processed.insert(*it).second)
554 continue;
555 if (!(*it)->NotifyViewVisibilityChangedDown(target)) {
556 // |*it| was deleted, |it| is invalid and |children_| has changed. We
557 // exit the current for-loop and enter a new one.
558 child_destroyed = true;
559 break;
560 }
561 }
562 } while (child_destroyed);
563 return true;
564 }
565
566 void View::NotifyViewVisibilityChangedUp(View* target) {
567 // Start with the parent as we already notified |this|
568 // in NotifyViewVisibilityChangedDown.
569 for (View* view = parent(); view; view = view->parent()) {
570 bool ret = view->NotifyViewVisibilityChangedAtReceiver(target);
571 DCHECK(ret);
572 }
573 }
574
575 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698