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
|
// 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 "components/mus/mus_app.h"
#include <set>
#include "base/stl_util.h"
#include "build/build_config.h"
#include "base/threading/platform_thread.h"
#include "components/mus/common/args.h"
#include "components/mus/gles2/gpu_impl.h"
#include "components/mus/ws/client_connection.h"
#include "components/mus/ws/connection_manager.h"
#include "components/mus/ws/window_tree_factory.h"
#include "components/mus/ws/window_tree_host_connection.h"
#include "components/mus/ws/window_tree_host_impl.h"
#include "components/mus/ws/window_tree_impl.h"
#include "components/resource_provider/public/cpp/resource_loader.h"
#include "mojo/public/c/system/main.h"
#include "mojo/services/tracing/public/cpp/tracing_impl.h"
#include "mojo/shell/public/cpp/connection.h"
#include "mojo/shell/public/cpp/shell.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#include "ui/events/event_switches.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/gl/gl_surface.h"
#if defined(USE_X11)
#include <X11/Xlib.h>
#include "base/command_line.h"
#include "ui/platform_window/x11/x11_window.h"
#elif defined(USE_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif
using mojo::Connection;
using mojo::InterfaceRequest;
using mus::mojom::WindowTreeHostFactory;
using mus::mojom::Gpu;
namespace mus {
namespace {
const char kResourceFileStrings[] = "mus_app_resources_strings.pak";
const char kResourceFile100[] = "mus_app_resources_100.pak";
const char kResourceFile200[] = "mus_app_resources_200.pak";
} // namespace
// TODO(sky): this is a pretty typical pattern, make it easier to do.
struct MandolineUIServicesApp::PendingRequest {
scoped_ptr<mojo::InterfaceRequest<mojom::DisplayManager>> dm_request;
scoped_ptr<mojo::InterfaceRequest<mojom::WindowTreeFactory>> wtf_request;
};
MandolineUIServicesApp::MandolineUIServicesApp()
: shell_(nullptr) {}
MandolineUIServicesApp::~MandolineUIServicesApp() {
if (gpu_state_)
gpu_state_->StopThreads();
// Destroy |connection_manager_| first, since it depends on |event_source_|.
connection_manager_.reset();
}
void MandolineUIServicesApp::InitializeResources(mojo::Shell* shell) {
if (ui::ResourceBundle::HasSharedInstance())
return;
std::set<std::string> resource_paths;
resource_paths.insert(kResourceFileStrings);
resource_paths.insert(kResourceFile100);
resource_paths.insert(kResourceFile200);
resource_provider::ResourceLoader resource_loader(shell, resource_paths);
if (!resource_loader.BlockUntilLoaded())
return;
CHECK(resource_loader.loaded());
ui::RegisterPathProvider();
// Initialize resource bundle with 1x and 2x cursor bitmaps.
ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
resource_loader.ReleaseFile(kResourceFileStrings),
base::MemoryMappedFile::Region::kWholeFile);
ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
resource_loader.ReleaseFile(kResourceFile100), ui::SCALE_FACTOR_100P);
ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
resource_loader.ReleaseFile(kResourceFile200), ui::SCALE_FACTOR_200P);
}
void MandolineUIServicesApp::Initialize(mojo::Shell* shell,
const std::string& url,
uint32_t id,
uint32_t user_id) {
shell_ = shell;
surfaces_state_ = new SurfacesState;
base::PlatformThread::SetName("mus");
#if defined(USE_X11)
XInitThreads();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(kUseX11TestConfig)) {
ui::test::SetUseOverrideRedirectWindowByDefault(true);
}
#endif
InitializeResources(shell);
#if defined(USE_OZONE)
// The ozone platform can provide its own event source. So initialize the
// platform before creating the default event source.
// TODO(rjkroege): Add tracing here.
// Because GL libraries need to be initialized before entering the sandbox,
// in MUS, |InitializeForUI| will load the GL libraries.
ui::OzonePlatform::InitializeForUI();
#endif
// TODO(rjkroege): Enter sandbox here before we start threads in GpuState
// http://crbug.com/584532
#if !defined(OS_ANDROID)
event_source_ = ui::PlatformEventSource::CreateDefault();
#endif
// TODO(rjkroege): It is possible that we might want to generalize the
// GpuState object.
gpu_state_ = new GpuState();
connection_manager_.reset(new ws::ConnectionManager(this, surfaces_state_));
tracing_.Initialize(shell, url);
}
bool MandolineUIServicesApp::AcceptConnection(Connection* connection) {
connection->AddInterface<Gpu>(this);
connection->AddInterface<mojom::DisplayManager>(this);
connection->AddInterface<mojom::WindowManagerFactoryService>(this);
connection->AddInterface<mojom::WindowTreeFactory>(this);
connection->AddInterface<WindowTreeHostFactory>(this);
return true;
}
void MandolineUIServicesApp::OnFirstRootConnectionCreated() {
PendingRequests requests;
requests.swap(pending_requests_);
for (auto& request : requests) {
if (request->dm_request)
Create(nullptr, std::move(*request->dm_request));
else
Create(nullptr, std::move(*request->wtf_request));
}
}
void MandolineUIServicesApp::OnNoMoreRootConnections() {
shell_->Quit();
}
ws::ClientConnection*
MandolineUIServicesApp::CreateClientConnectionForEmbedAtWindow(
ws::ConnectionManager* connection_manager,
mojo::InterfaceRequest<mojom::WindowTree> tree_request,
ws::ServerWindow* root,
uint32_t policy_bitmask,
mojom::WindowTreeClientPtr client) {
scoped_ptr<ws::WindowTreeImpl> service(
new ws::WindowTreeImpl(connection_manager, root, policy_bitmask));
return new ws::DefaultClientConnection(std::move(service), connection_manager,
std::move(tree_request),
std::move(client));
}
void MandolineUIServicesApp::Create(
mojo::Connection* connection,
mojo::InterfaceRequest<mojom::DisplayManager> request) {
if (!connection_manager_->has_tree_host_connections()) {
scoped_ptr<PendingRequest> pending_request(new PendingRequest);
pending_request->dm_request.reset(
new mojo::InterfaceRequest<mojom::DisplayManager>(std::move(request)));
pending_requests_.push_back(std::move(pending_request));
return;
}
connection_manager_->AddDisplayManagerBinding(std::move(request));
}
void MandolineUIServicesApp::Create(
mojo::Connection* connection,
mojo::InterfaceRequest<mojom::WindowManagerFactoryService> request) {
connection_manager_->CreateWindowManagerFactoryService(std::move(request));
}
void MandolineUIServicesApp::Create(
Connection* connection,
InterfaceRequest<mojom::WindowTreeFactory> request) {
if (!connection_manager_->has_tree_host_connections()) {
scoped_ptr<PendingRequest> pending_request(new PendingRequest);
pending_request->wtf_request.reset(
new mojo::InterfaceRequest<mojom::WindowTreeFactory>(
std::move(request)));
pending_requests_.push_back(std::move(pending_request));
return;
}
if (!window_tree_factory_) {
window_tree_factory_.reset(
new ws::WindowTreeFactory(connection_manager_.get()));
}
window_tree_factory_->AddBinding(std::move(request));
}
void MandolineUIServicesApp::Create(
Connection* connection,
InterfaceRequest<WindowTreeHostFactory> request) {
factory_bindings_.AddBinding(this, std::move(request));
}
void MandolineUIServicesApp::Create(mojo::Connection* connection,
mojo::InterfaceRequest<Gpu> request) {
DCHECK(gpu_state_);
new GpuImpl(std::move(request), gpu_state_);
}
void MandolineUIServicesApp::CreateWindowTreeHost(
mojo::InterfaceRequest<mojom::WindowTreeHost> host,
mojom::WindowTreeClientPtr tree_client) {
DCHECK(connection_manager_);
// TODO(fsamuel): We need to make sure that only the window manager can create
// new roots.
ws::WindowTreeHostImpl* host_impl = new ws::WindowTreeHostImpl(
connection_manager_.get(), shell_, gpu_state_, surfaces_state_);
// WindowTreeHostConnection manages its own lifetime.
host_impl->Init(new ws::WindowTreeHostConnectionImpl(
std::move(host), make_scoped_ptr(host_impl), std::move(tree_client),
connection_manager_.get()));
}
} // namespace mus
|