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

Side by Side Diff: services/ui/view_manager/view_registry.cc

Issue 1415493003: mozart: Initial commit of the view manager. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 1 month 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
« no previous file with comments | « services/ui/view_manager/view_registry.h ('k') | services/ui/view_manager/view_state.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 <algorithm>
6 #include <utility>
7
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "services/ui/view_manager/surface_manager.h"
11 #include "services/ui/view_manager/view_host_impl.h"
12 #include "services/ui/view_manager/view_registry.h"
13 #include "services/ui/view_manager/view_tree_host_impl.h"
14
15 namespace view_manager {
16
17 static bool AreViewLayoutParamsValid(mojo::ui::ViewLayoutParams* params) {
18 return params && params->constraints && params->constraints->min_width >= 0 &&
19 params->constraints->max_width >= params->constraints->min_width &&
20 params->constraints->min_height >= 0 &&
21 params->constraints->max_height >= params->constraints->min_height &&
22 params->device_pixel_ratio > 0;
23 }
24
25 static std::ostream& operator<<(std::ostream& os, mojo::Size* size) {
26 return size
27 ? os << "{width=" << size->width << ", height=" << size->height
28 << "}"
29 : os << "{null}";
30 }
31
32 static std::ostream& operator<<(std::ostream& os, mojo::SurfaceId* surface_id) {
33 return surface_id
34 ? os << "{id_namespace=" << surface_id->id_namespace
35 << ", local=" << surface_id->local << "}"
36 : os << "{null}";
37 }
38
39 static std::ostream& operator<<(std::ostream& os, mojo::ui::ViewToken* token) {
40 return token ? os << "{token=" << token->value << "}" : os << "{null}";
41 }
42
43 static std::ostream& operator<<(std::ostream& os, ViewState* view_state) {
44 return view_state ? os << "{token=" << view_state->view_token_value() << "}"
45 : os << "{null}";
46 }
47
48 static std::ostream& operator<<(std::ostream& os,
49 mojo::ui::BoxConstraints* constraints) {
50 return constraints
51 ? os << "{min_width=" << constraints->min_width
52 << ", max_width=" << constraints->max_width
53 << ", min_height=" << constraints->min_height
54 << ", max_height=" << constraints->max_height << "}"
55 : os << "{null}";
56 };
57
58 static std::ostream& operator<<(std::ostream& os,
59 mojo::ui::ViewLayoutParams* params) {
60 return params
61 ? os << "{constraints=" << params->constraints.get()
62 << ", device_pixel_ratio=" << params->device_pixel_ratio
63 << "}"
64 : os << "{null}";
65 }
66
67 static std::ostream& operator<<(std::ostream& os,
68 mojo::ui::ViewLayoutInfo* info) {
69 return info
70 ? os << "{size=" << info->size.get()
71 << ", surface_id=" << info->surface_id.get() << "}"
72 : os << "{null}";
73 }
74
75 ViewRegistry::ViewRegistry(SurfaceManager* surface_manager)
76 : surface_manager_(surface_manager), next_view_token_value_(1u) {}
77
78 ViewRegistry::~ViewRegistry() {}
79
80 mojo::ui::ViewTokenPtr ViewRegistry::RegisterView(
81 mojo::ui::ViewPtr view,
82 mojo::InterfaceRequest<mojo::ui::ViewHost> view_host_request) {
83 DCHECK(view);
84 uint32_t view_token_value = next_view_token_value_++;
85 DCHECK(!FindView(view_token_value));
86
87 // Create the state and bind host to it.
88 ViewState* view_state = new ViewState(view.Pass(), view_token_value);
89 ViewHostImpl* view_host =
90 new ViewHostImpl(this, view_state, view_host_request.Pass());
91 view_state->set_view_host(view_host);
92 view_state->set_view_connection_error_handler(
93 base::Bind(&ViewRegistry::OnViewConnectionError, base::Unretained(this),
94 view_state));
95 view_host->set_view_host_connection_error_handler(
96 base::Bind(&ViewRegistry::OnViewConnectionError, base::Unretained(this),
97 view_state));
98
99 // Add to registry and return token.
100 views_by_token_.insert({view_token_value, view_state});
101 mojo::ui::ViewTokenPtr token = mojo::ui::ViewToken::New();
102 token->value = view_state->view_token_value();
103 DVLOG(1) << "RegisterView: view=" << view_state;
104 return token;
105 }
106
107 void ViewRegistry::OnViewConnectionError(ViewState* view_state) {
108 DCHECK(IsViewStateRegisteredDebug(view_state));
109 DVLOG(1) << "OnViewConnectionError: view=" << view_state;
110
111 UnregisterView(view_state);
112 }
113
114 void ViewRegistry::UnregisterView(ViewState* view_state) {
115 DCHECK(IsViewStateRegisteredDebug(view_state));
116 DVLOG(1) << "UnregisterView: view=" << view_state;
117
118 // Remove from parent or roots.
119 HijackView(view_state);
120
121 // Remove from registry.
122 views_by_token_.erase(view_state->view_token_value());
123 delete view_state;
124 }
125
126 void ViewRegistry::RegisterViewTree(
127 mojo::ui::ViewTreePtr view_tree,
128 mojo::InterfaceRequest<mojo::ui::ViewTreeHost> view_tree_host_request) {
129 DCHECK(view_tree);
130
131 // Create the state and bind host to it.
132 ViewTreeState* tree_state = new ViewTreeState(view_tree.Pass());
133 ViewTreeHostImpl* tree_host =
134 new ViewTreeHostImpl(this, tree_state, view_tree_host_request.Pass());
135 tree_state->set_view_tree_host(tree_host);
136 tree_state->set_view_tree_connection_error_handler(
137 base::Bind(&ViewRegistry::OnViewTreeConnectionError,
138 base::Unretained(this), tree_state));
139 tree_host->set_view_tree_host_connection_error_handler(
140 base::Bind(&ViewRegistry::OnViewTreeConnectionError,
141 base::Unretained(this), tree_state));
142
143 // Add to registry.
144 view_trees_.push_back(tree_state);
145 DVLOG(1) << "RegisterViewTree: tree=" << tree_state;
146 }
147
148 void ViewRegistry::OnViewTreeConnectionError(ViewTreeState* tree_state) {
149 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
150 DVLOG(1) << "OnViewTreeConnectionError: tree=" << tree_state;
151
152 UnregisterViewTree(tree_state);
153 }
154
155 void ViewRegistry::UnregisterViewTree(ViewTreeState* tree_state) {
156 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
157 DVLOG(1) << "UnregisterViewTree: tree=" << tree_state;
158
159 // Unlink the root if needed.
160 if (tree_state->root())
161 UnlinkRoot(tree_state);
162
163 // Remove from registry.
164 view_trees_.erase(std::find_if(
165 view_trees_.begin(), view_trees_.end(),
166 [tree_state](ViewTreeState* other) { return tree_state == other; }));
167 delete tree_state;
168 }
169
170 void ViewRegistry::RequestLayout(ViewState* view_state) {
171 DCHECK(IsViewStateRegisteredDebug(view_state));
172 DVLOG(1) << "RequestLayout: view=" << view_state;
173
174 InvalidateLayout(view_state);
175 }
176
177 void ViewRegistry::AddChild(ViewState* parent_state,
178 uint32_t child_key,
179 mojo::ui::ViewTokenPtr child_view_token) {
180 DCHECK(IsViewStateRegisteredDebug(parent_state));
181 DCHECK(child_view_token);
182 DVLOG(1) << "AddChild: parent=" << parent_state << ", child_key=" << child_key
183 << ", child=" << child_view_token.get();
184
185 // Check for duplicate children.
186 if (parent_state->children().find(child_key) !=
187 parent_state->children().end()) {
188 LOG(ERROR) << "View attempted to add a child with a duplicate key: "
189 << "parent=" << parent_state << ", child_key=" << child_key
190 << ", child=" << child_view_token.get();
191 UnregisterView(parent_state);
192 return;
193 }
194
195 // Check whether the desired child view still exists.
196 // Adding a non-existent child still succeeds but the view manager will
197 // immediately report it as being unavailable.
198 ViewState* child_state = FindView(child_view_token->value);
199 if (!child_state) {
200 LinkChildAsUnavailable(parent_state, child_key);
201 return;
202 }
203
204 // Check whether the child needs to be reparented.
205 // The old parent will receive an unavailable event. For interface symmetry,
206 // we deliberately do this even if the old and new parents are the same.
207 HijackView(child_state);
208
209 // Link the child into its new parent.
210 LinkChild(parent_state, child_key, child_state);
211 }
212
213 void ViewRegistry::RemoveChild(ViewState* parent_state, uint32_t child_key) {
214 DCHECK(IsViewStateRegisteredDebug(parent_state));
215 DVLOG(1) << "RemoveChild: parent=" << parent_state
216 << ", child_key=" << child_key;
217
218 // Check whether the child key exists in the parent.
219 auto child_it = parent_state->children().find(child_key);
220 if (child_it == parent_state->children().end()) {
221 LOG(ERROR) << "View attempted to remove a child with an invalid key: "
222 << "parent=" << parent_state << ", child_key=" << child_key;
223 UnregisterView(parent_state);
224 return;
225 }
226
227 // Unlink the child from its parent.
228 UnlinkChild(parent_state, child_it);
229 }
230
231 void ViewRegistry::LayoutChild(
232 ViewState* parent_state,
233 uint32_t child_key,
234 mojo::ui::ViewLayoutParamsPtr child_layout_params,
235 const ViewLayoutCallback& callback) {
236 DCHECK(IsViewStateRegisteredDebug(parent_state));
237 DCHECK(child_layout_params);
238 DCHECK(child_layout_params->constraints);
239 DVLOG(1) << "LayoutChild: parent=" << parent_state
240 << ", child_key=" << child_key
241 << ", child_layout_params=" << child_layout_params.get();
242
243 // Check whether the layout parameters are well-formed.
244 if (!AreViewLayoutParamsValid(child_layout_params.get())) {
245 LOG(ERROR) << "View provided invalid child layout parameters: "
246 << "parent=" << parent_state << ", child_key=" << child_key
247 << ", child_layout_params=" << child_layout_params;
248 UnregisterView(parent_state);
249 callback.Run(nullptr);
250 return;
251 }
252
253 // Check whether the child key exists in the parent.
254 auto child_it = parent_state->children().find(child_key);
255 if (child_it == parent_state->children().end()) {
256 LOG(ERROR) << "View attempted to layout a child with an invalid key: "
257 << "parent=" << parent_state << ", child_key=" << child_key
258 << ", child_layout_params=" << child_layout_params;
259 UnregisterView(parent_state);
260 callback.Run(nullptr);
261 return;
262 }
263
264 SetLayout(child_it->second, child_layout_params.Pass(), callback);
265 }
266
267 void ViewRegistry::RequestLayout(ViewTreeState* tree_state) {
268 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
269 DVLOG(1) << "RequestLayout: tree=" << tree_state;
270
271 InvalidateLayoutForRoot(tree_state);
272 }
273
274 void ViewRegistry::SetRoot(ViewTreeState* tree_state,
275 uint32_t root_key,
276 mojo::ui::ViewTokenPtr root_view_token) {
277 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
278 DCHECK(root_view_token);
279 DVLOG(1) << "SetRoot: tree=" << tree_state << ", root_key=" << root_key
280 << ", root=" << root_view_token.get();
281
282 // Check whether the desired root view still exists.
283 // Using a non-existent root view still succeeds but the view manager will
284 // immediately report it as being unavailable.
285 ViewState* root_state = FindView(root_view_token->value);
286 if (root_state) {
287 HijackView(root_state);
288 LinkRoot(tree_state, root_state, root_key);
289 } else {
290 SendRootUnavailable(tree_state, root_key);
291 }
292 tree_state->set_explicit_root(true);
293 }
294
295 void ViewRegistry::ResetRoot(ViewTreeState* tree_state) {
296 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
297 DVLOG(1) << "ResetRoot: tree=" << tree_state;
298
299 if (tree_state->root())
300 UnlinkRoot(tree_state);
301 tree_state->set_explicit_root(false);
302 }
303
304 void ViewRegistry::LayoutRoot(ViewTreeState* tree_state,
305 mojo::ui::ViewLayoutParamsPtr root_layout_params,
306 const ViewLayoutCallback& callback) {
307 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
308 DCHECK(root_layout_params);
309 DCHECK(root_layout_params->constraints);
310 DVLOG(1) << "LayoutRoot: tree=" << tree_state
311 << ", root_layout_params=" << root_layout_params.get();
312
313 // Check whether the layout parameters are well-formed.
314 if (!AreViewLayoutParamsValid(root_layout_params.get())) {
315 LOG(ERROR) << "View tree provided invalid root layout parameters: "
316 << "tree=" << tree_state
317 << ", root_layout_params=" << root_layout_params;
318 UnregisterViewTree(tree_state);
319 callback.Run(nullptr);
320 return;
321 }
322
323 // Check whether the client called LayoutRoot without first having actually
324 // set a root.
325 if (!tree_state->explicit_root()) {
326 LOG(ERROR) << "View tree attempted to layout the rout without having "
327 "set one first: tree="
328 << tree_state << ", root_layout_params=" << root_layout_params;
329 UnregisterViewTree(tree_state);
330 callback.Run(nullptr);
331 return;
332 }
333
334 // Check whether the root is unavailable and therefore cannot be laid out.
335 // This is not an error.
336 if (!tree_state->root()) {
337 callback.Run(nullptr);
338 return;
339 }
340
341 SetLayout(tree_state->root(), root_layout_params.Pass(), callback);
342 }
343
344 ViewState* ViewRegistry::FindView(uint32_t view_token) {
345 auto it = views_by_token_.find(view_token);
346 return it != views_by_token_.end() ? it->second : nullptr;
347 }
348
349 void ViewRegistry::LinkChild(ViewState* parent_state,
350 uint32_t child_key,
351 ViewState* child_state) {
352 DCHECK(IsViewStateRegisteredDebug(parent_state));
353 DCHECK(parent_state->children().find(child_key) ==
354 parent_state->children().end());
355 DCHECK(IsViewStateRegisteredDebug(child_state));
356
357 DVLOG(2) << "Added child " << child_key << " {"
358 << child_state->view_token_value() << "} to parent {"
359 << parent_state->view_token_value() << "}";
360
361 parent_state->children().insert({child_key, child_state});
362 child_state->SetParent(parent_state, child_key);
363
364 // Schedule layout of the parent on behalf of its newly added child.
365 // We don't need to schedule layout of the child until the parent provides
366 // new layout parameters.
367 InvalidateLayoutForChild(parent_state, child_key);
368 }
369
370 void ViewRegistry::LinkChildAsUnavailable(ViewState* parent_state,
371 uint32_t child_key) {
372 DCHECK(IsViewStateRegisteredDebug(parent_state));
373 DCHECK(parent_state->children().find(child_key) ==
374 parent_state->children().end());
375
376 DVLOG(2) << "Added unavailable child " << child_key << " to parent {"
377 << parent_state->view_token_value() << "}";
378
379 parent_state->children().insert({child_key, nullptr});
380 SendChildUnavailable(parent_state, child_key);
381
382 // Don't schedule layout for the parent just yet. Wait for it to
383 // remove its child in response to the OnChildUnavailable notification.
384 }
385
386 void ViewRegistry::MarkChildAsUnavailable(ViewState* parent_state,
387 uint32_t child_key) {
388 DCHECK(IsViewStateRegisteredDebug(parent_state));
389 auto child_it = parent_state->children().find(child_key);
390 DCHECK(child_it != parent_state->children().end());
391 DCHECK(child_it->second);
392
393 DVLOG(2) << "Marked unavailable child " << child_key << " {"
394 << child_it->second->view_token_value() << "} from parent {"
395 << parent_state->view_token_value() << "}";
396
397 ResetStateWhenUnlinking(child_it->second);
398 child_it->second->ResetContainer();
399 child_it->second = nullptr;
400 SendChildUnavailable(parent_state, child_key);
401
402 // Don't schedule layout for the parent just yet. Wait for it to
403 // remove its child in response to the OnChildUnavailable notification.
404 // We don't need to schedule layout for the child either since it will
405 // retain its old layout parameters.
406 }
407
408 void ViewRegistry::UnlinkChild(ViewState* parent_state,
409 ViewState::ChildrenMap::iterator child_it) {
410 DCHECK(IsViewStateRegisteredDebug(parent_state));
411 DCHECK(child_it != parent_state->children().end());
412
413 ViewState* child_state = child_it->second;
414 if (child_state) {
415 DVLOG(2) << "Removed child " << child_state->key() << " {"
416 << child_state->view_token_value() << "} from parent {"
417 << parent_state->view_token_value() << "}";
418 ResetStateWhenUnlinking(child_it->second);
419 child_state->ResetContainer();
420 } else {
421 DVLOG(2) << "Removed unavailable child " << child_it->first
422 << "} from parent {" << parent_state->view_token_value() << "}";
423 }
424 parent_state->children().erase(child_it);
425
426 // Schedule layout for the parent now that it has lost its child.
427 // We don't need to schedule layout for the child itself since it will
428 // retain its old layout parameters.
429 InvalidateLayout(parent_state);
430 }
431
432 void ViewRegistry::LinkRoot(ViewTreeState* tree_state,
433 ViewState* root_state,
434 uint32_t root_key) {
435 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
436 DCHECK(IsViewStateRegisteredDebug(root_state));
437 DCHECK(!tree_state->root());
438 DCHECK(!root_state->parent());
439
440 DVLOG(2) << "Linked view tree root " << root_key << " {"
441 << root_state->view_token_value() << "}";
442
443 tree_state->SetRoot(root_state, root_key);
444
445 // Schedule layout of the tree on behalf of its newly added root.
446 // We don't need to schedule layout of the root until the tree provides
447 // new layout parameters.
448 InvalidateLayoutForRoot(tree_state);
449 }
450
451 void ViewRegistry::UnlinkRoot(ViewTreeState* tree_state) {
452 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
453 DCHECK(tree_state->root());
454
455 DVLOG(2) << "Unlinked view tree root " << tree_state->root()->key() << " {"
456 << tree_state->root()->view_token_value() << "}";
457
458 ResetStateWhenUnlinking(tree_state->root());
459 tree_state->ResetRoot();
460
461 // We don't need to schedule layout for the root since it will retain
462 // its old layout parameters.
463 }
464
465 void ViewRegistry::HijackView(ViewState* view_state) {
466 if (view_state->parent()) {
467 MarkChildAsUnavailable(view_state->parent(), view_state->key());
468 } else if (view_state->tree()) {
469 ViewTreeState* tree_state = view_state->tree();
470 uint32_t root_key = tree_state->root()->key();
471 UnlinkRoot(tree_state);
472 SendRootUnavailable(tree_state, root_key);
473 }
474 }
475
476 void ViewRegistry::InvalidateLayout(ViewState* view_state) {
477 DCHECK(IsViewStateRegisteredDebug(view_state));
478
479 // We can consider the layout request to have been satisfied if
480 // there is already a pending layout request in the queue that has not
481 // yet been issued (this is coalescing). Otherwise we must manufacture
482 // a new one based on the current layout parameters.
483 if (view_state->layout_params() &&
484 (view_state->pending_layout_requests().empty() ||
485 view_state->pending_layout_requests().back()->issued())) {
486 EnqueueLayoutRequest(view_state, view_state->layout_params().Clone());
487 IssueNextViewLayoutRequest(view_state);
488 }
489 }
490
491 void ViewRegistry::InvalidateLayoutForChild(ViewState* parent_state,
492 uint32_t child_key) {
493 DCHECK(IsViewStateRegisteredDebug(parent_state));
494 DCHECK(parent_state->children().find(child_key) !=
495 parent_state->children().end());
496
497 parent_state->children_needing_layout().insert(child_key);
498 InvalidateLayout(parent_state);
499 }
500
501 void ViewRegistry::InvalidateLayoutForRoot(ViewTreeState* tree_state) {
502 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
503
504 if (!tree_state->layout_request_pending()) {
505 tree_state->set_layout_request_pending(true);
506 IssueNextViewTreeLayoutRequest(tree_state);
507 }
508 }
509
510 void ViewRegistry::SetLayout(ViewState* view_state,
511 mojo::ui::ViewLayoutParamsPtr layout_params,
512 const ViewLayoutCallback& callback) {
513 DCHECK(IsViewStateRegisteredDebug(view_state));
514 DCHECK(AreViewLayoutParamsValid(layout_params.get()));
515
516 // Check whether the currently cached layout parameters are the same
517 // and we already have a result and we have no pending layout requests.
518 if (view_state->pending_layout_requests().empty() &&
519 view_state->layout_params() && view_state->layout_info() &&
520 view_state->layout_params()->Equals(*layout_params)) {
521 DVLOG(2) << "Layout cache hit";
522 callback.Run(view_state->layout_info().Clone());
523 return;
524 }
525
526 // Check whether the layout parameters are different from the most
527 // recent pending layout request if we have one.
528 if (view_state->pending_layout_requests().empty() ||
529 !view_state->pending_layout_requests().back()->layout_params()->Equals(
530 *layout_params)) {
531 // Enqueue a new request for these parameters.
532 EnqueueLayoutRequest(view_state, layout_params.Pass());
533 }
534
535 // Enlist ourselves into the callbacks for the pending request.
536 view_state->pending_layout_requests().back()->AddCallback(callback);
537 IssueNextViewLayoutRequest(view_state);
538 }
539
540 void ViewRegistry::EnqueueLayoutRequest(
541 ViewState* view_state,
542 mojo::ui::ViewLayoutParamsPtr layout_params) {
543 DCHECK(IsViewStateRegisteredDebug(view_state));
544 DCHECK(AreViewLayoutParamsValid(layout_params.get()));
545
546 // Drop the previous layout request if it hasn't been issued yet.
547 // This may cause callbacks to be invoked will null information.
548 if (!view_state->pending_layout_requests().empty() &&
549 !view_state->pending_layout_requests().back()->issued())
550 view_state->pending_layout_requests().pop_back();
551
552 // Enqueue the new request.
553 view_state->pending_layout_requests().emplace_back(
554 std::unique_ptr<ViewLayoutRequest>(
555 new ViewLayoutRequest(layout_params.Pass())));
556 }
557
558 void ViewRegistry::IssueNextViewLayoutRequest(ViewState* view_state) {
559 DCHECK(IsViewStateRegisteredDebug(view_state));
560
561 if (!view_state->pending_layout_requests().empty() &&
562 !view_state->pending_layout_requests().front()->issued()) {
563 view_state->pending_layout_requests().front()->set_issued(true);
564 SendViewLayoutRequest(view_state);
565 }
566 }
567
568 void ViewRegistry::IssueNextViewTreeLayoutRequest(ViewTreeState* tree_state) {
569 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
570
571 if (tree_state->layout_request_pending() &&
572 !tree_state->layout_request_issued()) {
573 tree_state->set_layout_request_pending(false);
574 tree_state->set_layout_request_issued(true);
575 SendViewTreeLayoutRequest(tree_state);
576 }
577 }
578
579 void ViewRegistry::ResetStateWhenUnlinking(ViewState* view_state) {
580 // Clean up parent's recorded state for the child.
581 if (view_state->parent()) {
582 view_state->parent()->children_needing_layout().erase(view_state->key());
583 }
584
585 // Clean up child's recorded state for the parent or tree.
586 if (view_state->wrapped_surface()) {
587 surface_manager_->DestroySurface(view_state->wrapped_surface().Pass());
588 }
589 }
590
591 void ViewRegistry::SendChildUnavailable(ViewState* parent_state,
592 uint32_t child_key) {
593 DCHECK(IsViewStateRegisteredDebug(parent_state));
594
595 // TODO: Detect ANRs
596 DVLOG(1) << "SendChildUnavailable: child_key=" << child_key;
597 parent_state->view()->OnChildUnavailable(child_key,
598 base::Bind(&base::DoNothing));
599 }
600
601 void ViewRegistry::SendRootUnavailable(ViewTreeState* tree_state,
602 uint32_t root_key) {
603 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
604
605 // TODO: Detect ANRs
606 DVLOG(1) << "SendRootUnavailable: root_key=" << root_key;
607 tree_state->view_tree()->OnRootUnavailable(root_key,
608 base::Bind(&base::DoNothing));
609 }
610
611 void ViewRegistry::SendViewLayoutRequest(ViewState* view_state) {
612 DCHECK(IsViewStateRegisteredDebug(view_state));
613 DCHECK(!view_state->pending_layout_requests().empty());
614 DCHECK(view_state->pending_layout_requests().front()->issued());
615
616 // TODO: Detect ANRs
617 DVLOG(1) << "SendViewLayoutRequest: view.token="
618 << view_state->view_token_value();
619 view_state->view()->OnLayout(
620 view_state->pending_layout_requests().front()->layout_params()->Clone(),
621 mojo::Array<uint32_t>::From(view_state->children_needing_layout()),
622 base::Bind(&ViewRegistry::OnViewLayoutResult, base::Unretained(this),
623 view_state->GetWeakPtr()));
624 view_state->children_needing_layout().clear();
625 }
626
627 void ViewRegistry::SendViewTreeLayoutRequest(ViewTreeState* tree_state) {
628 DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
629 DCHECK(tree_state->layout_request_issued());
630
631 // TODO: Detect ANRs
632 DVLOG(1) << "SendViewTreeLayoutRequest";
633 tree_state->view_tree()->OnLayout(
634 base::Bind(&ViewRegistry::OnViewTreeLayoutResult, base::Unretained(this),
635 tree_state->GetWeakPtr()));
636 }
637
638 static bool IsSizeInBounds(mojo::ui::BoxConstraints* constraints,
639 mojo::Size* size) {
640 return size && size->width >= constraints->min_width &&
641 size->width <= constraints->max_width &&
642 size->height >= constraints->min_height &&
643 size->height <= constraints->max_height;
644 }
645
646 void ViewRegistry::OnViewLayoutResult(base::WeakPtr<ViewState> view_state_weak,
647 mojo::ui::ViewLayoutInfoPtr info) {
648 DCHECK(info);
649 DCHECK(info->surface_id); // checked by mojom
650
651 ViewState* view_state = view_state_weak.get();
652 if (!view_state)
653 return;
654
655 DCHECK(!view_state->pending_layout_requests().empty());
656 DCHECK(view_state->pending_layout_requests().front()->issued());
657
658 std::unique_ptr<ViewLayoutRequest> request(
659 std::move(view_state->pending_layout_requests().front()));
660 view_state->pending_layout_requests().erase(
661 view_state->pending_layout_requests().begin());
662
663 DVLOG(1) << "OnViewLayoutResult: view=" << view_state
664 << ", params=" << request->layout_params()
665 << ", info=" << info.get();
666
667 // Validate the layout info.
668 if (!IsSizeInBounds(request->layout_params()->constraints.get(),
669 info->size.get())) {
670 LOG(ERROR) << "View returned invalid size in its layout info: "
671 << "view=" << view_state
672 << ", params=" << request->layout_params()
673 << ", info=" << info.get();
674 UnregisterView(view_state);
675 return;
676 }
677
678 // Assume the parent or root will not see the new layout information if
679 // there are no callbacks so we need to inform it when things change.
680 const bool size_changed =
681 !view_state->layout_info() ||
682 !view_state->layout_info()->size->Equals(*info->size);
683 const bool surface_changed =
684 !view_state->layout_info() ||
685 !view_state->layout_info()->surface_id->Equals(*info->surface_id);
686 const bool recurse =
687 !request->has_callbacks() && (surface_changed || size_changed);
688
689 view_state->layout_params() = request->TakeLayoutParams().Pass();
690 view_state->layout_info() = info.Pass();
691
692 if (surface_changed) {
693 if (view_state->wrapped_surface())
694 surface_manager_->DestroySurface(view_state->wrapped_surface().Pass());
695 view_state->wrapped_surface() = surface_manager_->CreateWrappedSurface(
696 view_state->layout_info()->surface_id.get());
697 }
698
699 request->DispatchLayoutInfo(view_state->layout_info().get());
700
701 if (recurse) {
702 if (view_state->parent()) {
703 InvalidateLayoutForChild(view_state->parent(), view_state->key());
704 } else if (view_state->tree()) {
705 InvalidateLayoutForRoot(view_state->tree());
706 }
707 }
708
709 IssueNextViewLayoutRequest(view_state);
710 }
711
712 void ViewRegistry::OnViewTreeLayoutResult(
713 base::WeakPtr<ViewTreeState> tree_state_weak) {
714 ViewTreeState* tree_state = tree_state_weak.get();
715 if (tree_state) {
716 DCHECK(tree_state->layout_request_issued());
717
718 DVLOG(1) << "OnViewTreeLayoutResult";
719
720 tree_state->set_layout_request_issued(false);
721 IssueNextViewTreeLayoutRequest(tree_state);
722 }
723 }
724
725 } // namespace view_manager
OLDNEW
« no previous file with comments | « services/ui/view_manager/view_registry.h ('k') | services/ui/view_manager/view_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698