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 "components/mus/mus_app.h"
7 #include "base/command_line.h"
8 #include "base/stl_util.h"
9 #include "components/mus/client_connection.h"
10 #include "components/mus/connection_manager.h"
11 #include "components/mus/gles2/gpu_impl.h"
12 #include "components/mus/public/cpp/args.h"
13 #include "components/mus/surfaces/surfaces_scheduler.h"
14 #include "components/mus/view_tree_host_connection.h"
15 #include "components/mus/view_tree_host_impl.h"
16 #include "components/mus/view_tree_impl.h"
17 #include "mojo/application/public/cpp/application_connection.h"
18 #include "mojo/application/public/cpp/application_impl.h"
19 #include "mojo/application/public/cpp/application_runner.h"
20 #include "mojo/common/tracing_impl.h"
21 #include "third_party/mojo/src/mojo/public/c/system/main.h"
22 #include "ui/events/event_switches.h"
23 #include "ui/events/platform/platform_event_source.h"
24 #include "ui/gl/gl_surface.h"
25 #include "ui/gl/test/gl_surface_test_support.h"
29 #include "ui/platform_window/x11/x11_window.h"
32 using mojo::ApplicationConnection
;
33 using mojo::ApplicationImpl
;
35 using mojo::InterfaceRequest
;
36 using mojo::ViewTreeHostFactory
;
40 MandolineUIServicesApp::MandolineUIServicesApp()
41 : app_impl_(nullptr), is_headless_(false) {}
43 MandolineUIServicesApp::~MandolineUIServicesApp() {
45 gpu_state_
->StopControlThread();
46 // Destroy |connection_manager_| first, since it depends on |event_source_|.
47 connection_manager_
.reset();
50 void MandolineUIServicesApp::Initialize(ApplicationImpl
* app
) {
52 tracing_
.Initialize(app
);
53 surfaces_state_
= new SurfacesState
;
55 #if !defined(OS_ANDROID)
56 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
57 is_headless_
= command_line
->HasSwitch(kUseHeadlessConfig
);
60 if (command_line
->HasSwitch(kUseX11TestConfig
)) {
62 ui::test::SetUseOverrideRedirectWindowByDefault(true);
65 gfx::GLSurface::InitializeOneOff();
66 event_source_
= ui::PlatformEventSource::CreateDefault();
70 if (!gpu_state_
.get())
71 gpu_state_
= new GpuState
;
72 connection_manager_
.reset(new ConnectionManager(this, surfaces_state_
));
75 bool MandolineUIServicesApp::ConfigureIncomingConnection(
76 ApplicationConnection
* connection
) {
77 // MandolineUIServices
78 connection
->AddService
<ViewTreeHostFactory
>(this);
80 connection
->AddService
<Gpu
>(this);
84 void MandolineUIServicesApp::OnNoMoreRootConnections() {
88 ClientConnection
* MandolineUIServicesApp::CreateClientConnectionForEmbedAtView(
89 ConnectionManager
* connection_manager
,
90 mojo::InterfaceRequest
<mojo::ViewTree
> tree_request
,
91 ConnectionSpecificId creator_id
,
92 mojo::URLRequestPtr request
,
93 const ViewId
& root_id
,
94 uint32_t policy_bitmask
) {
95 mojo::ViewTreeClientPtr client
;
96 app_impl_
->ConnectToService(request
.Pass(), &client
);
98 scoped_ptr
<ViewTreeImpl
> service(new ViewTreeImpl(
99 connection_manager
, creator_id
, root_id
, policy_bitmask
));
100 return new DefaultClientConnection(service
.Pass(), connection_manager
,
101 tree_request
.Pass(), client
.Pass());
104 ClientConnection
* MandolineUIServicesApp::CreateClientConnectionForEmbedAtView(
105 ConnectionManager
* connection_manager
,
106 mojo::InterfaceRequest
<mojo::ViewTree
> tree_request
,
107 ConnectionSpecificId creator_id
,
108 const ViewId
& root_id
,
109 uint32_t policy_bitmask
,
110 mojo::ViewTreeClientPtr client
) {
111 scoped_ptr
<ViewTreeImpl
> service(new ViewTreeImpl(
112 connection_manager
, creator_id
, root_id
, policy_bitmask
));
113 return new DefaultClientConnection(service
.Pass(), connection_manager
,
114 tree_request
.Pass(), client
.Pass());
117 void MandolineUIServicesApp::Create(
118 ApplicationConnection
* connection
,
119 InterfaceRequest
<ViewTreeHostFactory
> request
) {
120 factory_bindings_
.AddBinding(this, request
.Pass());
123 void MandolineUIServicesApp::Create(mojo::ApplicationConnection
* connection
,
124 mojo::InterfaceRequest
<Gpu
> request
) {
125 if (!gpu_state_
.get())
126 gpu_state_
= new GpuState
;
127 new GpuImpl(request
.Pass(), gpu_state_
);
130 void MandolineUIServicesApp::CreateViewTreeHost(
131 mojo::InterfaceRequest
<mojo::ViewTreeHost
> host
,
132 mojo::ViewTreeHostClientPtr host_client
,
133 mojo::ViewTreeClientPtr tree_client
) {
134 DCHECK(connection_manager_
.get());
136 // TODO(fsamuel): We need to make sure that only the window manager can create
138 ViewTreeHostImpl
* host_impl
= new ViewTreeHostImpl(
139 host_client
.Pass(), connection_manager_
.get(), is_headless_
, app_impl_
,
140 gpu_state_
, surfaces_state_
);
142 // ViewTreeHostConnection manages its own lifetime.
143 host_impl
->Init(new ViewTreeHostConnectionImpl(
144 host
.Pass(), make_scoped_ptr(host_impl
), tree_client
.Pass(),
145 connection_manager_
.get()));