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/connection_manager.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "cc/output/compositor_frame.h"
10 #include "cc/quads/shared_quad_state.h"
11 #include "components/view_manager/client_connection.h"
12 #include "components/view_manager/connection_manager_delegate.h"
13 #include "components/view_manager/server_view.h"
14 #include "components/view_manager/view_coordinate_conversions.h"
15 #include "components/view_manager/view_tree_host_connection.h"
16 #include "components/view_manager/view_tree_impl.h"
17 #include "mojo/application/public/cpp/application_connection.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h"
19 #include "mojo/converters/input_events/input_events_type_converters.h"
20 #include "mojo/converters/surfaces/surfaces_type_converters.h"
21 #include "ui/gfx/geometry/size_conversions.h"
23 using mojo::ConnectionSpecificId
;
25 namespace view_manager
{
27 ConnectionManager::ScopedChange::ScopedChange(
28 ViewTreeImpl
* connection
,
29 ConnectionManager
* connection_manager
,
31 : connection_manager_(connection_manager
),
32 connection_id_(connection
->id()),
33 is_delete_view_(is_delete_view
) {
34 connection_manager_
->PrepareForChange(this);
37 ConnectionManager::ScopedChange::~ScopedChange() {
38 connection_manager_
->FinishChange();
41 ConnectionManager::ConnectionManager(
42 ConnectionManagerDelegate
* delegate
,
43 const scoped_refptr
<surfaces::SurfacesState
>& surfaces_state
)
44 : delegate_(delegate
),
45 surfaces_state_(surfaces_state
),
46 next_connection_id_(1),
48 current_change_(nullptr),
49 in_destructor_(false) {
52 ConnectionManager::~ConnectionManager() {
53 in_destructor_
= true;
55 // Copy the HostConnectionMap because it will be mutated as the connections
57 HostConnectionMap
host_connection_map(host_connection_map_
);
58 for (auto& pair
: host_connection_map
)
59 pair
.second
->CloseConnection();
61 STLDeleteValues(&connection_map_
);
62 // All the connections should have been destroyed.
63 DCHECK(host_connection_map_
.empty());
64 DCHECK(connection_map_
.empty());
67 void ConnectionManager::AddHost(
68 ViewTreeHostConnection
* host_connection
) {
69 DCHECK_EQ(0u, host_connection_map_
.count(host_connection
->view_tree_host()));
70 host_connection_map_
[host_connection
->view_tree_host()] = host_connection
;
73 ServerView
* ConnectionManager::CreateServerView(const ViewId
& id
) {
74 ServerView
* view
= new ServerView(this, id
);
75 view
->AddObserver(this);
79 ConnectionSpecificId
ConnectionManager::GetAndAdvanceNextConnectionId() {
80 const ConnectionSpecificId id
= next_connection_id_
++;
81 DCHECK_LT(id
, next_connection_id_
);
85 uint16_t ConnectionManager::GetAndAdvanceNextHostId() {
86 const uint16_t id
= next_host_id_
++;
87 DCHECK_LT(id
, next_host_id_
);
91 void ConnectionManager::OnConnectionError(ClientConnection
* connection
) {
92 // This will be null if the root has been destroyed.
93 const ViewId
* view_id
= connection
->service()->root();
95 view_id
? GetView(*connection
->service()->root()) : nullptr;
96 // If the ViewTree root is a viewport root, then we'll wait until
97 // the root connection goes away to cleanup.
98 if (view
&& (GetRootView(view
) == view
))
101 scoped_ptr
<ClientConnection
> connection_owner(connection
);
103 connection_map_
.erase(connection
->service()->id());
105 // Notify remaining connections so that they can cleanup.
106 for (auto& pair
: connection_map_
) {
107 pair
.second
->service()->OnWillDestroyViewTreeImpl(
108 connection
->service());
112 void ConnectionManager::OnHostConnectionClosed(
113 ViewTreeHostConnection
* connection
) {
114 auto it
= host_connection_map_
.find(connection
->view_tree_host());
115 DCHECK(it
!= host_connection_map_
.end());
117 // Get the ClientConnection by ViewTreeImpl ID.
118 ConnectionMap::iterator service_connection_it
=
119 connection_map_
.find(it
->first
->GetViewTree()->id());
120 DCHECK(service_connection_it
!= connection_map_
.end());
122 // Tear down the associated ViewTree connection.
123 // TODO(fsamuel): I don't think this is quite right, we should tear down all
124 // connections within the root's viewport. We should probably employ an
125 // observer pattern to do this. Each ViewTreeImpl should track its
126 // parent's lifetime.
127 host_connection_map_
.erase(it
);
128 OnConnectionError(service_connection_it
->second
);
130 // If we have no more roots left, let the app know so it can terminate.
131 if (!host_connection_map_
.size())
132 delegate_
->OnNoMoreRootConnections();
135 void ConnectionManager::EmbedAtView(mojo::ConnectionSpecificId creator_id
,
136 const ViewId
& view_id
,
137 mojo::URLRequestPtr request
) {
138 mojo::ViewTreePtr service_ptr
;
139 ClientConnection
* client_connection
=
140 delegate_
->CreateClientConnectionForEmbedAtView(
141 this, GetProxy(&service_ptr
), creator_id
, request
.Pass(), view_id
);
142 AddConnection(client_connection
);
143 client_connection
->service()->Init(client_connection
->client(),
145 OnConnectionMessagedClient(client_connection
->service()->id());
148 ViewTreeImpl
* ConnectionManager::EmbedAtView(
149 mojo::ConnectionSpecificId creator_id
,
150 const ViewId
& view_id
,
151 mojo::ViewTreeClientPtr client
) {
152 mojo::ViewTreePtr service_ptr
;
153 ClientConnection
* client_connection
=
154 delegate_
->CreateClientConnectionForEmbedAtView(
155 this, GetProxy(&service_ptr
), creator_id
, view_id
, client
.Pass());
156 AddConnection(client_connection
);
157 client_connection
->service()->Init(client_connection
->client(),
159 OnConnectionMessagedClient(client_connection
->service()->id());
161 return client_connection
->service();
164 ViewTreeImpl
* ConnectionManager::GetConnection(
165 ConnectionSpecificId connection_id
) {
166 ConnectionMap::iterator i
= connection_map_
.find(connection_id
);
167 return i
== connection_map_
.end() ? nullptr : i
->second
->service();
170 ServerView
* ConnectionManager::GetView(const ViewId
& id
) {
171 for (auto& pair
: host_connection_map_
) {
172 if (pair
.first
->root_view()->id() == id
)
173 return pair
.first
->root_view();
175 ViewTreeImpl
* service
= GetConnection(id
.connection_id
);
176 return service
? service
->GetView(id
) : nullptr;
179 bool ConnectionManager::IsViewAttachedToRoot(const ServerView
* view
) const {
180 for (auto& pair
: host_connection_map_
) {
181 if (pair
.first
->IsViewAttachedToRoot(view
))
187 void ConnectionManager::SchedulePaint(const ServerView
* view
,
188 const gfx::Rect
& bounds
) {
189 for (auto& pair
: host_connection_map_
) {
190 if (pair
.first
->SchedulePaintIfInViewport(view
, bounds
))
195 void ConnectionManager::OnConnectionMessagedClient(ConnectionSpecificId id
) {
197 current_change_
->MarkConnectionAsMessaged(id
);
200 bool ConnectionManager::DidConnectionMessageClient(
201 ConnectionSpecificId id
) const {
202 return current_change_
&& current_change_
->DidMessageConnection(id
);
205 mojo::ViewportMetricsPtr
ConnectionManager::GetViewportMetricsForView(
206 const ServerView
* view
) {
207 ViewTreeHostImpl
* host
= GetViewTreeHostByView(view
);
209 return host
->GetViewportMetrics().Clone();
211 if (!host_connection_map_
.empty())
212 return host_connection_map_
.begin()->first
->GetViewportMetrics().Clone();
214 mojo::ViewportMetricsPtr metrics
= mojo::ViewportMetrics::New();
215 metrics
->size_in_pixels
= mojo::Size::New();
216 return metrics
.Pass();
219 const ViewTreeImpl
* ConnectionManager::GetConnectionWithRoot(
220 const ViewId
& id
) const {
221 for (auto& pair
: connection_map_
) {
222 if (pair
.second
->service()->IsRoot(id
))
223 return pair
.second
->service();
228 ViewTreeHostImpl
* ConnectionManager::GetViewTreeHostByView(
229 const ServerView
* view
) {
230 return const_cast<ViewTreeHostImpl
*>(
231 static_cast<const ConnectionManager
*>(this)->GetViewTreeHostByView(view
));
234 const ViewTreeHostImpl
* ConnectionManager::GetViewTreeHostByView(
235 const ServerView
* view
) const {
236 while (view
&& view
->parent())
237 view
= view
->parent();
238 for (auto& pair
: host_connection_map_
) {
239 if (view
== pair
.first
->root_view())
245 ViewTreeImpl
* ConnectionManager::GetEmbedRoot(ViewTreeImpl
* service
) {
247 const ViewId
* root_id
= service
->root();
248 if (!root_id
|| root_id
->connection_id
== service
->id())
251 ViewTreeImpl
* parent_service
= GetConnection(root_id
->connection_id
);
252 service
= parent_service
;
253 if (service
&& service
->is_embed_root())
259 void ConnectionManager::ProcessViewBoundsChanged(const ServerView
* view
,
260 const gfx::Rect
& old_bounds
,
261 const gfx::Rect
& new_bounds
) {
262 for (auto& pair
: connection_map_
) {
263 pair
.second
->service()->ProcessViewBoundsChanged(
264 view
, old_bounds
, new_bounds
, IsChangeSource(pair
.first
));
268 void ConnectionManager::ProcessWillChangeViewHierarchy(
269 const ServerView
* view
,
270 const ServerView
* new_parent
,
271 const ServerView
* old_parent
) {
272 for (auto& pair
: connection_map_
) {
273 pair
.second
->service()->ProcessWillChangeViewHierarchy(
274 view
, new_parent
, old_parent
, IsChangeSource(pair
.first
));
278 void ConnectionManager::ProcessViewHierarchyChanged(
279 const ServerView
* view
,
280 const ServerView
* new_parent
,
281 const ServerView
* old_parent
) {
282 for (auto& pair
: connection_map_
) {
283 pair
.second
->service()->ProcessViewHierarchyChanged(
284 view
, new_parent
, old_parent
, IsChangeSource(pair
.first
));
288 void ConnectionManager::ProcessViewReorder(
289 const ServerView
* view
,
290 const ServerView
* relative_view
,
291 const mojo::OrderDirection direction
) {
292 for (auto& pair
: connection_map_
) {
293 pair
.second
->service()->ProcessViewReorder(view
, relative_view
, direction
,
294 IsChangeSource(pair
.first
));
298 void ConnectionManager::ProcessViewDeleted(const ViewId
& view
) {
299 for (auto& pair
: connection_map_
) {
300 pair
.second
->service()->ProcessViewDeleted(view
,
301 IsChangeSource(pair
.first
));
305 void ConnectionManager::ProcessViewportMetricsChanged(
306 const mojo::ViewportMetrics
& old_metrics
,
307 const mojo::ViewportMetrics
& new_metrics
) {
308 for (auto& pair
: connection_map_
) {
309 pair
.second
->service()->ProcessViewportMetricsChanged(
310 old_metrics
, new_metrics
, IsChangeSource(pair
.first
));
314 void ConnectionManager::PrepareForChange(ScopedChange
* change
) {
315 // Should only ever have one change in flight.
316 CHECK(!current_change_
);
317 current_change_
= change
;
320 void ConnectionManager::FinishChange() {
321 // PrepareForChange/FinishChange should be balanced.
322 CHECK(current_change_
);
323 current_change_
= NULL
;
326 void ConnectionManager::AddConnection(ClientConnection
* connection
) {
327 DCHECK_EQ(0u, connection_map_
.count(connection
->service()->id()));
328 connection_map_
[connection
->service()->id()] = connection
;
331 scoped_ptr
<cc::CompositorFrame
>
332 ConnectionManager::UpdateViewTreeFromCompositorFrame(
333 const mojo::CompositorFramePtr
& input
) {
334 return ConvertToCompositorFrame(input
, this);
337 surfaces::SurfacesState
* ConnectionManager::GetSurfacesState() {
338 return surfaces_state_
.get();
341 void ConnectionManager::OnScheduleViewPaint(const ServerView
* view
) {
343 SchedulePaint(view
, gfx::Rect(view
->bounds().size()));
346 const ServerView
* ConnectionManager::GetRootView(const ServerView
* view
) const {
347 const ViewTreeHostImpl
* host
= GetViewTreeHostByView(view
);
348 return host
? host
->root_view() : nullptr;
351 void ConnectionManager::OnViewDestroyed(ServerView
* view
) {
353 ProcessViewDeleted(view
->id());
356 void ConnectionManager::OnWillChangeViewHierarchy(ServerView
* view
,
357 ServerView
* new_parent
,
358 ServerView
* old_parent
) {
362 ProcessWillChangeViewHierarchy(view
, new_parent
, old_parent
);
365 void ConnectionManager::OnViewHierarchyChanged(ServerView
* view
,
366 ServerView
* new_parent
,
367 ServerView
* old_parent
) {
371 ProcessViewHierarchyChanged(view
, new_parent
, old_parent
);
373 // TODO(beng): optimize.
375 SchedulePaint(old_parent
, gfx::Rect(old_parent
->bounds().size()));
377 SchedulePaint(new_parent
, gfx::Rect(new_parent
->bounds().size()));
380 void ConnectionManager::OnViewBoundsChanged(ServerView
* view
,
381 const gfx::Rect
& old_bounds
,
382 const gfx::Rect
& new_bounds
) {
386 ProcessViewBoundsChanged(view
, old_bounds
, new_bounds
);
390 // TODO(sky): optimize this.
391 SchedulePaint(view
->parent(), old_bounds
);
392 SchedulePaint(view
->parent(), new_bounds
);
395 void ConnectionManager::OnViewReordered(ServerView
* view
,
396 ServerView
* relative
,
397 mojo::OrderDirection direction
) {
399 SchedulePaint(view
, gfx::Rect(view
->bounds().size()));
402 void ConnectionManager::OnWillChangeViewVisibility(ServerView
* view
) {
406 // Need to repaint if the view was drawn (which means it's in the process of
407 // hiding) or the view is transitioning to drawn.
408 if (view
->parent() && (view
->IsDrawn() ||
409 (!view
->visible() && view
->parent()->IsDrawn()))) {
410 SchedulePaint(view
->parent(), view
->bounds());
413 for (auto& pair
: connection_map_
) {
414 pair
.second
->service()->ProcessWillChangeViewVisibility(
415 view
, IsChangeSource(pair
.first
));
419 void ConnectionManager::OnViewSharedPropertyChanged(
421 const std::string
& name
,
422 const std::vector
<uint8_t>* new_data
) {
423 for (auto& pair
: connection_map_
) {
424 pair
.second
->service()->ProcessViewPropertyChanged(
425 view
, name
, new_data
, IsChangeSource(pair
.first
));
429 void ConnectionManager::OnViewTextInputStateChanged(
431 const ui::TextInputState
& state
) {
432 ViewTreeHostImpl
* host
= GetViewTreeHostByView(view
);
433 host
->UpdateTextInputState(view
, state
);
436 bool ConnectionManager::ConvertSurfaceDrawQuad(
437 const mojo::QuadPtr
& input
,
438 const mojo::CompositorFrameMetadataPtr
& metadata
,
439 cc::SharedQuadState
* sqs
,
440 cc::RenderPass
* render_pass
) {
441 unsigned int id
= static_cast<unsigned int>(
442 input
->surface_quad_state
->surface
.To
<cc::SurfaceId
>().id
);
443 // TODO(fsamuel): Security checks:
444 // 1. We need to make sure the embedder can only position views it's allowed
446 // 2. We need to make sure that the embedder cannot place views in areas
447 // outside of its own bounds.
448 ServerView
* view
= GetView(ViewIdFromTransportId(id
));
449 // If a CompositorFrame message arrives late, say during a navigation, then
450 // it may contain view IDs that no longer exist.
453 gfx::Rect
bounds(input
->visible_rect
.To
<gfx::Rect
>());
455 sqs
->quad_to_target_transform
.TransformPoint(&p
);
456 bounds
.set_origin(p
);
457 // TODO(fsamuel): This seems like a crude way to set the size that probably
458 // doesn't work correctly in the general case. We need to get transforms
459 // working correctly in the general case.
460 bounds
.set_size(gfx::ToRoundedSize(
461 gfx::ScaleSize(bounds
.size(), metadata
->device_scale_factor
)));
462 view
->SetBounds(bounds
);
466 } // namespace view_manager