a833c1caf7f37a471a06cdabc812f9a1f7f5a4f3
[platform/framework/web/crosswalk.git] / src / mojo / services / view_manager / view_manager_init_service_context.cc
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.
4
5 #include "mojo/services/view_manager/view_manager_init_service_context.h"
6
7 #include "base/auto_reset.h"
8 #include "base/bind.h"
9 #include "mojo/services/view_manager/root_node_manager.h"
10 #include "mojo/services/view_manager/view_manager_init_service_impl.h"
11
12 namespace mojo {
13 namespace service {
14
15 ViewManagerInitServiceContext::ConnectParams::ConnectParams() {}
16
17 ViewManagerInitServiceContext::ConnectParams::~ConnectParams() {}
18
19 ViewManagerInitServiceContext::ViewManagerInitServiceContext()
20     : is_tree_host_ready_(false),
21       deleting_connection_(false) {}
22 ViewManagerInitServiceContext::~ViewManagerInitServiceContext() {}
23
24 void ViewManagerInitServiceContext::AddConnection(
25     ViewManagerInitServiceImpl* connection) {
26   DCHECK(std::find(connections_.begin(), connections_.end(), connection) ==
27                    connections_.end());
28   connections_.push_back(connection);
29 }
30
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);
38   }
39
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();
45 }
46
47 void ViewManagerInitServiceContext::ConfigureIncomingConnection(
48     ApplicationConnection* connection) {
49   if (!root_node_manager_.get()) {
50     root_node_manager_.reset(new RootNodeManager(
51         connection,
52         this,
53         base::Bind(&ViewManagerInitServiceContext::OnNativeViewportDeleted,
54                    base::Unretained(this))));
55   }
56 }
57
58 void ViewManagerInitServiceContext::Embed(
59     const String& url,
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);
67   MaybeEmbed();
68 }
69
70 void ViewManagerInitServiceContext::OnRootViewManagerWindowTreeHostCreated() {
71   DCHECK(!is_tree_host_ready_);
72   is_tree_host_ready_ = true;
73   MaybeEmbed();
74 }
75
76 void ViewManagerInitServiceContext::OnNativeViewportDeleted() {
77   // Prevent the connection from modifying the connection list during manual
78   // teardown.
79   base::AutoReset<bool> deleting_connection(&deleting_connection_, true);
80   for (Connections::const_iterator it = connections_.begin();
81        it != connections_.end(); ++it) {
82     delete *it;
83   }
84   connections_.clear();
85   root_node_manager_.reset();
86 }
87
88 void ViewManagerInitServiceContext::MaybeEmbed() {
89   if (!is_tree_host_ready_)
90     return;
91
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);
96   }
97   connect_params_.clear();
98 }
99
100 }  // namespace service
101 }  // namespace mojo