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/public/cpp/view_manager/view.h"
7 #include "mojo/public/cpp/application/connect.h"
8 #include "mojo/public/cpp/application/service_provider_impl.h"
9 #include "mojo/public/interfaces/application/shell.mojom.h"
10 #include "mojo/services/public/cpp/view_manager/lib/bitmap_uploader.h"
11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h"
12 #include "mojo/services/public/cpp/view_manager/lib/view_private.h"
13 #include "mojo/services/public/cpp/view_manager/view_observer.h"
14 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h"
15 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h"
16 #include "ui/gfx/canvas.h"
22 void NotifyViewTreeChangeAtReceiver(
24 const ViewObserver::TreeChangeParams& params,
25 bool change_applied) {
26 ViewObserver::TreeChangeParams local_params = params;
27 local_params.receiver = receiver;
29 FOR_EACH_OBSERVER(ViewObserver,
30 *ViewPrivate(receiver).observers(),
31 OnTreeChanged(local_params));
33 FOR_EACH_OBSERVER(ViewObserver,
34 *ViewPrivate(receiver).observers(),
35 OnTreeChanging(local_params));
39 void NotifyViewTreeChangeUp(
41 const ViewObserver::TreeChangeParams& params,
42 bool change_applied) {
43 for (View* current = start_at; current; current = current->parent())
44 NotifyViewTreeChangeAtReceiver(current, params, change_applied);
47 void NotifyViewTreeChangeDown(
49 const ViewObserver::TreeChangeParams& params,
50 bool change_applied) {
51 NotifyViewTreeChangeAtReceiver(start_at, params, change_applied);
52 View::Children::const_iterator it = start_at->children().begin();
53 for (; it != start_at->children().end(); ++it)
54 NotifyViewTreeChangeDown(*it, params, change_applied);
57 void NotifyViewTreeChange(
58 const ViewObserver::TreeChangeParams& params,
59 bool change_applied) {
60 NotifyViewTreeChangeDown(params.target, params, change_applied);
61 if (params.old_parent)
62 NotifyViewTreeChangeUp(params.old_parent, params, change_applied);
63 if (params.new_parent)
64 NotifyViewTreeChangeUp(params.new_parent, params, change_applied);
67 class ScopedTreeNotifier {
69 ScopedTreeNotifier(View* target, View* old_parent, View* new_parent) {
70 params_.target = target;
71 params_.old_parent = old_parent;
72 params_.new_parent = new_parent;
73 NotifyViewTreeChange(params_, false);
75 ~ScopedTreeNotifier() {
76 NotifyViewTreeChange(params_, true);
80 ViewObserver::TreeChangeParams params_;
82 DISALLOW_COPY_AND_ASSIGN(ScopedTreeNotifier);
85 void RemoveChildImpl(View* child, View::Children* children) {
86 View::Children::iterator it =
87 std::find(children->begin(), children->end(), child);
88 if (it != children->end()) {
90 ViewPrivate(child).ClearParent();
94 class ScopedOrderChangedNotifier {
96 ScopedOrderChangedNotifier(View* view,
98 OrderDirection direction)
100 relative_view_(relative_view),
101 direction_(direction) {
102 FOR_EACH_OBSERVER(ViewObserver,
103 *ViewPrivate(view_).observers(),
104 OnViewReordering(view_, relative_view_, direction_));
106 ~ScopedOrderChangedNotifier() {
107 FOR_EACH_OBSERVER(ViewObserver,
108 *ViewPrivate(view_).observers(),
109 OnViewReordered(view_, relative_view_, direction_));
114 View* relative_view_;
115 OrderDirection direction_;
117 DISALLOW_COPY_AND_ASSIGN(ScopedOrderChangedNotifier);
120 // Returns true if the order actually changed.
121 bool ReorderImpl(View::Children* children,
124 OrderDirection direction) {
126 DCHECK_NE(view, relative);
127 DCHECK_EQ(view->parent(), relative->parent());
129 const size_t child_i =
130 std::find(children->begin(), children->end(), view) - children->begin();
131 const size_t target_i =
132 std::find(children->begin(), children->end(), relative) -
134 if ((direction == ORDER_DIRECTION_ABOVE && child_i == target_i + 1) ||
135 (direction == ORDER_DIRECTION_BELOW && child_i + 1 == target_i)) {
139 ScopedOrderChangedNotifier notifier(view, relative, direction);
141 const size_t dest_i = direction == ORDER_DIRECTION_ABOVE
142 ? (child_i < target_i ? target_i : target_i + 1)
143 : (child_i < target_i ? target_i - 1 : target_i);
144 children->erase(children->begin() + child_i);
145 children->insert(children->begin() + dest_i, view);
150 class ScopedSetBoundsNotifier {
152 ScopedSetBoundsNotifier(View* view,
153 const gfx::Rect& old_bounds,
154 const gfx::Rect& new_bounds)
156 old_bounds_(old_bounds),
157 new_bounds_(new_bounds) {
158 FOR_EACH_OBSERVER(ViewObserver,
159 *ViewPrivate(view_).observers(),
160 OnViewBoundsChanging(view_, old_bounds_, new_bounds_));
162 ~ScopedSetBoundsNotifier() {
163 FOR_EACH_OBSERVER(ViewObserver,
164 *ViewPrivate(view_).observers(),
165 OnViewBoundsChanged(view_, old_bounds_, new_bounds_));
170 const gfx::Rect old_bounds_;
171 const gfx::Rect new_bounds_;
173 DISALLOW_COPY_AND_ASSIGN(ScopedSetBoundsNotifier);
176 // Some operations are only permitted in the connection that created the view.
177 bool OwnsView(ViewManager* manager, View* view) {
179 static_cast<ViewManagerClientImpl*>(manager)->OwnsView(view->id());
184 ////////////////////////////////////////////////////////////////////////////////
188 View* View::Create(ViewManager* view_manager) {
189 View* view = new View(view_manager);
190 static_cast<ViewManagerClientImpl*>(view_manager)->AddView(view);
194 void View::Destroy() {
195 if (!OwnsView(manager_, this))
199 static_cast<ViewManagerClientImpl*>(manager_)->DestroyView(id_);
200 while (!children_.empty()) {
201 View* child = children_.front();
202 if (!OwnsView(manager_, child)) {
203 ViewPrivate(child).ClearParent();
204 children_.erase(children_.begin());
207 DCHECK(std::find(children_.begin(), children_.end(), child) ==
214 void View::SetBounds(const gfx::Rect& bounds) {
215 if (!OwnsView(manager_, this))
219 static_cast<ViewManagerClientImpl*>(manager_)->SetBounds(id_, bounds);
220 LocalSetBounds(bounds_, bounds);
223 void View::SetVisible(bool value) {
225 static_cast<ViewManagerClientImpl*>(manager_)->SetVisible(id_, value);
228 void View::AddObserver(ViewObserver* observer) {
229 observers_.AddObserver(observer);
232 void View::RemoveObserver(ViewObserver* observer) {
233 observers_.RemoveObserver(observer);
236 void View::AddChild(View* child) {
237 // TODO(beng): not necessarily valid to all connections, but possibly to the
238 // embeddee in an embedder-embeddee relationship.
240 CHECK_EQ(ViewPrivate(child).view_manager(), manager_);
241 LocalAddChild(child);
243 static_cast<ViewManagerClientImpl*>(manager_)->AddChild(child->id(), id_);
246 void View::RemoveChild(View* child) {
247 // TODO(beng): not necessarily valid to all connections, but possibly to the
248 // embeddee in an embedder-embeddee relationship.
250 CHECK_EQ(ViewPrivate(child).view_manager(), manager_);
251 LocalRemoveChild(child);
253 static_cast<ViewManagerClientImpl*>(manager_)->RemoveChild(child->id(),
258 void View::MoveToFront() {
259 Reorder(parent_->children_.back(), ORDER_DIRECTION_ABOVE);
262 void View::MoveToBack() {
263 Reorder(parent_->children_.front(), ORDER_DIRECTION_BELOW);
266 void View::Reorder(View* relative, OrderDirection direction) {
267 if (!LocalReorder(relative, direction))
270 static_cast<ViewManagerClientImpl*>(manager_)->Reorder(id_,
276 bool View::Contains(View* child) const {
278 CHECK_EQ(ViewPrivate(child).view_manager(), manager_);
279 for (View* p = child->parent(); p; p = p->parent()) {
286 View* View::GetChildById(Id id) {
289 // TODO(beng): this could be improved depending on how we decide to own views.
290 Children::const_iterator it = children_.begin();
291 for (; it != children_.end(); ++it) {
292 View* view = (*it)->GetChildById(id);
299 void View::SetSurfaceId(SurfaceIdPtr id) {
301 static_cast<ViewManagerClientImpl*>(manager_)->SetSurfaceId(id_, id.Pass());
305 void View::SetContents(const SkBitmap& contents) {
307 if (!bitmap_uploader_)
308 CreateBitmapUploader();
309 bitmap_uploader_->SetSize(bounds_.size());
310 bitmap_uploader_->SetBitmap(contents);
314 void View::SetColor(SkColor color) {
316 if (!bitmap_uploader_)
317 CreateBitmapUploader();
318 bitmap_uploader_->SetSize(bounds_.size());
319 bitmap_uploader_->SetColor(color);
323 void View::SetFocus() {
325 static_cast<ViewManagerClientImpl*>(manager_)->SetFocus(id_);
328 void View::Embed(const String& url) {
329 static_cast<ViewManagerClientImpl*>(manager_)->Embed(url, id_);
332 scoped_ptr<ServiceProvider>
333 View::Embed(const String& url,
334 scoped_ptr<ServiceProviderImpl> exported_services) {
335 scoped_ptr<ServiceProvider> imported_services;
336 // BindToProxy() takes ownership of |exported_services|.
337 ServiceProviderImpl* registry = exported_services.release();
338 ServiceProviderPtr sp;
340 BindToProxy(registry, &sp);
341 imported_services.reset(registry->CreateRemoteServiceProvider());
343 static_cast<ViewManagerClientImpl*>(manager_)->Embed(url, id_, sp.Pass());
344 return imported_services.Pass();
347 ////////////////////////////////////////////////////////////////////////////////
352 id_(static_cast<Id>(-1)),
356 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroying(this));
358 parent_->LocalRemoveChild(this);
359 // TODO(beng): It'd be better to do this via a destruction observer in the
360 // ViewManagerClientImpl.
362 static_cast<ViewManagerClientImpl*>(manager_)->RemoveView(id_);
363 FOR_EACH_OBSERVER(ViewObserver, observers_, OnViewDestroyed(this));
366 ////////////////////////////////////////////////////////////////////////////////
369 View::View(ViewManager* manager)
371 id_(static_cast<ViewManagerClientImpl*>(manager_)->CreateView()),
374 void View::LocalDestroy() {
378 void View::LocalAddChild(View* child) {
379 ScopedTreeNotifier notifier(child, child->parent(), this);
381 RemoveChildImpl(child, &child->parent_->children_);
382 children_.push_back(child);
383 child->parent_ = this;
386 void View::LocalRemoveChild(View* child) {
387 DCHECK_EQ(this, child->parent());
388 ScopedTreeNotifier notifier(child, this, NULL);
389 RemoveChildImpl(child, &children_);
392 bool View::LocalReorder(View* relative, OrderDirection direction) {
393 return ReorderImpl(&parent_->children_, this, relative, direction);
396 void View::LocalSetBounds(const gfx::Rect& old_bounds,
397 const gfx::Rect& new_bounds) {
398 DCHECK(old_bounds == bounds_);
399 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds);
400 bounds_ = new_bounds;
403 void View::CreateBitmapUploader() {
404 ViewManagerClientImpl* vmci = static_cast<ViewManagerClientImpl*>(manager_);
405 SurfacesServicePtr surfaces_service;
406 InterfacePtr<ServiceProvider> surfaces_service_provider;
407 vmci->shell()->ConnectToApplication("mojo:mojo_surfaces_service",
408 Get(&surfaces_service_provider));
409 ConnectToService(surfaces_service_provider.get(), &surfaces_service);
411 InterfacePtr<ServiceProvider> gpu_service_provider;
412 vmci->shell()->ConnectToApplication("mojo:mojo_native_viewport_service",
413 Get(&gpu_service_provider));
414 ConnectToService(gpu_service_provider.get(), &gpu_service);
415 bitmap_uploader_.reset(new BitmapUploader(
416 vmci, id_, surfaces_service.Pass(), gpu_service.Pass()));