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

Unified Diff: services/ui/view_manager/view_registry.cc

Issue 1679023006: Reify view ownership as a message pipe. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: services/ui/view_manager/view_registry.cc
diff --git a/services/ui/view_manager/view_registry.cc b/services/ui/view_manager/view_registry.cc
index a92c7f1d8f5a7bbc6e75ec28c4178c0690321cd4..c86177a399344692bd1b908b1e967a3ec2aed774 100644
--- a/services/ui/view_manager/view_registry.cc
+++ b/services/ui/view_manager/view_registry.cc
@@ -14,8 +14,8 @@
#include "services/ui/view_manager/view_tree_host_impl.h"
namespace view_manager {
-
-static bool AreViewLayoutParamsValid(const mojo::ui::ViewLayoutParams* params) {
+namespace {
+bool AreViewLayoutParamsValid(const mojo::ui::ViewLayoutParams* params) {
return params && params->constraints && params->constraints->min_width >= 0 &&
params->constraints->max_width >= params->constraints->min_width &&
params->constraints->min_height >= 0 &&
@@ -23,6 +23,14 @@ static bool AreViewLayoutParamsValid(const mojo::ui::ViewLayoutParams* params) {
params->device_pixel_ratio > 0;
}
+bool IsSizeInBounds(mojo::ui::BoxConstraints* constraints, mojo::Size* size) {
+ return size && size->width >= constraints->min_width &&
+ size->width <= constraints->max_width &&
+ size->height >= constraints->min_height &&
+ size->height <= constraints->max_height;
+}
+} // namespace
+
ViewRegistry::ViewRegistry(mojo::gfx::composition::CompositorPtr compositor)
: compositor_(compositor.Pass()) {}
@@ -36,11 +44,14 @@ void ViewRegistry::ConnectAssociates(
connection_error_callback);
}
-mojo::ui::ViewTokenPtr ViewRegistry::RegisterView(
+void ViewRegistry::RegisterView(
mojo::ui::ViewPtr view,
mojo::InterfaceRequest<mojo::ui::ViewHost> view_host_request,
+ mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request,
const mojo::String& label) {
DCHECK(view);
+ DCHECK(view_host_request.is_pending());
+ DCHECK(view_owner_request.is_pending());
auto view_token = mojo::ui::ViewToken::New();
view_token->value = next_view_token_value_++;
@@ -51,26 +62,19 @@ mojo::ui::ViewTokenPtr ViewRegistry::RegisterView(
std::string sanitized_label =
label.get().substr(0, mojo::ui::kLabelMaxLength);
ViewState* view_state =
- new ViewState(view.Pass(), view_token.Pass(), sanitized_label);
- ViewHostImpl* view_host =
- new ViewHostImpl(this, view_state, view_host_request.Pass());
- view_state->set_view_host(view_host);
- view_state->set_view_connection_error_handler(
- base::Bind(&ViewRegistry::OnViewConnectionError, base::Unretained(this),
- view_state));
- view_host->set_view_host_connection_error_handler(
- base::Bind(&ViewRegistry::OnViewConnectionError, base::Unretained(this),
- view_state));
+ new ViewState(this, view.Pass(), view_token.Pass(),
+ view_host_request.Pass(), sanitized_label);
+ view_state->BindOwner(view_owner_request.Pass());
// Add to registry and return token.
views_by_token_.insert({view_state->view_token()->value, view_state});
DVLOG(1) << "RegisterView: view=" << view_state;
- return view_state->view_token()->Clone();
}
-void ViewRegistry::OnViewConnectionError(ViewState* view_state) {
+void ViewRegistry::OnViewDied(ViewState* view_state,
+ const std::string& reason) {
DCHECK(IsViewStateRegisteredDebug(view_state));
- DVLOG(1) << "OnViewConnectionError: view=" << view_state;
+ DVLOG(1) << "OnViewDied: view=" << view_state << ", reason=" << reason;
UnregisterView(view_state);
}
@@ -80,18 +84,25 @@ void ViewRegistry::UnregisterView(ViewState* view_state) {
DVLOG(1) << "UnregisterView: view=" << view_state;
// Remove from parent or roots.
+ // This may send a view unavailable message to the view's parent or tree.
HijackView(view_state);
+ // Recursively unregister all children since they will become unowned
+ // at this point taking care to unlink each one before its unregistration.
+ for (auto& child : view_state->UnlinkAllChildren())
+ UnregisterViewStub(std::move(child));
+
// Remove from registry.
views_by_token_.erase(view_state->view_token()->value);
delete view_state;
}
-mojo::ui::ViewTreeTokenPtr ViewRegistry::RegisterViewTree(
+void ViewRegistry::RegisterViewTree(
mojo::ui::ViewTreePtr view_tree,
mojo::InterfaceRequest<mojo::ui::ViewTreeHost> view_tree_host_request,
const mojo::String& label) {
DCHECK(view_tree);
+ DCHECK(view_tree_host_request.is_pending());
auto view_tree_token = mojo::ui::ViewTreeToken::New();
view_tree_token->value = next_view_tree_token_value_++;
@@ -101,28 +112,20 @@ mojo::ui::ViewTreeTokenPtr ViewRegistry::RegisterViewTree(
// Create the state and bind host to it.
std::string sanitized_label =
label.get().substr(0, mojo::ui::kLabelMaxLength);
- ViewTreeState* tree_state = new ViewTreeState(
- view_tree.Pass(), view_tree_token.Pass(), sanitized_label);
- ViewTreeHostImpl* tree_host =
- new ViewTreeHostImpl(this, tree_state, view_tree_host_request.Pass());
- tree_state->set_view_tree_host(tree_host);
- tree_state->set_view_tree_connection_error_handler(
- base::Bind(&ViewRegistry::OnViewTreeConnectionError,
- base::Unretained(this), tree_state));
- tree_host->set_view_tree_host_connection_error_handler(
- base::Bind(&ViewRegistry::OnViewTreeConnectionError,
- base::Unretained(this), tree_state));
+ ViewTreeState* tree_state =
+ new ViewTreeState(this, view_tree.Pass(), view_tree_token.Pass(),
+ view_tree_host_request.Pass(), sanitized_label);
// Add to registry.
view_trees_by_token_.insert(
{tree_state->view_tree_token()->value, tree_state});
DVLOG(1) << "RegisterViewTree: tree=" << tree_state;
- return tree_state->view_tree_token()->Clone();
}
-void ViewRegistry::OnViewTreeConnectionError(ViewTreeState* tree_state) {
+void ViewRegistry::OnViewTreeDied(ViewTreeState* tree_state,
+ const std::string& reason) {
DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- DVLOG(1) << "OnViewTreeConnectionError: tree=" << tree_state;
+ DVLOG(1) << "OnViewTreeDied: tree=" << tree_state << ", reason=" << reason;
UnregisterViewTree(tree_state);
}
@@ -133,7 +136,7 @@ void ViewRegistry::UnregisterViewTree(ViewTreeState* tree_state) {
// Unlink the root if needed.
if (tree_state->root())
- UnlinkRoot(tree_state);
+ UnregisterViewStub(tree_state->UnlinkRoot());
// Remove from registry.
view_trees_by_token_.erase(tree_state->view_tree_token()->value);
@@ -144,6 +147,7 @@ void ViewRegistry::CreateScene(
ViewState* view_state,
mojo::InterfaceRequest<mojo::gfx::composition::Scene> scene) {
DCHECK(IsViewStateRegisteredDebug(view_state));
+ DCHECK(scene.is_pending());
DVLOG(1) << "CreateScene: view=" << view_state;
compositor_->CreateScene(
@@ -175,46 +179,40 @@ void ViewRegistry::RequestLayout(ViewState* view_state) {
void ViewRegistry::AddChild(ViewState* parent_state,
uint32_t child_key,
- mojo::ui::ViewTokenPtr child_view_token) {
+ mojo::ui::ViewOwnerPtr child_view_owner) {
DCHECK(IsViewStateRegisteredDebug(parent_state));
- DCHECK(child_view_token);
- DVLOG(1) << "AddChild: parent=" << parent_state << ", child_key=" << child_key
- << ", child=" << child_view_token;
+ DCHECK(child_view_owner);
+ DVLOG(1) << "AddChild: parent=" << parent_state
+ << ", child_key=" << child_key;
- // Check for duplicate children.
+ // Ensure there are no other children with the same key.
if (parent_state->children().find(child_key) !=
parent_state->children().end()) {
LOG(ERROR) << "View attempted to add a child with a duplicate key: "
- << "parent=" << parent_state << ", child_key=" << child_key
- << ", child=" << child_view_token;
+ << "parent=" << parent_state << ", child_key=" << child_key;
UnregisterView(parent_state);
return;
}
- // Check whether the desired child view still exists.
- // Adding a non-existent child still succeeds but the view manager will
- // immediately report it as being unavailable.
- ViewState* child_state = FindView(child_view_token->value);
- if (!child_state) {
- LinkChildAsUnavailable(parent_state, child_key);
- return;
- }
-
- // Check whether the child needs to be reparented.
- // The old parent will receive an unavailable event. For interface symmetry,
- // we deliberately do this even if the old and new parents are the same.
- HijackView(child_state);
+ // Add a stub, pending resolution of the view owner.
+ parent_state->LinkChild(child_key, std::unique_ptr<ViewStub>(new ViewStub(
+ this, child_view_owner.Pass())));
- // Link the child into its new parent.
- LinkChild(parent_state, child_key, child_state);
+ // Schedule layout of the parent on behalf of its newly added child.
+ // We don't need to schedule layout of the child until the parent provides
+ // new layout parameters.
+ InvalidateLayoutForChild(parent_state, child_key);
}
-void ViewRegistry::RemoveChild(ViewState* parent_state, uint32_t child_key) {
+void ViewRegistry::RemoveChild(ViewState* parent_state,
+ uint32_t child_key,
+ mojo::InterfaceRequest<mojo::ui::ViewOwner>
+ transferred_view_owner_request) {
DCHECK(IsViewStateRegisteredDebug(parent_state));
DVLOG(1) << "RemoveChild: parent=" << parent_state
<< ", child_key=" << child_key;
- // Check whether the child key exists in the parent.
+ // Ensure the child key exists in the parent.
auto child_it = parent_state->children().find(child_key);
if (child_it == parent_state->children().end()) {
LOG(ERROR) << "View attempted to remove a child with an invalid key: "
@@ -224,7 +222,13 @@ void ViewRegistry::RemoveChild(ViewState* parent_state, uint32_t child_key) {
}
// Unlink the child from its parent.
- UnlinkChild(parent_state, child_it);
+ TransferOrUnregisterViewStub(parent_state->UnlinkChild(child_key),
+ transferred_view_owner_request.Pass());
+
+ // Schedule layout for the parent now that it has lost its child.
+ // We don't need to schedule layout for the child itself since it will
+ // retain its old layout parameters until it is reparented.
+ InvalidateLayout(parent_state);
}
void ViewRegistry::LayoutChild(
@@ -260,7 +264,7 @@ void ViewRegistry::LayoutChild(
return;
}
- SetLayout(child_it->second, child_layout_params.Pass(), callback);
+ SetLayout(child_it->second.get(), child_layout_params.Pass(), callback);
}
void ViewRegistry::ConnectToViewService(
@@ -282,32 +286,52 @@ void ViewRegistry::RequestLayout(ViewTreeState* tree_state) {
void ViewRegistry::SetRoot(ViewTreeState* tree_state,
uint32_t root_key,
- mojo::ui::ViewTokenPtr root_view_token) {
+ mojo::ui::ViewOwnerPtr root_view_owner) {
DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- DCHECK(root_view_token);
- DVLOG(1) << "SetRoot: tree=" << tree_state << ", root_key=" << root_key
- << ", root=" << root_view_token;
-
- // Check whether the desired root view still exists.
- // Using a non-existent root view still succeeds but the view manager will
- // immediately report it as being unavailable.
- ViewState* root_state = FindView(root_view_token->value);
- if (root_state) {
- HijackView(root_state);
- LinkRoot(tree_state, root_state, root_key);
- } else {
- SendRootUnavailable(tree_state, root_key);
+ DCHECK(root_view_owner);
+ DVLOG(1) << "SetRoot: tree=" << tree_state << ", root_key=" << root_key;
+
+ // Ensure there isn't already a root.
+ if (tree_state->root()) {
+ LOG(ERROR)
+ << "View tree attempted to set the root while one is already set: tree="
+ << tree_state << ", root_key=" << root_key;
+ UnregisterViewTree(tree_state);
+ return;
}
- tree_state->set_explicit_root(true);
+
+ // Set the root to a stub, pending resolution of the view owner.
+ tree_state->LinkRoot(root_key, std::unique_ptr<ViewStub>(new ViewStub(
+ this, root_view_owner.Pass())));
+
+ // Schedule layout of the tree on behalf of its newly added root.
+ // We don't need to schedule layout of the root until the tree provides
+ // new layout parameters.
+ InvalidateLayoutForRoot(tree_state);
}
-void ViewRegistry::ResetRoot(ViewTreeState* tree_state) {
+void ViewRegistry::ResetRoot(ViewTreeState* tree_state,
+ mojo::InterfaceRequest<mojo::ui::ViewOwner>
+ transferred_view_owner_request) {
DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
DVLOG(1) << "ResetRoot: tree=" << tree_state;
- if (tree_state->root())
- UnlinkRoot(tree_state);
- tree_state->set_explicit_root(false);
+ // Ensure there is a root.
+ if (!tree_state->root()) {
+ LOG(ERROR)
+ << "View tree attempted to reset the root but there is none: tree="
+ << tree_state;
+ UnregisterViewTree(tree_state);
+ return;
+ }
+
+ // Unlink the root from its tree.
+ TransferOrUnregisterViewStub(tree_state->UnlinkRoot(),
+ transferred_view_owner_request.Pass());
+
+ // Note: We don't need to schedule layout for the root since it will retain
+ // its old layout parameters. And there's no need to tell the tree
+ // either since it won't have any work to do. So we're done.
}
void ViewRegistry::LayoutRoot(ViewTreeState* tree_state,
@@ -331,8 +355,8 @@ void ViewRegistry::LayoutRoot(ViewTreeState* tree_state,
// Check whether the client called LayoutRoot without first having actually
// set a root.
- if (!tree_state->explicit_root()) {
- LOG(ERROR) << "View tree attempted to layout the rout without having "
+ if (!tree_state->root()) {
+ LOG(ERROR) << "View tree attempted to layout the root without having "
"set one first: tree="
<< tree_state << ", root_layout_params=" << root_layout_params;
UnregisterViewTree(tree_state);
@@ -340,13 +364,6 @@ void ViewRegistry::LayoutRoot(ViewTreeState* tree_state,
return;
}
- // Check whether the root is unavailable and therefore cannot be laid out.
- // This is not an error.
- if (!tree_state->root()) {
- callback.Run(nullptr);
- return;
- }
-
SetLayout(tree_state->root(), root_layout_params.Pass(), callback);
}
@@ -366,176 +383,112 @@ ViewState* ViewRegistry::FindView(uint32_t view_token_value) {
return it != views_by_token_.end() ? it->second : nullptr;
}
-void ViewRegistry::LinkChild(ViewState* parent_state,
- uint32_t child_key,
- ViewState* child_state) {
- DCHECK(IsViewStateRegisteredDebug(parent_state));
- DCHECK(parent_state->children().find(child_key) ==
- parent_state->children().end());
- DCHECK(IsViewStateRegisteredDebug(child_state));
-
- DVLOG(2) << "Added child " << child_key << " {" << child_state->label()
- << "} to parent {" << parent_state->label() << "}";
-
- parent_state->children().insert({child_key, child_state});
- child_state->SetParent(parent_state, child_key);
-
- // Schedule layout of the parent on behalf of its newly added child.
- // We don't need to schedule layout of the child until the parent provides
- // new layout parameters.
- InvalidateLayoutForChild(parent_state, child_key);
+ViewTreeState* ViewRegistry::FindViewTree(uint32_t view_tree_token_value) {
+ auto it = view_trees_by_token_.find(view_tree_token_value);
+ return it != view_trees_by_token_.end() ? it->second : nullptr;
}
-void ViewRegistry::LinkChildAsUnavailable(ViewState* parent_state,
- uint32_t child_key) {
- DCHECK(IsViewStateRegisteredDebug(parent_state));
- DCHECK(parent_state->children().find(child_key) ==
- parent_state->children().end());
-
- DVLOG(2) << "Added unavailable child " << child_key << " to parent {"
- << parent_state->label() << "}";
-
- parent_state->children().insert({child_key, nullptr});
- SendChildUnavailable(parent_state, child_key);
+void ViewRegistry::HijackView(ViewState* view_state) {
+ DCHECK(IsViewStateRegisteredDebug(view_state));
- // Don't schedule layout for the parent just yet. Wait for it to
- // remove its child in response to the OnChildUnavailable notification.
+ ViewStub* view_stub = view_state->view_stub();
+ if (view_stub)
+ ReleaseViewStubAndNotify(view_stub);
}
-void ViewRegistry::MarkChildAsUnavailable(ViewState* parent_state,
- uint32_t child_key) {
- DCHECK(IsViewStateRegisteredDebug(parent_state));
- auto child_it = parent_state->children().find(child_key);
- DCHECK(child_it != parent_state->children().end());
- DCHECK(child_it->second);
+void ViewRegistry::OnViewResolved(ViewStub* view_stub,
+ mojo::ui::ViewTokenPtr view_token) {
+ DCHECK(view_stub);
- DVLOG(2) << "Marked unavailable child " << child_key << " {"
- << child_it->second->label() << "} from parent {"
- << parent_state->label() << "}";
-
- ResetStateWhenUnlinking(child_it->second);
- child_it->second->ResetContainer();
- child_it->second = nullptr;
- SendChildUnavailable(parent_state, child_key);
-
- // Don't schedule layout for the parent just yet. Wait for it to
- // remove its child in response to the OnChildUnavailable notification.
- // We don't need to schedule layout for the child either since it will
- // retain its old layout parameters.
+ ViewState* view_state = view_token ? FindView(view_token->value) : nullptr;
+ if (view_state)
+ AttachViewStubAndNotify(view_stub, view_state);
+ else
+ ReleaseViewStubAndNotify(view_stub);
}
-void ViewRegistry::UnlinkChild(ViewState* parent_state,
- ViewState::ChildrenMap::iterator child_it) {
- DCHECK(IsViewStateRegisteredDebug(parent_state));
- DCHECK(child_it != parent_state->children().end());
-
- ViewState* child_state = child_it->second;
- if (child_state) {
- DVLOG(2) << "Removed child " << child_state->key() << " {"
- << child_state->label() << "} from parent {"
- << parent_state->label() << "}";
- ResetStateWhenUnlinking(child_it->second);
- child_state->ResetContainer();
- } else {
- DVLOG(2) << "Removed unavailable child " << child_it->first
- << "} from parent {" << parent_state->label() << "}";
- }
- parent_state->children().erase(child_it);
+void ViewRegistry::AttachViewStubAndNotify(ViewStub* view_stub,
+ ViewState* view_state) {
+ DCHECK(view_stub);
- // Schedule layout for the parent now that it has lost its child.
- // We don't need to schedule layout for the child itself since it will
- // retain its old layout parameters.
- InvalidateLayout(parent_state);
-}
+ view_state->ReleaseOwner(); // don't need the ViewOwner pipe anymore
+ view_stub->AttachView(view_state);
-ViewTreeState* ViewRegistry::FindViewTree(uint32_t view_tree_token_value) {
- auto it = view_trees_by_token_.find(view_tree_token_value);
- return it != view_trees_by_token_.end() ? it->second : nullptr;
+ if (view_stub->pending_layout_request()) {
+ view_state->pending_layout_requests().push_back(
+ std::move(view_stub->pending_layout_request()));
+ IssueNextViewLayoutRequest(view_state);
+ }
}
-void ViewRegistry::LinkRoot(ViewTreeState* tree_state,
- ViewState* root_state,
- uint32_t root_key) {
- DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- DCHECK(IsViewStateRegisteredDebug(root_state));
- DCHECK(!tree_state->root());
- DCHECK(!root_state->parent());
+void ViewRegistry::ReleaseViewStubAndNotify(ViewStub* view_stub) {
+ DCHECK(view_stub);
- DVLOG(2) << "Linked view tree root " << root_key << " {"
- << root_state->label() << "}";
+ view_stub->ReleaseView();
- tree_state->SetRoot(root_state, root_key);
+ if (view_stub->parent())
+ SendChildUnavailable(view_stub->parent(), view_stub->key());
+ else if (view_stub->tree())
+ SendRootUnavailable(view_stub->tree(), view_stub->key());
- // Schedule layout of the tree on behalf of its newly added root.
- // We don't need to schedule layout of the root until the tree provides
- // new layout parameters.
- InvalidateLayoutForRoot(tree_state);
+ // Note: We don't need to schedule layout for the previous owner.
+ // We can simply wait for it to remove its unavailable child or root in
+ // response to the notification at which point layout will occur.
+ // We don't need to schedule layout for the child either since it will
+ // retain its old layout parameters.
}
-void ViewRegistry::UnlinkRoot(ViewTreeState* tree_state) {
- DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- DCHECK(tree_state->root());
-
- DVLOG(2) << "Unlinked view tree root " << tree_state->root()->key() << " {"
- << tree_state->root()->label() << "}";
-
- ResetStateWhenUnlinking(tree_state->root());
- tree_state->ResetRoot();
-
- // We don't need to schedule layout for the root since it will retain
- // its old layout parameters.
-}
+void ViewRegistry::TransferOrUnregisterViewStub(
+ std::unique_ptr<ViewStub> view_stub,
+ mojo::InterfaceRequest<mojo::ui::ViewOwner>
+ transferred_view_owner_request) {
+ DCHECK(view_stub);
-void ViewRegistry::HijackView(ViewState* view_state) {
- if (view_state->parent()) {
- MarkChildAsUnavailable(view_state->parent(), view_state->key());
- } else if (view_state->tree()) {
- ViewTreeState* tree_state = view_state->tree();
- uint32_t root_key = tree_state->root()->key();
- UnlinkRoot(tree_state);
- SendRootUnavailable(tree_state, root_key);
+ if (transferred_view_owner_request.is_pending()) {
+ if (view_stub->state())
+ view_stub->state()->BindOwner(transferred_view_owner_request.Pass());
+ else if (view_stub->is_pending())
+ CHECK(false); // TODO(jeffbrown): Handle transfer of pending view
+ } else {
+ UnregisterViewStub(std::move(view_stub));
}
}
-void ViewRegistry::InvalidateLayout(ViewState* view_state) {
- DCHECK(IsViewStateRegisteredDebug(view_state));
+void ViewRegistry::UnregisterViewStub(std::unique_ptr<ViewStub> view_stub) {
+ DCHECK(view_stub);
- // We can consider the layout request to have been satisfied if
- // there is already a pending layout request in the queue that has not
- // yet been issued (this is coalescing). Otherwise we must manufacture
- // a new one based on the current layout parameters.
- if (view_state->layout_params() &&
- (view_state->pending_layout_requests().empty() ||
- view_state->pending_layout_requests().back()->issued())) {
- EnqueueLayoutRequest(view_state, view_state->layout_params()->Clone());
- IssueNextViewLayoutRequest(view_state);
- }
+ if (view_stub->state())
+ UnregisterView(view_stub->state());
}
-void ViewRegistry::InvalidateLayoutForChild(ViewState* parent_state,
- uint32_t child_key) {
- DCHECK(IsViewStateRegisteredDebug(parent_state));
- DCHECK(parent_state->children().find(child_key) !=
- parent_state->children().end());
-
- parent_state->children_needing_layout().insert(child_key);
- InvalidateLayout(parent_state);
-}
+void ViewRegistry::SetLayout(ViewStub* view_stub,
+ mojo::ui::ViewLayoutParamsPtr layout_params,
+ const ViewLayoutCallback& callback) {
+ DCHECK(view_stub);
+ DCHECK(AreViewLayoutParamsValid(layout_params.get()));
-void ViewRegistry::InvalidateLayoutForRoot(ViewTreeState* tree_state) {
- DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
+ // Immediately discard layout requests on unavailable views.
+ if (view_stub->is_unavailable()) {
+ callback.Run(nullptr);
+ return;
+ }
- if (!tree_state->layout_request_pending()) {
- tree_state->set_layout_request_pending(true);
- IssueNextViewTreeLayoutRequest(tree_state);
+ // For pending views, only remember the most recent distinct layout request.
+ if (view_stub->is_pending()) {
+ if (!view_stub->pending_layout_request() ||
+ !view_stub->pending_layout_request()->layout_params()->Equals(
+ *layout_params)) {
+ view_stub->pending_layout_request().reset(
+ new ViewLayoutRequest(layout_params.Pass()));
+ }
+ view_stub->pending_layout_request()->AddCallback(callback);
+ return;
}
-}
-void ViewRegistry::SetLayout(ViewState* view_state,
- mojo::ui::ViewLayoutParamsPtr layout_params,
- const ViewLayoutCallback& callback) {
- DCHECK(IsViewStateRegisteredDebug(view_state));
- DCHECK(AreViewLayoutParamsValid(layout_params.get()));
+ // For actual views, maintain a queue of pending layout requests.
+ ViewState* view_state = view_stub->state();
+ DCHECK(view_state);
+ DCHECK(!view_stub->pending_layout_request());
// Check whether the currently cached layout parameters are the same
// and we already have a result and we have no pending layout requests.
@@ -568,7 +521,7 @@ void ViewRegistry::SetLayout(ViewState* view_state,
void ViewRegistry::EnqueueLayoutRequest(
ViewState* view_state,
mojo::ui::ViewLayoutParamsPtr layout_params) {
- DCHECK(IsViewStateRegisteredDebug(view_state));
+ DCHECK(view_state);
DCHECK(AreViewLayoutParamsValid(layout_params.get()));
// Drop the previous layout request if it hasn't been issued yet.
@@ -583,86 +536,76 @@ void ViewRegistry::EnqueueLayoutRequest(
new ViewLayoutRequest(layout_params.Pass())));
}
-void ViewRegistry::IssueNextViewLayoutRequest(ViewState* view_state) {
+void ViewRegistry::InvalidateLayout(ViewState* view_state) {
DCHECK(IsViewStateRegisteredDebug(view_state));
- if (!view_state->pending_layout_requests().empty() &&
- !view_state->pending_layout_requests().front()->issued()) {
- view_state->pending_layout_requests().front()->set_issued(true);
- SendViewLayoutRequest(view_state);
- }
-}
-
-void ViewRegistry::IssueNextViewTreeLayoutRequest(ViewTreeState* tree_state) {
- DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
-
- if (tree_state->layout_request_pending() &&
- !tree_state->layout_request_issued()) {
- tree_state->set_layout_request_pending(false);
- tree_state->set_layout_request_issued(true);
- SendViewTreeLayoutRequest(tree_state);
- }
-}
-
-void ViewRegistry::ResetStateWhenUnlinking(ViewState* view_state) {
- // Clean up parent's recorded state for the child.
- if (view_state->parent()) {
- view_state->parent()->children_needing_layout().erase(view_state->key());
+ // We can consider the layout request to have been satisfied if
+ // there is already a pending layout request in the queue that has not
+ // yet been issued (this is coalescing). Otherwise we must manufacture
+ // a new one based on the current layout parameters.
+ if (view_state->layout_params() &&
+ (view_state->pending_layout_requests().empty() ||
+ view_state->pending_layout_requests().back()->issued())) {
+ EnqueueLayoutRequest(view_state, view_state->layout_params()->Clone());
+ IssueNextViewLayoutRequest(view_state);
}
}
-void ViewRegistry::SendChildUnavailable(ViewState* parent_state,
- uint32_t child_key) {
+void ViewRegistry::InvalidateLayoutForChild(ViewState* parent_state,
+ uint32_t child_key) {
DCHECK(IsViewStateRegisteredDebug(parent_state));
+ DCHECK(parent_state->children().find(child_key) !=
+ parent_state->children().end());
- // TODO: Detect ANRs
- DVLOG(1) << "SendChildUnavailable: child_key=" << child_key;
- parent_state->view()->OnChildUnavailable(child_key,
- base::Bind(&base::DoNothing));
+ parent_state->children_needing_layout().insert(child_key);
+ InvalidateLayout(parent_state);
}
-void ViewRegistry::SendRootUnavailable(ViewTreeState* tree_state,
- uint32_t root_key) {
+void ViewRegistry::InvalidateLayoutForRoot(ViewTreeState* tree_state) {
DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- // TODO: Detect ANRs
- DVLOG(1) << "SendRootUnavailable: root_key=" << root_key;
- tree_state->view_tree()->OnRootUnavailable(root_key,
- base::Bind(&base::DoNothing));
+ if (!tree_state->layout_request_pending()) {
+ tree_state->set_layout_request_pending(true);
+ IssueNextViewTreeLayoutRequest(tree_state);
+ }
}
-void ViewRegistry::SendViewLayoutRequest(ViewState* view_state) {
+void ViewRegistry::IssueNextViewLayoutRequest(ViewState* view_state) {
DCHECK(IsViewStateRegisteredDebug(view_state));
- DCHECK(!view_state->pending_layout_requests().empty());
- DCHECK(view_state->pending_layout_requests().front()->issued());
+
+ if (view_state->pending_layout_requests().empty())
+ return;
+
+ ViewLayoutRequest* request =
+ view_state->pending_layout_requests().front().get();
+ if (request->issued())
+ return;
// TODO: Detect ANRs
- DVLOG(1) << "SendViewLayoutRequest: view.token=" << view_state->label();
+ DVLOG(1) << "IssueNextViewLayoutRequest: view_state=" << view_state;
view_state->view()->OnLayout(
- view_state->pending_layout_requests().front()->layout_params()->Clone(),
+ request->layout_params()->Clone(),
mojo::Array<uint32_t>::From(view_state->children_needing_layout()),
base::Bind(&ViewRegistry::OnViewLayoutResult, base::Unretained(this),
view_state->GetWeakPtr()));
view_state->children_needing_layout().clear();
+ request->set_issued(true);
}
-void ViewRegistry::SendViewTreeLayoutRequest(ViewTreeState* tree_state) {
+void ViewRegistry::IssueNextViewTreeLayoutRequest(ViewTreeState* tree_state) {
DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
- DCHECK(tree_state->layout_request_issued());
+
+ if (!tree_state->layout_request_pending() ||
+ tree_state->layout_request_issued())
+ return;
// TODO: Detect ANRs
DVLOG(1) << "SendViewTreeLayoutRequest";
tree_state->view_tree()->OnLayout(
base::Bind(&ViewRegistry::OnViewTreeLayoutResult, base::Unretained(this),
tree_state->GetWeakPtr()));
-}
-
-static bool IsSizeInBounds(mojo::ui::BoxConstraints* constraints,
- mojo::Size* size) {
- return size && size->width >= constraints->min_width &&
- size->width <= constraints->max_width &&
- size->height >= constraints->min_height &&
- size->height <= constraints->max_height;
+ tree_state->set_layout_request_pending(false);
+ tree_state->set_layout_request_issued(true);
}
void ViewRegistry::OnViewLayoutResult(base::WeakPtr<ViewState> view_state_weak,
@@ -682,14 +625,14 @@ void ViewRegistry::OnViewLayoutResult(base::WeakPtr<ViewState> view_state_weak,
view_state->pending_layout_requests().begin());
DVLOG(1) << "OnViewLayoutResult: view=" << view_state
- << ", params=" << request->layout_params() << ", result=" << result;
+ << ", params=" << *request->layout_params() << ", result=" << result;
// Validate the layout info.
if (!IsSizeInBounds(request->layout_params()->constraints.get(),
result->size.get())) {
LOG(ERROR) << "View returned invalid size in its layout info: "
<< "view=" << view_state
- << ", params=" << request->layout_params()
+ << ", params=" << *request->layout_params()
<< ", result=" << result;
UnregisterView(view_state);
return;
@@ -713,11 +656,12 @@ void ViewRegistry::OnViewLayoutResult(base::WeakPtr<ViewState> view_state_weak,
request->DispatchLayoutInfo(info.Pass());
}
- if (recurse) {
- if (view_state->parent()) {
- InvalidateLayoutForChild(view_state->parent(), view_state->key());
- } else if (view_state->tree()) {
- InvalidateLayoutForRoot(view_state->tree());
+ if (recurse && view_state->view_stub()) {
+ if (view_state->view_stub()->parent()) {
+ InvalidateLayoutForChild(view_state->view_stub()->parent(),
+ view_state->view_stub()->key());
+ } else if (view_state->view_stub()->tree()) {
+ InvalidateLayoutForRoot(view_state->view_stub()->tree());
}
}
@@ -737,4 +681,26 @@ void ViewRegistry::OnViewTreeLayoutResult(
}
}
+void ViewRegistry::SendChildUnavailable(ViewState* parent_state,
+ uint32_t child_key) {
+ DCHECK(IsViewStateRegisteredDebug(parent_state));
+
+ // TODO: Detect ANRs
+ DVLOG(1) << "SendChildUnavailable: parent_state=" << parent_state
+ << ", child_key=" << child_key;
+ parent_state->view()->OnChildUnavailable(child_key,
+ base::Bind(&base::DoNothing));
+}
+
+void ViewRegistry::SendRootUnavailable(ViewTreeState* tree_state,
+ uint32_t root_key) {
+ DCHECK(IsViewTreeStateRegisteredDebug(tree_state));
+
+ // TODO: Detect ANRs
+ DVLOG(1) << "SendRootUnavailable: tree_state=" << tree_state
+ << ", root_key=" << root_key;
+ tree_state->view_tree()->OnRootUnavailable(root_key,
+ base::Bind(&base::DoNothing));
+}
+
} // namespace view_manager
« 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