OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/view_manager/root_node_manager.h" | 5 #include "mojo/services/view_manager/connection_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
9 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 9 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
10 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" | 10 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" |
11 #include "mojo/services/view_manager/view_manager_service_impl.h" | 11 #include "mojo/services/view_manager/view_manager_service_impl.h" |
12 #include "ui/aura/env.h" | 12 #include "ui/aura/env.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace service { | 15 namespace service { |
16 | 16 |
17 RootNodeManager::ScopedChange::ScopedChange( | 17 ConnectionManager::ScopedChange::ScopedChange( |
18 ViewManagerServiceImpl* connection, | 18 ViewManagerServiceImpl* connection, |
19 RootNodeManager* root, | 19 ConnectionManager* connection_manager, |
20 bool is_delete_node) | 20 bool is_delete_view) |
21 : root_(root), | 21 : connection_manager_(connection_manager), |
22 connection_id_(connection->id()), | 22 connection_id_(connection->id()), |
23 is_delete_node_(is_delete_node) { | 23 is_delete_view_(is_delete_view) { |
24 root_->PrepareForChange(this); | 24 connection_manager_->PrepareForChange(this); |
25 } | 25 } |
26 | 26 |
27 RootNodeManager::ScopedChange::~ScopedChange() { | 27 ConnectionManager::ScopedChange::~ScopedChange() { |
28 root_->FinishChange(); | 28 connection_manager_->FinishChange(); |
29 } | 29 } |
30 | 30 |
31 RootNodeManager::Context::Context() { | 31 ConnectionManager::Context::Context() { |
32 // Pass in false as native viewport creates the PlatformEventSource. | 32 // Pass in false as native viewport creates the PlatformEventSource. |
33 aura::Env::CreateInstance(false); | 33 aura::Env::CreateInstance(false); |
34 } | 34 } |
35 | 35 |
36 RootNodeManager::Context::~Context() { | 36 ConnectionManager::Context::~Context() { |
37 aura::Env::DeleteInstance(); | 37 aura::Env::DeleteInstance(); |
38 } | 38 } |
39 | 39 |
40 RootNodeManager::RootNodeManager( | 40 ConnectionManager::ConnectionManager( |
41 ApplicationConnection* app_connection, | 41 ApplicationConnection* app_connection, |
42 DisplayManagerDelegate* display_manager_delegate, | 42 DisplayManagerDelegate* display_manager_delegate, |
43 const Callback<void()>& native_viewport_closed_callback) | 43 const Callback<void()>& native_viewport_closed_callback) |
44 : app_connection_(app_connection), | 44 : app_connection_(app_connection), |
45 next_connection_id_(1), | 45 next_connection_id_(1), |
46 display_manager_(app_connection, | 46 display_manager_(app_connection, |
47 this, | 47 this, |
48 display_manager_delegate, | 48 display_manager_delegate, |
49 native_viewport_closed_callback), | 49 native_viewport_closed_callback), |
50 root_(new Node(this, RootNodeId())), | 50 root_(new ServerView(this, RootViewId())), |
51 current_change_(NULL) { | 51 current_change_(NULL) { |
52 } | 52 } |
53 | 53 |
54 RootNodeManager::~RootNodeManager() { | 54 ConnectionManager::~ConnectionManager() { |
55 while (!connections_created_by_connect_.empty()) | 55 while (!connections_created_by_connect_.empty()) |
56 delete *(connections_created_by_connect_.begin()); | 56 delete *(connections_created_by_connect_.begin()); |
57 // All the connections should have been destroyed. | 57 // All the connections should have been destroyed. |
58 DCHECK(connection_map_.empty()); | 58 DCHECK(connection_map_.empty()); |
59 root_.reset(); | 59 root_.reset(); |
60 } | 60 } |
61 | 61 |
62 ConnectionSpecificId RootNodeManager::GetAndAdvanceNextConnectionId() { | 62 ConnectionSpecificId ConnectionManager::GetAndAdvanceNextConnectionId() { |
63 const ConnectionSpecificId id = next_connection_id_++; | 63 const ConnectionSpecificId id = next_connection_id_++; |
64 DCHECK_LT(id, next_connection_id_); | 64 DCHECK_LT(id, next_connection_id_); |
65 return id; | 65 return id; |
66 } | 66 } |
67 | 67 |
68 void RootNodeManager::AddConnection(ViewManagerServiceImpl* connection) { | 68 void ConnectionManager::AddConnection(ViewManagerServiceImpl* connection) { |
69 DCHECK_EQ(0u, connection_map_.count(connection->id())); | 69 DCHECK_EQ(0u, connection_map_.count(connection->id())); |
70 connection_map_[connection->id()] = connection; | 70 connection_map_[connection->id()] = connection; |
71 } | 71 } |
72 | 72 |
73 void RootNodeManager::RemoveConnection(ViewManagerServiceImpl* connection) { | 73 void ConnectionManager::RemoveConnection(ViewManagerServiceImpl* connection) { |
74 connection_map_.erase(connection->id()); | 74 connection_map_.erase(connection->id()); |
75 connections_created_by_connect_.erase(connection); | 75 connections_created_by_connect_.erase(connection); |
76 | 76 |
77 // Notify remaining connections so that they can cleanup. | 77 // Notify remaining connections so that they can cleanup. |
78 for (ConnectionMap::const_iterator i = connection_map_.begin(); | 78 for (ConnectionMap::const_iterator i = connection_map_.begin(); |
79 i != connection_map_.end(); ++i) { | 79 i != connection_map_.end(); |
| 80 ++i) { |
80 i->second->OnViewManagerServiceImplDestroyed(connection->id()); | 81 i->second->OnViewManagerServiceImplDestroyed(connection->id()); |
81 } | 82 } |
82 } | 83 } |
83 | 84 |
84 void RootNodeManager::EmbedRoot( | 85 void ConnectionManager::EmbedRoot( |
85 const std::string& url, | 86 const std::string& url, |
86 InterfaceRequest<ServiceProvider> service_provider) { | 87 InterfaceRequest<ServiceProvider> service_provider) { |
87 if (connection_map_.empty()) { | 88 if (connection_map_.empty()) { |
88 EmbedImpl(kInvalidConnectionId, String::From(url), RootNodeId(), | 89 EmbedImpl(kInvalidConnectionId, |
| 90 String::From(url), |
| 91 RootViewId(), |
89 service_provider.Pass()); | 92 service_provider.Pass()); |
90 return; | 93 return; |
91 } | 94 } |
92 ViewManagerServiceImpl* connection = GetConnection(kWindowManagerConnection); | 95 ViewManagerServiceImpl* connection = GetConnection(kWindowManagerConnection); |
93 connection->client()->Embed(url, service_provider.Pass()); | 96 connection->client()->Embed(url, service_provider.Pass()); |
94 } | 97 } |
95 | 98 |
96 void RootNodeManager::Embed( | 99 void ConnectionManager::Embed( |
97 ConnectionSpecificId creator_id, | 100 ConnectionSpecificId creator_id, |
98 const String& url, | 101 const String& url, |
99 Id transport_node_id, | 102 Id transport_view_id, |
100 InterfaceRequest<ServiceProvider> service_provider) { | 103 InterfaceRequest<ServiceProvider> service_provider) { |
101 EmbedImpl(creator_id, | 104 EmbedImpl(creator_id, |
102 url, | 105 url, |
103 NodeIdFromTransportId(transport_node_id), | 106 ViewIdFromTransportId(transport_view_id), |
104 service_provider.Pass())->set_delete_on_connection_error(); | 107 service_provider.Pass())->set_delete_on_connection_error(); |
105 } | 108 } |
106 | 109 |
107 ViewManagerServiceImpl* RootNodeManager::GetConnection( | 110 ViewManagerServiceImpl* ConnectionManager::GetConnection( |
108 ConnectionSpecificId connection_id) { | 111 ConnectionSpecificId connection_id) { |
109 ConnectionMap::iterator i = connection_map_.find(connection_id); | 112 ConnectionMap::iterator i = connection_map_.find(connection_id); |
110 return i == connection_map_.end() ? NULL : i->second; | 113 return i == connection_map_.end() ? NULL : i->second; |
111 } | 114 } |
112 | 115 |
113 Node* RootNodeManager::GetNode(const NodeId& id) { | 116 ServerView* ConnectionManager::GetView(const ViewId& id) { |
114 if (id == root_->id()) | 117 if (id == root_->id()) |
115 return root_.get(); | 118 return root_.get(); |
116 ConnectionMap::iterator i = connection_map_.find(id.connection_id); | 119 ConnectionMap::iterator i = connection_map_.find(id.connection_id); |
117 return i == connection_map_.end() ? NULL : i->second->GetNode(id); | 120 return i == connection_map_.end() ? NULL : i->second->GetView(id); |
118 } | 121 } |
119 | 122 |
120 void RootNodeManager::OnConnectionMessagedClient(ConnectionSpecificId id) { | 123 void ConnectionManager::OnConnectionMessagedClient(ConnectionSpecificId id) { |
121 if (current_change_) | 124 if (current_change_) |
122 current_change_->MarkConnectionAsMessaged(id); | 125 current_change_->MarkConnectionAsMessaged(id); |
123 } | 126 } |
124 | 127 |
125 bool RootNodeManager::DidConnectionMessageClient( | 128 bool ConnectionManager::DidConnectionMessageClient( |
126 ConnectionSpecificId id) const { | 129 ConnectionSpecificId id) const { |
127 return current_change_ && current_change_->DidMessageConnection(id); | 130 return current_change_ && current_change_->DidMessageConnection(id); |
128 } | 131 } |
129 | 132 |
130 ViewManagerServiceImpl* RootNodeManager::GetConnectionByCreator( | 133 ViewManagerServiceImpl* ConnectionManager::GetConnectionByCreator( |
131 ConnectionSpecificId creator_id, | 134 ConnectionSpecificId creator_id, |
132 const std::string& url) const { | 135 const std::string& url) const { |
133 for (ConnectionMap::const_iterator i = connection_map_.begin(); | 136 for (ConnectionMap::const_iterator i = connection_map_.begin(); |
134 i != connection_map_.end(); ++i) { | 137 i != connection_map_.end(); |
| 138 ++i) { |
135 if (i->second->creator_id() == creator_id && i->second->url() == url) | 139 if (i->second->creator_id() == creator_id && i->second->url() == url) |
136 return i->second; | 140 return i->second; |
137 } | 141 } |
138 return NULL; | 142 return NULL; |
139 } | 143 } |
140 | 144 |
141 const ViewManagerServiceImpl* RootNodeManager::GetConnectionWithRoot( | 145 const ViewManagerServiceImpl* ConnectionManager::GetConnectionWithRoot( |
142 const NodeId& id) const { | 146 const ViewId& id) const { |
143 for (ConnectionMap::const_iterator i = connection_map_.begin(); | 147 for (ConnectionMap::const_iterator i = connection_map_.begin(); |
144 i != connection_map_.end(); ++i) { | 148 i != connection_map_.end(); |
| 149 ++i) { |
145 if (i->second->HasRoot(id)) | 150 if (i->second->HasRoot(id)) |
146 return i->second; | 151 return i->second; |
147 } | 152 } |
148 return NULL; | 153 return NULL; |
149 } | 154 } |
150 | 155 |
151 void RootNodeManager::DispatchNodeInputEventToWindowManager(EventPtr event) { | 156 void ConnectionManager::DispatchViewInputEventToWindowManager(EventPtr event) { |
152 // Input events are forwarded to the WindowManager. The WindowManager | 157 // Input events are forwarded to the WindowManager. The WindowManager |
153 // eventually calls back to us with DispatchOnViewInputEvent(). | 158 // eventually calls back to us with DispatchOnViewInputEvent(). |
154 ViewManagerServiceImpl* connection = GetConnection(kWindowManagerConnection); | 159 ViewManagerServiceImpl* connection = GetConnection(kWindowManagerConnection); |
155 if (!connection) | 160 if (!connection) |
156 return; | 161 return; |
157 connection->client()->DispatchOnViewInputEvent(event.Pass()); | 162 connection->client()->DispatchOnViewInputEvent(event.Pass()); |
158 } | 163 } |
159 | 164 |
160 void RootNodeManager::ProcessNodeBoundsChanged(const Node* node, | 165 void ConnectionManager::ProcessViewBoundsChanged(const ServerView* view, |
161 const gfx::Rect& old_bounds, | 166 const gfx::Rect& old_bounds, |
162 const gfx::Rect& new_bounds) { | 167 const gfx::Rect& new_bounds) { |
163 for (ConnectionMap::iterator i = connection_map_.begin(); | 168 for (ConnectionMap::iterator i = connection_map_.begin(); |
164 i != connection_map_.end(); ++i) { | 169 i != connection_map_.end(); |
165 i->second->ProcessNodeBoundsChanged(node, old_bounds, new_bounds, | 170 ++i) { |
166 IsChangeSource(i->first)); | 171 i->second->ProcessViewBoundsChanged( |
| 172 view, old_bounds, new_bounds, IsChangeSource(i->first)); |
167 } | 173 } |
168 } | 174 } |
169 | 175 |
170 void RootNodeManager::ProcessNodeHierarchyChanged(const Node* node, | 176 void ConnectionManager::ProcessViewHierarchyChanged( |
171 const Node* new_parent, | 177 const ServerView* view, |
172 const Node* old_parent) { | 178 const ServerView* new_parent, |
| 179 const ServerView* old_parent) { |
173 for (ConnectionMap::iterator i = connection_map_.begin(); | 180 for (ConnectionMap::iterator i = connection_map_.begin(); |
174 i != connection_map_.end(); ++i) { | 181 i != connection_map_.end(); |
175 i->second->ProcessNodeHierarchyChanged( | 182 ++i) { |
176 node, new_parent, old_parent, IsChangeSource(i->first)); | 183 i->second->ProcessViewHierarchyChanged( |
| 184 view, new_parent, old_parent, IsChangeSource(i->first)); |
177 } | 185 } |
178 } | 186 } |
179 | 187 |
180 void RootNodeManager::ProcessNodeReorder(const Node* node, | 188 void ConnectionManager::ProcessViewReorder(const ServerView* view, |
181 const Node* relative_node, | 189 const ServerView* relative_view, |
182 const OrderDirection direction) { | 190 const OrderDirection direction) { |
183 for (ConnectionMap::iterator i = connection_map_.begin(); | 191 for (ConnectionMap::iterator i = connection_map_.begin(); |
184 i != connection_map_.end(); ++i) { | 192 i != connection_map_.end(); |
185 i->second->ProcessNodeReorder( | 193 ++i) { |
186 node, relative_node, direction, IsChangeSource(i->first)); | 194 i->second->ProcessViewReorder( |
| 195 view, relative_view, direction, IsChangeSource(i->first)); |
187 } | 196 } |
188 } | 197 } |
189 | 198 |
190 void RootNodeManager::ProcessNodeDeleted(const NodeId& node) { | 199 void ConnectionManager::ProcessViewDeleted(const ViewId& view) { |
191 for (ConnectionMap::iterator i = connection_map_.begin(); | 200 for (ConnectionMap::iterator i = connection_map_.begin(); |
192 i != connection_map_.end(); ++i) { | 201 i != connection_map_.end(); |
193 i->second->ProcessNodeDeleted(node, IsChangeSource(i->first)); | 202 ++i) { |
| 203 i->second->ProcessViewDeleted(view, IsChangeSource(i->first)); |
194 } | 204 } |
195 } | 205 } |
196 | 206 |
197 void RootNodeManager::PrepareForChange(ScopedChange* change) { | 207 void ConnectionManager::PrepareForChange(ScopedChange* change) { |
198 // Should only ever have one change in flight. | 208 // Should only ever have one change in flight. |
199 CHECK(!current_change_); | 209 CHECK(!current_change_); |
200 current_change_ = change; | 210 current_change_ = change; |
201 } | 211 } |
202 | 212 |
203 void RootNodeManager::FinishChange() { | 213 void ConnectionManager::FinishChange() { |
204 // PrepareForChange/FinishChange should be balanced. | 214 // PrepareForChange/FinishChange should be balanced. |
205 CHECK(current_change_); | 215 CHECK(current_change_); |
206 current_change_ = NULL; | 216 current_change_ = NULL; |
207 } | 217 } |
208 | 218 |
209 ViewManagerServiceImpl* RootNodeManager::EmbedImpl( | 219 ViewManagerServiceImpl* ConnectionManager::EmbedImpl( |
210 const ConnectionSpecificId creator_id, | 220 const ConnectionSpecificId creator_id, |
211 const String& url, | 221 const String& url, |
212 const NodeId& root_id, | 222 const ViewId& root_id, |
213 InterfaceRequest<ServiceProvider> service_provider) { | 223 InterfaceRequest<ServiceProvider> service_provider) { |
214 MessagePipe pipe; | 224 MessagePipe pipe; |
215 | 225 |
216 ServiceProvider* view_manager_service_provider = | 226 ServiceProvider* view_manager_service_provider = |
217 app_connection_->ConnectToApplication(url)->GetServiceProvider(); | 227 app_connection_->ConnectToApplication(url)->GetServiceProvider(); |
218 view_manager_service_provider->ConnectToService( | 228 view_manager_service_provider->ConnectToService( |
219 ViewManagerServiceImpl::Client::Name_, | 229 ViewManagerServiceImpl::Client::Name_, pipe.handle1.Pass()); |
220 pipe.handle1.Pass()); | |
221 | 230 |
222 std::string creator_url; | 231 std::string creator_url; |
223 ConnectionMap::const_iterator it = connection_map_.find(creator_id); | 232 ConnectionMap::const_iterator it = connection_map_.find(creator_id); |
224 if (it != connection_map_.end()) | 233 if (it != connection_map_.end()) |
225 creator_url = it->second->url(); | 234 creator_url = it->second->url(); |
226 | 235 |
227 ViewManagerServiceImpl* connection = | 236 ViewManagerServiceImpl* connection = |
228 new ViewManagerServiceImpl(this, | 237 new ViewManagerServiceImpl(this, |
229 creator_id, | 238 creator_id, |
230 creator_url, | 239 creator_url, |
231 url.To<std::string>(), | 240 url.To<std::string>(), |
232 root_id, | 241 root_id, |
233 service_provider.Pass()); | 242 service_provider.Pass()); |
234 WeakBindToPipe(connection, pipe.handle0.Pass()); | 243 WeakBindToPipe(connection, pipe.handle0.Pass()); |
235 connections_created_by_connect_.insert(connection); | 244 connections_created_by_connect_.insert(connection); |
236 OnConnectionMessagedClient(connection->id()); | 245 OnConnectionMessagedClient(connection->id()); |
237 return connection; | 246 return connection; |
238 } | 247 } |
239 | 248 |
240 void RootNodeManager::OnNodeDestroyed(const Node* node) { | 249 void ConnectionManager::OnViewDestroyed(const ServerView* view) { |
241 ProcessNodeDeleted(node->id()); | 250 ProcessViewDeleted(view->id()); |
242 } | 251 } |
243 | 252 |
244 void RootNodeManager::OnNodeHierarchyChanged(const Node* node, | 253 void ConnectionManager::OnViewHierarchyChanged(const ServerView* view, |
245 const Node* new_parent, | 254 const ServerView* new_parent, |
246 const Node* old_parent) { | 255 const ServerView* old_parent) { |
247 if (!display_manager_.in_setup()) | 256 if (!display_manager_.in_setup()) |
248 ProcessNodeHierarchyChanged(node, new_parent, old_parent); | 257 ProcessViewHierarchyChanged(view, new_parent, old_parent); |
249 } | 258 } |
250 | 259 |
251 void RootNodeManager::OnNodeBoundsChanged(const Node* node, | 260 void ConnectionManager::OnViewBoundsChanged(const ServerView* view, |
252 const gfx::Rect& old_bounds, | 261 const gfx::Rect& old_bounds, |
253 const gfx::Rect& new_bounds) { | 262 const gfx::Rect& new_bounds) { |
254 ProcessNodeBoundsChanged(node, old_bounds, new_bounds); | 263 ProcessViewBoundsChanged(view, old_bounds, new_bounds); |
255 if (!node->parent()) | 264 if (!view->parent()) |
256 return; | 265 return; |
257 | 266 |
258 // TODO(sky): optimize this. | 267 // TODO(sky): optimize this. |
259 display_manager_.SchedulePaint(node->parent(), old_bounds); | 268 display_manager_.SchedulePaint(view->parent(), old_bounds); |
260 display_manager_.SchedulePaint(node->parent(), new_bounds); | 269 display_manager_.SchedulePaint(view->parent(), new_bounds); |
261 } | 270 } |
262 | 271 |
263 void RootNodeManager::OnNodeBitmapChanged(const Node* node) { | 272 void ConnectionManager::OnViewBitmapChanged(const ServerView* view) { |
264 display_manager_.SchedulePaint(node, gfx::Rect(node->bounds().size())); | 273 display_manager_.SchedulePaint(view, gfx::Rect(view->bounds().size())); |
265 } | 274 } |
266 | 275 |
267 } // namespace service | 276 } // namespace service |
268 } // namespace mojo | 277 } // namespace mojo |
OLD | NEW |