1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/services/view_manager/view_manager_init_service_context.h"
7 #include "base/auto_reset.h"
9 #include "mojo/services/view_manager/root_node_manager.h"
10 #include "mojo/services/view_manager/view_manager_init_service_impl.h"
15 ViewManagerInitServiceContext::ConnectParams::ConnectParams() {}
17 ViewManagerInitServiceContext::ConnectParams::~ConnectParams() {}
19 ViewManagerInitServiceContext::ViewManagerInitServiceContext()
20 : is_tree_host_ready_(false),
21 deleting_connection_(false) {}
22 ViewManagerInitServiceContext::~ViewManagerInitServiceContext() {}
24 void ViewManagerInitServiceContext::AddConnection(
25 ViewManagerInitServiceImpl* connection) {
26 DCHECK(std::find(connections_.begin(), connections_.end(), connection) ==
28 connections_.push_back(connection);
31 void ViewManagerInitServiceContext::RemoveConnection(
32 ViewManagerInitServiceImpl* connection) {
33 if (!deleting_connection_) {
34 Connections::iterator it =
35 std::find(connections_.begin(), connections_.end(), connection);
36 DCHECK(it != connections_.end());
37 connections_.erase(it);
40 // This object is owned by an object that outlives the current thread's
41 // message loop, so we need to destroy the RootNodeManager earlier, as it may
42 // attempt to post tasks during its destruction.
43 if (connections_.empty())
44 root_node_manager_.reset();
47 void ViewManagerInitServiceContext::ConfigureIncomingConnection(
48 ApplicationConnection* connection) {
49 if (!root_node_manager_.get()) {
50 root_node_manager_.reset(new RootNodeManager(
53 base::Bind(&ViewManagerInitServiceContext::OnNativeViewportDeleted,
54 base::Unretained(this))));
58 void ViewManagerInitServiceContext::Embed(
60 ServiceProviderPtr service_provider,
61 const Callback<void(bool)>& callback) {
62 ConnectParams* params = new ConnectParams;
63 params->url = url.To<std::string>();
64 params->callback = callback;
65 params->service_provider.Bind(service_provider.PassMessagePipe());
66 connect_params_.push_back(params);
70 void ViewManagerInitServiceContext::OnRootViewManagerWindowTreeHostCreated() {
71 DCHECK(!is_tree_host_ready_);
72 is_tree_host_ready_ = true;
76 void ViewManagerInitServiceContext::OnNativeViewportDeleted() {
77 // Prevent the connection from modifying the connection list during manual
79 base::AutoReset<bool> deleting_connection(&deleting_connection_, true);
80 for (Connections::const_iterator it = connections_.begin();
81 it != connections_.end(); ++it) {
85 root_node_manager_.reset();
88 void ViewManagerInitServiceContext::MaybeEmbed() {
89 if (!is_tree_host_ready_)
92 for (ScopedVector<ConnectParams>::const_iterator it = connect_params_.begin();
93 it != connect_params_.end(); ++it) {
94 root_node_manager_->EmbedRoot((*it)->url, (*it)->service_provider.Pass());
95 (*it)->callback.Run(true);
97 connect_params_.clear();
100 } // namespace service