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/view_manager/view_manager_app.h"
7 #include "base/command_line.h"
8 #include "base/stl_util.h"
9 #include "components/view_manager/client_connection.h"
10 #include "components/view_manager/connection_manager.h"
11 #include "components/view_manager/gles2/gpu_impl.h"
12 #include "components/view_manager/public/cpp/args.h"
13 #include "components/view_manager/surfaces/surfaces_impl.h"
14 #include "components/view_manager/surfaces/surfaces_scheduler.h"
15 #include "components/view_manager/view_manager_root_connection.h"
16 #include "components/view_manager/view_manager_root_impl.h"
17 #include "components/view_manager/view_manager_service_impl.h"
18 #include "mojo/application/public/cpp/application_connection.h"
19 #include "mojo/application/public/cpp/application_impl.h"
20 #include "mojo/application/public/cpp/application_runner.h"
21 #include "mojo/common/tracing_impl.h"
22 #include "third_party/mojo/src/mojo/public/c/system/main.h"
23 #include "ui/events/event_switches.h"
24 #include "ui/events/platform/platform_event_source.h"
25 #include "ui/gl/gl_surface.h"
26 #include "ui/gl/test/gl_surface_test_support.h"
28 using mojo::ApplicationConnection
;
29 using mojo::ApplicationImpl
;
31 using mojo::InterfaceRequest
;
32 using mojo::ViewManagerRoot
;
33 using mojo::ViewManagerService
;
35 namespace view_manager
{
37 ViewManagerApp::ViewManagerApp()
42 ViewManagerApp::~ViewManagerApp() {
44 gpu_state_
->StopControlThread();
46 auto surfaces
= surfaces_
;
47 for (auto& surface
: surfaces
)
48 surface
->CloseConnection();
51 void ViewManagerApp::Initialize(ApplicationImpl
* app
) {
53 tracing_
.Initialize(app
);
54 surfaces_state_
= new surfaces::SurfacesState
;
56 #if !defined(OS_ANDROID)
57 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
58 is_headless_
= command_line
->HasSwitch(mojo::kUseHeadlessConfig
);
60 event_source_
= ui::PlatformEventSource::CreateDefault();
61 if (command_line
->HasSwitch(mojo::kUseTestConfig
))
62 gfx::GLSurfaceTestSupport::InitializeOneOff();
64 gfx::GLSurface::InitializeOneOff();
68 if (!gpu_state_
.get())
69 gpu_state_
= new gles2::GpuState
;
70 connection_manager_
.reset(new ConnectionManager(this));
73 bool ViewManagerApp::ConfigureIncomingConnection(
74 ApplicationConnection
* connection
) {
76 connection
->AddService
<ViewManagerRoot
>(this);
78 // TODO(fsamuel): This should go away soon.
79 connection
->AddService
<mojo::Surface
>(this);
81 connection
->AddService
<Gpu
>(this);
85 void ViewManagerApp::OnNoMoreRootConnections() {
89 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
90 ConnectionManager
* connection_manager
,
91 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
92 mojo::ConnectionSpecificId creator_id
,
93 mojo::URLRequestPtr request
,
94 const ViewId
& root_id
) {
95 mojo::ViewManagerClientPtr client
;
96 app_impl_
->ConnectToService(request
.Pass(), &client
);
98 scoped_ptr
<ViewManagerServiceImpl
> service(
99 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
100 return new DefaultClientConnection(service
.Pass(), connection_manager
,
101 service_request
.Pass(), client
.Pass());
104 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
105 ConnectionManager
* connection_manager
,
106 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
107 mojo::ConnectionSpecificId creator_id
,
108 const ViewId
& root_id
,
109 mojo::ViewManagerClientPtr view_manager_client
) {
110 scoped_ptr
<ViewManagerServiceImpl
> service(
111 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
112 return new DefaultClientConnection(service
.Pass(), connection_manager
,
113 service_request
.Pass(),
114 view_manager_client
.Pass());
117 void ViewManagerApp::Create(ApplicationConnection
* connection
,
118 InterfaceRequest
<ViewManagerRoot
> request
) {
119 DCHECK(connection_manager_
.get());
120 // TODO(fsamuel): We need to make sure that only the window manager can create
122 ViewManagerRootImpl
* view_manager_root
= new ViewManagerRootImpl(
123 connection_manager_
.get(), is_headless_
, app_impl_
, gpu_state_
,
126 mojo::ViewManagerClientPtr client
;
127 connection
->ConnectToService(&client
);
129 // ViewManagerRootConnection manages its own lifetime.
130 view_manager_root
->Init(new ViewManagerRootConnectionImpl(
131 request
.Pass(), make_scoped_ptr(view_manager_root
), client
.Pass(),
132 connection_manager_
.get()));
135 void ViewManagerApp::Create(
136 mojo::ApplicationConnection
* connection
,
137 mojo::InterfaceRequest
<Gpu
> request
) {
138 if (!gpu_state_
.get())
139 gpu_state_
= new gles2::GpuState
;
140 new gles2::GpuImpl(request
.Pass(), gpu_state_
);
143 void ViewManagerApp::Create(
144 mojo::ApplicationConnection
* connection
,
145 mojo::InterfaceRequest
<mojo::Surface
> request
) {
147 new surfaces::SurfacesImpl(this, surfaces_state_
, request
.Pass()));
150 void ViewManagerApp::OnSurfaceConnectionClosed(
151 surfaces::SurfacesImpl
* surface
) {
152 surfaces_
.erase(surface
);
155 } // namespace view_manager