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 "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 MOJO_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 MOJO_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 == OrderDirection::ABOVE && child_i == target_i + 1) || | |
133 (direction == OrderDirection::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 == OrderDirection::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 MOJO_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 LocalSetVisible(value); | |
224 } | |
225 | |
226 void View::SetSharedProperty(const std::string& name, | |
227 const std::vector<uint8_t>* value) { | |
228 std::vector<uint8_t> old_value; | |
229 std::vector<uint8_t>* old_value_ptr = nullptr; | |
230 auto it = properties_.find(name); | |
231 if (it != properties_.end()) { | |
232 old_value = it->second; | |
233 old_value_ptr = &old_value; | |
234 | |
235 if (value && old_value == *value) | |
236 return; | |
237 } else if (!value) { | |
238 // This property isn't set in |properties_| and |value| is NULL, so there's | |
239 // no change. | |
240 return; | |
241 } | |
242 | |
243 if (value) { | |
244 properties_[name] = *value; | |
245 } else if (it != properties_.end()) { | |
246 properties_.erase(it); | |
247 } | |
248 | |
249 // TODO: add test coverage of this (450303). | |
250 if (manager_) { | |
251 Array<uint8_t> transport_value; | |
252 if (value) { | |
253 transport_value.resize(value->size()); | |
254 if (value->size()) | |
255 memcpy(&transport_value.front(), &(value->front()), value->size()); | |
256 } | |
257 static_cast<ViewManagerClientImpl*>(manager_)->SetProperty( | |
258 id_, name, transport_value.Pass()); | |
259 } | |
260 | |
261 FOR_EACH_OBSERVER( | |
262 ViewObserver, observers_, | |
263 OnViewSharedPropertyChanged(this, name, old_value_ptr, value)); | |
264 } | |
265 | |
266 bool View::IsDrawn() const { | |
267 if (!visible_) | |
268 return false; | |
269 return parent_ ? parent_->IsDrawn() : drawn_; | |
270 } | |
271 | |
272 void View::AddObserver(ViewObserver* observer) { | |
273 observers_.AddObserver(observer); | |
274 } | |
275 | |
276 void View::RemoveObserver(ViewObserver* observer) { | |
277 observers_.RemoveObserver(observer); | |
278 } | |
279 | |
280 const View* View::GetRoot() const { | |
281 const View* root = this; | |
282 for (const View* parent = this; parent; parent = parent->parent()) | |
283 root = parent; | |
284 return root; | |
285 } | |
286 | |
287 void View::AddChild(View* child) { | |
288 // TODO(beng): not necessarily valid to all connections, but possibly to the | |
289 // embeddee in an embedder-embeddee relationship. | |
290 if (manager_) | |
291 CHECK_EQ(child->view_manager(), manager_); | |
292 LocalAddChild(child); | |
293 if (manager_) | |
294 static_cast<ViewManagerClientImpl*>(manager_)->AddChild(child->id(), id_); | |
295 } | |
296 | |
297 void View::RemoveChild(View* child) { | |
298 // TODO(beng): not necessarily valid to all connections, but possibly to the | |
299 // embeddee in an embedder-embeddee relationship. | |
300 if (manager_) | |
301 CHECK_EQ(child->view_manager(), manager_); | |
302 LocalRemoveChild(child); | |
303 if (manager_) { | |
304 static_cast<ViewManagerClientImpl*>(manager_)->RemoveChild(child->id(), | |
305 id_); | |
306 } | |
307 } | |
308 | |
309 void View::MoveToFront() { | |
310 if (!parent_ || parent_->children_.back() == this) | |
311 return; | |
312 Reorder(parent_->children_.back(), OrderDirection::ABOVE); | |
313 } | |
314 | |
315 void View::MoveToBack() { | |
316 if (!parent_ || parent_->children_.front() == this) | |
317 return; | |
318 Reorder(parent_->children_.front(), OrderDirection::BELOW); | |
319 } | |
320 | |
321 void View::Reorder(View* relative, OrderDirection direction) { | |
322 if (!LocalReorder(relative, direction)) | |
323 return; | |
324 if (manager_) { | |
325 static_cast<ViewManagerClientImpl*>(manager_)->Reorder(id_, | |
326 relative->id(), | |
327 direction); | |
328 } | |
329 } | |
330 | |
331 bool View::Contains(View* child) const { | |
332 if (!child) | |
333 return false; | |
334 if (child == this) | |
335 return true; | |
336 if (manager_) | |
337 CHECK_EQ(child->view_manager(), manager_); | |
338 for (View* p = child->parent(); p; p = p->parent()) { | |
339 if (p == this) | |
340 return true; | |
341 } | |
342 return false; | |
343 } | |
344 | |
345 View* View::GetChildById(Id id) { | |
346 if (id == id_) | |
347 return this; | |
348 // TODO(beng): this could be improved depending on how we decide to own views. | |
349 Children::const_iterator it = children_.begin(); | |
350 for (; it != children_.end(); ++it) { | |
351 View* view = (*it)->GetChildById(id); | |
352 if (view) | |
353 return view; | |
354 } | |
355 return NULL; | |
356 } | |
357 | |
358 void View::SetSurfaceId(SurfaceIdPtr id) { | |
359 if (manager_) { | |
360 static_cast<ViewManagerClientImpl*>(manager_)->SetSurfaceId(id_, id.Pass()); | |
361 } | |
362 } | |
363 | |
364 void View::SetFocus() { | |
365 if (manager_) | |
366 static_cast<ViewManagerClientImpl*>(manager_)->SetFocus(id_); | |
367 } | |
368 | |
369 void View::Embed(const String& url) { | |
370 if (PrepareForEmbed()) | |
371 static_cast<ViewManagerClientImpl*>(manager_)->Embed(url, id_); | |
372 } | |
373 | |
374 void View::Embed(const String& url, | |
375 InterfaceRequest<ServiceProvider> services, | |
376 ServiceProviderPtr exposed_services) { | |
377 if (PrepareForEmbed()) { | |
378 static_cast<ViewManagerClientImpl*>(manager_) | |
379 ->Embed(url, id_, services.Pass(), exposed_services.Pass()); | |
380 } | |
381 } | |
382 | |
383 void View::Embed(ViewManagerClientPtr client) { | |
384 if (PrepareForEmbed()) | |
385 static_cast<ViewManagerClientImpl*>(manager_)->Embed(id_, client.Pass()); | |
386 } | |
387 | |
388 //////////////////////////////////////////////////////////////////////////////// | |
389 // View, protected: | |
390 | |
391 namespace { | |
392 | |
393 ViewportMetricsPtr CreateEmptyViewportMetrics() { | |
394 ViewportMetricsPtr metrics = ViewportMetrics::New(); | |
395 metrics->size = Size::New(); | |
396 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it | |
397 // once that's fixed. | |
398 return metrics.Pass(); | |
399 } | |
400 | |
401 } // namespace | |
402 | |
403 View::View() | |
404 : manager_(NULL), | |
405 id_(static_cast<Id>(-1)), | |
406 parent_(NULL), | |
407 viewport_metrics_(CreateEmptyViewportMetrics()), | |
408 visible_(true), | |
409 drawn_(false) { | |
410 } | |
411 | |
412 View::~View() { | |
413 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroying(this)); | |
414 if (parent_) | |
415 parent_->LocalRemoveChild(this); | |
416 | |
417 // We may still have children. This can happen if the embedder destroys the | |
418 // root while we're still alive. | |
419 while (!children_.empty()) { | |
420 View* child = children_.front(); | |
421 LocalRemoveChild(child); | |
422 DCHECK(children_.empty() || children_.front() != child); | |
423 } | |
424 | |
425 // TODO(beng): It'd be better to do this via a destruction observer in the | |
426 // ViewManagerClientImpl. | |
427 if (manager_) | |
428 static_cast<ViewManagerClientImpl*>(manager_)->RemoveView(id_); | |
429 | |
430 // Clear properties. | |
431 for (auto& pair : prop_map_) { | |
432 if (pair.second.deallocator) | |
433 (*pair.second.deallocator)(pair.second.value); | |
434 } | |
435 prop_map_.clear(); | |
436 | |
437 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroyed(this)); | |
438 } | |
439 | |
440 //////////////////////////////////////////////////////////////////////////////// | |
441 // View, private: | |
442 | |
443 View::View(ViewManager* manager, Id id) | |
444 : manager_(manager), | |
445 id_(id), | |
446 parent_(nullptr), | |
447 viewport_metrics_(CreateEmptyViewportMetrics()), | |
448 visible_(false), | |
449 drawn_(false) { | |
450 } | |
451 | |
452 int64 View::SetLocalPropertyInternal(const void* key, | |
453 const char* name, | |
454 PropertyDeallocator deallocator, | |
455 int64 value, | |
456 int64 default_value) { | |
457 int64 old = GetLocalPropertyInternal(key, default_value); | |
458 if (value == default_value) { | |
459 prop_map_.erase(key); | |
460 } else { | |
461 Value prop_value; | |
462 prop_value.name = name; | |
463 prop_value.value = value; | |
464 prop_value.deallocator = deallocator; | |
465 prop_map_[key] = prop_value; | |
466 } | |
467 FOR_EACH_OBSERVER(ViewObserver, observers_, | |
468 OnViewLocalPropertyChanged(this, key, old)); | |
469 return old; | |
470 } | |
471 | |
472 int64 View::GetLocalPropertyInternal(const void* key, | |
473 int64 default_value) const { | |
474 std::map<const void*, Value>::const_iterator iter = prop_map_.find(key); | |
475 if (iter == prop_map_.end()) | |
476 return default_value; | |
477 return iter->second.value; | |
478 } | |
479 | |
480 void View::LocalDestroy() { | |
481 delete this; | |
482 } | |
483 | |
484 void View::LocalAddChild(View* child) { | |
485 ScopedTreeNotifier notifier(child, child->parent(), this); | |
486 if (child->parent()) | |
487 RemoveChildImpl(child, &child->parent_->children_); | |
488 children_.push_back(child); | |
489 child->parent_ = this; | |
490 } | |
491 | |
492 void View::LocalRemoveChild(View* child) { | |
493 DCHECK_EQ(this, child->parent()); | |
494 ScopedTreeNotifier notifier(child, this, NULL); | |
495 RemoveChildImpl(child, &children_); | |
496 } | |
497 | |
498 bool View::LocalReorder(View* relative, OrderDirection direction) { | |
499 return ReorderImpl(&parent_->children_, this, relative, direction); | |
500 } | |
501 | |
502 void View::LocalSetBounds(const Rect& old_bounds, | |
503 const Rect& new_bounds) { | |
504 DCHECK(old_bounds.x == bounds_.x); | |
505 DCHECK(old_bounds.y == bounds_.y); | |
506 DCHECK(old_bounds.width == bounds_.width); | |
507 DCHECK(old_bounds.height == bounds_.height); | |
508 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds); | |
509 bounds_ = new_bounds; | |
510 } | |
511 | |
512 void View::LocalSetViewportMetrics(const ViewportMetrics& old_metrics, | |
513 const ViewportMetrics& new_metrics) { | |
514 // TODO(eseidel): We could check old_metrics against viewport_metrics_. | |
515 viewport_metrics_ = new_metrics.Clone(); | |
516 FOR_EACH_OBSERVER( | |
517 ViewObserver, observers_, | |
518 OnViewViewportMetricsChanged(this, old_metrics, new_metrics)); | |
519 } | |
520 | |
521 void View::LocalSetDrawn(bool value) { | |
522 if (drawn_ == value) | |
523 return; | |
524 | |
525 // As IsDrawn() is derived from |visible_| and |drawn_|, only send drawn | |
526 // notification is the value of IsDrawn() is really changing. | |
527 if (IsDrawn() == value) { | |
528 drawn_ = value; | |
529 return; | |
530 } | |
531 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanging(this)); | |
532 drawn_ = value; | |
533 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDrawnChanged(this)); | |
534 } | |
535 | |
536 void View::LocalSetVisible(bool visible) { | |
537 if (visible_ == visible) | |
538 return; | |
539 | |
540 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanging(this)); | |
541 visible_ = visible; | |
542 NotifyViewVisibilityChanged(this); | |
543 } | |
544 | |
545 void View::NotifyViewVisibilityChanged(View* target) { | |
546 if (!NotifyViewVisibilityChangedDown(target)) { | |
547 return; // |this| has been deleted. | |
548 } | |
549 NotifyViewVisibilityChangedUp(target); | |
550 } | |
551 | |
552 bool View::NotifyViewVisibilityChangedAtReceiver(View* target) { | |
553 // |this| may be deleted during a call to OnViewVisibilityChanged() on one | |
554 // of the observers. We create an local observer for that. In that case we | |
555 // exit without further access to any members. | |
556 ViewTracker tracker; | |
557 tracker.Add(this); | |
558 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewVisibilityChanged(target)); | |
559 return tracker.Contains(this); | |
560 } | |
561 | |
562 bool View::NotifyViewVisibilityChangedDown(View* target) { | |
563 if (!NotifyViewVisibilityChangedAtReceiver(target)) | |
564 return false; // |this| was deleted. | |
565 std::set<const View*> child_already_processed; | |
566 bool child_destroyed = false; | |
567 do { | |
568 child_destroyed = false; | |
569 for (View::Children::const_iterator it = children_.begin(); | |
570 it != children_.end(); ++it) { | |
571 if (!child_already_processed.insert(*it).second) | |
572 continue; | |
573 if (!(*it)->NotifyViewVisibilityChangedDown(target)) { | |
574 // |*it| was deleted, |it| is invalid and |children_| has changed. We | |
575 // exit the current for-loop and enter a new one. | |
576 child_destroyed = true; | |
577 break; | |
578 } | |
579 } | |
580 } while (child_destroyed); | |
581 return true; | |
582 } | |
583 | |
584 void View::NotifyViewVisibilityChangedUp(View* target) { | |
585 // Start with the parent as we already notified |this| | |
586 // in NotifyViewVisibilityChangedDown. | |
587 for (View* view = parent(); view; view = view->parent()) { | |
588 bool ret = view->NotifyViewVisibilityChangedAtReceiver(target); | |
589 DCHECK(ret); | |
590 } | |
591 } | |
592 | |
593 bool View::PrepareForEmbed() { | |
594 if (!OwnsView(manager_, this)) | |
595 return false; | |
596 | |
597 while (!children_.empty()) | |
598 RemoveChild(children_[0]); | |
599 return true; | |
600 } | |
601 | |
602 } // namespace mojo | |
OLD | NEW |