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