Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_(1) {} | |
|
jamesr
2015/10/27 23:13:11
1u
jeffbrown
2015/10/28 01:37:29
Done.
| |
| 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 auto it = std::find_if( | |
| 165 view_trees_.begin(), view_trees_.end(), | |
| 166 [tree_state](ViewTreeState* other) { return tree_state == other; }); | |
| 167 view_trees_.erase(it); | |
|
jamesr
2015/10/27 23:13:11
erase-remove would be slightly more idiomatic, i.e
jeffbrown
2015/10/28 01:37:29
There's only one match here so I can use erase(fin
| |
| 168 delete tree_state; | |
| 169 } | |
| 170 | |
| 171 void ViewRegistry::RequestLayout(ViewState* view_state) { | |
| 172 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 173 DVLOG(1) << "RequestLayout: view=" << view_state; | |
| 174 | |
| 175 InvalidateLayout(view_state); | |
| 176 } | |
| 177 | |
| 178 void ViewRegistry::AddChild(ViewState* parent_state, | |
| 179 uint32_t child_key, | |
| 180 mojo::ui::ViewTokenPtr child_view_token) { | |
| 181 DCHECK(IsViewStateRegisteredDebug(parent_state)); | |
| 182 DCHECK(child_view_token); | |
| 183 DVLOG(1) << "AddChild: parent=" << parent_state << ", child_key=" << child_key | |
| 184 << ", child=" << child_view_token.get(); | |
| 185 | |
| 186 // Check for duplicate children. | |
| 187 if (parent_state->children.find(child_key) != 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->root_was_set = 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->root_was_set = 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->root_was_set) { | |
| 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( | |
| 409 ViewState* parent_state, | |
| 410 decltype(ViewState::children)::iterator child_it) { | |
| 411 DCHECK(IsViewStateRegisteredDebug(parent_state)); | |
| 412 DCHECK(child_it != parent_state->children.end()); | |
| 413 | |
| 414 ViewState* child_state = child_it->second; | |
| 415 if (child_state) { | |
| 416 DVLOG(2) << "Removed child " << child_state->key() << " {" | |
| 417 << child_state->view_token_value() << "} from parent {" | |
| 418 << parent_state->view_token_value() << "}"; | |
| 419 ResetStateWhenUnlinking(child_it->second); | |
| 420 child_state->ResetContainer(); | |
| 421 } else { | |
| 422 DVLOG(2) << "Removed unavailable child " << child_it->first | |
| 423 << "} from parent {" << parent_state->view_token_value() << "}"; | |
| 424 } | |
| 425 parent_state->children.erase(child_it); | |
| 426 | |
| 427 // Schedule layout for the parent now that it has lost its child. | |
| 428 // We don't need to schedule layout for the child itself since it will | |
| 429 // retain its old layout parameters. | |
| 430 InvalidateLayout(parent_state); | |
| 431 } | |
| 432 | |
| 433 void ViewRegistry::LinkRoot(ViewTreeState* tree_state, | |
| 434 ViewState* root_state, | |
| 435 uint32_t root_key) { | |
| 436 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 437 DCHECK(IsViewStateRegisteredDebug(root_state)); | |
| 438 DCHECK(!tree_state->root()); | |
| 439 DCHECK(!root_state->parent()); | |
| 440 | |
| 441 DVLOG(2) << "Linked view tree root " << root_key << " {" | |
| 442 << root_state->view_token_value() << "}"; | |
| 443 | |
| 444 tree_state->SetRoot(root_state, root_key); | |
| 445 | |
| 446 // Schedule layout of the tree on behalf of its newly added root. | |
| 447 // We don't need to schedule layout of the root until the tree provides | |
| 448 // new layout parameters. | |
| 449 InvalidateLayoutForRoot(tree_state); | |
| 450 } | |
| 451 | |
| 452 void ViewRegistry::UnlinkRoot(ViewTreeState* tree_state) { | |
| 453 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 454 DCHECK(tree_state->root()); | |
| 455 | |
| 456 DVLOG(2) << "Unlinked view tree root " << tree_state->root()->key() << " {" | |
| 457 << tree_state->root()->view_token_value() << "}"; | |
| 458 | |
| 459 ResetStateWhenUnlinking(tree_state->root()); | |
| 460 tree_state->ResetRoot(); | |
| 461 | |
| 462 // We don't need to schedule layout for the root since it will retain | |
| 463 // its old layout parameters. | |
| 464 } | |
| 465 | |
| 466 void ViewRegistry::HijackView(ViewState* view_state) { | |
| 467 if (view_state->parent()) { | |
| 468 MarkChildAsUnavailable(view_state->parent(), view_state->key()); | |
| 469 } else if (view_state->tree()) { | |
| 470 ViewTreeState* tree_state = view_state->tree(); | |
| 471 uint32_t root_key = tree_state->root()->key(); | |
| 472 UnlinkRoot(tree_state); | |
| 473 SendRootUnavailable(tree_state, root_key); | |
| 474 } | |
| 475 } | |
| 476 | |
| 477 void ViewRegistry::InvalidateLayout(ViewState* view_state) { | |
| 478 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 479 | |
| 480 // We can consider the layout request to have been satisfied if | |
| 481 // there is already a pending layout request in the queue that has not | |
| 482 // yet been issued (this is coalescing). Otherwise we must manufacture | |
| 483 // a new one based on the current layout parameters. | |
| 484 if (view_state->layout_params && | |
| 485 (view_state->pending_layout_requests.empty() || | |
| 486 view_state->pending_layout_requests.back()->issued)) { | |
| 487 EnqueueLayoutRequest(view_state, view_state->layout_params.Clone()); | |
| 488 IssueNextViewLayoutRequest(view_state); | |
| 489 } | |
| 490 } | |
| 491 | |
| 492 void ViewRegistry::InvalidateLayoutForChild(ViewState* parent_state, | |
| 493 uint32_t child_key) { | |
| 494 DCHECK(IsViewStateRegisteredDebug(parent_state)); | |
| 495 DCHECK(parent_state->children.find(child_key) != | |
| 496 parent_state->children.end()); | |
| 497 | |
| 498 parent_state->children_needing_layout.insert(child_key); | |
| 499 InvalidateLayout(parent_state); | |
| 500 } | |
| 501 | |
| 502 void ViewRegistry::InvalidateLayoutForRoot(ViewTreeState* tree_state) { | |
| 503 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 504 | |
| 505 if (!tree_state->layout_request_pending) { | |
| 506 tree_state->layout_request_pending = true; | |
| 507 IssueNextViewTreeLayoutRequest(tree_state); | |
| 508 } | |
| 509 } | |
| 510 | |
| 511 void ViewRegistry::SetLayout(ViewState* view_state, | |
| 512 mojo::ui::ViewLayoutParamsPtr layout_params, | |
| 513 const ViewLayoutCallback& callback) { | |
| 514 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 515 DCHECK(AreViewLayoutParamsValid(layout_params.get())); | |
| 516 | |
| 517 // Check whether the currently cached layout parameters are the same | |
| 518 // and we already have a result and we have no pending layout requests. | |
| 519 if (view_state->pending_layout_requests.empty() && | |
| 520 view_state->layout_params && view_state->layout_info.get() && | |
| 521 view_state->layout_params->Equals(*layout_params)) { | |
| 522 DVLOG(2) << "Layout cache hit"; | |
| 523 callback.Run(view_state->layout_info.Clone()); | |
| 524 return; | |
| 525 } | |
| 526 | |
| 527 // Check whether the layout parameters are different from the most | |
| 528 // recent pending layout request if we have one. | |
| 529 if (view_state->pending_layout_requests.empty() || | |
| 530 !view_state->pending_layout_requests.back()->layout_params()->Equals( | |
| 531 *layout_params)) { | |
| 532 // Enqueue a new request for these parameters. | |
| 533 EnqueueLayoutRequest(view_state, layout_params.Pass()); | |
| 534 } | |
| 535 | |
| 536 // Enlist ourselves into the callbacks for the pending request. | |
| 537 view_state->pending_layout_requests.back()->AddCallback(callback); | |
| 538 IssueNextViewLayoutRequest(view_state); | |
| 539 } | |
| 540 | |
| 541 void ViewRegistry::EnqueueLayoutRequest( | |
| 542 ViewState* view_state, | |
| 543 mojo::ui::ViewLayoutParamsPtr layout_params) { | |
| 544 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 545 DCHECK(AreViewLayoutParamsValid(layout_params.get())); | |
| 546 | |
| 547 // Drop the previous layout request if it hasn't been issued yet. | |
| 548 // This may cause callbacks to be invoked will null information. | |
| 549 if (!view_state->pending_layout_requests.empty() && | |
| 550 !view_state->pending_layout_requests.back()->issued) | |
| 551 view_state->pending_layout_requests.pop_back(); | |
| 552 | |
| 553 // Enqueue the new request. | |
| 554 view_state->pending_layout_requests.emplace_back( | |
| 555 std::unique_ptr<ViewLayoutRequest>( | |
| 556 new ViewLayoutRequest(layout_params.Pass()))); | |
| 557 } | |
| 558 | |
| 559 void ViewRegistry::IssueNextViewLayoutRequest(ViewState* view_state) { | |
| 560 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 561 | |
| 562 if (!view_state->pending_layout_requests.empty() && | |
| 563 !view_state->pending_layout_requests.front()->issued) { | |
| 564 view_state->pending_layout_requests.front()->issued = true; | |
| 565 SendViewLayoutRequest(view_state); | |
| 566 } | |
| 567 } | |
| 568 | |
| 569 void ViewRegistry::IssueNextViewTreeLayoutRequest(ViewTreeState* tree_state) { | |
| 570 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 571 | |
| 572 if (tree_state->layout_request_pending && | |
| 573 !tree_state->layout_request_issued) { | |
| 574 tree_state->layout_request_pending = false; | |
| 575 tree_state->layout_request_issued = true; | |
| 576 SendViewTreeLayoutRequest(tree_state); | |
| 577 } | |
| 578 } | |
| 579 | |
| 580 void ViewRegistry::ResetStateWhenUnlinking(ViewState* view_state) { | |
| 581 // Clean up parent's recorded state for the child. | |
| 582 if (view_state->parent()) { | |
| 583 view_state->parent()->children_needing_layout.erase(view_state->key()); | |
| 584 } | |
| 585 | |
| 586 // Clean up child's recorded state for the parent or tree. | |
| 587 if (view_state->wrapped_surface) { | |
| 588 surface_manager_->DestroySurface(view_state->wrapped_surface.Pass()); | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 void ViewRegistry::SendChildUnavailable(ViewState* parent_state, | |
| 593 uint32_t child_key) { | |
| 594 DCHECK(IsViewStateRegisteredDebug(parent_state)); | |
| 595 | |
| 596 // TODO: Detect ANRs | |
| 597 DVLOG(1) << "SendChildUnavailable: child_key=" << child_key; | |
| 598 parent_state->view()->OnChildUnavailable(child_key, | |
| 599 base::Bind(&base::DoNothing)); | |
| 600 } | |
| 601 | |
| 602 void ViewRegistry::SendRootUnavailable(ViewTreeState* tree_state, | |
| 603 uint32_t root_key) { | |
| 604 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 605 | |
| 606 // TODO: Detect ANRs | |
| 607 DVLOG(1) << "SendRootUnavailable: root_key=" << root_key; | |
| 608 tree_state->view_tree()->OnRootUnavailable(root_key, | |
| 609 base::Bind(&base::DoNothing)); | |
| 610 } | |
| 611 | |
| 612 void ViewRegistry::SendViewLayoutRequest(ViewState* view_state) { | |
| 613 DCHECK(IsViewStateRegisteredDebug(view_state)); | |
| 614 DCHECK(!view_state->pending_layout_requests.empty()); | |
| 615 DCHECK(view_state->pending_layout_requests.front()->issued); | |
| 616 | |
| 617 // TODO: Detect ANRs | |
| 618 DVLOG(1) << "SendViewLayoutRequest: view.token=" | |
| 619 << view_state->view_token_value(); | |
| 620 view_state->view()->OnLayout( | |
| 621 view_state->pending_layout_requests.front()->layout_params()->Clone(), | |
| 622 mojo::Array<uint32_t>::From(view_state->children_needing_layout), | |
| 623 base::Bind(&ViewRegistry::OnViewLayoutResult, base::Unretained(this), | |
| 624 view_state->AsWeakPtr())); | |
| 625 view_state->children_needing_layout.clear(); | |
| 626 } | |
| 627 | |
| 628 void ViewRegistry::SendViewTreeLayoutRequest(ViewTreeState* tree_state) { | |
| 629 DCHECK(IsViewTreeStateRegisteredDebug(tree_state)); | |
| 630 DCHECK(tree_state->layout_request_issued); | |
| 631 | |
| 632 // TODO: Detect ANRs | |
| 633 DVLOG(1) << "SendViewTreeLayoutRequest"; | |
| 634 tree_state->view_tree()->OnLayout( | |
| 635 base::Bind(&ViewRegistry::OnViewTreeLayoutResult, base::Unretained(this), | |
| 636 tree_state->AsWeakPtr())); | |
| 637 } | |
| 638 | |
| 639 static bool IsSizeInBounds(mojo::ui::BoxConstraints* constraints, | |
| 640 mojo::Size* size) { | |
| 641 return size && size->width >= constraints->min_width && | |
| 642 size->width <= constraints->max_width && | |
| 643 size->height >= constraints->min_height && | |
| 644 size->height <= constraints->max_height; | |
| 645 } | |
| 646 | |
| 647 void ViewRegistry::OnViewLayoutResult(base::WeakPtr<ViewState> view_state_weak, | |
| 648 mojo::ui::ViewLayoutInfoPtr info) { | |
| 649 DCHECK(info); | |
| 650 DCHECK(info->surface_id); // checked by mojom | |
| 651 | |
| 652 ViewState* view_state = view_state_weak.get(); | |
| 653 if (view_state) { | |
|
jamesr
2015/10/27 23:13:11
early return would be easier to read than this big
jeffbrown
2015/10/28 01:37:29
Done.
| |
| 654 DCHECK(!view_state->pending_layout_requests.empty()); | |
| 655 DCHECK(view_state->pending_layout_requests.front()->issued); | |
| 656 | |
| 657 std::unique_ptr<ViewLayoutRequest> request( | |
| 658 std::move(view_state->pending_layout_requests.front())); | |
| 659 view_state->pending_layout_requests.erase( | |
| 660 view_state->pending_layout_requests.begin()); | |
| 661 | |
| 662 DVLOG(1) << "OnViewLayoutResult: view=" << view_state | |
| 663 << ", params=" << request->layout_params() | |
| 664 << ", info=" << info.get(); | |
| 665 | |
| 666 // Validate the layout info. | |
| 667 if (!IsSizeInBounds(request->layout_params()->constraints.get(), | |
| 668 info->size.get())) { | |
| 669 LOG(ERROR) << "View returned invalid size in its layout info: " | |
| 670 << "view=" << view_state | |
| 671 << ", params=" << request->layout_params() | |
| 672 << ", info=" << info.get(); | |
| 673 UnregisterView(view_state); | |
| 674 return; | |
| 675 } | |
| 676 | |
| 677 // Assume the parent or root will not see the new layout information if | |
| 678 // there are no callbacks so we need to inform it when things change. | |
| 679 const bool size_changed = | |
| 680 !view_state->layout_info || | |
| 681 !view_state->layout_info->size->Equals(*info->size); | |
| 682 const bool surface_changed = | |
| 683 !view_state->layout_info || | |
| 684 !view_state->layout_info->surface_id->Equals(*info->surface_id); | |
| 685 const bool recurse = | |
| 686 !request->has_callbacks() && (surface_changed || size_changed); | |
| 687 | |
| 688 view_state->layout_params = request->TakeLayoutParams().Pass(); | |
| 689 view_state->layout_info = info.Pass(); | |
| 690 | |
| 691 if (surface_changed) { | |
| 692 if (view_state->wrapped_surface) | |
| 693 surface_manager_->DestroySurface(view_state->wrapped_surface.Pass()); | |
| 694 view_state->wrapped_surface = surface_manager_->CreateWrappedSurface( | |
| 695 view_state->layout_info->surface_id.get()); | |
| 696 } | |
| 697 | |
| 698 request->DispatchLayoutInfo(view_state->layout_info.get()); | |
| 699 | |
| 700 if (recurse) { | |
| 701 if (view_state->parent()) { | |
| 702 InvalidateLayoutForChild(view_state->parent(), view_state->key()); | |
| 703 } else if (view_state->tree()) { | |
| 704 InvalidateLayoutForRoot(view_state->tree()); | |
| 705 } | |
| 706 } | |
| 707 | |
| 708 IssueNextViewLayoutRequest(view_state); | |
| 709 } | |
| 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->layout_request_issued = false; | |
| 721 IssueNextViewTreeLayoutRequest(tree_state); | |
| 722 } | |
| 723 } | |
| 724 | |
| 725 } // namespace view_manager | |
| OLD | NEW |